cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From and...@apache.org
Subject svn commit: r700708 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/access/ main/java/org/apache/cayenne/access/reveng/ main/java/org/apache/cayenne/util/ test/java/org/apache/cayenne/access/reveng/
Date Wed, 01 Oct 2008 07:43:35 GMT
Author: andrey
Date: Wed Oct  1 00:43:34 2008
New Revision: 700708

URL: http://svn.apache.org/viewvc?rev=700708&view=rev
Log: (empty)

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/BasicNamingStrategy.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/ExportedKey.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/NamingStrategy.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/reveng/
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/reveng/BasicNamingStrategyTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DbLoader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DbLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DbLoader.java?rev=700708&r1=700707&r2=700708&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DbLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/DbLoader.java
Wed Oct  1 00:43:34 2008
@@ -35,6 +35,9 @@
 import java.util.Set;
 
 import org.apache.cayenne.CayenneException;
+import org.apache.cayenne.access.reveng.BasicNamingStrategy;
+import org.apache.cayenne.access.reveng.ExportedKey;
+import org.apache.cayenne.access.reveng.NamingStrategy;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.TypesMapping;
 import org.apache.cayenne.map.DataMap;
@@ -48,7 +51,6 @@
 import org.apache.cayenne.map.Procedure;
 import org.apache.cayenne.map.ProcedureParameter;
 import org.apache.cayenne.util.EntityMergeSupport;
-import org.apache.cayenne.util.NameConverter;
 import org.apache.cayenne.util.Util;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -84,20 +86,13 @@
      */
     private Set<DbEntity> skippedEntities = new HashSet<DbEntity>();
 
-    /** Creates default name for loaded relationship */
-    private static String defaultRelName(String dstName, boolean toMany) {
-        String uglyName = (toMany) ? dstName + "_ARRAY" : "to_" + dstName;
-        return NameConverter.underscoredToJava(uglyName, false);
-    }
-
     /** Creates a unique name for loaded relationship on the given entity. */
-    private static String uniqueRelName(Entity entity, String dstName, boolean toMany) {
+    private static String uniqueRelName(Entity entity, String preferredName) {
         int currentSuffix = 1;
-        String baseRelName = defaultRelName(dstName, toMany);
-        String relName = baseRelName;
+        String relName = preferredName;
 
-        while (entity.getRelationship(relName) != null) {
-            relName = baseRelName + currentSuffix;
+        while (entity.getRelationship(relName) != null || entity.getAttribute(relName) !=
null) {
+            relName = preferredName + currentSuffix;
             currentSuffix++;
         }
         return relName;
@@ -108,12 +103,24 @@
     protected DatabaseMetaData metaData;
     protected DbLoaderDelegate delegate;
     protected String genericClassName;
+    
+    /**
+     * Strategy for choosing names for entities, attributes and relationships
+     */
+    protected NamingStrategy namingStrategy;
 
     /** Creates new DbLoader. */
     public DbLoader(Connection con, DbAdapter adapter, DbLoaderDelegate delegate) {
+        this(con, adapter, delegate, new BasicNamingStrategy());
+    }
+    
+    /** Creates new DbLoader with specified naming strategy. */
+    public DbLoader(Connection con, DbAdapter adapter, DbLoaderDelegate delegate, NamingStrategy
strategy) {
         this.adapter = adapter;
         this.con = con;
         this.delegate = delegate;
+        
+        setNamingStrategy(strategy);
     }
 
     /**
@@ -492,9 +499,7 @@
                 continue;
             }
 
-            String objEntityName = NameConverter.underscoredToJava(
-                    dbEntity.getName(),
-                    true);
+            String objEntityName = namingStrategy.createObjEntityName(dbEntity);
             // this loop will terminate even if no valid name is found
             // to prevent loader from looping forever (though such case is very unlikely)
             String baseName = objEntityName;
@@ -518,7 +523,7 @@
         }
 
         // update ObjEntity attributes and relationships
-        new EntityMergeSupport(map).synchronizeWithDbEntities(loadedEntities);
+        new EntityMergeSupport(map, namingStrategy).synchronizeWithDbEntities(loadedEntities);
     }
 
     /** Loads database relationships into a DataMap. */
@@ -559,19 +564,23 @@
             DbRelationship forwardRelationship = null;
             DbRelationshipDetected reverseRelationship = null;
             DbEntity fkEntity = null;
+            ExportedKey key = null;
 
             do {
+                //extract data from resultset
+                key = ExportedKey.extractData(rs);
+                
                 short keySeq = rs.getShort("KEY_SEQ");
                 if (keySeq == 1) {
 
                     if (forwardRelationship != null) {
-                        postprocessMasterDbRelationship(forwardRelationship);
+                        postprocessMasterDbRelationship(forwardRelationship, key);
                         forwardRelationship = null;
                     }
 
                     // start new entity
-                    String fkEntityName = rs.getString("FKTABLE_NAME");
-                    String fkName = rs.getString("FK_NAME");
+                    String fkEntityName = key.getFKTableName();
+                    String fkName = key.getFKName();
                     
                     if (!includeTableName(fkEntityName)) {
                         continue;
@@ -589,19 +598,19 @@
                         continue;
                     }
                     else {
-
                         // init relationship
-                        forwardRelationship = new DbRelationship(DbLoader
-                                .uniqueRelName(pkEntity, fkEntityName, true));
+                        String forwardPreferredName = namingStrategy.createDbRelationshipName(key,
true);
+                        forwardRelationship = new DbRelationship(
+                                uniqueRelName(pkEntity, forwardPreferredName));
 
                         forwardRelationship.setSourceEntity(pkEntity);
                         forwardRelationship.setTargetEntity(fkEntity);
                         pkEntity.addRelationship(forwardRelationship);
 
+                        String reversePreferredName = namingStrategy.createDbRelationshipName(key,
false);
                         reverseRelationship = new DbRelationshipDetected(uniqueRelName(
                                 fkEntity,
-                                pkEntName,
-                                false));
+                                reversePreferredName));
                         reverseRelationship.setFkName(fkName);
                         reverseRelationship.setToMany(false);
                         reverseRelationship.setSourceEntity(fkEntity);
@@ -612,8 +621,8 @@
 
                 if (fkEntity != null) {
                     // Create and append joins
-                    String pkName = rs.getString("PKCOLUMN_NAME");
-                    String fkName = rs.getString("FKCOLUMN_NAME");
+                    String pkName = key.getPKColumnName();
+                    String fkName = key.getFKColumnName();
 
                     // skip invalid joins...
                     DbAttribute pkAtt = (DbAttribute) pkEntity.getAttribute(pkName);
@@ -642,7 +651,7 @@
             } while (rs.next());
 
             if (forwardRelationship != null) {
-                postprocessMasterDbRelationship(forwardRelationship);
+                postprocessMasterDbRelationship(forwardRelationship, key);
                 forwardRelationship = null;
             }
 
@@ -655,7 +664,7 @@
      * Detects correct relationship multiplicity and "to dep pk" flag. Only called on
      * relationships from PK to FK, not the reverse ones.
      */
-    protected void postprocessMasterDbRelationship(DbRelationship relationship) {
+    protected void postprocessMasterDbRelationship(DbRelationship relationship, ExportedKey
key) {
         boolean toPK = true;
         List<DbJoin> joins = relationship.getJoins();
 
@@ -682,8 +691,8 @@
         if (!toMany) {
             Entity source = relationship.getSourceEntity();
             source.removeRelationship(relationship.getName());
-            relationship.setName(DbLoader.uniqueRelName(source, relationship
-                    .getTargetEntityName(), false));
+            relationship.setName(DbLoader.uniqueRelName(source, 
+                    namingStrategy.createDbRelationshipName(key, false)));
             source.addRelationship(relationship);
         }
 
@@ -912,4 +921,23 @@
             dataMap.addProcedure(procedure);
         }
     }
+    
+    /**
+     * Sets new naming strategy for reverse engineering
+     */
+    public void setNamingStrategy(NamingStrategy strategy) {
+        //null values are not allowed
+        if (strategy == null) {
+            throw new NullPointerException("Null strategy not allowed");
+        }
+        
+        this.namingStrategy = strategy;
+    }
+    
+    /**
+     * @return naming strategy for reverse engineering
+     */
+    public NamingStrategy getNamingStrategy() {
+        return namingStrategy;
+    }
 }

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/BasicNamingStrategy.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/BasicNamingStrategy.java?rev=700708&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/BasicNamingStrategy.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/BasicNamingStrategy.java
Wed Oct  1 00:43:34 2008
@@ -0,0 +1,53 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.access.reveng;
+
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbRelationship;
+import org.apache.cayenne.util.NameConverter;
+
+/**
+ * BasicNamingStrategy is an naming strategy that creates names in Cayenne's
+ * old-fashioned manner, i.e. the same way Cayenne did before 3.0
+ * 
+ * @since 3.0
+ * @author Andrey Razumovsky
+ */
+public class BasicNamingStrategy implements NamingStrategy {
+    public String createDbRelationshipName(
+            ExportedKey key,
+            boolean toMany) {
+        
+        String uglyName = (toMany) ? key.getFKTableName() + "_ARRAY" : "to_" + key.getPKTableName();
+        return NameConverter.underscoredToJava(uglyName, false);
+    }
+
+    public String createObjEntityName(DbEntity dbEntity) {
+        return NameConverter.underscoredToJava(dbEntity.getName(), true);
+    }
+
+    public String createObjAttributeName(DbAttribute attr) {
+        return NameConverter.underscoredToJava(attr.getName(), false);
+    }
+
+    public String createObjRelationshipName(DbRelationship dbRel) {
+        return NameConverter.underscoredToJava(dbRel.getName(), false);
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/ExportedKey.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/ExportedKey.java?rev=700708&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/ExportedKey.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/ExportedKey.java
Wed Oct  1 00:43:34 2008
@@ -0,0 +1,132 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.access.reveng;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * ExportedKey is an representation of relationship between two tables 
+ * in database. It can be used for creating names for relationships
+ * 
+ * @author Andrey Razumovsky
+ */
+public class ExportedKey {
+    /**
+     * Name of source table
+     */
+    String pkTable;
+    
+    /**
+     * Name of source column
+     */
+    String pkColumn;
+    
+    /**
+     * Name of destination table
+     */
+    String fkTable;
+    
+    /**
+     * Name of destination column
+     */
+    String fkColumn;
+    
+    /**
+     * Name of foreign key (might be null)
+     */
+    String fkName;
+    
+    /**
+     * Name of primary key (might be null)
+     */
+    String pkName;
+    
+    public ExportedKey(String pkTable, String pkColumn, String pkName,
+            String fkTable, String fkColumn, String fkName) {
+       this.pkTable  = pkTable;
+       this.pkColumn = pkColumn;
+       this.pkName   = pkName;
+       this.fkTable  = fkTable;
+       this.fkColumn = fkColumn;
+       this.fkName   = fkName;
+    }
+    
+    /**
+     * Extracts data from a resultset pointing to a exported key to
+     * ExportedKey class instance
+     * 
+     * @param rs ResultSet pointing to a exported key, fetched using
+     * DataBaseMetaData.getExportedKeys(...) 
+     */
+    public static ExportedKey extractData(ResultSet rs) throws SQLException {
+        ExportedKey key = new ExportedKey(
+                rs.getString("PKTABLE_NAME"),
+                rs.getString("PKCOLUMN_NAME"),
+                rs.getString("PK_NAME"),
+                rs.getString("FKTABLE_NAME"),
+                rs.getString("FKCOLUMN_NAME"),
+                rs.getString("FK_NAME")
+        );
+        
+        return key;
+    }
+    
+    /**
+     * @return source table name
+     */
+    public String getPKTableName() {
+        return pkTable;
+    }
+    
+    /**
+     * @return destination table name
+     */
+    public String getFKTableName() {
+        return fkTable;
+    }
+    
+    /**
+     * @return source column name
+     */
+    public String getPKColumnName() {
+        return pkColumn;
+    }
+    
+    /**
+     * @return destination column name
+     */
+    public String getFKColumnName() {
+        return fkColumn;
+    }
+    
+    /**
+     * @return PK name
+     */
+    public String getPKName() {
+        return pkName;
+    }
+    
+    /**
+     * @return FK name
+     */
+    public String getFKName() {
+        return fkName;
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/NamingStrategy.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/NamingStrategy.java?rev=700708&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/NamingStrategy.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/reveng/NamingStrategy.java
Wed Oct  1 00:43:34 2008
@@ -0,0 +1,54 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.access.reveng;
+
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbRelationship;
+
+/**
+ * NamingStrategy is a strategy for creating names for entities, attributes, relationships
+ * during reverse engineering.
+ * 
+ * @since 3.0
+ * @author Andrey Razumovsky
+ */
+public interface NamingStrategy {
+    /**
+     * Creates new name for Obj Entity 
+     */
+    public String createObjEntityName(DbEntity entity);
+    
+    /**
+     * Creates new name for Obj Attribute 
+     */
+    public String createObjAttributeName(DbAttribute attr);
+    
+    /**
+     * Creates new name for Db Relationship 
+     */
+    public String createDbRelationshipName(
+            ExportedKey key,
+            boolean toMany);
+    
+    /**
+     * Creates new name for Obj Relationship 
+     */
+    public String createObjRelationshipName(DbRelationship dbRel);
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java?rev=700708&r1=700707&r2=700708&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java
Wed Oct  1 00:43:34 2008
@@ -19,15 +19,25 @@
 
 package org.apache.cayenne.util;
 
-import org.apache.cayenne.dba.TypesMapping;
-import org.apache.cayenne.map.*;
-import org.apache.cayenne.project.NamedObjectFactory;
-
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.cayenne.access.reveng.BasicNamingStrategy;
+import org.apache.cayenne.access.reveng.NamingStrategy;
+import org.apache.cayenne.dba.TypesMapping;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbJoin;
+import org.apache.cayenne.map.DbRelationship;
+import org.apache.cayenne.map.Entity;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.ObjRelationship;
+import org.apache.cayenne.project.NamedObjectFactory;
+
 /**
  * Implements methods for entity merging.
  * 
@@ -39,14 +49,25 @@
     protected boolean removeMeaningfulFKs;
     
     /**
+     * Strategy for choosing names for entities, attributes and relationships
+     */
+    protected NamingStrategy namingStrategy;
+    
+    /**
      * Listeners of merge process. 
      */
     protected List<EntityMergeListener> listeners;
     
     public EntityMergeSupport(DataMap map) {
+        this(map, new BasicNamingStrategy()); 
+    }
+    
+    public EntityMergeSupport(DataMap map, NamingStrategy namingStrategy) {
         this.map = map;
         this.removeMeaningfulFKs = true;
         this.listeners = new ArrayList<EntityMergeListener>(); 
+        
+        this.namingStrategy = namingStrategy;
     }
 
     /**
@@ -103,7 +124,7 @@
 
             // add missing attributes
             for (DbAttribute da : getAttributesToAdd(entity)) {
-                String attrName = NameConverter.underscoredToJava(da.getName(), false);
+                String attrName = namingStrategy.createObjAttributeName(da);
 
                 // avoid duplicate names
                 attrName = NamedObjectFactory.createName(
@@ -128,8 +149,7 @@
                 for (Entity mappedTarget : map.getMappedEntities(dbEntity)) {
 
                     // avoid duplicate names
-                    String relationshipName = NameConverter.underscoredToJava(dr
-                            .getName(), false);
+                    String relationshipName = namingStrategy.createObjRelationshipName(dr);
                     relationshipName = NamedObjectFactory.createName(
                             ObjRelationship.class,
                             entity,
@@ -319,4 +339,18 @@
             listeners.get(i).objRelationshipAdded(rel);
         }
     }
+    
+    /**
+     * Sets new naming strategy for reverse engineering
+     */
+    public void setNamingStrategy(NamingStrategy strategy) {
+        this.namingStrategy = strategy;
+    }
+    
+    /**
+     * @return naming strategy for reverse engineering
+     */
+    public NamingStrategy getNamingStrategy() {
+        return namingStrategy;
+    }
 }

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/reveng/BasicNamingStrategyTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/reveng/BasicNamingStrategyTest.java?rev=700708&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/reveng/BasicNamingStrategyTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/reveng/BasicNamingStrategyTest.java
Wed Oct  1 00:43:34 2008
@@ -0,0 +1,50 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.access.reveng;
+
+import junit.framework.TestCase;
+
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbRelationship;
+
+public class BasicNamingStrategyTest extends TestCase {
+    public void testStrategy() throws Exception {
+        BasicNamingStrategy strategy = new BasicNamingStrategy();
+        
+        ExportedKey key = new ExportedKey("ARTIST", "ARTIST_ID", null,
+                "PAINTING", "ARTIST_ID", null);
+        assertEquals(strategy.createDbRelationshipName(key, false), "toArtist"); 
+        assertEquals(strategy.createDbRelationshipName(key, true), "paintingArray");
+        
+        key = new ExportedKey("PERSON", "PERSON_ID", null,
+                "PERSON", "MOTHER_ID", null);
+        assertEquals(strategy.createDbRelationshipName(key, false), "toPerson"); 
+        assertEquals(strategy.createDbRelationshipName(key, true), "personArray");
+        
+        assertEquals(strategy.createObjEntityName(new DbEntity("ARTIST")), "Artist");
+        assertEquals(strategy.createObjEntityName(new DbEntity("ARTIST_WORK")), "ArtistWork");
+        
+        assertEquals(strategy.createObjAttributeName(new DbAttribute("NAME")), "name");
+        assertEquals(strategy.createObjAttributeName(new DbAttribute("ARTIST_NAME")), "artistName");
+        
+        assertEquals(strategy.createObjRelationshipName(new DbRelationship("toArtist")),
"toArtist");
+        assertEquals(strategy.createObjRelationshipName(new DbRelationship("paintingArray")),
"paintingArray");
+    }
+}



Mime
View raw message