Skip to content

Commit

Permalink
ISIS-268: Fixed tests to run with-out errors.
Browse files Browse the repository at this point in the history
 - Added @FixMethodOrder(MethodSorters.NAME_ASCENDING) annotation to force tests to run in alphabetical order
 - Tests need to run in a specific order to test full life-cycle of nothing, create, test - with the database being closed and re-opened between tests.
 - added @BeforeClass on deleteHsqlDbFiles to remove hsqldb files before test suite (clears all residual database files)
- added JdbcImageValueMapper to handle images as a test of blob support.
  • Loading branch information
Kevin Meyer committed Jan 26, 2013
1 parent bac67aa commit ed19fe9
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 122 deletions.
Expand Up @@ -38,7 +38,6 @@
import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
import org.apache.isis.core.metamodel.spec.feature.OneToOneAssociation;
import org.apache.isis.core.runtime.system.context.IsisContext;
import org.apache.isis.core.runtime.system.persistence.AdapterManagerSpi;
import org.apache.isis.core.runtime.system.persistence.PersistenceSession;
import org.apache.isis.objectstore.sql.AbstractMapper;
import org.apache.isis.objectstore.sql.CollectionMapper;
Expand All @@ -51,11 +50,11 @@
import org.apache.isis.objectstore.sql.mapping.FieldMapping;

public abstract class AbstractAutoMapper extends AbstractMapper {

private static final Logger LOG = Logger.getLogger(AbstractAutoMapper.class);

protected final Map<ObjectAssociation, FieldMapping> fieldMappingByField = Maps.newLinkedHashMap();

protected CollectionMapper collectionMappers[];
protected String collectionMapperFields[];
protected boolean dbCreatesId;
Expand All @@ -68,15 +67,17 @@ public abstract class AbstractAutoMapper extends AbstractMapper {
final FieldMappingLookup lookup;
final ObjectMappingLookup objectMappingLookup;

protected AbstractAutoMapper(final String className, final String parameterBase, final FieldMappingLookup lookup, final ObjectMappingLookup objectMappingLookup) {
this.specification = specificationFor(className);
protected AbstractAutoMapper(final String className, final String parameterBase, final FieldMappingLookup lookup,
final ObjectMappingLookup objectMappingLookup) {
this.specification = specificationFor(className);
this.className = className;
this.parameterBase = parameterBase;
this.lookup = lookup;
this.objectMappingLookup = objectMappingLookup;
}

protected AbstractAutoMapper(final FieldMappingLookup lookup, final AbstractAutoMapper abstractAutoMapper, final String className) {
protected AbstractAutoMapper(final FieldMappingLookup lookup, final AbstractAutoMapper abstractAutoMapper,
final String className) {

this.specification = getSpecificationLoader().loadSpecification(className);
this.className = className;
Expand All @@ -86,21 +87,21 @@ protected AbstractAutoMapper(final FieldMappingLookup lookup, final AbstractAuto
this.objectMappingLookup = null;
}

private static ObjectSpecification specificationFor(final String className) {
ObjectSpecification specification = IsisContext.getSpecificationLoader().loadSpecification(className);
private static ObjectSpecification specificationFor(final String className) {
ObjectSpecification specification = IsisContext.getSpecificationLoader().loadSpecification(className);
List<OneToOneAssociation> properties = specification.getProperties();
if (isNullOrEmpty(properties) && !specification.isAbstract()) {
if (isNullOrEmpty(properties) && !specification.isAbstract()) {
throw new SqlObjectStoreException(specification.getFullIdentifier() + " has no fields: " + specification);
}
return specification;
}

return specification;
}

protected void setUpFieldMappers() {
setUpFieldMappers(lookup, objectMappingLookup, className, parameterBase);
}

private void setUpFieldMappers(final FieldMappingLookup lookup, final ObjectMappingLookup objectMappingLookup, final String className, final String parameterBase) {
private void setUpFieldMappers(final FieldMappingLookup lookup, final ObjectMappingLookup objectMappingLookup,
final String className, final String parameterBase) {
final IsisConfiguration configParameters = getConfiguration();
table = configParameters.getString(parameterBase + ".table." + className);
if (table == null) {
Expand Down Expand Up @@ -130,7 +131,8 @@ protected String getTableNameFromSpecification(final ObjectSpecification objectS
protected void getExtraFields(final List<ObjectAssociation> fields) {
}

private void setupFullMapping(final FieldMappingLookup lookup, final ObjectMappingLookup objectMapperLookup, final String className, final IsisConfiguration configParameters, final String parameterBase) {
private void setupFullMapping(final FieldMappingLookup lookup, final ObjectMappingLookup objectMapperLookup,
final String className, final IsisConfiguration configParameters, final String parameterBase) {

fields.addAll(specification.getAssociations());

Expand Down Expand Up @@ -198,9 +200,14 @@ private void setupFullMapping(final FieldMappingLookup lookup, final ObjectMappi
if (fieldSpecification.hasSubclasses() || fieldSpecification.isAbstract()) {
// PolymorphicForeignKeyInChildCollectionBaseMapper
// Or PolymorphicForeignKeyInChildCollectionMapper
collectionMapper = new PolymorphicForeignKeyInChildCollectionBaseMapper(oneToManyProperties[collectionFieldNo], parameterBase, lookup, objectMapperLookup, this, field);
collectionMapper =
new PolymorphicForeignKeyInChildCollectionBaseMapper(
oneToManyProperties[collectionFieldNo], parameterBase, lookup, objectMapperLookup,
this, field);
} else {
final ForeignKeyInChildCollectionMapper mapper = new ForeignKeyInChildCollectionMapper(oneToManyProperties[collectionFieldNo], parameterBase, lookup, objectMapperLookup, this, field);
final ForeignKeyInChildCollectionMapper mapper =
new ForeignKeyInChildCollectionMapper(oneToManyProperties[collectionFieldNo],
parameterBase, lookup, objectMapperLookup, this, field);
mapper.setUpFieldMappers();
collectionMapper = mapper;
}
Expand All @@ -216,10 +223,8 @@ private void setupFullMapping(final FieldMappingLookup lookup, final ObjectMappi
throw new SqlObjectStoreException("Expected property " + property);
}
/*
* collectionMappers[collectionFieldNo] = new
* ForeignKeyCollectionMapper(elementType,
* oneToManyProperties[collectionFieldNo], parameterBase,
* lookup, objectMapperLookup);
* collectionMappers[collectionFieldNo] = new ForeignKeyCollectionMapper(elementType,
* oneToManyProperties[collectionFieldNo], parameterBase, lookup, objectMapperLookup);
*/
} else {
// TODO use other mappers where necessary
Expand All @@ -236,53 +241,40 @@ private void setupFullMapping(final FieldMappingLookup lookup, final ObjectMappi
}
}


for (final ObjectAssociation field : oneToOneProperties) {
if(fieldMappingByField.containsKey(field)) {
continue;
}
final FieldMapping mapping = lookup.createMapping(specification, field);
if (fieldMappingByField.containsKey(field)) {
continue;
}
final FieldMapping mapping = lookup.createMapping(specification, field);
fieldMappingByField.put(field, mapping);
}
}

protected String columnList(final Map<ObjectAssociation, FieldMapping> fieldMappingByField) {
return columnList(fieldMappingByField.values());
return columnList(fieldMappingByField.values());
}

/*
* private void setupSpecifiedMapping( final ObjectSpecification
* specification, final IsisConfiguration configParameters, final String
* parameterBase) { IsisConfiguration columnMappings =
* IsisContext.getConfiguration().createSubset(parameterBase + "column");
* int columnsSize = columnMappings.size(); // columnNames = new
* String[columnsSize]; oneToOneProperties = new
* ObjectAssociation[columnsSize];
* private void setupSpecifiedMapping( final ObjectSpecification specification, final IsisConfiguration
* configParameters, final String parameterBase) { IsisConfiguration columnMappings =
* IsisContext.getConfiguration().createSubset(parameterBase + "column"); int columnsSize = columnMappings.size();
* // columnNames = new String[columnsSize]; oneToOneProperties = new ObjectAssociation[columnsSize];
*
* int i = 0; for (Enumeration names = columnMappings.propertyNames();
* names.hasMoreElements(); i++) { String columnName = (String)
* names.nextElement(); String fieldName =
* columnMappings.getString(columnName); oneToOneProperties[i] =
* specification.getAssociation(fieldName); // columnNames[i] = columnName;
* }
* int i = 0; for (Enumeration names = columnMappings.propertyNames(); names.hasMoreElements(); i++) { String
* columnName = (String) names.nextElement(); String fieldName = columnMappings.getString(columnName);
* oneToOneProperties[i] = specification.getAssociation(fieldName); // columnNames[i] = columnName; }
*
* IsisConfiguration collectionMappings =
* IsisContext.getConfiguration().createSubset( parameterBase +
* "collection"); int collectionsSize = collectionMappings.size();
* collectionMappers = new AutoCollectionMapper[collectionsSize];
* oneToManyProperties = new ObjectAssociation[collectionsSize];
* IsisConfiguration collectionMappings = IsisContext.getConfiguration().createSubset( parameterBase +
* "collection"); int collectionsSize = collectionMappings.size(); collectionMappers = new
* AutoCollectionMapper[collectionsSize]; oneToManyProperties = new ObjectAssociation[collectionsSize];
*
* int j = 0; for (Enumeration names = collectionMappings.propertyNames();
* names.hasMoreElements(); j++) { String propertyName = (String)
* names.nextElement(); String collectionName =
* collectionMappings.getString(propertyName); String type =
* collectionMappings.getString(collectionName);
* int j = 0; for (Enumeration names = collectionMappings.propertyNames(); names.hasMoreElements(); j++) { String
* propertyName = (String) names.nextElement(); String collectionName = collectionMappings.getString(propertyName);
* String type = collectionMappings.getString(collectionName);
*
* oneToManyProperties[j] = specification.getAssociation(collectionName); if
* (type.equals("auto")) { collectionMappers[j] = new
* AutoCollectionMapper(this, specification, oneToManyProperties[j],
* getLookup()); } else { // TODO use other mappers where necessary // new
* ReversedAutoAssociationMapper(specification, collectionName,
* oneToManyProperties[j] = specification.getAssociation(collectionName); if (type.equals("auto")) {
* collectionMappers[j] = new AutoCollectionMapper(this, specification, oneToManyProperties[j], getLookup()); } else
* { // TODO use other mappers where necessary // new ReversedAutoAssociationMapper(specification, collectionName,
* parameterBase);
*
* throw new NotYetImplementedException(); } } }
Expand All @@ -303,8 +295,9 @@ protected ObjectAdapter getAdapter(final ObjectSpecification spec, final Oid oid
if (adapter != null) {
return adapter;
}

// REVIEW: where the oid is a TypedOid, the following two lines could be replaced by getPersistenceSession().recreatePersistentAdapter(oid)

// REVIEW: where the oid is a TypedOid, the following two lines could be replaced by
// getPersistenceSession().recreatePersistentAdapter(oid)
// is preferable, since then reuses the PojoRecreator impl defined within SqlPersistorInstaller
final Object recreatedPojo = spec.createObject();
return getPersistenceSession().mapRecreatedPojo(oid, recreatedPojo);
Expand Down Expand Up @@ -333,25 +326,23 @@ protected String values(final DatabaseConnector connector, final ObjectAdapter o
return sql.toString();
}


private static boolean isNullOrEmpty(List<?> list) {
return list == null || list.size() == 0;
return list == null || list.size() == 0;
}



@Override
public String toString() {
return "AbstractAutoMapper [table=" + table + ",noColumns=" + fieldMappingByField.size() + ",specification=" + specification.getFullIdentifier() + "]";
return "AbstractAutoMapper [table=" + table + ",noColumns=" + fieldMappingByField.size() + ",specification="
+ specification.getFullIdentifier() + "]";
}

protected SpecificationLoaderSpi getSpecificationLoader() {
return IsisContext.getSpecificationLoader();
}
protected SpecificationLoaderSpi getSpecificationLoader() {
return IsisContext.getSpecificationLoader();
}

protected IsisConfiguration getConfiguration() {
return IsisContext.getConfiguration();
}
protected IsisConfiguration getConfiguration() {
return IsisContext.getConfiguration();
}

protected PersistenceSession getPersistenceSession() {
return IsisContext.getPersistenceSession();
Expand All @@ -361,5 +352,4 @@ protected AdapterManager getAdapterManager() {
return getPersistenceSession().getAdapterManager();
}


}
Expand Up @@ -26,8 +26,6 @@

import org.apache.isis.core.commons.debug.DebugBuilder;
import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
import org.apache.isis.core.metamodel.adapter.ResolveState;
import org.apache.isis.core.metamodel.adapter.oid.Oid;
import org.apache.isis.core.metamodel.adapter.oid.RootOid;
import org.apache.isis.core.metamodel.facets.collections.modify.CollectionFacet;
import org.apache.isis.core.metamodel.facets.typeof.TypeOfFacet;
Expand All @@ -51,7 +49,8 @@ public class AutoCollectionMapper extends AbstractMapper implements CollectionMa
private final ObjectReferenceMapping elementMapping;
private final IdMapping idMapping;

public AutoCollectionMapper(final ObjectSpecification specification, final ObjectAssociation field, final FieldMappingLookup lookup) {
public AutoCollectionMapper(final ObjectSpecification specification, final ObjectAssociation field,
final FieldMappingLookup lookup) {
this.field = field;

final ObjectSpecification spec = field.getFacet(TypeOfFacet.class).valueSpec();
Expand Down Expand Up @@ -87,33 +86,34 @@ public void loadInternalCollection(final DatabaseConnector connector, final Obje
final ObjectAdapter collectionAdapter = field.get(parentAdapter);
if (!collectionAdapter.canTransitionToResolving()) {
return;
}
if(LOG.isDebugEnabled()) {
}

if (LOG.isDebugEnabled()) {
LOG.debug("loading internal collection " + field);
}

try {
PersistorUtil.startResolving(collectionAdapter);

final StringBuffer sql = new StringBuffer();
sql.append("select ");
idMapping.appendColumnNames(sql);
sql.append(", ");
elementMapping.appendColumnNames(sql);
sql.append(" from ");
sql.append(tableName);

final Results rs = connector.select(sql.toString());
final List<ObjectAdapter> list = new ArrayList<ObjectAdapter>();
while (rs.next()) {
final ObjectAdapter element = ((JdbcObjectReferenceMapping) elementMapping).initializeField(rs);
if(LOG.isDebugEnabled()) {
if (LOG.isDebugEnabled()) {
LOG.debug(" element " + element.getOid());
}
list.add(element);
}
final CollectionFacet collectionFacet = collectionAdapter.getSpecification().getFacet(CollectionFacet.class);
final CollectionFacet collectionFacet =
collectionAdapter.getSpecification().getFacet(CollectionFacet.class);
collectionFacet.init(collectionAdapter, list.toArray(new ObjectAdapter[list.size()]));
rs.close();
} finally {
Expand Down

0 comments on commit ed19fe9

Please sign in to comment.