### Question:
JavaCode extends Code { public Collection<String> typeAnnotations() { Matcher matcher = getTypeNameMatcher(); List<String> result = new ArrayList<>(); String annotations = matcher.group(1); if (annotations != null) { for (String annotation : annotations.split("\\n")) { result.add(annotation.trim()); } Collections.sort(result); } return result; } 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 extractsEmptyClassAnnotations() { code.add("public class %s {", randomJavaId()); code.add("}"); assertEquals("Class annotations", Collections.emptyList(), code.typeAnnotations()); }
@Test public void extractsClassAnnotations() { String annotation1 = "@Z" + someValue(); String annotation2 = "@A" + someValue(); code.add(annotation1); code.add(annotation2); code.add("public class %s {", randomJavaId()); code.add("}"); assertEquals("Class annotations", Arrays.asList(annotation2, annotation1), code.typeAnnotations()); }