EntityUtils { public static Class<?> getEntityClassFromNodeLabels(final List<String> labels, final List<Class<?>> classes) throws NoSuchClassException { for (final String label : labels) { final Optional<Class<?>> classHit = classes.stream().filter(c -> { if (c.getName().endsWith(label)) { return true; } else { final Table annotation = c.getAnnotation(Table.class); return annotation != null && annotation.name().equals(label); } }).findFirst(); if (classHit.isPresent()) { return classHit.get(); } } throw new NoSuchClassException("could not find class for a node with " + labels + " labels."); } private  EntityUtils(); static List<String> getNamesOfIdProperties(final Class<?> clazz); static Class<?> getEntityClassFromNodeLabels(final List<String> labels, final List<Class<?>> classes);  }
@Test public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithoutInheritance() throws Exception { final String simpleClassName = "EntityClass"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, simpleClassName); jClass.annotate(Entity.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> entityClass = loadClass(testFolder.getRoot(), jClass.name()); final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(simpleClassName), Arrays.asList(entityClass)); assertThat(clazz, equalTo(entityClass)); }
@Test public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithoutInheritance() throws Exception { final String simpleClassName = "EntityClass"; final String nodeLabel = "ENTITY_CLASS"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, simpleClassName); jClass.annotate(Entity.class); jClass.annotate(Table.class).param("name", nodeLabel); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> entityClass = loadClass(testFolder.getRoot(), jClass.name()); final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabel), Arrays.asList(entityClass)); assertThat(clazz, equalTo(entityClass)); }
@Test public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithSingleTableInheritance() throws Exception { final String simpleClassNameBase = "EntityClass"; final String simpleClassNameA = "SubEntityClassA"; final String simpleClassNameB = "SubEntityClassB"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase); jBaseClass.annotate(Entity.class); jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.SINGLE_TABLE); jBaseClass.annotate(DiscriminatorColumn.class).param("name", "TYPE"); final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass); jSubclassA.annotate(Entity.class); jSubclassA.annotate(DiscriminatorValue.class).param("value", "A"); final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass); jSubclassB.annotate(Entity.class); jSubclassB.annotate(DiscriminatorValue.class).param("value", "B"); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name()); final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name()); final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name()); final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(simpleClassNameBase), Arrays.asList(baseClass, subClassA, subClassB)); assertThat(clazz, equalTo(baseClass)); }
@Test public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithSingleTableInheritance() throws Exception { final String simpleClassNameBase = "EntityClass"; final String simpleClassNameA = "SubEntityClassA"; final String simpleClassNameB = "SubEntityClassB"; final String nodeLabel = "ENTITY_CLASS"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase); jBaseClass.annotate(Entity.class); jBaseClass.annotate(Table.class).param("name", nodeLabel); jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.SINGLE_TABLE); jBaseClass.annotate(DiscriminatorColumn.class).param("name", "TYPE"); final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass); jSubclassA.annotate(Entity.class); jSubclassA.annotate(DiscriminatorValue.class).param("value", "A"); final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass); jSubclassB.annotate(Entity.class); jSubclassB.annotate(DiscriminatorValue.class).param("value", "B"); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name()); final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name()); final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name()); final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabel), Arrays.asList(baseClass, subClassA, subClassB)); assertThat(clazz, equalTo(baseClass)); }
@Test public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithTablePerClassInheritance() throws Exception { final String simpleClassNameBase = "EntityClass"; final String simpleClassNameA = "SubEntityClassA"; final String simpleClassNameB = "SubEntityClassB"; 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); final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass); jSubclassA.annotate(Entity.class); final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass); jSubclassB.annotate(Entity.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name()); final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name()); final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name()); final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(simpleClassNameB), Arrays.asList(baseClass, subClassA, subClassB)); assertThat(clazz, equalTo(subClassB)); }
@Test public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithTablePerClassInheritance() throws Exception { final String simpleClassNameBase = "EntityClass"; final String simpleClassNameA = "SubEntityClassA"; final String simpleClassNameB = "SubEntityClassB"; final String nodeLabelBase = "ENTITY_CLASS"; final String nodeLabelA = "ENTITY_CLASS_A"; final String nodeLabelB = "ENTITY_CLASS_B"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase); jBaseClass.annotate(Entity.class); jBaseClass.annotate(Table.class).param("name", nodeLabelBase); jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS); final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass); jSubclassA.annotate(Entity.class); jSubclassA.annotate(Table.class).param("name", nodeLabelA); final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass); jSubclassB.annotate(Entity.class); jSubclassB.annotate(Table.class).param("name", nodeLabelB); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name()); final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name()); final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name()); final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabelA), Arrays.asList(baseClass, subClassA, subClassB)); assertThat(clazz, equalTo(subClassA)); }
@Test public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithJoinedInheritance() throws Exception { final String simpleClassNameBase = "EntityClass"; final String simpleClassNameA = "SubEntityClassA"; final String simpleClassNameB = "SubEntityClassB"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase); jBaseClass.annotate(Entity.class); jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.JOINED); final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass); jSubclassA.annotate(Entity.class); final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass); jSubclassB.annotate(Entity.class); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name()); final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name()); final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name()); final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(simpleClassNameB), Arrays.asList(baseClass, subClassA, subClassB)); assertThat(clazz, equalTo(subClassB)); }
@Test public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithJoinedInheritance() throws Exception { final String simpleClassNameBase = "EntityClass"; final String simpleClassNameA = "SubEntityClassA"; final String simpleClassNameB = "SubEntityClassB"; final String nodeLabelBase = "ENTITY_CLASS"; final String nodeLabelA = "ENTITY_CLASS_A"; final String nodeLabelB = "ENTITY_CLASS_B"; final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase); jBaseClass.annotate(Entity.class); jBaseClass.annotate(Table.class).param("name", nodeLabelBase); jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.JOINED); final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass); jSubclassA.annotate(Entity.class); jSubclassA.annotate(Table.class).param("name", nodeLabelA); final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass); jSubclassB.annotate(Entity.class); jSubclassB.annotate(Table.class).param("name", nodeLabelB); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name()); final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name()); final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name()); final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabelA), Arrays.asList(baseClass, subClassA, subClassB)); assertThat(clazz, equalTo(subClassA)); }