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
implements Comparator, Serializable {
private Class _base = Object.class;
private Class _base = null;
/**
* Set the least-derived type possible; defaults to
* <code>Object.class</code>.
* Set the least-derived type possible; defaults to <code>null</code>.
*/
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;

View File

@ -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 {
}

View File

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

View File

@ -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<C> cs;
private Set<A> as;

View File

@ -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;

View File

@ -20,15 +20,44 @@ 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();
if (base)
comp.setBase(AbstractThing.class);
EntityManagerFactory emf = createEMF(A.class, B.class, C.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();
}
}