Skip to content

Commit

Permalink
fix basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elifKurtay committed Sep 24, 2024
1 parent 29dba08 commit f542dec
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,8 @@ public void visitClass(ClassElement element, VisitorContext context) {
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
addAnnotations(utilsBuilder, element.getAnnotation(Utils.class));

// create all fields
// TODO: should I check for properties that have getters?
List<PropertyElement> properties = element.getBeanProperties();
for (PropertyElement beanProperty : properties) {
createPropertyField(utilsBuilder, beanProperty);
}

// constructors and builders needed
utilsBuilder.addMethod(MethodDef.constructor().build());
if (!properties.isEmpty()) {
utilsBuilder.addMethod(createAllPropertiesConstructor(builderType, properties));
}

// create the utils functions
createToStringMethod(utilsBuilder, simpleName, properties);
Expand Down Expand Up @@ -143,46 +134,8 @@ static void addAnnotations(ClassDef.ClassDefBuilder builder, AnnotationValue<?>
}
}

private static void createPropertyField(ClassDef.ClassDefBuilder classDefBuilder,
PropertyElement beanProperty) {
if (beanProperty.hasAnnotation(Singular.class)) {
String propertyName = beanProperty.getSimpleName();
String singularName = beanProperty.stringValue(Singular.class).orElse(null);
if (singularName == null) {
singularName = singularize(propertyName);
if (singularName == null) {
throw new IllegalStateException("Cannot determine singular name for property: " + beanProperty.getName() + ". Please specify a singular name: @Singular(\"singularName\")");
}
}
if (beanProperty.getType().isAssignable(Iterable.class)) {
TypeDef singularTypeDef = beanProperty.getType().getFirstTypeArgument().<TypeDef>map(ClassTypeDef::of).orElse(TypeDef.OBJECT);
TypeDef fieldType = TypeDef.parameterized(ArrayList.class, singularTypeDef);
FieldDef field = createField(beanProperty, fieldType);
classDefBuilder.addField(field);
} else if (beanProperty.getType().isAssignable(Map.class)) {
TypeDef keyType = beanProperty.getType().getFirstTypeArgument().<TypeDef>map(ClassTypeDef::of).orElse(TypeDef.OBJECT);
TypeDef valueType = beanProperty.getType().getTypeArguments().values().stream().skip(1).findFirst().<TypeDef>map(ClassTypeDef::of).orElse(TypeDef.OBJECT);
ClassTypeDef mapEntryType = TypeDef.parameterized(
Map.Entry.class,
keyType,
valueType
);
ClassTypeDef fieldType = TypeDef.parameterized(ArrayList.class, mapEntryType);
FieldDef field = createField(beanProperty, fieldType);
classDefBuilder.addField(field);
} else {
throw new IllegalStateException("Unsupported singular collection type [" + beanProperty.getType().getName() + "] for property: " + beanProperty.getName());
}
} else {
TypeDef propertyTypeDef = TypeDef.of(beanProperty.getType());
FieldDef field = createField(beanProperty, propertyTypeDef);
classDefBuilder.addField(field);
}
}

/* TODO: complete toString method
- Added with @ToString annotation
- change method signature
*/
private static void createToStringMethod(ClassDef.ClassDefBuilder classDefBuilder, String simpleName, List<PropertyElement> properties) {
List<StatementDef> statements = new ArrayList<>();
Expand All @@ -194,7 +147,7 @@ private static void createToStringMethod(ClassDef.ClassDefBuilder classDefBuilde
statements.add(strBuilder.invoke(
"append",
ClassTypeDef.of(strBuilder.getClass()),
ExpressionDef.constant(simpleName + "[")
ExpressionDef.constant(simpleName.substring(0, simpleName.length() - 5) + "[")
));
for (int i = 0; i < properties.size(); i++) {
PropertyElement beanProperty = properties.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,30 @@
//tag::clazz[]
import io.micronaut.sourcegen.annotations.Utils;

import java.util.Arrays;

@Utils
public record Person4(long id, String name, int age, byte[] bytes) {
public class Person4 {
private long id;
private String name;
private byte[] bytes;

public Person4(long id, String name, byte[] bytes) {
this.id = id;
this.name = name;
this.bytes = Arrays.copyOf(bytes, bytes.length);
}

public long getId() {
return id;
}

public String getName() {
return name;
}

public byte[] getBytes() {
return bytes;
}
}
//end::clazz[]
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,34 @@ public class PersonUtilsTest {

@Test
public void testToString() {
var person = new Person4Utils(123L, "Cédric", 20, new byte[]{1,2,3});
assertNotNull(person.toString());
assertTrue(person.toString().contains("Person4Utils["));
var person = new Person4(123L, "Cédric", new byte[]{1,2,3});
assertNotNull(Person4Utils.toString(person));
assertTrue(Person4Utils.toString(person).contains("Person4["));
}


@Test
public void testEquals() {
var person = new Person4Utils(123L, "Cédric", 20, new byte[]{1,2,3});
var personSame = new Person4Utils(123L, "Cédric", 20, new byte[]{1,2,3});
var personDiffAll = new Person4Utils(124L, "Cédric 2", 21, new byte[]{1,2,3, 4});
var personDiffPrimitive = new Person4Utils(123L, "Cédric", 21, new byte[]{1,2,3});
var personDiffObject = new Person4Utils(123L, "Cédric", 20, new byte[]{1,2,3, 4});

assertTrue(person.equals(personSame));
assertTrue(person.equals(person));
assertFalse(person.equals(personDiffAll));
assertFalse(person.equals(personDiffPrimitive));
assertFalse(person.equals(personDiffObject));
var person = new Person4(123L, "Cédric", new byte[]{1,2,3});
var personSame = new Person4(123L, "Cédric", new byte[]{1,2,3});
var personDiffAll = new Person4(124L, "Cédric 2", new byte[]{1,2,3, 4});
var personDiffPrimitive = new Person4(124L, "Cédric", new byte[]{1,2,3});
var personDiffObject = new Person4(123L, "Cédric", new byte[]{1,2,3, 4});

assertTrue(Person4Utils.equals(person, personSame));
assertTrue(Person4Utils.equals(person, person));
assertFalse(Person4Utils.equals(person, personDiffAll));
assertFalse(Person4Utils.equals(person, personDiffPrimitive));
assertFalse(Person4Utils.equals(person, personDiffObject));
}

@Test
public void testHashCode() {
var person = new Person4Utils(123L, "Cédric", 20, new byte[]{1,2,3});
var personSame = new Person4Utils(123L, "Cédric", 20, new byte[]{1,2,3});
var personDiffAll = new Person4Utils(124L, "Cédric 2", 21, new byte[]{1,2,3, 4});
assertEquals(person.hashCode(), personSame.hashCode());
assertNotEquals(person.hashCode(), personDiffAll.hashCode());
var person = new Person4(123L, "Cédric", new byte[]{1,2,3});
var personSame = new Person4(123L, "Cédric", new byte[]{1,2,3});
var personDiffAll = new Person4(124L, "Cédric 2", new byte[]{1,2,3, 4});
assertEquals(Person4Utils.hashCode(person), Person4Utils.hashCode(personSame));
assertNotEquals(Person4Utils.hashCode(person), Person4Utils.hashCode(personDiffAll));
}

}

0 comments on commit f542dec

Please sign in to comment.