### Question:
JavaCode extends Code { public String packageName() { Matcher matcher = PACKAGE_PATTERN.matcher(text()); if (matcher.matches()) { return matcher.group(1); } return ""; } JavaCode(); String typeName(); boolean isClass(); Collection<String> typeAnnotations(); Collection<String> imports(); Collection<String> methods(); Collection<String> methodAnnotations(String method); String methodReturns(String method); String methodArguments(String method); String packageName(); String fullyQualifiedName(); String methodBody(String method); Collection<String> fieldNames(); String fieldType(String fieldName); Iterable<String> fieldAnnotations(String fieldName); String fieldValue(String fieldName); Iterable<String> fileComments(); String superTypeName(); Iterable<String> implementedInterfaces(); boolean isType(String type); boolean fieldIsContant(String fieldName); Iterable<String> fieldComments(String fieldName); String constructorBody(); void ensureImport(String packageName, String typeName); String simpleTypeName(); static final String TYPE_PATTERN; static final String NAME_PATTERN; }### Answer:
@Test public void extractsPackage() throws Exception { String packageName = String.format("%s.%s", someValue(), someValue()); code.add("package %s;", packageName); code.add("public class %s {", someValue()); code.add("}"); assertEquals("Package", packageName, code.packageName()); }
@Test public void extractsDefaultPackage() throws Exception { code.add("public class %s {", someValue()); code.add("}"); assertEquals("Package", "", code.packageName()); }
@Test public void extractsPackageWhenLineEndsWithComment() throws Exception { String packageName = String.format("%s.%s", someValue(), someValue()); code.add("package %s; code.add("public class %s {", someValue()); code.add("}"); assertEquals("Package", packageName, code.packageName()); }