From 2f97465f7aef7c22994dcb11041beb6f1f1fd8eb Mon Sep 17 00:00:00 2001 From: "jesus m. rodriguez" Date: Fri, 10 Jul 2009 14:52:54 -0400 Subject: added unit test for ObjectFactory --- .../candlepin/model/ObjectFactory.java | 29 ++++--- .../candlepin/model/test/ObjectFactoryTest.java | 94 ++++++++++++++++++++++ 2 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 proxy/code/src/org/fedoraproject/candlepin/model/test/ObjectFactoryTest.java (limited to 'proxy/code/src') diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java b/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java index b96f8eb..ef0f4eb 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java @@ -14,9 +14,10 @@ */ package org.fedoraproject.candlepin.model; -import org.apache.log4j.Logger; import org.fedoraproject.candlepin.util.MethodUtil; +import org.apache.log4j.Logger; + import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; @@ -25,25 +26,31 @@ import java.util.List; import java.util.Map; /** + * ObjectFactory is used to create and persist the data model. * @author mmccune - * */ public class ObjectFactory { - /** - * Logger for this class - */ + /** Logger for this class */ private static final Logger logger = Logger.getLogger(ObjectFactory.class); + /** the singleton instance of the ObjectFactory */ private static ObjectFactory instance = new ObjectFactory(); - private Map objects; + private Map objects; + + /** + * default constructor + */ private ObjectFactory() { objects = new HashMap(); initMockObjects(); } + /** + * @deprecated demo method + */ private void initMockObjects() { Organization org = new Organization(BaseModel.generateUUID()); org.setName("test-org"); @@ -75,6 +82,10 @@ public class ObjectFactory { this.store(pool); } + /** + * Returns the instance of the ObjectFactory. + * @return the instance of the ObjectFactory. + */ public static ObjectFactory get() { return instance; } @@ -82,10 +93,10 @@ public class ObjectFactory { /** * Get a List of objects by type * @param clazz - * @return List if found. null if not. + * @return List if found. null if not. */ - public List listObjectsByClass(Class clazz) { - return (List) objects.get(clazz.getName()); + public List listObjectsByClass(Class clazz) { + return (List) objects.get(clazz.getName()); } /** diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/test/ObjectFactoryTest.java b/proxy/code/src/org/fedoraproject/candlepin/model/test/ObjectFactoryTest.java new file mode 100644 index 0000000..2439356 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/test/ObjectFactoryTest.java @@ -0,0 +1,94 @@ +/** + * Copyright (c) 2008 Red Hat, Inc. + * + * This software is licensed to you under the GNU General Public License, + * version 2 (GPLv2). There is NO WARRANTY for this software, express or + * implied, including the implied warranties of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 + * along with this software; if not, see + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. + * + * Red Hat trademarks are not licensed under GPLv2. No permission is + * granted to use or replicate Red Hat trademarks that are incorporated + * in this software or its documentation. + */ +package org.fedoraproject.candlepin.model.test; + +import org.fedoraproject.candlepin.model.BaseModel; +import org.fedoraproject.candlepin.model.ObjectFactory; +import org.fedoraproject.candlepin.model.Organization; + +import java.util.List; + +import junit.framework.TestCase; + + +/** + * ObjectFactoryTest + */ +public class ObjectFactoryTest extends TestCase { + + public void testGet() { + ObjectFactory o1 = ObjectFactory.get(); + ObjectFactory o2 = ObjectFactory.get(); + + assertNotNull(o1); + assertNotNull(o2); + assertEquals(o1, o2); + } + + public void testListObjectsByClass() { + List l = ObjectFactory.get().listObjectsByClass(Object.class); + assertNull(l); + + l = ObjectFactory.get().listObjectsByClass(Organization.class); + assertNotNull(l); + assertFalse(l.isEmpty()); + Object o = l.get(0); + assertNotNull(o); + assertEquals(o.getClass(), Organization.class); + } + + public void testStore() { + // make sure we don't have one stored already + List list = ObjectFactory.get().listObjectsByClass(Long.class); + assertNull(list); + + Long l = new Long(10); + ObjectFactory.get().store(l); + + // verify it got stored + list = ObjectFactory.get().listObjectsByClass(Long.class); + assertNotNull(list); + assertFalse(list.isEmpty()); + Long l2 = (Long) list.get(0); + assertEquals(l, l2); + } + + public void testLookupByUUID() { + String uuid = BaseModel.generateUUID(); + assertNull(ObjectFactory.get().lookupByUUID(Organization.class, uuid)); + + Organization org = new Organization(uuid); + org.setName("unit-test-org"); + ObjectFactory.get().store(org); + Object o = ObjectFactory.get().lookupByUUID(Organization.class, uuid); + assertNotNull(o); + assertEquals(o.getClass(), Organization.class); + assertEquals(((Organization)o).getUuid(), org.getUuid()); + } + + public void testLookupByFieldName() { + String uuid = BaseModel.generateUUID(); + assertNull(ObjectFactory.get().lookupByUUID(Organization.class, uuid)); + + Organization org = new Organization(uuid); + org.setName("unit-test-org"); + ObjectFactory.get().store(org); + + Organization o2 = (Organization) ObjectFactory.get().lookupByFieldName( + Organization.class, "uuid", uuid); + assertNotNull(o2); + assertEquals(org, o2); + } +} -- cgit