cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r713284 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/map/ test/java/org/apache/cayenne/access/ test/java/org/apache/cayenne/map/
Date Wed, 12 Nov 2008 07:19:51 GMT
Author: aadamchik
Date: Tue Nov 11 23:19:50 2008
New Revision: 713284

URL: http://svn.apache.org/viewvc?rev=713284&view=rev
Log:
CAY-795 Horizontal inheritance

code cleanup
attribute override unit tests

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntitySingleTableInheritanceTest.java
      - copied, changed from r713283, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityInheritanceTest.java
Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityInheritanceTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/HorizontalInheritanceTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java?rev=713284&r1=713283&r2=713284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjAttribute.java
Tue Nov 11 23:19:50 2008
@@ -50,7 +50,11 @@
         setEntity(entity);
     }
 
-    /** @since 3.0 */
+    /**
+     * Creates a clone of an ObjAttribute argument.
+     * 
+     * @since 3.0
+     */
     public ObjAttribute(ObjAttribute attribute) {
         setName(attribute.getName());
         setType(attribute.getType());
@@ -99,8 +103,9 @@
         }
 
         // If this obj attribute is mapped to db attribute
-        if (getDbAttribute() != null || 
-                (((ObjEntity)getEntity()).getIsAbstract() && !Util.isEmptyString(getDbAttributePath())))
{
+        if (getDbAttribute() != null
+                || (((ObjEntity) getEntity()).getIsAbstract() && !Util
+                        .isEmptyString(getDbAttributePath()))) {
             encoder.print(" db-attribute-path=\"");
             encoder.print(Util.encodeXmlAttribute(getDbAttributePath()));
             encoder.print('\"');
@@ -124,15 +129,15 @@
     public void setType(String type) {
         this.type = type;
     }
-    
+
     /**
      * @since 3.0
      */
     public boolean isPrimaryKey() {
-        if(dbAttributePath == null) {
+        if (dbAttributePath == null) {
             return false;
         }
-        
+
         DbAttribute dbAttribute = getDbAttribute();
         return dbAttribute != null && dbAttribute.isPrimaryKey();
     }
@@ -188,24 +193,25 @@
 
         return null;
     }
-    
+
     /**
-     * Return <code>true</code> if attribute inhertit from parent {@link ObjEntity}.
-     *  
-     * @since 3.0 
+     * Returns <code>true</code> if attribute inherited from a super entity.
+     * 
+     * @since 3.0
      */
     public boolean isInherited() {
-        if (getEntity() == null) {
+        ObjEntity owningEntity = (ObjEntity) getEntity();
+        if (owningEntity == null) {
             return false;
         }
-        
-        ObjEntity parent = ((ObjEntity) getEntity()).getSuperEntity();
 
-        if (parent == null) {
+        ObjEntity superEntity = owningEntity.getSuperEntity();
+
+        if (superEntity == null) {
             return false;
         }
 
-        return parent.getAttribute(getName()) != null;
+        return superEntity.getAttribute(getName()) != null;
     }
 
     public Iterator<CayenneMapEntry> getDbPathIterator() {
@@ -216,7 +222,7 @@
         if (dbAttributePath == null) {
             return IteratorUtils.EMPTY_ITERATOR;
         }
-       
+
         if (entity == null) {
             return IteratorUtils.EMPTY_ITERATOR;
         }
@@ -282,9 +288,9 @@
 
     public void setDbAttributePath(String dbAttributePath) {
         this.dbAttributePath = dbAttributePath;
-        
+
         if (isInherited()) {
-            ((ObjEntity)entity).addAttributeOverride(getName(), dbAttributePath);
+            ((ObjEntity) entity).addAttributeOverride(getName(), dbAttributePath);
         }
     }
 
@@ -326,7 +332,7 @@
 
         DbAttribute dbAttribute = getDbAttribute();
         if (dbAttribute != null) {
-            
+
             // expose PK attribute names - the client may need those to build ObjectIds
             if (dbAttribute.isPrimaryKey()) {
                 attribute.setDbAttributePath(dbAttribute.getName());

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java?rev=713284&r1=713283&r2=713284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java
Tue Nov 11 23:19:50 2008
@@ -652,8 +652,9 @@
     }
 
     /**
-     * Returns a named attribute that either belongs to this ObjEntity or is inherited.
-     * Returns null if no matching attribute is found.
+     * Returns a named attribute that is either declared in this ObjEntity or is
+     * inherited. In any case returned attribute 'getEntity' method will return this
+     * entity. Returns null if no matching attribute is found.
      */
     @Override
     public Attribute getAttribute(String name) {
@@ -672,26 +673,27 @@
             }
         }
 
-        if (superEntityName == null) {
-            return null;
-        }
-
+        // check super attribute
         ObjEntity superEntity = getSuperEntity();
         if (superEntity != null) {
-            Attribute attr = superEntity.getAttribute(name);
-            ObjAttribute result = null;
-            if (attr instanceof ObjAttribute) {
-                String overriddedDbPath = overriddenAttributes.get(name);
-                result = new ObjAttribute((ObjAttribute) attr);
-                result.setEntity(this);
-                if (overriddedDbPath != null) {
-                    result.setDbAttributePath(overriddedDbPath);
-                }
-                return result;
+
+            ObjAttribute superAttribute = (ObjAttribute) superEntity.getAttribute(name);
+            if (superAttribute == null) {
+                return null;
             }
 
-            return attr;
+            // decorate returned attribute to make it appear as if it belongs to this
+            // entity
+
+            ObjAttribute decoratedAttribute = new ObjAttribute(superAttribute);
+            decoratedAttribute.setEntity(this);
+
+            String pathOverride = overriddenAttributes.get(name);
+            if (pathOverride != null) {
+                decoratedAttribute.setDbAttributePath(pathOverride);
+            }
 
+            return decoratedAttribute;
         }
 
         return null;

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/HorizontalInheritanceTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/HorizontalInheritanceTest.java?rev=713284&r1=713283&r2=713284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/HorizontalInheritanceTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/HorizontalInheritanceTest.java
Tue Nov 11 23:19:50 2008
@@ -18,16 +18,14 @@
  ****************************************************************/
 package org.apache.cayenne.access;
 
-import org.apache.cayenne.ObjectContext;
-import org.apache.cayenne.unit.InheritanceCase;
-import org.apache.cayenne.testdo.horizontalinherit.SubEntity1;
-import org.apache.cayenne.testdo.horizontalinherit.AbstractSuperEntity;
-import org.apache.cayenne.testdo.horizontalinherit.SubEntity2;
+import java.util.List;
+
+import org.apache.cayenne.PersistenceState;
 import org.apache.cayenne.query.QueryChain;
-import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.query.SQLTemplate;
-
-import java.util.List;
+import org.apache.cayenne.query.SelectQuery;
+import org.apache.cayenne.testdo.horizontalinherit.SubEntity1;
+import org.apache.cayenne.unit.InheritanceCase;
 
 /**
  * Tests for horizontal inheritance implementation.
@@ -41,30 +39,32 @@
         deleteTestData();
     }
 
-    public void testAbstractSuperEntity() {
-        ObjectContext context = createDataContext();
-        SubEntity1 subEntity1 = context.newObject(SubEntity1.class);
-        subEntity1.setSuperIntAttr(666);
-        subEntity1.setSuperStringAttr("stringValue");
-        subEntity1.setSubEntityStringAttr("anotherStringValue");
-        context.commitChanges();
-
-        SelectQuery concreteSelect = new SelectQuery(SubEntity1.class);
-        List result = context.performQuery(concreteSelect);
-        assertNotNull(result);
-        assertEquals(1, result.size());
-
-        SubEntity2 subEntity2 = context.newObject(SubEntity2.class);
-        subEntity2.setSuperIntAttr(666);
-        subEntity2.setSuperStringAttr("stringValue");
-        subEntity2.setSubEntityIntAttr(13);
-        context.commitChanges();
-
-        SelectQuery abstractSelect = new SelectQuery(AbstractSuperEntity.class);
-        // this fails for now
-        // List result1 = context.performQuery(abstractSelect);
-        // assertNotNull(result1);
-        // assertEquals(2, result1.size());
+    public void testSelectQueryOnConcreteLeafEntity() {
+
+        QueryChain inserts = new QueryChain();
+        inserts
+                .addQuery(new SQLTemplate(
+                        SubEntity1.class,
+                        "INSERT INTO INHERITANCE_SUB_ENTITY1 "
+                                + "(ID, SUBENTITY_STRING_DB_ATTR, SUPER_INT_DB_ATTR, SUPER_STRING_DB_ATTR)
"
+                                + "VALUES (1, 'V11', 1, 'V21')"));
+        inserts
+                .addQuery(new SQLTemplate(
+                        SubEntity1.class,
+                        "INSERT INTO INHERITANCE_SUB_ENTITY1 "
+                                + "(ID, SUBENTITY_STRING_DB_ATTR, SUPER_INT_DB_ATTR, SUPER_STRING_DB_ATTR)
"
+                                + "VALUES (2, 'V12',2, 'V22')"));
+        createDataContext().performGenericQuery(inserts);
+
+        SelectQuery select = new SelectQuery(SubEntity1.class);
+        select.addOrdering(SubEntity1.SUB_ENTITY_STRING_ATTR_PROPERTY, true);
+
+        List<SubEntity1> result = createDataContext().performQuery(select);
+        assertEquals(2, result.size());
+        assertEquals(PersistenceState.COMMITTED, result.get(0).getPersistenceState());
+        assertEquals("V11", result.get(0).getSubEntityStringAttr());
+        assertEquals(PersistenceState.COMMITTED, result.get(1).getPersistenceState());
+        assertEquals("V12", result.get(1).getSubEntityStringAttr());
     }
 
     public void testDatabaseUnionCapabilities() {

Copied: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntitySingleTableInheritanceTest.java
(from r713283, cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityInheritanceTest.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntitySingleTableInheritanceTest.java?p2=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntitySingleTableInheritanceTest.java&p1=cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityInheritanceTest.java&r1=713283&r2=713284&rev=713284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityInheritanceTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntitySingleTableInheritanceTest.java
Tue Nov 11 23:19:50 2008
@@ -25,7 +25,7 @@
 
 /**
  */
-public class ObjEntityInheritanceTest extends BasicCase {
+public class ObjEntitySingleTableInheritanceTest extends BasicCase {
 
     protected DataMap map;
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityTest.java?rev=713284&r1=713283&r2=713284&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/ObjEntityTest.java
Tue Nov 11 23:19:50 2008
@@ -33,6 +33,39 @@
 
 public class ObjEntityTest extends CayenneCase {
 
+    public void testGetAttributeWithOverrides() {
+
+        DataMap map = new DataMap("dm");
+
+        ObjEntity superEntity = new ObjEntity("super");
+        superEntity.addAttribute(new ObjAttribute("a1", "int", superEntity));
+        superEntity.addAttribute(new ObjAttribute("a2", "int", superEntity));
+
+        map.addObjEntity(superEntity);
+
+        ObjEntity subEntity = new ObjEntity("sub");
+        subEntity.setSuperEntityName(superEntity.getName());
+        subEntity.addAttributeOverride("a1", "overridden.path");
+        subEntity.addAttribute(new ObjAttribute("a3", "int", subEntity));
+
+        map.addObjEntity(subEntity);
+
+        ObjAttribute a1 = (ObjAttribute) subEntity.getAttribute("a1");
+        assertNotNull(a1);
+        assertSame(subEntity, a1.getEntity());
+        assertEquals("overridden.path", a1.getDbAttributePath());
+        assertEquals("int", a1.getType());
+
+        ObjAttribute a2 = (ObjAttribute) subEntity.getAttribute("a2");
+        assertNotNull(a2);
+        assertSame(subEntity, a2.getEntity());
+        assertNull(a2.getDbAttributePath());
+
+        ObjAttribute a3 = (ObjAttribute) subEntity.getAttribute("a3");
+        assertNotNull(a3);
+        assertSame(subEntity, a3.getEntity());
+    }
+
     public void testGetPrimaryKeys() {
         ObjEntity artistE = getObjEntity("Artist");
         Collection<ObjAttribute> pks = artistE.getPrimaryKeys();
@@ -61,7 +94,7 @@
 
         ObjAttribute mpk = mpks.iterator().next();
         assertTrue(meaningfulPKE.getAttributes().contains(mpk));
-        
+
         ObjEntity clientMeaningfulPKE = meaningfulPKE.getClientEntity();
         Collection<ObjAttribute> clientmpks = clientMeaningfulPKE.getPrimaryKeys();
         assertEquals(1, clientmpks.size());



Mime
View raw message