Skip to content

Commit

Permalink
Add support for entities that have a primary key of type Long (issue #48
Browse files Browse the repository at this point in the history
)

Currently, WOUnit does not support entities that have a primary key of type long. This small change fixes the issue by using a long global fake id. It takes a look at the type of the PK and casts to long/int as needed.

This fix was originally provided by Giancarlo Dessena. See pull request for more details.

#48
  • Loading branch information
Zigdra authored and hprange committed Jan 14, 2017
1 parent f389785 commit 9846f69
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/com/wounit/rules/MockEditingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.lang.reflect.Field;

import com.webobjects.eoaccess.EOAttribute;
import com.webobjects.eoaccess.EOEntity;
import com.webobjects.eoaccess.EOModelGroup;
import com.webobjects.eoaccess.EOUtilities;
Expand All @@ -36,6 +37,7 @@
import com.webobjects.foundation.NSRange;
import com.wounit.annotations.Dummy;

import er.extensions.eof.ERXModelGroup;
import er.extensions.eof.ERXQ;
import er.extensions.eof.ERXS;
import er.extensions.foundation.ERXArrayUtilities;
Expand Down Expand Up @@ -96,10 +98,18 @@ public void insert(EOEnterpriseObject object) {

private static final long serialVersionUID = 1L;

private static boolean hasLongPrimaryKey(String entityName) {
EOEntity entity = ERXModelGroup.defaultGroup().entityNamed(entityName);

NSArray<EOAttribute> primaryKeys = entity.primaryKeyAttributes();

return primaryKeys.size() == 1 && primaryKeys.get(0).valueTypeClassName().equals(Long.class.getName());
}

/**
* A counter for fake global IDs.
*/
private int globalFakeId = 0;
private long globalFakeId = 0L;

/**
* An array of objects whose changes must be ignored during the test cycle.
Expand Down Expand Up @@ -152,7 +162,11 @@ protected void before() {
private EOGlobalID createPermanentGlobalFakeId(String entityName) {
globalFakeId++;

return new _EOIntegralKeyGlobalID(entityName, globalFakeId);
if (hasLongPrimaryKey(entityName)) {
return new _EOIntegralKeyGlobalID(entityName, globalFakeId);
}

return new _EOIntegralKeyGlobalID(entityName, (int) globalFakeId);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/wounit/model/LongPKEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (C) 2009 hprange <[email protected]>
*
* Licensed 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 com.wounit.model;

public class LongPKEntity extends _LongPKEntity {
}
128 changes: 128 additions & 0 deletions src/test/java/com/wounit/model/_LongPKEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* Copyright (C) 2009 hprange <[email protected]>
*
* Licensed 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.
*/
// $LastChangedRevision$ DO NOT EDIT. Make changes to LongPKEntity.java instead.
package com.wounit.model;

import com.webobjects.eoaccess.*;
import com.webobjects.eocontrol.*;
import com.webobjects.foundation.*;
import java.math.*;
import java.util.*;
import org.apache.log4j.Logger;

@SuppressWarnings("all")
public abstract class _LongPKEntity extends er.extensions.eof.ERXGenericRecord {
public static final String ENTITY_NAME = "LongPKEntity";

// Attributes
public static final String BAR_KEY = "bar";
public static final String TYPE_KEY = "type";

// Relationships

private static Logger LOG = Logger.getLogger(_LongPKEntity.class);

public LongPKEntity localInstanceIn(EOEditingContext editingContext) {
LongPKEntity localInstance = (LongPKEntity)EOUtilities.localInstanceOfObject(editingContext, this);
if (localInstance == null) {
throw new IllegalStateException("You attempted to localInstance " + this + ", which has not yet committed.");
}
return localInstance;
}

public String bar() {
return (String) storedValueForKey("bar");
}

public void setBar(String value) {
if (_LongPKEntity.LOG.isDebugEnabled()) {
_LongPKEntity.LOG.debug( "updating bar from " + bar() + " to " + value);
}
takeStoredValueForKey(value, "bar");
}

public Integer type() {
return (Integer) storedValueForKey("type");
}

public void setType(Integer value) {
if (_LongPKEntity.LOG.isDebugEnabled()) {
_LongPKEntity.LOG.debug( "updating type from " + type() + " to " + value);
}
takeStoredValueForKey(value, "type");
}


public static LongPKEntity createLongPKEntity(EOEditingContext editingContext) {
LongPKEntity eo = (LongPKEntity) EOUtilities.createAndInsertInstance(editingContext, _LongPKEntity.ENTITY_NAME);
return eo;
}

public static NSArray<LongPKEntity> fetchAllLongPKEntities(EOEditingContext editingContext) {
return _LongPKEntity.fetchAllLongPKEntities(editingContext, null);
}

public static NSArray<LongPKEntity> fetchAllLongPKEntities(EOEditingContext editingContext, NSArray<EOSortOrdering> sortOrderings) {
return _LongPKEntity.fetchLongPKEntities(editingContext, null, sortOrderings);
}

public static NSArray<LongPKEntity> fetchLongPKEntities(EOEditingContext editingContext, EOQualifier qualifier, NSArray<EOSortOrdering> sortOrderings) {
EOFetchSpecification fetchSpec = new EOFetchSpecification(_LongPKEntity.ENTITY_NAME, qualifier, sortOrderings);
fetchSpec.setIsDeep(true);
NSArray<LongPKEntity> eoObjects = (NSArray<LongPKEntity>)editingContext.objectsWithFetchSpecification(fetchSpec);
return eoObjects;
}

public static LongPKEntity fetchLongPKEntity(EOEditingContext editingContext, String keyName, Object value) {
return _LongPKEntity.fetchLongPKEntity(editingContext, new EOKeyValueQualifier(keyName, EOQualifier.QualifierOperatorEqual, value));
}

public static LongPKEntity fetchLongPKEntity(EOEditingContext editingContext, EOQualifier qualifier) {
NSArray<LongPKEntity> eoObjects = _LongPKEntity.fetchLongPKEntities(editingContext, qualifier, null);
LongPKEntity eoObject;
int count = eoObjects.count();
if (count == 0) {
eoObject = null;
}
else if (count == 1) {
eoObject = (LongPKEntity)eoObjects.objectAtIndex(0);
}
else {
throw new IllegalStateException("There was more than one LongPKEntity that matched the qualifier '" + qualifier + "'.");
}
return eoObject;
}

public static LongPKEntity fetchRequiredLongPKEntity(EOEditingContext editingContext, String keyName, Object value) {
return _LongPKEntity.fetchRequiredLongPKEntity(editingContext, new EOKeyValueQualifier(keyName, EOQualifier.QualifierOperatorEqual, value));
}

public static LongPKEntity fetchRequiredLongPKEntity(EOEditingContext editingContext, EOQualifier qualifier) {
LongPKEntity eoObject = _LongPKEntity.fetchLongPKEntity(editingContext, qualifier);
if (eoObject == null) {
throw new NoSuchElementException("There was no LongPKEntity that matched the qualifier '" + qualifier + "'.");
}
return eoObject;
}

public static LongPKEntity localInstanceIn(EOEditingContext editingContext, LongPKEntity eo) {
LongPKEntity localInstance = (eo == null) ? null : (LongPKEntity)EOUtilities.localInstanceOfObject(editingContext, eo);
if (localInstance == null && eo != null) {
throw new IllegalStateException("You attempted to localInstance " + eo + ", which has not yet committed.");
}
return localInstance;
}
}
20 changes: 20 additions & 0 deletions src/test/java/com/wounit/rules/TestMockEditingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.wounit.rules;

import static er.extensions.eof.ERXEOControlUtilities.primaryKeyObjectForObject;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
Expand Down Expand Up @@ -48,6 +49,7 @@
import com.wounit.model.DifferentClassNameForEntity;
import com.wounit.model.FooEntity;
import com.wounit.model.FooEntityWithRequiredField;
import com.wounit.model.LongPKEntity;
import com.wounit.model.StubEntity;
import com.wounit.model.SubFooEntity;
import com.wounit.stubs.StubTestCase;
Expand Down Expand Up @@ -559,6 +561,24 @@ public void savedObjectUsesGlobalFakeId() throws Exception {
editingContext.after();
}

@Test
public void savedObjectUsesGlobalFakeIdInCaseOfALongPK() throws Exception {
MockEditingContext editingContext = (MockEditingContext) initEditingContext(TEST_MODEL_NAME);

editingContext.before();

LongPKEntity.createLongPKEntity(editingContext);

editingContext.saveChanges();

LongPKEntity mockLongPKEntity = editingContext.createSavedObject(LongPKEntity.class);

assertThat(primaryKeyObjectForObject(mockLongPKEntity), instanceOf(Long.class));
assertThat(primaryKeyObjectForObject(mockLongPKEntity), is((Object) 2L));

editingContext.after();
}

@Before
public void setup() {
when(mockFetchSpecification.entityName()).thenReturn(FooEntity.ENTITY_NAME);
Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/Test.eomodeld/LongPKEntity.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
attributes = (
{allowsNull = Y; columnName = bar; name = bar; prototypeName = shortString; },
{allowsNull = N; columnName = id; name = id; prototypeName = longNumber; },
{allowsNull = Y; columnName = type; name = type; prototypeName = intNumber; }
);
attributesUsedForLocking = (bar, id, type);
className = "com.wounit.model.LongPKEntity";
classProperties = (bar, type);
externalName = LongPKEntity;
fetchSpecificationDictionary = {};
name = LongPKEntity;
primaryKeyAttributes = (id);
}
1 change: 1 addition & 0 deletions src/test/resources/Test.eomodeld/index.eomodeld
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
name = FooEntityWithRequiredField;
},
{className = "com.wounit.model.JodaEntity"; name = JodaEntity; },
{className = "com.wounit.model.LongPKEntity"; name = LongPKEntity; },
{
className = "com.wounit.model.SubFooEntity";
name = SubFooEntity;
Expand Down

0 comments on commit 9846f69

Please sign in to comment.