Compilations { @VisibleForTesting static String getPublicClassName(String source) { Matcher matcher = CLASS_NAME_PATTERN.matcher(source); if (matcher.find()) { return getPackagePrefix(source) + matcher.group(1); } else { return ""; } } private  Compilations(); static Class<?> compile(String source);  }
@Test public void testClassNameParsing() { assertThat(Compilations.getPublicClassName( "public class A { public static void main(String[] args) {} } class B {}")) .isEqualTo("A"); assertThat(Compilations.getPublicClassName("public class X extends Object {}")) .isEqualTo("X"); assertThat(Compilations.getPublicClassName( "public class X extends Object {} public class Y extends Object {}")) .isEqualTo("X"); assertThat(Compilations .getPublicClassName("class X extends Object {} public class Y extends Object {}")) .isEqualTo("Y"); assertThat(Compilations .getPublicClassName("class X extends Object {}\npublic class Y extends Object {}")) .isEqualTo("Y"); assertThat(Compilations .getPublicClassName("class X extends Object {}public class Y extends Object {}")) .isEqualTo("Y"); assertThat(Compilations.getPublicClassName("class X extends Object {}")).isEqualTo(""); }
@Test public void testNonDefaultPackageClassNameParsing() { assertThat(Compilations.getPublicClassName("package a.b; public class X {}")) .isEqualTo("a.b.X"); assertThat(Compilations.getPublicClassName(" package a.b; public class X {}")) .isEqualTo("a.b.X"); assertThat(Compilations.getPublicClassName("\n package a.b; \n public class X {}")) .isEqualTo("a.b.X"); }