OPENJPA-258 Merge from ../branches/1.0.x. svn merge -c 617073

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@617199 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Patrick Linskey 2008-01-31 19:40:34 +00:00
parent 62c8c548d9
commit 171dc5d46a
6 changed files with 65 additions and 19 deletions

View File

@ -30,11 +30,10 @@ import java.util.Comparator;
public class InheritanceComparator public class InheritanceComparator
implements Comparator, Serializable { implements Comparator, Serializable {
private Class _base = Object.class; private Class _base = null;
/** /**
* Set the least-derived type possible; defaults to * Set the least-derived type possible; defaults to <code>null</code>.
* <code>Object.class</code>.
*/ */
public void setBase(Class base) { public void setBase(Class base) {
_base = base; _base = base;
@ -75,10 +74,9 @@ public class InheritanceComparator
return 1; return 1;
return c1.getName().compareTo(c2.getName()); return c1.getName().compareTo(c2.getName());
} }
int diff = i1 - i2; if (i1 < i2)
if (diff < 0)
return -1; return -1;
else if (diff > 0) else if (i1 > i2)
return 1; return 1;
else else
return 0; return 0;
@ -90,6 +88,8 @@ public class InheritanceComparator
private int levels(Class to) { private int levels(Class to) {
if (to.isInterface()) if (to.isInterface())
return to.getInterfaces().length; return to.getInterfaces().length;
if (_base == null)
return 0;
for (int i = 0; to != null; i++, to = to.getSuperclass()) for (int i = 0; to != null; i++, to = to.getSuperclass())
if (to == _base) if (to == _base)
return i; return i;

View File

@ -19,8 +19,10 @@
package org.apache.openjpa.meta; package org.apache.openjpa.meta;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table;
@Entity @Entity
@Table(name="meta_A")
public class A extends AbstractThing { public class A extends AbstractThing {
} }

View File

@ -35,7 +35,7 @@ public class AbstractThing {
return id; return id;
} }
void setId(final String id) { protected void setId(final String id) {
this.id = id; this.id = id;
} }
} }

View File

@ -21,10 +21,12 @@ package org.apache.openjpa.meta;
import java.util.Set; import java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.apache.openjpa.meta.C; import org.apache.openjpa.meta.C;
@Entity @Entity
@Table(name="meta_B")
public class B extends AbstractThing { public class B extends AbstractThing {
private Set<C> cs; private Set<C> cs;
private Set<A> as; private Set<A> as;

View File

@ -23,11 +23,13 @@ import javax.persistence.IdClass;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.openjpa.meta.C.Identity; import org.apache.openjpa.meta.C.Identity;
@Entity @Entity
@IdClass(Identity.class) @IdClass(Identity.class)
@Table(name="meta_C")
public class C { public class C {
private A a; private A a;
private B b; private B b;

View File

@ -20,16 +20,45 @@ package org.apache.openjpa.meta;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import junit.framework.TestCase;
import org.apache.openjpa.persistence.test.PersistenceTestCase; import org.apache.openjpa.persistence.test.PersistenceTestCase;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
import org.apache.openjpa.persistence.JPAFacadeHelper; import org.apache.openjpa.persistence.JPAFacadeHelper;
public class TestMetaDataInheritanceComparator extends PersistenceTestCase { 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(); InheritanceComparator comp = new MetaDataInheritanceComparator();
comp.setBase(AbstractThing.class); if (base)
comp.setBase(AbstractThing.class);
EntityManagerFactory emf = createEMF(A.class, B.class, C.class, EntityManagerFactory emf = createEMF(A.class, B.class, C.class,
AbstractThing.class); AbstractThing.class);
@ -37,18 +66,29 @@ public class TestMetaDataInheritanceComparator extends PersistenceTestCase {
ClassMetaData a = JPAFacadeHelper.getMetaData(emf, A.class); ClassMetaData a = JPAFacadeHelper.getMetaData(emf, A.class);
ClassMetaData b = JPAFacadeHelper.getMetaData(emf, B.class); ClassMetaData b = JPAFacadeHelper.getMetaData(emf, B.class);
ClassMetaData c = JPAFacadeHelper.getMetaData(emf, C.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(a, b));
assertEquals(-1, comp.compare(b, c)); 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() { public void testEndToEnd() {
InheritanceComparator comp = new InheritanceComparator(); // make sure we can get things fully instantiated
comp.setBase(AbstractThing.class); EntityManagerFactory emf = createEMF(A.class, B.class, C.class,
AbstractThing.class);
assertEquals(-1, comp.compare(A.class, B.class)); emf.createEntityManager().close();
assertEquals(-1, comp.compare(B.class, C.class)); emf.close();
assertEquals(-1, comp.compare(A.class, C.class));
} }
} }