EntanglingClass { @VisibleForTesting TypeSpec createTypeSpec() { TypeSpec.Builder result = TypeSpec.classBuilder(className) .addModifiers(PUBLIC) .addTypeVariable(TypeVariableName.get("T", ClassName.bestGuess(targetClass))); result.addSuperinterface(ClassName.get("java.util", "Observer")); result.addMethod(createEntangleMethod()); result.addMethod(createDetangleMethod()); result.addMethod(createUpdateMethod()); result.addField(FieldSpec.builder(TypeVariableName.get("T", ClassName.bestGuess(targetClass)), "target") .addModifiers(PRIVATE) .build()); result.addField(FieldSpec.builder(String.class, "id").initializer("\"$L\"", connectionId) .addModifiers(PRIVATE) .build()); return result.build(); } EntanglingClass(String classPackage, String className, String targetClass, String fieldName, String connectionId);   }
@Test public void typeSpecShouldHaveOnlyOneModifier() { TypeSpec typeSpec = entanglingClass.createTypeSpec(); assertThat(typeSpec.modifiers, hasSize(1)); }
@Test public void typeSpecShouldImplementObserver() { TypeSpec typeSpec = entanglingClass.createTypeSpec(); assertThat(typeSpec.superinterfaces, hasItem(ClassName.get("java.util", "Observer"))); }
@Test public void typeSpecShouldOnlyImplementOneInterface() { TypeSpec typeSpec = entanglingClass.createTypeSpec(); assertThat(typeSpec.superinterfaces, hasSize(1)); }
@Test public void typeSpecShouldHaveTwoFields() { TypeSpec typeSpec = entanglingClass.createTypeSpec(); assertThat(typeSpec.fieldSpecs, hasSize(2)); }
@Test public void typeSpecShouldHaveTargetAndIdFields() { TypeSpec typeSpec = entanglingClass.createTypeSpec(); assertThat(typeSpec.fieldSpecs, allOf( hasItem(FieldSpec.builder(TypeVariableName.get("T", ClassName.bestGuess(TARGET_CLASS)), "target") .addModifiers(PRIVATE) .build()), hasItem(FieldSpec.builder(String.class, "id").initializer("\"$L\"", ID) .addModifiers(PRIVATE) .build()))); }
@Test public void typeSpecShouldHaveCorrectName() { TypeSpec typeSpec = entanglingClass.createTypeSpec(); assertThat(typeSpec.name, is(CLASS_NAME)); }
@Test public void typeSpecShouldHaveParameterizedTypeVariable() { TypeSpec typeSpec = entanglingClass.createTypeSpec(); assertThat(typeSpec.typeVariables, hasItem(TypeVariableName.get("T", ClassName.bestGuess(TARGET_CLASS)))); }
@Test public void typeSpecShouldHaveOnlyOneTypeVariable() { TypeSpec typeSpec = entanglingClass.createTypeSpec(); assertThat(typeSpec.typeVariables, hasSize(1)); }
@Test public void typeSpecShouldBePublic() { TypeSpec typeSpec = entanglingClass.createTypeSpec(); assertThat(typeSpec.modifiers, hasItem(Modifier.PUBLIC)); }