EntityUtils { public static List<String> getNamesOfIdProperties(final Class<?> clazz) { final List<String> idList = new ArrayList<>(); for (Class<?> c = clazz; c != null; c = c.getSuperclass()) { collectIdNamesFromFields(idList, c); collectIdNamesFromMethods(idList, c); } return idList; } private  EntityUtils(); static List<String> getNamesOfIdProperties(final Class<?> clazz); static Class<?> getEntityClassFromNodeLabels(final List<String> labels, final List<Class<?>> classes);  }
@Test public void testGetNamesOfIdPropertiesFromASingleClassHavingAFieldAnnotatedWithId() throws Exception { final String simpleClassName = "EntityClass"; final String idPropertyName = "key"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, simpleClassName); jClass.annotate(Entity.class); jClass.field(JMod.PRIVATE, String.class, idPropertyName).annotate(Id.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> entityClass = loadClass(testFolder.getRoot(), jClass.name()); final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(entityClass); assertThat(namesOfIdProperties.size(), equalTo(1)); assertThat(namesOfIdProperties, hasItem(idPropertyName)); }
@Test public void testGetNamesOfIdPropertiesFromASingleClassHavingAMethodAnnotatedWithId() throws Exception { final String simpleClassName = "EntityClass"; final String idPropertyName = "key"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, simpleClassName); jClass.annotate(Entity.class); jClass.method(JMod.PUBLIC, jCodeModel.VOID, "getKey").annotate(Id.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> entityClass = loadClass(testFolder.getRoot(), jClass.name()); final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(entityClass); assertThat(namesOfIdProperties.size(), equalTo(1)); assertThat(namesOfIdProperties, hasItem(idPropertyName)); }
@Test public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAFieldAnnotatedWithId() throws Exception { final String simpleClassNameBase = "EntityClass"; final String simpleClassNameB = "SubEntityClass"; final String idPropertyName = "key"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase); jBaseClass.annotate(Entity.class); jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS); jBaseClass.field(JMod.PRIVATE, String.class, idPropertyName).annotate(Id.class); final JDefinedClass jSubclass = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass); jSubclass.annotate(Entity.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> subClass = loadClass(testFolder.getRoot(), jSubclass.name()); final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(subClass); assertThat(namesOfIdProperties.size(), equalTo(1)); assertThat(namesOfIdProperties, hasItem(idPropertyName)); }
@Test public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAMethodAnnotatedWithId() throws Exception { final String simpleClassNameBase = "EntityClass"; final String simpleClassNameB = "SubEntityClass"; final String idPropertyName = "key"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase); jBaseClass.annotate(Entity.class); jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS); jBaseClass.method(JMod.PUBLIC, jCodeModel.VOID, "getKey").annotate(Id.class); final JDefinedClass jSubclass = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass); jSubclass.annotate(Entity.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> subClass = loadClass(testFolder.getRoot(), jSubclass.name()); final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(subClass); assertThat(namesOfIdProperties.size(), equalTo(1)); assertThat(namesOfIdProperties, hasItem(idPropertyName)); }
@Test public void testGetNamesOfIdPropertiesFromASingleClassHavingAFieldAnnotatedWithEmbeddedId() throws Exception { final String simpleClassName = "EntityClass"; final String compositeIdPropertyName = "compositeKey"; final String id1PropertyName = "key1"; final String id2PropertyName = "key2"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jIdTypeClass = jp._class(JMod.PUBLIC, "IdType"); jIdTypeClass.annotate(Embeddable.class); jIdTypeClass.field(JMod.PRIVATE, Integer.class, id1PropertyName); jIdTypeClass.field(JMod.PRIVATE, String.class, id2PropertyName); final JDefinedClass jClass = jp._class(JMod.PUBLIC, simpleClassName); jClass.annotate(Entity.class); jClass.field(JMod.PRIVATE, jIdTypeClass, compositeIdPropertyName).annotate(EmbeddedId.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> entityClass = loadClass(testFolder.getRoot(), jClass.name()); final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(entityClass); assertThat(namesOfIdProperties.size(), equalTo(2)); assertThat(namesOfIdProperties, hasItems(compositeIdPropertyName + "." + id1PropertyName, compositeIdPropertyName + "." + id2PropertyName)); }
@Test public void testGetNamesOfIdPropertiesFromASingleClassHavingAMethodAnnotatedWithEmbeddedId() throws Exception { final String simpleClassName = "EntityClass"; final String compositeIdPropertyName = "compositeKey"; final String id1PropertyName = "key1"; final String id2PropertyName = "key2"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jIdTypeClass = jp._class(JMod.PUBLIC, "IdType"); jIdTypeClass.annotate(Embeddable.class); jIdTypeClass.field(JMod.PRIVATE, Integer.class, id1PropertyName); jIdTypeClass.field(JMod.PRIVATE, String.class, id2PropertyName); final JDefinedClass jClass = jp._class(JMod.PUBLIC, simpleClassName); jClass.annotate(Entity.class); final JMethod method = jClass.method(JMod.PUBLIC, jIdTypeClass, "getCompositeKey"); method.annotate(EmbeddedId.class); method.body()._return(JExpr._null()); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> entityClass = loadClass(testFolder.getRoot(), jClass.name()); final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(entityClass); assertThat(namesOfIdProperties.size(), equalTo(2)); assertThat(namesOfIdProperties, hasItems(compositeIdPropertyName + "." + id1PropertyName, compositeIdPropertyName + "." + id2PropertyName)); }
@Test public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAFieldAnnotatedWithEmbeddedId() throws Exception { final String simpleClassNameBase = "EntityClass"; final String simpleClassNameB = "SubEntityClass"; final String compositeIdPropertyName = "compositeKey"; final String id1PropertyName = "key1"; final String id2PropertyName = "key2"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jIdTypeClass = jp._class(JMod.PUBLIC, "IdType"); jIdTypeClass.annotate(Embeddable.class); jIdTypeClass.field(JMod.PRIVATE, Integer.class, id1PropertyName); jIdTypeClass.field(JMod.PRIVATE, String.class, id2PropertyName); final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase); jBaseClass.annotate(Entity.class); jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS); jBaseClass.field(JMod.PRIVATE, jIdTypeClass, compositeIdPropertyName).annotate(EmbeddedId.class); final JDefinedClass jSubclass = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass); jSubclass.annotate(Entity.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> entityClass = loadClass(testFolder.getRoot(), jSubclass.name()); final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(entityClass); assertThat(namesOfIdProperties.size(), equalTo(2)); assertThat(namesOfIdProperties, hasItems(compositeIdPropertyName + "." + id1PropertyName, compositeIdPropertyName + "." + id2PropertyName)); }
@Test public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAMethodAnnotatedWithEmbeddedId() throws Exception { final String simpleClassNameBase = "EntityClass"; final String simpleClassNameB = "SubEntityClass"; final String compositeIdPropertyName = "compositeKey"; final String id1PropertyName = "key1"; final String id2PropertyName = "key2"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jIdTypeClass = jp._class(JMod.PUBLIC, "IdType"); jIdTypeClass.annotate(Embeddable.class); jIdTypeClass.field(JMod.PRIVATE, Integer.class, id1PropertyName); jIdTypeClass.field(JMod.PRIVATE, String.class, id2PropertyName); final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase); jBaseClass.annotate(Entity.class); jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS); final JMethod method = jBaseClass.method(JMod.PUBLIC, jIdTypeClass, "getCompositeKey"); method.annotate(EmbeddedId.class); method.body()._return(JExpr._null()); final JDefinedClass jSubclass = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass); jSubclass.annotate(Entity.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> entityClass = loadClass(testFolder.getRoot(), jSubclass.name()); final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(entityClass); assertThat(namesOfIdProperties.size(), equalTo(2)); assertThat(namesOfIdProperties, hasItems(compositeIdPropertyName + "." + id1PropertyName, compositeIdPropertyName + "." + id2PropertyName)); }