### Question:
SmartStringParser { public static String toJavaString(@Nullable String s) { if (s == null || s.isEmpty()) { return s; } State state = State.INIT; StringBuilder sb = new StringBuilder(); for (int i = 0, n = s.length(); i < n; i++) { char c = s.charAt(i); char next = i + 1 < n ? s.charAt(i + 1) : 0; switch (state) { case INIT: if (c == '\'') { state = State.SINGLE; sb.append('"'); } else if (c == '"') { state = State.DOUBLE; sb.append(c); } else { sb.append(c); } break; case SINGLE: if (c == '\\' && next == '\'') { sb.append("\\\""); i += 1; if (i + 1 == n) { state = State.INIT; } } else if (c == '\'') { sb.append('"'); state = State.INIT; } else if (c == '"') { sb.append("\\\""); } else { sb.append(c); } break; case DOUBLE: if (c == '\\' && next == '"') { sb.append("\\\""); i += 1; if (i + 1 == n) { state = State.INIT; } } else if (c == '"') { sb.append('"'); state = State.INIT; } else { sb.append(c); } break; } } if (state != State.INIT) { throw new ParseException("String literal is not properly closed with a %s-quote: ", state.name() .toLowerCase()); } return sb.toString(); } private  SmartStringParser(); static String toJavaString(@Nullable String s);  }### Answer:
@Test public void nullEmpty() { assertThat(toJavaString(null)).isNull(); assertThat(toJavaString("")).isEqualTo(""); }
@Test public void backslash() { assertThat(toJavaString("'\\'")).isEqualTo("\"\\\""); }
@Test public void java() throws Exception { assertThat(toJavaString("\"\"")).isEqualTo("\"\""); assertThat(toJavaString("\"'\"")).isEqualTo("\"'\""); assertThat(toJavaString("\"\\\"\"")).isEqualTo("\"\\\"\""); assertParse(toJavaString("'\"'"), "\"\\\"\""); }
@Test public void simple() throws Exception { assertParse(toJavaString("''"), "\"\""); assertParse(toJavaString("'a'"), "\"a\""); }
@Test public void malformed() { expectedEx.expect(ParseException.class); expectedEx.expectMessage(containsString("not properly closed")); expectedEx.expectMessage(containsString("single-quote")); toJavaString("'"); }
@Test public void malformed2() { expectedEx.expect(ParseException.class); expectedEx.expectMessage(containsString("not properly closed")); expectedEx.expectMessage(containsString("single-quote")); toJavaString("a'"); }
@Test public void malformed3() { expectedEx.expect(ParseException.class); expectedEx.expectMessage(containsString("not properly closed")); expectedEx.expectMessage(containsString("double-quote")); toJavaString("\""); }
@Test public void singleQuote() throws Exception { assertParse(toJavaString("'\\''"), "\"\\\"\""); }
@Test public void code() throws Exception { assertParse(toJavaString("'b'.equals('a') ? 'x' : 'z'"), "\"b\".equals(\"a\") ? \"x\" : \"z\""); assertParse(toJavaString("'\\'a'"), "\"\\\"a\""); }