diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/InheritanceComparator.java b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/InheritanceComparator.java index 80c5e6735..bd56b81fe 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/InheritanceComparator.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/InheritanceComparator.java @@ -30,11 +30,10 @@ import java.util.Comparator; public class InheritanceComparator implements Comparator, Serializable { - private Class _base = Object.class; + private Class _base = null; /** - * Set the least-derived type possible; defaults to - * Object.class. + * Set the least-derived type possible; defaults to null. */ public void setBase(Class base) { _base = base; @@ -75,10 +74,9 @@ public class InheritanceComparator return 1; return c1.getName().compareTo(c2.getName()); } - int diff = i1 - i2; - if (diff < 0) + if (i1 < i2) return -1; - else if (diff > 0) + else if (i1 > i2) return 1; else return 0; @@ -90,6 +88,8 @@ public class InheritanceComparator private int levels(Class to) { if (to.isInterface()) return to.getInterfaces().length; + if (_base == null) + return 0; for (int i = 0; to != null; i++, to = to.getSuperclass()) if (to == _base) return i; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/A.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/A.java index 5e702b132..2062d3f5a 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/A.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/A.java @@ -19,8 +19,10 @@ package org.apache.openjpa.meta; import javax.persistence.Entity; +import javax.persistence.Table; @Entity +@Table(name="meta_A") public class A extends AbstractThing { } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/AbstractThing.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/AbstractThing.java index 79e837dae..7fb70373e 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/AbstractThing.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/AbstractThing.java @@ -35,7 +35,7 @@ public class AbstractThing { return id; } - void setId(final String id) { + protected void setId(final String id) { this.id = id; } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/B.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/B.java index 16ecf5979..e89ba2425 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/B.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/B.java @@ -21,10 +21,12 @@ package org.apache.openjpa.meta; import java.util.Set; import javax.persistence.Entity; import javax.persistence.OneToMany; +import javax.persistence.Table; import org.apache.openjpa.meta.C; @Entity +@Table(name="meta_B") public class B extends AbstractThing { private Set cs; private Set as; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/C.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/C.java index c7f901e6f..83f99aaa1 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/C.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/C.java @@ -23,11 +23,13 @@ import javax.persistence.IdClass; import javax.persistence.ManyToOne; import javax.persistence.Column; import javax.persistence.Id; +import javax.persistence.Table; import org.apache.openjpa.meta.C.Identity; @Entity @IdClass(Identity.class) +@Table(name="meta_C") public class C { private A a; private B b; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/TestMetaDataInheritanceComparator.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/TestMetaDataInheritanceComparator.java index 84ad50492..257e66605 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/TestMetaDataInheritanceComparator.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/TestMetaDataInheritanceComparator.java @@ -20,16 +20,45 @@ package org.apache.openjpa.meta; import javax.persistence.EntityManagerFactory; -import junit.framework.TestCase; import org.apache.openjpa.persistence.test.PersistenceTestCase; -import org.apache.openjpa.persistence.test.SingleEMFTestCase; import org.apache.openjpa.persistence.JPAFacadeHelper; public class TestMetaDataInheritanceComparator extends PersistenceTestCase { - public void testMetaDataInheritanceComparator() { + public void testInheritanceComparatorWithoutBase() { + inheritanceComparatorHelper(false); + } + + public void testInheritanceComparatorWithBase() { + inheritanceComparatorHelper(true); + } + + private void inheritanceComparatorHelper(boolean base) { + InheritanceComparator comp = new InheritanceComparator(); + if (base) + comp.setBase(AbstractThing.class); + + assertEquals(-1, comp.compare(A.class, B.class)); + assertEquals(-1, comp.compare(B.class, C.class)); + assertTrue(comp.compare(A.class, C.class) < 0); + + assertEquals(-1, comp.compare(AbstractThing.class, A.class)); + assertEquals(-1, comp.compare(AbstractThing.class, B.class)); + assertTrue(comp.compare(AbstractThing.class, C.class) < 0); + } + + public void testMetaDataInheritanceComparatorWithoutBase() { + metaDataInheritanceComparatorHelper(false); + } + + public void testMetaDataInheritanceComparatorWithBase() { + metaDataInheritanceComparatorHelper(true); + } + + private void metaDataInheritanceComparatorHelper(boolean base) { InheritanceComparator comp = new MetaDataInheritanceComparator(); - comp.setBase(AbstractThing.class); + if (base) + comp.setBase(AbstractThing.class); EntityManagerFactory emf = createEMF(A.class, B.class, C.class, AbstractThing.class); @@ -37,18 +66,29 @@ public class TestMetaDataInheritanceComparator extends PersistenceTestCase { ClassMetaData a = JPAFacadeHelper.getMetaData(emf, A.class); ClassMetaData b = JPAFacadeHelper.getMetaData(emf, B.class); ClassMetaData c = JPAFacadeHelper.getMetaData(emf, C.class); + ClassMetaData at = JPAFacadeHelper.getMetaData(emf, + AbstractThing.class); + + emf.close(); assertEquals(-1, comp.compare(a, b)); assertEquals(-1, comp.compare(b, c)); - assertEquals(-1, comp.compare(a, c)); + assertTrue(comp.compare(a, c) < 0); + + assertEquals(1, comp.compare(b, a)); + assertEquals(1, comp.compare(c, b)); + assertTrue(comp.compare(c, a) > 0); + + assertEquals(-1, comp.compare(at, a)); + assertEquals(-1, comp.compare(at, b)); + assertEquals(-1, comp.compare(at, c)); } - public void testInheritanceComparator() { - InheritanceComparator comp = new InheritanceComparator(); - comp.setBase(AbstractThing.class); - - assertEquals(-1, comp.compare(A.class, B.class)); - assertEquals(-1, comp.compare(B.class, C.class)); - assertEquals(-1, comp.compare(A.class, C.class)); + public void testEndToEnd() { + // make sure we can get things fully instantiated + EntityManagerFactory emf = createEMF(A.class, B.class, C.class, + AbstractThing.class); + emf.createEntityManager().close(); + emf.close(); } }