Skip to content

Commit

Permalink
Getting class attributes and class relationships always seemed oddly …
Browse files Browse the repository at this point in the history
…difficult, and why should this be in a static method in ERXCopyable.
  • Loading branch information
rkiddy committed Feb 18, 2013
1 parent 832ecf7 commit 3d6b237
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

import com.webobjects.eocontrol.EOClassDescription;
import com.webobjects.eocontrol.EOKeyGlobalID;

import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSDictionary;
import com.webobjects.foundation.NSMutableArray;
import com.webobjects.foundation.NSMutableDictionary;

/**
Expand Down Expand Up @@ -102,4 +105,22 @@ public void setClassDescription(EOClassDescription classDescription) {
protected EOKeyGlobalID _globalIDWithoutTypeCoercion(Object[] values) {
return ERXSingleValueID.globalIDWithEntityName(name(), values);
}

public NSArray<EOAttribute> classAttributes() {
NSMutableArray<EOAttribute> found = new NSMutableArray<EOAttribute>();
for (String name : (NSArray<String>)this.classPropertyNames()) {
if (this.attributeNamed(name) != null)
found.add(this.attributeNamed(name));
}
return found.immutableClone();
}

public NSArray<EORelationship> classRelationships() {
NSMutableArray<EORelationship> found = new NSMutableArray<EORelationship>();
for (String name : (NSArray<String>)this.classPropertyNames()) {
if (this.relationshipNamed(name) != null)
found.add(this.relationshipNamed(name));
}
return found.immutableClone();
}
}
157 changes: 89 additions & 68 deletions Tests/ERXTest/Sources/com/webobjects/eoaccess/ERXEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,109 +7,130 @@

import com.webobjects.eocontrol.EOClassDescription;
import com.webobjects.eocontrol.EOEditingContext;

import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSDictionary;
import com.webobjects.foundation.NSPropertyListSerialization;
import com.webobjects.foundation.NSSet;

import er.erxtest.ERXTestCase;

import er.extensions.eof.ERXModelGroup;
import er.extensions.eof.ERXEC;

/* Test the ERXEntity methods. These tests are extremely minimal.
* This class exists in order to deal with some issues in inheritance, and
* this system is not configured to use models that use inheritance yet.
*
* @author Ray Kiddy, [email protected]
* @author Ray Kiddy, [email protected]
*/
public class ERXEntityTest extends ERXTestCase {

static String buildRoot;
static {
buildRoot = System.getProperty("build.root");
}
EOEditingContext ec;
String adaptorName = "Memory";
String modelName;
EOModel model;

@Override
public void setUp() throws Exception {
super.setUp();

// if (ec != null) ec.dispose();
// if (model != null) model.dispose();
static String buildRoot;
static {
buildRoot = System.getProperty("build.root");
}
EOEditingContext ec;
String adaptorName = "Memory";
String modelName;
EOModel model;

@Override
public void setUp() throws Exception {
super.setUp();

// if (ec != null) ec.dispose();
// if (model != null) model.dispose();
//
// EOModelGroup.setDefaultGroup(new EOModelGroup());
// EOModelGroup.setDefaultGroup(new EOModelGroup());
//
// modelName = "ERXTest";
// modelName = "ERXTest";
//
// URL modelUrl = ERXFileUtilities.pathURLForResourceNamed(modelName+".eomodeld", null, null);
// URL modelUrl = ERXFileUtilities.pathURLForResourceNamed(modelName+".eomodeld", null, null);
//
// EOModelGroup.defaultGroup().addModel(new EOModel(modelUrl));
// EOModelGroup.defaultGroup().addModel(new EOModel(modelUrl));
//
// model = EOModelGroup.defaultGroup().modelNamed(modelName);
// model.setConnectionDictionary(ERExtensionsTest.connectionDict(adaptorName));
// model = EOModelGroup.defaultGroup().modelNamed(modelName);
// model.setConnectionDictionary(ERExtensionsTest.connectionDict(adaptorName));

model = EOModelGroup.defaultGroup().modelNamed("ERXTest");
ec = ERXEC.newEditingContext();
}

public void testConstructor() {
ERXEntity entity = new ERXEntity();
Assert.assertNotNull(entity);
}

model = EOModelGroup.defaultGroup().modelNamed("ERXTest");
ec = ERXEC.newEditingContext();
}
public void testPlistConstructor() {
URL entityUrl = null;
try {
entityUrl = new java.net.URL(model.pathURL()+"/Company.plist");
} catch (java.net.MalformedURLException e) { throw new IllegalArgumentException(e.getMessage()); }

public void testOne() { }

public void testConstructor() {
ERXEntity entity = new ERXEntity();
Assert.assertNotNull(entity);
}
NSDictionary plist = (NSDictionary)NSPropertyListSerialization.propertyListWithPathURL(entityUrl);

public void testPlistConstructor() {
URL entityUrl = null;
try {
entityUrl = new java.net.URL(model.pathURL()+"/Company.plist");
} catch (java.net.MalformedURLException e) { throw new IllegalArgumentException(e.getMessage()); }
Assert.assertNotNull(new ERXEntity(plist, model));
}

NSDictionary plist = (NSDictionary)NSPropertyListSerialization.propertyListWithPathURL(entityUrl);
public void _testAnyAttributeNamed() {

Assert.assertNotNull(new ERXEntity(plist, model));
}
// Should this not return just a "new ERXEntity()"? It returns null.
//
//Assert.assertNotNull(new ERXEntity(null, null));
}

public void _testAnyAttributeNamed() {
public void testClassAttributes() {
NSArray<EOAttribute> attrs = ((ERXEntity)ERXModelGroup.defaultGroup().entityNamed("Employee")).classAttributes();
@SuppressWarnings("unchecked")
NSSet<String> foundAttrs = new NSSet<String>((NSArray<String>)attrs.valueForKey("name"));
NSSet<String> expectedAttrs = new NSSet<String>(new String[] {
"address1", "address2", "bestSalesTotal", "city", "firstName", "lastName", "manager", "state", "zipcode"
} );
Assert.assertEquals(expectedAttrs, foundAttrs);
}

// Should this not return just a "new ERXEntity()"? It returns null.
//
//Assert.assertNotNull(new ERXEntity(null, null));
}
public void testClassRelationships() {
NSArray<EORelationship> rels = ((ERXEntity)ERXModelGroup.defaultGroup().entityNamed("Employee")).classRelationships();
@SuppressWarnings("unchecked")
NSSet<String> foundRels = new NSSet<String>((NSArray<String>)rels.valueForKey("name"));
NSSet<String> expectedRels = new NSSet<String>(new String[] { "company", "department", "paychecks", "roles" } );
Assert.assertEquals(expectedRels, foundRels);
}

public void testHasExternalName() {
URL entityUrl = null;
try {
entityUrl = new java.net.URL(model.pathURL()+"/Company.plist");
} catch (java.net.MalformedURLException e) { throw new IllegalArgumentException(e.getMessage()); }
public void testHasExternalName() {
URL entityUrl = null;
try {
entityUrl = new java.net.URL(model.pathURL()+"/Company.plist");
} catch (java.net.MalformedURLException e) { throw new IllegalArgumentException(e.getMessage()); }

NSDictionary plist = (NSDictionary)NSPropertyListSerialization.propertyListWithPathURL(entityUrl);
NSDictionary plist = (NSDictionary)NSPropertyListSerialization.propertyListWithPathURL(entityUrl);

ERXEntity erxentity = new ERXEntity(plist, model);
ERXEntity erxentity = new ERXEntity(plist, model);

Assert.assertTrue(erxentity.hasExternalName());
}
Assert.assertTrue(erxentity.hasExternalName());
}

public void testSetClassDescription() {
public void testSetClassDescription() {

EOEntity entity1 = EOModelGroup.defaultGroup().entityNamed("Company");
EOClassDescription desc = entity1.classDescriptionForInstances();
EOEntity entity1 = EOModelGroup.defaultGroup().entityNamed("Company");
EOClassDescription desc = entity1.classDescriptionForInstances();

Assert.assertNotNull(desc);
Assert.assertNotNull(desc);

URL entityUrl = null;
try {
entityUrl = new java.net.URL(model.pathURL()+"/Employee.plist");
} catch (java.net.MalformedURLException e) { throw new IllegalArgumentException(e.getMessage()); }
URL entityUrl = null;
try {
entityUrl = new java.net.URL(model.pathURL()+"/Employee.plist");
} catch (java.net.MalformedURLException e) { throw new IllegalArgumentException(e.getMessage()); }

NSDictionary plist = (NSDictionary)NSPropertyListSerialization.propertyListWithPathURL(entityUrl);
NSDictionary plist = (NSDictionary)NSPropertyListSerialization.propertyListWithPathURL(entityUrl);

ERXEntity entity2 = new ERXEntity(plist, model);
ERXEntity entity2 = new ERXEntity(plist, model);

// Using a mis-matched EOClassDescription here, but doing that on purpose so we can verify the superclass did not just ignore the set.
//
entity2.setClassDescription(desc);
// Using a mis-matched EOClassDescription here, but doing that on purpose so we can verify the superclass did not just ignore the set.
//
entity2.setClassDescription(desc);

//Assert.assertTrue(ERExtensionsTest.equalsForEOAccessObjects(desc, entity2.classDescriptionForInstances()));
}
//Assert.assertTrue(ERExtensionsTest.equalsForEOAccessObjects(desc, entity2.classDescriptionForInstances()));
}
}

0 comments on commit 3d6b237

Please sign in to comment.