http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToModel.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToModel.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToModel.java deleted file mode 100644 index 10ec4fe..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToModel.java +++ /dev/null @@ -1,77 +0,0 @@ -/***************************************************************** - * 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.merge; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.map.event.AttributeEvent; - -public class SetPrimaryKeyToModel extends AbstractToModelToken.Entity { - - private Collection<DbAttribute> primaryKeyOriginal; - private Collection<DbAttribute> primaryKeyNew; - private String detectedPrimaryKeyName; - private Set<String> primaryKeyNewAttributeNames = new HashSet<String>(); - - public SetPrimaryKeyToModel(DbEntity entity, - Collection<DbAttribute> primaryKeyOriginal, - Collection<DbAttribute> primaryKeyNew, String detectedPrimaryKeyName) { - super("Set Primary Key", entity); - - this.primaryKeyOriginal = primaryKeyOriginal; - this.primaryKeyNew = primaryKeyNew; - this.detectedPrimaryKeyName = detectedPrimaryKeyName; - - for (DbAttribute attr : primaryKeyNew) { - primaryKeyNewAttributeNames.add(attr.getName().toUpperCase()); - } - } - - public MergerToken createReverse(MergerFactory factory) { - return factory.createSetPrimaryKeyToDb( - getEntity(), - primaryKeyNew, - primaryKeyOriginal, - detectedPrimaryKeyName); - } - - public void execute(MergerContext mergerContext) { - DbEntity e = getEntity(); - - for (DbAttribute attr : e.getAttributes()) { - - boolean wasPrimaryKey = attr.isPrimaryKey(); - boolean willBePrimaryKey = primaryKeyNewAttributeNames.contains(attr - .getName() - .toUpperCase()); - - if (wasPrimaryKey != willBePrimaryKey) { - attr.setPrimaryKey(willBePrimaryKey); - e.dbAttributeChanged(new AttributeEvent(this, attr, e)); - mergerContext.getModelMergeDelegate().dbAttributeModified(attr); - } - - } - - } -}
http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/SetValueForNullToDb.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetValueForNullToDb.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/SetValueForNullToDb.java deleted file mode 100644 index 1991154..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/SetValueForNullToDb.java +++ /dev/null @@ -1,46 +0,0 @@ -/***************************************************************** - * 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.merge; - -import java.util.List; - -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - - -public class SetValueForNullToDb extends AbstractToDbToken.EntityAndColumn { - - private ValueForNullProvider valueForNullProvider; - - public SetValueForNullToDb(DbEntity entity, DbAttribute column, ValueForNullProvider valueForNullProvider) { - super("Set value for null", entity, column); - this.valueForNullProvider = valueForNullProvider; - } - - @Override - public List<String> createSql(DbAdapter adapter) { - return valueForNullProvider.createSql(getEntity(), getColumn()); - } - - public MergerToken createReverse(MergerFactory factory) { - return new DummyReverseToken(this); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/merge/ValueForNullProvider.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/merge/ValueForNullProvider.java b/cayenne-server/src/main/java/org/apache/cayenne/merge/ValueForNullProvider.java deleted file mode 100644 index ff147eb..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/merge/ValueForNullProvider.java +++ /dev/null @@ -1,42 +0,0 @@ -/***************************************************************** - * 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.merge; - -import java.util.List; - -import org.apache.cayenne.map.DbAttribute; -import org.apache.cayenne.map.DbEntity; - -/** - * Class that will be used to set value for null on not - * null columns - */ -public interface ValueForNullProvider { - - /** - * @return true if there exist a value that should be inserted for null values - */ - public boolean hasValueFor(DbEntity entity, DbAttribute column); - - /** - * @return a {@link List} of sql to set value for null - */ - public List<String> createSql(DbEntity entity, DbAttribute column); - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java b/cayenne-server/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java deleted file mode 100644 index 7c12600..0000000 --- a/cayenne-server/src/main/java/org/apache/cayenne/util/EntityMergeSupport.java +++ /dev/null @@ -1,520 +0,0 @@ -/***************************************************************** - * 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.util; - -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.map.naming.DefaultUniqueNameGenerator; -import org.apache.cayenne.map.naming.LegacyNameGenerator; -import org.apache.cayenne.map.naming.NameCheckers; -import org.apache.cayenne.map.naming.ObjectNameGenerator; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -/** - * Implements methods for entity merging. - */ -public class EntityMergeSupport { - - private static final Log LOG = LogFactory.getLog(EntityMergeSupport.class); - - private static final Map<String, String> CLASS_TO_PRIMITIVE; - - static { - CLASS_TO_PRIMITIVE = new HashMap<>(); - CLASS_TO_PRIMITIVE.put(Byte.class.getName(), "byte"); - CLASS_TO_PRIMITIVE.put(Long.class.getName(), "long"); - CLASS_TO_PRIMITIVE.put(Double.class.getName(), "double"); - CLASS_TO_PRIMITIVE.put(Boolean.class.getName(), "boolean"); - CLASS_TO_PRIMITIVE.put(Float.class.getName(), "float"); - CLASS_TO_PRIMITIVE.put(Short.class.getName(), "short"); - CLASS_TO_PRIMITIVE.put(Integer.class.getName(), "int"); - } - - private final DataMap map; - /** - * Strategy for choosing names for entities, attributes and relationships - */ - private final ObjectNameGenerator nameGenerator; - /** - * Listeners of merge process. - */ - private final List<EntityMergeListener> listeners = new ArrayList<EntityMergeListener>(); - protected boolean removeMeaningfulFKs; - protected boolean removeMeaningfulPKs; - protected boolean usePrimitives; - - public EntityMergeSupport(DataMap map) { - this(map, new LegacyNameGenerator(), true); - } - - /** - * @since 3.0 - */ - public EntityMergeSupport(DataMap map, ObjectNameGenerator nameGenerator, boolean removeMeaningfulPKs) { - this.map = map; - this.nameGenerator = nameGenerator; - this.removeMeaningfulFKs = true; - this.removeMeaningfulPKs = removeMeaningfulPKs; - - /** - * Adding a listener, so that all created ObjRelationships would have - * default delete rule - */ - addEntityMergeListener(DeleteRuleUpdater.getEntityMergeListener()); - } - - /** - * Updates each one of the collection of ObjEntities, adding attributes and - * relationships based on the current state of its DbEntity. - * - * @return true if any ObjEntity has changed as a result of synchronization. - * @since 1.2 changed signature to use Collection instead of List. - */ - public boolean synchronizeWithDbEntities(Iterable<ObjEntity> objEntities) { - boolean changed = false; - for (ObjEntity nextEntity : objEntities) { - if (synchronizeWithDbEntity(nextEntity)) { - changed = true; - } - } - - return changed; - } - - /** - * @since 4.0 - */ - protected boolean removePK(DbEntity dbEntity) { - return removeMeaningfulPKs; - } - - /** - * @since 4.0 - */ - protected boolean removeFK(DbEntity dbEntity) { - return removeMeaningfulFKs; - } - - /** - * Updates ObjEntity attributes and relationships based on the current state - * of its DbEntity. - * - * @return true if the ObjEntity has changed as a result of synchronization. - */ - public boolean synchronizeWithDbEntity(ObjEntity entity) { - - if (entity == null) { - return false; - } - - DbEntity dbEntity = entity.getDbEntity(); - if (dbEntity == null) { - return false; - } - - boolean changed = false; - - // synchronization on DataMap is some (weak) protection - // against simultaneous modification of the map (like double-clicking on sync button) - synchronized (map) { - - if (removeFK(dbEntity)) { - changed = getRidOfAttributesThatAreNowSrcAttributesForRelationships(entity); - } - - changed |= addMissingAttributes(entity); - changed |= addMissingRelationships(entity); - } - - return changed; - } - - /** - * @since 4.0 - */ - public boolean synchronizeOnDbAttributeAdded(ObjEntity entity, DbAttribute dbAttribute) { - - Collection<DbRelationship> incomingRels = getIncomingRelationships(dbAttribute.getEntity()); - if (isMissingFromObjEntity(entity, dbAttribute, incomingRels)) { - addMissingAttribute(entity, dbAttribute); - return true; - } - - return false; - } - - /** - * @since 4.0 - */ - public boolean synchronizeOnDbRelationshipAdded(ObjEntity entity, DbRelationship dbRelationship) { - - if (isMissingFromObjEntity(entity, dbRelationship)) { - addMissingRelationship(entity, dbRelationship); - } - - return true; - } - - private boolean addMissingRelationships(ObjEntity entity) { - List<DbRelationship> relationshipsToAdd = getRelationshipsToAdd(entity); - if (relationshipsToAdd.isEmpty()) { - return false; - } - - for (DbRelationship dr : relationshipsToAdd) { - addMissingRelationship(entity, dr); - } - - return true; - } - - private boolean createObjRelationship(ObjEntity entity, DbRelationship dr, String targetEntityName) { - String relationshipName = nameGenerator.createObjRelationshipName(dr); - relationshipName = DefaultUniqueNameGenerator.generate(NameCheckers.objRelationship, entity, relationshipName); - - ObjRelationship or = new ObjRelationship(relationshipName); - or.addDbRelationship(dr); - Map<String, ObjEntity> objEntities = entity.getDataMap().getSubclassesForObjEntity(entity); - - boolean hasFlattingAttributes = false; - boolean needGeneratedEntity = true; - - if (objEntities.containsKey(targetEntityName)) { - needGeneratedEntity = false; - } - - for (ObjEntity subObjEntity : objEntities.values()) { - for (ObjAttribute objAttribute : subObjEntity.getAttributes()) { - String path = objAttribute.getDbAttributePath(); - if (path != null) { - if (path.startsWith(or.getDbRelationshipPath())) { - hasFlattingAttributes = true; - break; - } - } - } - } - - if (!hasFlattingAttributes) { - if (needGeneratedEntity) { - or.setTargetEntityName(targetEntityName); - or.setSourceEntity(entity); - } - - entity.addRelationship(or); - fireRelationshipAdded(or); - } - - return needGeneratedEntity; - } - - private boolean addMissingAttributes(ObjEntity entity) { - boolean changed = false; - - for (DbAttribute da : getAttributesToAdd(entity)) { - addMissingAttribute(entity, da); - changed = true; - } - return changed; - } - - private void addMissingRelationship(ObjEntity entity, DbRelationship dbRelationship) { - DbEntity targetEntity = dbRelationship.getTargetEntity(); - - Collection<ObjEntity> mappedObjEntities = map.getMappedEntities(targetEntity); - if (!mappedObjEntities.isEmpty()) { - for (Entity mappedTarget : mappedObjEntities) { - createObjRelationship(entity, dbRelationship, mappedTarget.getName()); - } - } else { - - if (targetEntity == null) { - targetEntity = new DbEntity(dbRelationship.getTargetEntityName()); - } - - if (dbRelationship.getTargetEntityName() != null) { - boolean needGeneratedEntity = createObjRelationship(entity, dbRelationship, - nameGenerator.createObjEntityName(targetEntity)); - if (needGeneratedEntity) { - LOG.warn("Can't find ObjEntity for " + dbRelationship.getTargetEntityName()); - LOG.warn("Db Relationship (" + dbRelationship + ") will have GUESSED Obj Relationship reflection. "); - } - } - } - } - - private void addMissingAttribute(ObjEntity entity, DbAttribute da) { - String attrName = DefaultUniqueNameGenerator.generate(NameCheckers.objAttribute, entity, - nameGenerator.createObjAttributeName(da)); - - String type = TypesMapping.getJavaBySqlType(da.getType()); - if (usePrimitives) { - String primitive = CLASS_TO_PRIMITIVE.get(type); - if (primitive != null) { - type = primitive; - } - } - - ObjAttribute oa = new ObjAttribute(attrName, type, entity); - oa.setDbAttributePath(da.getName()); - entity.addAttribute(oa); - fireAttributeAdded(oa); - } - - private boolean getRidOfAttributesThatAreNowSrcAttributesForRelationships(ObjEntity entity) { - boolean changed = false; - for (DbAttribute da : getMeaningfulFKs(entity)) { - ObjAttribute oa = entity.getAttributeForDbAttribute(da); - while (oa != null) { - String attrName = oa.getName(); - entity.removeAttribute(attrName); - changed = true; - oa = entity.getAttributeForDbAttribute(da); - } - } - return changed; - } - - /** - * Returns a list of DbAttributes that are mapped to foreign keys. - * - * @since 1.2 - */ - public Collection<DbAttribute> getMeaningfulFKs(ObjEntity objEntity) { - List<DbAttribute> fks = new ArrayList<DbAttribute>(2); - - for (ObjAttribute property : objEntity.getAttributes()) { - DbAttribute column = property.getDbAttribute(); - - // check if adding it makes sense at all - if (column != null && column.isForeignKey()) { - fks.add(column); - } - } - - return fks; - } - - /** - * Returns a list of attributes that exist in the DbEntity, but are missing - * from the ObjEntity. - */ - protected List<DbAttribute> getAttributesToAdd(ObjEntity objEntity) { - DbEntity dbEntity = objEntity.getDbEntity(); - - List<DbAttribute> missing = new ArrayList<DbAttribute>(); - Collection<DbRelationship> incomingRels = getIncomingRelationships(dbEntity); - - for (DbAttribute dba : dbEntity.getAttributes()) { - - if (isMissingFromObjEntity(objEntity, dba, incomingRels)) { - missing.add(dba); - } - } - - return missing; - } - - protected boolean isMissingFromObjEntity(ObjEntity entity, DbAttribute dbAttribute, Collection<DbRelationship> incomingRels) { - - if (dbAttribute.getName() == null || entity.getAttributeForDbAttribute(dbAttribute) != null) { - return false; - } - - boolean removeMeaningfulPKs = removePK(dbAttribute.getEntity()); - if (removeMeaningfulPKs && dbAttribute.isPrimaryKey()) { - return false; - } - - // check FK's - boolean isFK = false; - Iterator<DbRelationship> rit = dbAttribute.getEntity().getRelationships().iterator(); - while (!isFK && rit.hasNext()) { - DbRelationship rel = rit.next(); - for (DbJoin join : rel.getJoins()) { - if (join.getSource() == dbAttribute) { - isFK = true; - break; - } - } - } - - if (!removeMeaningfulPKs) { - if (!dbAttribute.isPrimaryKey() && isFK) { - return false; - } - } else { - if (isFK) { - return false; - } - } - - // check incoming relationships - rit = incomingRels.iterator(); - while (!isFK && rit.hasNext()) { - DbRelationship rel = rit.next(); - for (DbJoin join : rel.getJoins()) { - if (join.getTarget() == dbAttribute) { - isFK = true; - break; - } - } - } - - if (!removeMeaningfulPKs) { - if (!dbAttribute.isPrimaryKey() && isFK) { - return false; - } - } else { - if (isFK) { - return false; - } - } - - return true; - } - - protected boolean isMissingFromObjEntity(ObjEntity entity, DbRelationship dbRelationship) { - return dbRelationship.getName() != null && entity.getRelationshipForDbRelationship(dbRelationship) == null; - } - - private Collection<DbRelationship> getIncomingRelationships(DbEntity entity) { - Collection<DbRelationship> incoming = new ArrayList<DbRelationship>(); - - for (DbEntity nextEntity : entity.getDataMap().getDbEntities()) { - for (DbRelationship relationship : nextEntity.getRelationships()) { - - // TODO: PERFORMANCE 'getTargetEntity' is generally slow, called - // in this iterator it is showing (e.g. in YourKit profiles).. - // perhaps use cheaper 'getTargetEntityName()' or even better - - // pre-cache all relationships by target entity to avoid O(n) - // search ? - // (need to profile to prove the difference) - if (entity == relationship.getTargetEntity()) { - incoming.add(relationship); - } - } - } - - return incoming; - } - - protected List<DbRelationship> getRelationshipsToAdd(ObjEntity objEntity) { - List<DbRelationship> missing = new ArrayList<DbRelationship>(); - for (DbRelationship dbRel : objEntity.getDbEntity().getRelationships()) { - if (isMissingFromObjEntity(objEntity, dbRel)) { - missing.add(dbRel); - } - } - - return missing; - } - - /** - * @since 1.2 - */ - public boolean isRemoveMeaningfulFKs() { - return removeMeaningfulFKs; - } - - /** - * @since 1.2 - */ - public void setRemoveMeaningfulFKs(boolean removeMeaningfulFKs) { - this.removeMeaningfulFKs = removeMeaningfulFKs; - } - - /** - * Registers new EntityMergeListener - */ - public void addEntityMergeListener(EntityMergeListener listener) { - listeners.add(listener); - } - - /** - * Unregisters an EntityMergeListener - */ - public void removeEntityMergeListener(EntityMergeListener listener) { - listeners.remove(listener); - } - - /** - * Returns registered listeners - */ - public EntityMergeListener[] getEntityMergeListeners() { - return listeners.toArray(new EntityMergeListener[listeners.size()]); - } - - /** - * Notifies all listeners that an ObjAttribute was added - */ - protected void fireAttributeAdded(ObjAttribute attr) { - for (EntityMergeListener listener : listeners) { - listener.objAttributeAdded(attr); - } - } - - /** - * Notifies all listeners that an ObjRelationship was added - */ - protected void fireRelationshipAdded(ObjRelationship rel) { - for (EntityMergeListener listener : listeners) { - listener.objRelationshipAdded(rel); - } - } - - /** - * @return naming strategy for reverse engineering - */ - public ObjectNameGenerator getNameGenerator() { - return nameGenerator; - } - - /** - * @since 4.0 - */ - public boolean isUsePrimitives() { - return usePrimitives; - } - - /** - * @param usePrimitives - * @since 4.0 - */ - public void setUsePrimitives(boolean usePrimitives) { - this.usePrimitives = usePrimitives; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderIT.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderIT.java b/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderIT.java deleted file mode 100644 index 6d9a682..0000000 --- a/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderIT.java +++ /dev/null @@ -1,431 +0,0 @@ -/***************************************************************** - * 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; - -import org.apache.cayenne.access.loader.DbLoaderConfiguration; -import org.apache.cayenne.access.loader.filters.FiltersConfig; -import org.apache.cayenne.access.loader.filters.PatternFilter; -import org.apache.cayenne.access.loader.filters.TableFilter; -import org.apache.cayenne.configuration.server.ServerRuntime; -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.dba.TypesMapping; -import org.apache.cayenne.di.Inject; -import org.apache.cayenne.map.*; -import org.apache.cayenne.unit.UnitDbAdapter; -import org.apache.cayenne.unit.di.server.CayenneProjects; -import org.apache.cayenne.unit.di.server.ServerCase; -import org.apache.cayenne.unit.di.server.ServerCaseDataSourceFactory; -import org.apache.cayenne.unit.di.server.UseServerRuntime; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.sql.Types; -import java.util.Collection; -import java.util.List; - -import static org.junit.Assert.*; - -@UseServerRuntime(CayenneProjects.TESTMAP_PROJECT) -public class DbLoaderIT extends ServerCase { - - public static final DbLoaderConfiguration CONFIG = new DbLoaderConfiguration(); - @Inject - private ServerRuntime runtime; - - @Inject - private DbAdapter adapter; - - @Inject - private ServerCaseDataSourceFactory dataSourceFactory; - - @Inject - private UnitDbAdapter accessStackAdapter; - - private DbLoader loader; - - @Before - public void setUp() throws Exception { - loader = new DbLoader(dataSourceFactory.getSharedDataSource().getConnection(), adapter, null); - } - - @After - public void tearDown() throws Exception { - loader.getConnection().close(); - } - - @Test - public void testGetTableTypes() throws Exception { - - List<?> tableTypes = loader.getTableTypes(); - - assertNotNull(tableTypes); - - String tableLabel = adapter.tableTypeForTable(); - if (tableLabel != null) { - assertTrue("Missing type for table '" + tableLabel + "' - " + tableTypes, tableTypes.contains(tableLabel)); - } - - String viewLabel = adapter.tableTypeForView(); - if (viewLabel != null) { - assertTrue("Missing type for view '" + viewLabel + "' - " + tableTypes, tableTypes.contains(viewLabel)); - } - } - - @Test - public void testGetTables() throws Exception { - - String tableLabel = adapter.tableTypeForTable(); - - List<DetectedDbEntity> tables = loader.createTableLoader(null, null, TableFilter.everything()) - .getDbEntities(TableFilter.everything(), new String[]{tableLabel}); - - assertNotNull(tables); - - boolean foundArtist = false; - - for (DetectedDbEntity table : tables) { - if ("ARTIST".equalsIgnoreCase(table.getName())) { - foundArtist = true; - break; - } - } - - assertTrue("'ARTIST' is missing from the table list: " + tables, foundArtist); - } - - @Test - public void testGetTablesWithWrongCatalog() throws Exception { - - DbLoaderConfiguration config = new DbLoaderConfiguration(); - config.setFiltersConfig( - FiltersConfig.create("WRONG", null, TableFilter.everything(), PatternFilter.INCLUDE_NOTHING)); - List<DetectedDbEntity> tables = loader - .createTableLoader("WRONG", null, TableFilter.everything()) - .getDbEntities(TableFilter.everything(), new String[]{adapter.tableTypeForTable()}); - - assertNotNull(tables); - assertTrue(tables.isEmpty()); - } - - @Test - public void testGetTablesWithWrongSchema() throws Exception { - - DbLoaderConfiguration config = new DbLoaderConfiguration(); - config.setFiltersConfig( - FiltersConfig.create(null, "WRONG", TableFilter.everything(), PatternFilter.INCLUDE_NOTHING)); - List<DetectedDbEntity> tables = loader - .createTableLoader(null, "WRONG", TableFilter.everything()) - .getDbEntities(TableFilter.everything(), new String[]{adapter.tableTypeForTable()}); - - assertNotNull(tables); - assertTrue(tables.isEmpty()); - } - - @Test - public void testLoadWithMeaningfulPK() throws Exception { - - DataMap map = new DataMap(); - String[] tableLabel = { adapter.tableTypeForTable() }; - - loader.setCreatingMeaningfulPK(true); - - List<DbEntity> entities = loader - .createTableLoader(null, null, TableFilter.everything()) - .loadDbEntities(map, CONFIG, tableLabel); - - loader.loadObjEntities(map, CONFIG, entities); - - ObjEntity artist = map.getObjEntity("Artist"); - assertNotNull(artist); - - ObjAttribute id = artist.getAttribute("artistId"); - assertNotNull(id); - } - - /** - * DataMap loading is in one big test method, since breaking it in - * individual tests would require multiple reads of metatdata which is - * extremely slow on some RDBMS (Sybase). - */ - @Test - public void testLoad() throws Exception { - - boolean supportsUnique = runtime.getDataDomain().getDataNodes().iterator().next().getAdapter() - .supportsUniqueConstraints(); - boolean supportsLobs = accessStackAdapter.supportsLobs(); - boolean supportsFK = accessStackAdapter.supportsFKConstraints(); - - DataMap map = new DataMap(); - map.setDefaultPackage("foo.x"); - - String tableLabel = adapter.tableTypeForTable(); - - // *** TESTING THIS *** - List<DbEntity> entities = loader - .createTableLoader(null, null, TableFilter.everything()) - .loadDbEntities(map, CONFIG, new String[]{adapter.tableTypeForTable()}); - - - assertDbEntities(map); - - if (supportsLobs) { - assertLobDbEntities(map); - } - - // *** TESTING THIS *** - loader.loadDbRelationships(CONFIG, null, null, entities); - - if (supportsFK) { - Collection<DbRelationship> rels = getDbEntity(map, "ARTIST").getRelationships(); - assertNotNull(rels); - assertTrue(!rels.isEmpty()); - - // test one-to-one - rels = getDbEntity(map, "PAINTING").getRelationships(); - assertNotNull(rels); - - // find relationship to PAINTING_INFO - DbRelationship oneToOne = null; - for (DbRelationship rel : rels) { - if ("PAINTING_INFO".equalsIgnoreCase(rel.getTargetEntityName())) { - oneToOne = rel; - break; - } - } - - assertNotNull("No relationship to PAINTING_INFO", oneToOne); - assertFalse("Relationship to PAINTING_INFO must be to-one", oneToOne.isToMany()); - assertTrue("Relationship to PAINTING_INFO must be to-one", oneToOne.isToDependentPK()); - - // test UNIQUE only if FK is supported... - if (supportsUnique) { - assertUniqueConstraintsInRelationships(map); - } - } - - // *** TESTING THIS *** - loader.setCreatingMeaningfulPK(false); - loader.loadObjEntities(map, CONFIG, entities); - - assertObjEntities(map); - - // now when the map is loaded, test - // various things - // selectively check how different types were processed - if (accessStackAdapter.supportsColumnTypeReengineering()) { - checkTypes(map); - } - } - - private void assertUniqueConstraintsInRelationships(DataMap map) { - // unfortunately JDBC metadata doesn't provide info for UNIQUE - // constraints.... - // cant reengineer them... - - // find rel to TO_ONEFK1 - /* - * Iterator it = getDbEntity(map, - * "TO_ONEFK2").getRelationships().iterator(); DbRelationship rel = - * (DbRelationship) it.next(); assertEquals("TO_ONEFK1", - * rel.getTargetEntityName()); - * assertFalse("UNIQUE constraint was ignored...", rel.isToMany()); - */ - } - - private void assertDbEntities(DataMap map) { - DbEntity dae = getDbEntity(map, "ARTIST"); - assertNotNull("Null 'ARTIST' entity, other DbEntities: " + map.getDbEntityMap(), dae); - assertEquals("ARTIST", dae.getName().toUpperCase()); - - DbAttribute a = getDbAttribute(dae, "ARTIST_ID"); - assertNotNull(a); - assertTrue(a.isPrimaryKey()); - assertFalse(a.isGenerated()); - - if (adapter.supportsGeneratedKeys()) { - DbEntity bag = getDbEntity(map, "GENERATED_COLUMN_TEST"); - DbAttribute id = getDbAttribute(bag, "GENERATED_COLUMN"); - assertTrue(id.isPrimaryKey()); - assertTrue(id.isGenerated()); - } - } - - private void assertObjEntities(DataMap map) { - - boolean supportsLobs = accessStackAdapter.supportsLobs(); - boolean supportsFK = accessStackAdapter.supportsFKConstraints(); - - ObjEntity ae = map.getObjEntity("Artist"); - assertNotNull(ae); - assertEquals("Artist", ae.getName()); - - // assert primary key is not an attribute - assertNull(ae.getAttribute("artistId")); - - if (supportsLobs) { - assertLobObjEntities(map); - } - - if (supportsFK) { - Collection<?> rels1 = ae.getRelationships(); - assertNotNull(rels1); - assertTrue(rels1.size() > 0); - } - - assertEquals("foo.x.Artist", ae.getClassName()); - } - - private void assertLobDbEntities(DataMap map) { - DbEntity blobEnt = getDbEntity(map, "BLOB_TEST"); - assertNotNull(blobEnt); - DbAttribute blobAttr = getDbAttribute(blobEnt, "BLOB_COL"); - assertNotNull(blobAttr); - assertTrue(msgForTypeMismatch(Types.BLOB, blobAttr), Types.BLOB == blobAttr.getType() - || Types.LONGVARBINARY == blobAttr.getType()); - - DbEntity clobEnt = getDbEntity(map, "CLOB_TEST"); - assertNotNull(clobEnt); - DbAttribute clobAttr = getDbAttribute(clobEnt, "CLOB_COL"); - assertNotNull(clobAttr); - assertTrue(msgForTypeMismatch(Types.CLOB, clobAttr), Types.CLOB == clobAttr.getType() - || Types.LONGVARCHAR == clobAttr.getType()); - -/* - DbEntity nclobEnt = getDbEntity(map, "NCLOB_TEST"); - assertNotNull(nclobEnt); - DbAttribute nclobAttr = getDbAttribute(nclobEnt, "NCLOB_COL"); - assertNotNull(nclobAttr); - assertTrue(msgForTypeMismatch(Types.NCLOB, nclobAttr), Types.NCLOB == nclobAttr.getType() - || Types.LONGVARCHAR == nclobAttr.getType()); -*/ - } - - private void assertLobObjEntities(DataMap map) { - ObjEntity blobEnt = map.getObjEntity("BlobTest"); - assertNotNull(blobEnt); - // BLOBs should be mapped as byte[] - ObjAttribute blobAttr = blobEnt.getAttribute("blobCol"); - assertNotNull("BlobTest.blobCol failed to doLoad", blobAttr); - assertEquals("byte[]", blobAttr.getType()); - - - ObjEntity clobEnt = map.getObjEntity("ClobTest"); - assertNotNull(clobEnt); - // CLOBs should be mapped as Strings by default - ObjAttribute clobAttr = clobEnt.getAttribute("clobCol"); - assertNotNull(clobAttr); - assertEquals(String.class.getName(), clobAttr.getType()); - - - ObjEntity nclobEnt = map.getObjEntity("NclobTest"); - assertNotNull(nclobEnt); - // CLOBs should be mapped as Strings by default - ObjAttribute nclobAttr = nclobEnt.getAttribute("nclobCol"); - assertNotNull(nclobAttr); - assertEquals(String.class.getName(), nclobAttr.getType()); - } - - private DbEntity getDbEntity(DataMap map, String name) { - DbEntity de = map.getDbEntity(name); - // sometimes table names get converted to lowercase - if (de == null) { - de = map.getDbEntity(name.toLowerCase()); - } - - return de; - } - - private DbAttribute getDbAttribute(DbEntity ent, String name) { - DbAttribute da = ent.getAttribute(name); - // sometimes table names get converted to lowercase - if (da == null) { - da = ent.getAttribute(name.toLowerCase()); - } - - return da; - } - - private DataMap originalMap() { - return runtime.getDataDomain().getDataNodes().iterator().next().getDataMaps().iterator().next(); - } - - /** - * Selectively check how different types were processed. - */ - public void checkTypes(DataMap map) { - DbEntity dbe = getDbEntity(map, "PAINTING"); - DbEntity floatTest = getDbEntity(map, "FLOAT_TEST"); - DbEntity smallintTest = getDbEntity(map, "SMALLINT_TEST"); - DbAttribute integerAttr = getDbAttribute(dbe, "PAINTING_ID"); - DbAttribute decimalAttr = getDbAttribute(dbe, "ESTIMATED_PRICE"); - DbAttribute varcharAttr = getDbAttribute(dbe, "PAINTING_TITLE"); - DbAttribute floatAttr = getDbAttribute(floatTest, "FLOAT_COL"); - DbAttribute smallintAttr = getDbAttribute(smallintTest, "SMALLINT_COL"); - - // check decimal - assertTrue(msgForTypeMismatch(Types.DECIMAL, decimalAttr), Types.DECIMAL == decimalAttr.getType() - || Types.NUMERIC == decimalAttr.getType()); - assertEquals(2, decimalAttr.getScale()); - - // check varchar - assertEquals(msgForTypeMismatch(Types.VARCHAR, varcharAttr), Types.VARCHAR, varcharAttr.getType()); - assertEquals(255, varcharAttr.getMaxLength()); - // check integer - assertEquals(msgForTypeMismatch(Types.INTEGER, integerAttr), Types.INTEGER, integerAttr.getType()); - // check float - assertTrue(msgForTypeMismatch(Types.FLOAT, floatAttr), Types.FLOAT == floatAttr.getType() - || Types.DOUBLE == floatAttr.getType() || Types.REAL == floatAttr.getType()); - - // check smallint - assertTrue(msgForTypeMismatch(Types.SMALLINT, smallintAttr), Types.SMALLINT == smallintAttr.getType() - || Types.INTEGER == smallintAttr.getType()); - } - - public void checkAllDBEntities(DataMap map) { - - for (DbEntity origEnt : originalMap().getDbEntities()) { - DbEntity newEnt = map.getDbEntity(origEnt.getName()); - for (DbAttribute origAttr : origEnt.getAttributes()) { - DbAttribute newAttr = newEnt.getAttribute(origAttr.getName()); - assertNotNull("No matching DbAttribute for '" + origAttr.getName(), newAttr); - assertEquals(msgForTypeMismatch(origAttr, newAttr), origAttr.getType(), newAttr.getType()); - // length and precision doesn't have to be the same - // it must be greater or equal - assertTrue(origAttr.getMaxLength() <= newAttr.getMaxLength()); - assertTrue(origAttr.getScale() <= newAttr.getScale()); - } - } - } - - private static String msgForTypeMismatch(DbAttribute origAttr, DbAttribute newAttr) { - return msgForTypeMismatch(origAttr.getType(), newAttr); - } - - private static String msgForTypeMismatch(int origType, DbAttribute newAttr) { - String nt = TypesMapping.getSqlNameByType(newAttr.getType()); - String ot = TypesMapping.getSqlNameByType(origType); - return attrMismatch(newAttr.getName(), "expected type: <" + ot + ">, but was <" + nt + ">"); - } - - private static String attrMismatch(String attrName, String msg) { - return "[Error loading attribute '" + attrName + "': " + msg + "]"; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderPartialIT.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderPartialIT.java b/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderPartialIT.java deleted file mode 100644 index 7514b99..0000000 --- a/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderPartialIT.java +++ /dev/null @@ -1,118 +0,0 @@ -/***************************************************************** - * 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; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.Collection; - -import org.apache.cayenne.CayenneException; -import org.apache.cayenne.access.loader.DefaultDbLoaderDelegate; -import org.apache.cayenne.dba.DbAdapter; -import org.apache.cayenne.di.Inject; -import org.apache.cayenne.map.DataMap; -import org.apache.cayenne.map.DbEntity; -import org.apache.cayenne.unit.di.server.CayenneProjects; -import org.apache.cayenne.unit.di.server.ServerCase; -import org.apache.cayenne.unit.di.server.ServerCaseDataSourceFactory; -import org.apache.cayenne.unit.di.server.UseServerRuntime; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -@UseServerRuntime(CayenneProjects.TESTMAP_PROJECT) -public class DbLoaderPartialIT extends ServerCase { - - @Inject - private DbAdapter adapter; - - @Inject - private ServerCaseDataSourceFactory dataSourceFactory; - - private DbLoader loader; - - @Before - public void setUp() throws Exception { - loader = new DbLoader( - dataSourceFactory.getSharedDataSource().getConnection(), - adapter, - new DefaultDbLoaderDelegate()); - } - - @After - public void tearDown() throws Exception { - loader.getConnection().close(); - } - - /** - * Tests that FKs are properly loaded when the relationship source is not loaded. See - * CAY-479. This test will perform two reverse engineers. The second reverse engineer - * will skip two tables that share relationships with PAINTING. Relationships in - * ARTIST and GALLERY should remain unmodified, and all PAINTING relationships should - * be loaded. - */ - @Test - public void testPartialLoad() throws Exception { - - DataMap map = new DataMap(); - String tableLabel = adapter.tableTypeForTable(); - - loader.loadDataMapFromDB(null, "%", new String[] {tableLabel}, map); - - Collection<?> rels = getDbEntity(map, "ARTIST").getRelationships(); - assertNotNull(rels); - int artistRels = rels.size(); - - rels = getDbEntity(map, "GALLERY").getRelationships(); - assertNotNull(rels); - int galleryRels = rels.size(); - - rels = getDbEntity(map, "PAINTING").getRelationships(); - assertNotNull(rels); - int paintingRels = rels.size(); - - loader.loadDataMapFromDB(null, "%", new String[] { - tableLabel - }, map); - - rels = getDbEntity(map, "ARTIST").getRelationships(); - assertNotNull(rels); - assertEquals(artistRels, rels.size()); - - rels = getDbEntity(map, "GALLERY").getRelationships(); - assertNotNull(rels); - assertEquals(galleryRels, rels.size()); - - rels = getDbEntity(map, "PAINTING").getRelationships(); - assertNotNull(rels); - assertEquals(paintingRels, rels.size()); - } - - private DbEntity getDbEntity(DataMap map, String name) { - DbEntity de = map.getDbEntity(name); - // sometimes table names get converted to lowercase - if (de == null) { - de = map.getDbEntity(name.toLowerCase()); - } - - return de; - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/test/java/org/apache/cayenne/access/loader/ManyToManyCandidateEntityTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/ManyToManyCandidateEntityTest.java b/cayenne-server/src/test/java/org/apache/cayenne/access/loader/ManyToManyCandidateEntityTest.java deleted file mode 100644 index ad4238f..0000000 --- a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/ManyToManyCandidateEntityTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/***************************************************************** - * 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.loader; - -import org.apache.cayenne.configuration.ConfigurationNameMapper; -import org.apache.cayenne.configuration.ConfigurationTree; -import org.apache.cayenne.configuration.DataChannelDescriptor; -import org.apache.cayenne.configuration.DataMapLoader; -import org.apache.cayenne.configuration.DefaultConfigurationNameMapper; -import org.apache.cayenne.configuration.XMLDataChannelDescriptorLoader; -import org.apache.cayenne.configuration.XMLDataMapLoader; -import org.apache.cayenne.di.AdhocObjectFactory; -import org.apache.cayenne.di.Binder; -import org.apache.cayenne.di.ClassLoaderManager; -import org.apache.cayenne.di.DIBootstrap; -import org.apache.cayenne.di.Injector; -import org.apache.cayenne.di.Module; -import org.apache.cayenne.di.spi.DefaultAdhocObjectFactory; -import org.apache.cayenne.di.spi.DefaultClassLoaderManager; -import org.apache.cayenne.map.DataMap; -import org.apache.cayenne.map.ObjEntity; -import org.apache.cayenne.map.Relationship; -import org.apache.cayenne.map.naming.LegacyNameGenerator; -import org.apache.cayenne.resource.URLResource; -import org.junit.Before; -import org.junit.Test; - -import java.net.URL; -import java.util.ArrayList; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -public class ManyToManyCandidateEntityTest { - - private DataMap map; - - @Before - public void setUp() throws Exception { - Module testModule = new Module() { - - public void configure(Binder binder) { - binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class); - binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class); - binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class); - binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class); - } - }; - - Injector injector = DIBootstrap.createInjector(testModule); - - // create and initialize loader instance to test - XMLDataChannelDescriptorLoader loader = new XMLDataChannelDescriptorLoader(); - injector.injectMembers(loader); - - String testConfigName = "relationship-optimisation"; - URL url = getClass().getResource("cayenne-" + testConfigName + ".xml"); - - ConfigurationTree<DataChannelDescriptor> tree = loader.load(new URLResource(url)); - - map = tree.getRootNode().getDataMap(testConfigName); - } - - @Test - public void testMatchingForManyToManyEntity() throws Exception { - ObjEntity manyToManyEntity = map.getObjEntity("Table1Table2"); - - assertNotNull(ManyToManyCandidateEntity.build(manyToManyEntity)); - } - - @Test - public void testMatchingForNotManyToManyEntity() throws Exception { - ObjEntity entity = map.getObjEntity("Table1"); - - assertNull(ManyToManyCandidateEntity.build(entity)); - } - - @Test - public void testOptimisationForManyToManyEntity() { - ObjEntity manyToManyEntity = map.getObjEntity("Table1Table2"); - - ManyToManyCandidateEntity.build(manyToManyEntity).optimizeRelationships(new LegacyNameGenerator()); - - ObjEntity table1Entity = map.getObjEntity("Table1"); - ObjEntity table2Entity = map.getObjEntity("Table2"); - - assertEquals(1, table1Entity.getRelationships().size()); - assertEquals(table2Entity, new ArrayList<Relationship>(table1Entity.getRelationships()).get(0) - .getTargetEntity()); - - assertEquals(1, table2Entity.getRelationships().size()); - assertEquals(table1Entity, new ArrayList<Relationship>(table2Entity.getRelationships()).get(0) - .getTargetEntity()); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/FiltersConfigTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/FiltersConfigTest.java b/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/FiltersConfigTest.java deleted file mode 100644 index a43f890..0000000 --- a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/FiltersConfigTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/***************************************************************** - * 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.loader.filters; - -import junit.framework.TestCase; - -import java.util.Collections; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.regex.Pattern; - -public class FiltersConfigTest extends TestCase { - - public void testToString_01() { - FiltersConfig config = FiltersConfig.create(null, null, - TableFilter.everything(), PatternFilter.INCLUDE_EVERYTHING); - - assertEquals("Catalog: null\n" + - " Schema: null\n" + - " Tables: \n" + - " Include: null Columns: ALL\n" + - " Procedures: ALL\n", config.toString()); - } - - public void testToString_02() { - FiltersConfig config = new FiltersConfig( - new CatalogFilter("catalog_01", - new SchemaFilter("schema_11", TableFilter.everything(), PatternFilter.INCLUDE_EVERYTHING)), - new CatalogFilter("catalog_02", - new SchemaFilter("schema_21", TableFilter.everything(), PatternFilter.INCLUDE_NOTHING), - new SchemaFilter("schema_22", - new TableFilter( - includes(new IncludeTableFilter(null, PatternFilter.INCLUDE_NOTHING)), - excludes("aaa")), - PatternFilter.INCLUDE_NOTHING), - new SchemaFilter("schema_23", TableFilter.include("include"), PatternFilter.INCLUDE_NOTHING) - ) - ); - - assertEquals("Catalog: catalog_01\n" + - " Schema: schema_11\n" + - " Tables: \n" + - " Include: null Columns: ALL\n" + - " Procedures: ALL\n" + - "Catalog: catalog_02\n" + - " Schema: schema_21\n" + - " Tables: \n" + - " Include: null Columns: ALL\n" + - " Procedures: NONE\n" + - " Schema: schema_22\n" + - " Tables: \n" + - " Include: null Columns: NONE\n" + - " aaa\n" + - " Procedures: NONE\n" + - " Schema: schema_23\n" + - " Tables: \n" + - " Include: include Columns: ALL\n" + - " Procedures: NONE\n", config.toString()); - } - - private SortedSet<Pattern> excludes(String ... p) { - SortedSet<Pattern> patterns = new TreeSet<Pattern>(PatternFilter.PATTERN_COMPARATOR); - for (String pattern : p) { - patterns.add(PatternFilter.pattern(pattern)); - } - return patterns; - } - - protected SortedSet<IncludeTableFilter> includes(IncludeTableFilter ... filters) { - SortedSet<IncludeTableFilter> includeTableFilters = new TreeSet<IncludeTableFilter>(); - Collections.addAll(includeTableFilters, filters); - - return includeTableFilters; - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/IncludeFilterTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/IncludeFilterTest.java b/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/IncludeFilterTest.java deleted file mode 100644 index c8cbe5b..0000000 --- a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/IncludeFilterTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.loader.filters; - -import org.junit.Test; - -public class IncludeFilterTest { - - @Test - public void testIsInclude() throws Exception { -// IncludeFilter filter = new IncludeFilter(pattern("^v_.*$")); -// assertTrue(filter.isInclude("v_new_view")); -// assertFalse(filter.isInclude("new_view")); -// assertFalse(filter.isInclude("view")); -// assertFalse(filter.isInclude("girl")); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/PatternFilterTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/PatternFilterTest.java b/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/PatternFilterTest.java deleted file mode 100644 index 5bd07d4..0000000 --- a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/PatternFilterTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/***************************************************************** - * 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.loader.filters; - -import junit.framework.TestCase; - -public class PatternFilterTest extends TestCase { - - public void testInclude() throws Exception { - PatternFilter filter = new PatternFilter() - .include("aaa") - .include("bbb"); - - assertTrue(filter.isInclude("aaa")); - assertTrue(filter.isInclude("bbb")); - assertFalse(filter.isInclude("aaaa")); - assertFalse(filter.isInclude("aa")); - assertFalse(filter.isInclude("abb")); - - filter = new PatternFilter().include("^v_.*$"); - assertTrue(filter.isInclude("v_new_view")); - assertFalse(filter.isInclude("new_view")); - assertFalse(filter.isInclude("view")); - assertFalse(filter.isInclude("girl")); - } - - public void testExclude() throws Exception { - PatternFilter filter = new PatternFilter() - .exclude("aaa") - .exclude("bbb"); - - assertFalse(filter.isInclude("aaa")); - assertFalse(filter.isInclude("bbb")); - assertTrue(filter.isInclude("aaaa")); - assertTrue(filter.isInclude("aa")); - assertTrue(filter.isInclude("abb")); - } - - public void testIncludeExclude() throws Exception { - PatternFilter filter = new PatternFilter() - .include("aa.*") - .exclude("aaa"); - - assertFalse(filter.isInclude("aaa")); - assertFalse(filter.isInclude("bbb")); - assertTrue(filter.isInclude("aaaa")); - assertTrue(filter.isInclude("aa")); - assertFalse(filter.isInclude("abb")); - } - - public void testIncludeAllFilter() { - assertTrue(PatternFilter.INCLUDE_EVERYTHING.isInclude("qwe")); - assertTrue(PatternFilter.INCLUDE_EVERYTHING.isInclude("")); - assertTrue(PatternFilter.INCLUDE_EVERYTHING.isInclude(null)); - } - - public void testIncludeNoneFilter() { - assertFalse(PatternFilter.INCLUDE_NOTHING.isInclude("qwe")); - assertFalse(PatternFilter.INCLUDE_NOTHING.isInclude("")); - assertFalse(PatternFilter.INCLUDE_NOTHING.isInclude(null)); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/TableFilterTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/TableFilterTest.java b/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/TableFilterTest.java deleted file mode 100644 index 4fad476..0000000 --- a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/filters/TableFilterTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/***************************************************************** - * 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.loader.filters; - -import junit.framework.TestCase; - -import java.util.TreeSet; -import java.util.regex.Pattern; - -public class TableFilterTest extends TestCase { - - public void testIncludeEverything() { - TableFilter filter = TableFilter.everything(); - - assertNotNull(filter.isIncludeTable("table")); - assertNotNull(filter.isIncludeTable("aaaa")); - assertNotNull(filter.isIncludeTable("")); - assertNotNull(filter.isIncludeTable("alex")); - } - - public void testInclude() { - TreeSet<IncludeTableFilter> includes = new TreeSet<IncludeTableFilter>(); - includes.add(new IncludeTableFilter("aaa")); - includes.add(new IncludeTableFilter("bb")); - - TableFilter filter = new TableFilter(includes, new TreeSet<Pattern>(PatternFilter.PATTERN_COMPARATOR)); - - assertNotNull(filter.isIncludeTable("aaa")); - assertNull(filter.isIncludeTable("aa")); - assertNull(filter.isIncludeTable("aaaa")); - - assertNotNull(filter.isIncludeTable("bb")); - assertNull(filter.isIncludeTable("")); - assertNull(filter.isIncludeTable("bbbb")); - } - - - public void testExclude() { - TreeSet<Pattern> excludes = new TreeSet<Pattern>(PatternFilter.PATTERN_COMPARATOR); - excludes.add(Pattern.compile("aaa")); - excludes.add(Pattern.compile("bb")); - - TreeSet<IncludeTableFilter> includes = new TreeSet<IncludeTableFilter>(); - includes.add(new IncludeTableFilter(null, PatternFilter.INCLUDE_EVERYTHING)); - - TableFilter filter = new TableFilter(includes, excludes); - - assertNull(filter.isIncludeTable("aaa")); - assertNotNull(filter.isIncludeTable("aa")); - assertNotNull(filter.isIncludeTable("aaaa")); - - assertNull(filter.isIncludeTable("bb")); - assertNotNull(filter.isIncludeTable("")); - assertNotNull(filter.isIncludeTable("bbbb")); - } - - public void testIncludeExclude() { - TreeSet<Pattern> excludes = new TreeSet<Pattern>(PatternFilter.PATTERN_COMPARATOR); - excludes.add(Pattern.compile("aaa")); - excludes.add(Pattern.compile("bb")); - - TreeSet<IncludeTableFilter> includes = new TreeSet<IncludeTableFilter>(); - includes.add(new IncludeTableFilter("aa.*")); - - TableFilter filter = new TableFilter(includes, excludes); - - assertNull(filter.isIncludeTable("aaa")); - assertNotNull(filter.isIncludeTable("aa")); - assertNotNull(filter.isIncludeTable("aaaa")); - - assertNull(filter.isIncludeTable("bb")); - assertNull(filter.isIncludeTable("")); - assertNull(filter.isIncludeTable("bbbb")); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/test/java/org/apache/cayenne/access/loader/mapper/DbTypeTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/mapper/DbTypeTest.java b/cayenne-server/src/test/java/org/apache/cayenne/access/loader/mapper/DbTypeTest.java deleted file mode 100644 index 8b2e8d4..0000000 --- a/cayenne-server/src/test/java/org/apache/cayenne/access/loader/mapper/DbTypeTest.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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.loader.mapper; - -import org.junit.Test; - -import java.util.Iterator; -import java.util.TreeSet; - -import static org.junit.Assert.*; - -public class DbTypeTest { - - @Test - public void testCompareTo() throws Exception { - TreeSet<DbType> set = new TreeSet<DbType>(); - set.add(new DbType("type-01", null, null, null, null)); - set.add(new DbType("type-02", null, null, null, null)); - set.add(new DbType("type-02", 1, null, null, null)); - set.add(new DbType("type-02", 2, null, null, null)); - set.add(new DbType("type-02", 2, null, null, true)); - set.add(new DbType("type-02", 2, null, null, false)); - set.add(new DbType("type-02", 2, null, 5, null)); - set.add(new DbType("type-02", 2, null, 5, false)); - set.add(new DbType("type-02", 2, null, 5, true)); - set.add(new DbType("type-02", null, 8, 5, true)); - set.add(new DbType("type-02", null, 9, 5, true)); - - Iterator<DbType> iterator = set.iterator(); - assertEquals(new DbType("type-02", 2, null, 5, true), iterator.next()); - assertEquals(new DbType("type-02", 2, null, 5, false), iterator.next()); - assertEquals(new DbType("type-02", null, 9, 5, true), iterator.next()); - assertEquals(new DbType("type-02", null, 8, 5, true), iterator.next()); - assertEquals(new DbType("type-02", 2, null, 5, null), iterator.next()); - assertEquals(new DbType("type-02", 2, null, null, true), iterator.next()); - assertEquals(new DbType("type-02", 2, null, null, false), iterator.next()); - assertEquals(new DbType("type-02", 2, null, null, null), iterator.next()); - assertEquals(new DbType("type-02", 1, null, null, null), iterator.next()); - assertEquals(new DbType("type-02", null, null, null, null), iterator.next()); - assertEquals(new DbType("type-01", null, null, null, null), iterator.next()); - } - - @Test - public void testCover() throws Exception { - DbType typeJava = new DbType("java"); - assertTrue(typeJava.isCover(typeJava)); - assertTrue(typeJava.isCover(new DbType("java", 1, 1, 1, null))); - assertTrue(typeJava.isCover(new DbType("java", 1, null, null, null))); - assertTrue(typeJava.isCover(new DbType("java", null, 1, null, null))); - assertTrue(typeJava.isCover(new DbType("java", null, null, 1, null))); - assertTrue(typeJava.isCover(new DbType("java", null, null, null, true))); - assertTrue(typeJava.isCover(new DbType("java", null, null, null, false))); - assertFalse(typeJava.isCover(new DbType("java1", null, null, null, null))); - - DbType typeWithLength = new DbType("java", 1, null, null, null); - assertTrue(typeWithLength.isCover(typeWithLength)); - assertTrue(typeWithLength.isCover(new DbType("java", 1, null, 1, null))); - assertTrue(typeWithLength.isCover(new DbType("java", 1, null, 1, true))); - assertTrue(typeWithLength.isCover(new DbType("java", 1, null, null, true))); - assertTrue(typeWithLength.isCover(new DbType("java", 1, 1, null, true))); - assertFalse(typeWithLength.isCover(new DbType("java", 2, null, null, null))); - assertFalse(typeWithLength.isCover(new DbType("java", null, null, null, true))); - assertFalse(typeWithLength.isCover(new DbType("java1", 2, null, null, null))); - - DbType typeWithLengthAndNotNull = new DbType("java", 1, null, null, true); - assertTrue(typeWithLength.isCover(typeWithLengthAndNotNull)); - assertTrue(typeWithLength.isCover(new DbType("java", 1, null, 1, true))); - assertTrue(typeWithLength.isCover(new DbType("java", 1, 1, 1, true))); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/2f7b1d53/cayenne-server/src/test/java/org/apache/cayenne/dba/PerAdapterProviderTest.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/dba/PerAdapterProviderTest.java b/cayenne-server/src/test/java/org/apache/cayenne/dba/PerAdapterProviderTest.java new file mode 100644 index 0000000..fb2ebcb --- /dev/null +++ b/cayenne-server/src/test/java/org/apache/cayenne/dba/PerAdapterProviderTest.java @@ -0,0 +1,83 @@ +/***************************************************************** + * 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.dba; + +import org.apache.cayenne.access.types.ExtendedType; +import org.apache.cayenne.access.types.ExtendedTypeFactory; +import org.apache.cayenne.configuration.RuntimeProperties; +import org.apache.cayenne.dba.derby.DerbyAdapter; +import org.apache.cayenne.dba.oracle.OracleAdapter; +import org.apache.cayenne.di.DIRuntimeException; +import org.apache.cayenne.di.Provider; +import org.apache.cayenne.di.spi.DefaultClassLoaderManager; +import org.apache.cayenne.log.CommonsJdbcEventLogger; +import org.apache.cayenne.resource.ClassLoaderResourceLocator; +import org.apache.cayenne.resource.ResourceLocator; +import org.junit.Before; +import org.junit.Test; + +import java.util.Collections; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +public class PerAdapterProviderTest { + + private OracleAdapter oracleAdapter; + private DerbyAdapter derbyAdapter; + private AutoAdapter autoDerbyAdapter; + + @Before + public void before() { + + ResourceLocator locator = new ClassLoaderResourceLocator(new DefaultClassLoaderManager()); + RuntimeProperties runtimeProperties = mock(RuntimeProperties.class); + + this.oracleAdapter = new OracleAdapter(runtimeProperties, + Collections.<ExtendedType>emptyList(), + Collections.<ExtendedType>emptyList(), + Collections.<ExtendedTypeFactory>emptyList(), + locator); + + this.derbyAdapter = new DerbyAdapter(runtimeProperties, + Collections.<ExtendedType>emptyList(), + Collections.<ExtendedType>emptyList(), + Collections.<ExtendedTypeFactory>emptyList(), + locator); + + this.autoDerbyAdapter = new AutoAdapter(new Provider<DbAdapter>() { + @Override + public DbAdapter get() throws DIRuntimeException { + return derbyAdapter; + } + }, new CommonsJdbcEventLogger(runtimeProperties)); + } + + @Test + public void testGet() { + + Map<String, String> map = Collections.singletonMap(DerbyAdapter.class.getName(), "x"); + PerAdapterProvider<String> provider = new PerAdapterProvider<>(map, "default"); + + assertEquals("default", provider.get(oracleAdapter)); + assertEquals("x", provider.get(derbyAdapter)); + assertEquals("x", provider.get(autoDerbyAdapter)); + } +}