### Question:
JavaCode extends Code { public String typeName() { return getTypeNameMatcher().group(3); } 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 extractsClassNameFromFinalClass() { String className = randomJavaId(); code.add("final class %s {", className); code.add("}"); assertEquals("Class name", className, code.typeName()); }
@Test public void extractsClassNameFromGenericClass() { String className = randomJavaId() + '<' + randomJavaId() + '>'; code.add("final class %s {", className); code.add("}"); assertEquals("Class name", className, code.typeName()); }
@Test public void extractsClassNameFromAbstractClass() { String className = randomJavaId(); code.add("public abstract class %s {", className); code.add("}"); assertEquals("Class name", className, code.typeName()); }
@Test public void extractsClassNameFromMultiGenericClass() { String className = randomJavaId(); String superClass = randomJavaId() + '<' + randomJavaId() + ", " + randomJavaId().toLowerCase(Locale.getDefault()) + '.' + randomJavaId() + '>'; code.add("public class %s extends %s {", className, superClass); code.add("}"); assertEquals("Class name", className, code.typeName()); }
@Test public void extractsClassNameFromExtendedGenericClass() { String className = randomJavaId() + '<' + randomJavaId() + " extends " + randomJavaId() + '>'; code.add("class %s {", className); code.add("}"); assertEquals("Class name", className, code.typeName()); }
@Test public void extractsClassNameWithComment() { String className = randomJavaId(); code.add("public class %s { code.add("}"); assertEquals("Class name", className, code.typeName()); }
@Test public void extractsClassNameFromGenericGenericClass() { String className = randomJavaId(); String genericClass1 = randomJavaId(); String genericClass2 = randomJavaId(); String fullClass = className + "<T extends " + genericClass1 + "<" + genericClass2 + ">>"; code.add("public abstract class %s {", fullClass); code.add("}"); assertEquals("Class name", fullClass, code.typeName()); }
@Test public void extractsTypeNameFromInterface() { String interfaceName = randomJavaId(); code.add("public interface %s {", interfaceName); code.add("}"); assertEquals("Type name", interfaceName, code.typeName()); }
@Test(expected = IllegalStateException.class) public void throwsExceptionOnInvalidJavaCode() { code.add(someValue()); code.typeName(); }
@Test public void extractsClassName() { String className = randomJavaId(); code.add("public class %s {", className); code.add("}"); assertEquals("Class name", className, code.typeName()); }