diff options
author | PKI Team <PKI Team@c9f7a03b-bd48-0410-a16d-cbbf54688b0b> | 2008-03-18 22:36:57 +0000 |
---|---|---|
committer | PKI Team <PKI Team@c9f7a03b-bd48-0410-a16d-cbbf54688b0b> | 2008-03-18 22:36:57 +0000 |
commit | d0f2e4efbd3eb0f1d7f5a28e7f97c1fb4ec027bb (patch) | |
tree | 7e7473fae8af5ad7e6cda7eabbef787093fc59a7 /pki/base/common/test/com/netscape/cmscore/dbs | |
parent | 273f8d85df5c31293a908185622b378c8f3cf7e8 (diff) | |
download | pki-d0f2e4efbd3eb0f1d7f5a28e7f97c1fb4ec027bb.tar.gz pki-d0f2e4efbd3eb0f1d7f5a28e7f97c1fb4ec027bb.tar.xz pki-d0f2e4efbd3eb0f1d7f5a28e7f97c1fb4ec027bb.zip |
Initial open source version based upon proprietary Red Hat Certificate System (RHCS) 7.3.
git-svn-id: svn+ssh://svn.fedorahosted.org/svn/pki/trunk@2 c9f7a03b-bd48-0410-a16d-cbbf54688b0b
Diffstat (limited to 'pki/base/common/test/com/netscape/cmscore/dbs')
7 files changed, 610 insertions, 0 deletions
diff --git a/pki/base/common/test/com/netscape/cmscore/dbs/CertRecordListTest.java b/pki/base/common/test/com/netscape/cmscore/dbs/CertRecordListTest.java new file mode 100644 index 000000000..2d419b720 --- /dev/null +++ b/pki/base/common/test/com/netscape/cmscore/dbs/CertRecordListTest.java @@ -0,0 +1,86 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package com.netscape.cmscore.dbs; + +import junit.framework.*; + +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.dbs.IElementProcessor; +import com.netscape.cmscore.test.CMSBaseTestCase; + +public class CertRecordListTest extends CMSBaseTestCase { + + public CertRecordListTest(String name) { + super(name); + } + + public void cmsTestSetUp() { + } + + public void cmsTestTearDown() { + } + + public static Test suite() { + return new TestSuite(CertRecordListTest.class); + } + + public void testProcessCertRecordsUsesSize() throws EBaseException { + DBVirtualListStub dbList = new DBVirtualListStub(); + dbList.size = 5; + + CertRecordList certList = new CertRecordList(dbList); + + assertEquals(5, dbList.size); + assertEquals(0, dbList.getElementAtCallCount); + assertEquals(0, dbList.lastIndexGetElementAtCalledWith); + + certList.processCertRecords(0, 4, new ElementProcessorStub()); + + assertEquals(8, dbList.size); + assertEquals(8, dbList.getElementAtCallCount); + assertEquals(7, dbList.lastIndexGetElementAtCalledWith); + } + + + public class DBVirtualListStub extends DBVirtualListDefaultStub { + public int size = 0; + public int getElementAtCallCount = 0; + public int lastIndexGetElementAtCalledWith = 0; + + public Object getElementAt(int index) { + getElementAtCallCount++; + lastIndexGetElementAtCalledWith = index; + + // This simulates the size changing in the middle of + // processing + if (index == 3) { + size = 8; + } + return null; + } + + public int getSize() { + return size; + } + } + + public class ElementProcessorStub implements IElementProcessor { + public void process(Object o) throws EBaseException { + } + } +} diff --git a/pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryDefaultStub.java b/pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryDefaultStub.java new file mode 100644 index 000000000..b533475a1 --- /dev/null +++ b/pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryDefaultStub.java @@ -0,0 +1,74 @@ +package com.netscape.cmscore.dbs; + +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.base.IConfigStore; +import com.netscape.certsrv.base.ISubsystem; +import com.netscape.certsrv.dbs.*; +import netscape.ldap.LDAPAttributeSet; + +/** + * A default stub ojbect for tests to extend. + */ +public class DBRegistryDefaultStub implements IDBRegistry { + + + public void registerObjectClass(String className, String ldapNames[]) throws EDBException { + } + + public boolean isObjectClassRegistered(String className) { + return false; + } + + public void registerAttribute(String ufName, IDBAttrMapper mapper) throws EDBException { + } + + public boolean isAttributeRegistered(String ufName) { + return false; + } + + public void registerDynamicMapper(IDBDynAttrMapper mapper) { + } + + public String getFilter(String filter) throws EBaseException { + return null; + } + + public String getFilter(String filter, IFilterConverter c) throws EBaseException { + return null; + } + + public void mapObject(IDBObj parent, String name, Object obj, LDAPAttributeSet attrs) throws EBaseException { + } + + public String[] getLDAPAttributes(String attrs[]) throws EBaseException { + return new String[0]; + } + + public LDAPAttributeSet createLDAPAttributeSet(IDBObj obj) throws EBaseException { + return null; + } + + public IDBObj createObject(LDAPAttributeSet attrs) throws EBaseException { + return null; + } + + public String getId() { + return null; + } + + public void setId(String id) throws EBaseException { + } + + public void init(ISubsystem owner, IConfigStore config) throws EBaseException { + } + + public void startup() throws EBaseException { + } + + public void shutdown() { + } + + public IConfigStore getConfigStore() { + return null; + } +} diff --git a/pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryTest.java b/pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryTest.java new file mode 100644 index 000000000..6517c6bf1 --- /dev/null +++ b/pki/base/common/test/com/netscape/cmscore/dbs/DBRegistryTest.java @@ -0,0 +1,170 @@ +package com.netscape.cmscore.dbs; + +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.dbs.EDBException; +import com.netscape.certsrv.dbs.IDBObj; +import com.netscape.certsrv.dbs.IDBRegistry; +import com.netscape.certsrv.request.IRequestRecord; +import com.netscape.cmscore.request.DBDynAttrMapperDefaultStub; +import com.netscape.cmscore.request.RequestRecord; +import com.netscape.cmscore.test.CMSBaseTestCase; +import com.netscape.cmscore.test.TestHelper; +import junit.framework.Test; +import junit.framework.TestSuite; +import netscape.ldap.LDAPAttributeSet; +import netscape.ldap.LDAPAttribute; + +import java.util.Enumeration; +import java.util.Collections; +import java.util.Arrays; + +public class DBRegistryTest extends CMSBaseTestCase { + + DBSubsystemStub db; + DBRegistry registry; + DBDynAttrMapperStub extAttrMapper; + RequestRecordStub requestRecordStub = new RequestRecordStub(); + + public DBRegistryTest(String name) { + super(name); + } + + public void cmsTestSetUp() { + db = new DBSubsystemStub(); + registry = new DBRegistry(); + db.registry = registry; + + // Emulate the registration of mappers. + // Normally RequestRepository calls RequestRecord.register() as part + // of a long chain of initialization calls. + extAttrMapper = new DBDynAttrMapperStub(); + try { + registry.registerObjectClass(requestRecordStub.getClass().getName(), + new String[] {"ocvalue"} ); + registry.registerAttribute(IRequestRecord.ATTR_EXT_DATA, extAttrMapper); + registry.registerAttribute(IRequestRecord.ATTR_SOURCE_ID, + new StringMapper("sourceIdOut")); + registry.registerDynamicMapper(extAttrMapper); + } catch (EDBException e) { + e.printStackTrace(); + } + } + + public void cmsTestTearDown() { + } + + public static Test suite() { + return new TestSuite(DBRegistryTest.class); + } + + public void testMapObject() throws EBaseException { + assertFalse(extAttrMapper.mapObjectCalled); + registry.mapObject(null, IRequestRecord.ATTR_EXT_DATA, null, + new LDAPAttributeSet()); + assertTrue(extAttrMapper.mapObjectCalled); + } + + public void testGetLDAPAttributesForExtData() throws EBaseException { + String inAttrs[] = new String[] { + "extData-foo", + "extData-foo12", + "EXTDATA-bar;baz", + IRequestRecord.ATTR_SOURCE_ID + }; + String outAttrs[] = registry.getLDAPAttributes(inAttrs); + + assertTrue(TestHelper.contains(outAttrs, inAttrs[0])); + assertTrue(TestHelper.contains(outAttrs, inAttrs[1])); + assertTrue(TestHelper.contains(outAttrs, inAttrs[2])); + assertTrue(TestHelper.contains(outAttrs, "sourceIdOut")); + + try { + registry.getLDAPAttributes(new String[] {"badattr"}); + fail("Should not be able to map badattr"); + } catch (EBaseException e) { /* good */ } + } + + public void testCreateLDAPAttributeSet() throws EBaseException { + assertFalse(extAttrMapper.mapObjectCalled); + + registry.createLDAPAttributeSet(requestRecordStub); + assertTrue(requestRecordStub.getCalled); + assertEquals(requestRecordStub.getCalledWith, + IRequestRecord.ATTR_EXT_DATA); + + // This asserts that mapObject() is called and makes it down to the + // extDataDynAttrMapper.mapObjectToLDAPAttributeSet() call. + assertTrue(extAttrMapper.mapObjectCalled); + } + + public void testCreateObject() throws EBaseException { + LDAPAttributeSet attrs = new LDAPAttributeSet(); + attrs.add(new LDAPAttribute("objectclass", "ocvalue")); + attrs.add(new LDAPAttribute("extdata-foo")); + + assertFalse(extAttrMapper.mapLDAPAttrsCalled); + + registry.createObject(attrs); + + assertTrue(extAttrMapper.mapLDAPAttrsCalled); + } + + + class DBSubsystemStub extends DBSubsystemDefaultStub { + DBRegistry registry; + + public IDBRegistry getRegistry() { + return registry; + } + } + + + class DBDynAttrMapperStub extends DBDynAttrMapperDefaultStub { + boolean mapObjectCalled = false; + Object mapObjectCalledWithObject = null; + boolean mapLDAPAttrsCalled = false; + + public boolean supportsLDAPAttributeName(String attrName) { + return (attrName != null) && + attrName.toLowerCase().startsWith("extdata-"); + } + + public void mapLDAPAttributeSetToObject(LDAPAttributeSet attrs, String name, IDBObj parent) throws EBaseException { + mapLDAPAttrsCalled = true; + } + + public void mapObjectToLDAPAttributeSet(IDBObj parent, String name, + Object obj, + LDAPAttributeSet attrs) + throws EBaseException { + mapObjectCalled = true; + mapObjectCalledWithObject = obj; + } + } + +} + +/* + * This class is purposefully placed outside the test because + * DBRegistry.createObject() calls Class.newInstance() to create + * this stub. This fails if the class is nested. + */ + class RequestRecordStub extends RequestRecordDefaultStub { + + String[] attrs = new String[] { IRequestRecord.ATTR_EXT_DATA }; + + boolean getCalled = false; + String getCalledWith = null; + boolean getSerializedAttrNamesCalled = false; + + public Object get(String name) { + getCalled = true; + getCalledWith = name; + return "foo"; + } + + public Enumeration getSerializableAttrNames() { + getSerializedAttrNamesCalled = true; + return Collections.enumeration(Arrays.asList(attrs)); + } +} diff --git a/pki/base/common/test/com/netscape/cmscore/dbs/DBSSessionDefaultStub.java b/pki/base/common/test/com/netscape/cmscore/dbs/DBSSessionDefaultStub.java new file mode 100644 index 000000000..d5a6088d4 --- /dev/null +++ b/pki/base/common/test/com/netscape/cmscore/dbs/DBSSessionDefaultStub.java @@ -0,0 +1,70 @@ +package com.netscape.cmscore.dbs; + +import com.netscape.certsrv.dbs.*; +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.base.ISubsystem; +import com.netscape.certsrv.base.IConfigStore; +import netscape.ldap.LDAPAttributeSet; +import netscape.ldap.LDAPSearchResults; + +/** + * A default stub ojbect for tests to extend. + */ +public class DBSSessionDefaultStub implements IDBSSession { + + public ISubsystem getDBSubsystem() { + return null; + } + + public void close() throws EDBException { + } + + public void add(String name, IDBObj obj) throws EBaseException { + } + + public IDBObj read(String name) throws EBaseException { + return null; + } + + public IDBObj read(String name, String attrs[]) throws EBaseException { + return null; + } + + public void delete(String name) throws EBaseException { + } + + public void modify(String name, ModificationSet mods) throws EBaseException { + } + + public IDBSearchResults search(String base, String filter) throws EBaseException { + return null; + } + + public IDBSearchResults search(String base, String filter, int maxSize) throws EBaseException { + return null; + } + + public IDBSearchResults search(String base, String filter, int maxSize, int timeLimit) throws EBaseException { + return null; + } + + public IDBSearchResults search(String base, String filter, String attrs[]) throws EBaseException { + return null; + } + + public IDBVirtualList createVirtualList(String base, String filter, String attrs[]) throws EBaseException { + return null; + } + + public LDAPSearchResults persistentSearch(String base, String filter, String attrs[]) throws EBaseException { + return null; + } + + public IDBVirtualList createVirtualList(String base, String filter, String attrs[], String sortKey, int pageSize) throws EBaseException { + return null; + } + + public IDBVirtualList createVirtualList(String base, String filter, String attrs[], String startFrom, String sortKey, int pageSize) throws EBaseException { + return null; + } +} diff --git a/pki/base/common/test/com/netscape/cmscore/dbs/DBSubsystemDefaultStub.java b/pki/base/common/test/com/netscape/cmscore/dbs/DBSubsystemDefaultStub.java new file mode 100644 index 000000000..84e0c1b3e --- /dev/null +++ b/pki/base/common/test/com/netscape/cmscore/dbs/DBSubsystemDefaultStub.java @@ -0,0 +1,84 @@ +package com.netscape.cmscore.dbs; + +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.base.IConfigStore; +import com.netscape.certsrv.base.ISubsystem; +import com.netscape.certsrv.dbs.EDBException; +import com.netscape.certsrv.dbs.IDBRegistry; +import com.netscape.certsrv.dbs.IDBSSession; +import com.netscape.certsrv.dbs.IDBSubsystem; +import netscape.ldap.LDAPConnection; + +import java.math.BigInteger; + +/** + * A default stub ojbect for tests to extend. + */ +public class DBSubsystemDefaultStub implements IDBSubsystem { + + + public String getBaseDN() { + return null; + } + + public IDBRegistry getRegistry() { + return null; + } + + public IDBSSession createSession() throws EDBException { + return null; + } + + public boolean enableSerialNumberRecovery() { + return false; + } + + public void setNextSerialConfig(BigInteger serial) throws EBaseException { + } + + public BigInteger getNextSerialConfig() { + return null; + } + + public void setMaxSerialConfig(String serial) throws EBaseException { + } + + public String getMinSerialConfig() { + return null; + } + + public String getMaxSerialConfig() { + return null; + } + + public String getMinRequestConfig() { + return null; + } + + public String getMaxRequestConfig() { + return null; + } + + public void returnConn(LDAPConnection conn) { + } + + public String getId() { + return null; + } + + public void setId(String id) throws EBaseException { + } + + public void init(ISubsystem owner, IConfigStore config) throws EBaseException { + } + + public void startup() throws EBaseException { + } + + public void shutdown() { + } + + public IConfigStore getConfigStore() { + return null; + } +} diff --git a/pki/base/common/test/com/netscape/cmscore/dbs/DBVirtualListDefaultStub.java b/pki/base/common/test/com/netscape/cmscore/dbs/DBVirtualListDefaultStub.java new file mode 100644 index 000000000..45fda77db --- /dev/null +++ b/pki/base/common/test/com/netscape/cmscore/dbs/DBVirtualListDefaultStub.java @@ -0,0 +1,87 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package com.netscape.cmscore.dbs; + +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.dbs.IDBVirtualList; +import com.netscape.certsrv.dbs.IElementProcessor; + +/** + * A default stub ojbect for tests to extend. + * This class helps test avoid the problem of test stubs having to + * implement a new stub method every time the interface changes. + * It also makes the tests clearer by not cluttered them with empty methods. + * + * Do not put any behaviour in this class. + */ +public class DBVirtualListDefaultStub implements IDBVirtualList { + + public void setPageSize(int size) { + } + + public void setSortKey(String sortKey) throws EBaseException { + } + + public void setSortKey(String[] sortKeys) throws EBaseException { + } + + public int getSize() { + return 0; + } + + public int getSizeBeforeJumpTo() { + return 0; + } + + public int getSizeAfterJumpTo() { + return 0; + } + + public int getCurrentIndex() { + return 0; + } + + public boolean getPage(int first) { + return false; + } + + public boolean getPage(String text) { + return false; + } + + public Object getElementAt(int index) { + return null; + } + + public Object getJumpToElementAt(int i) { + return null; + } + + public void processElements(int startidx, int endidx, IElementProcessor ep) + throws EBaseException { + } + + public int getSelectedIndex() { + return 0; + } + + public int getFirstIndex() { + return 0; + } + +} diff --git a/pki/base/common/test/com/netscape/cmscore/dbs/RequestRecordDefaultStub.java b/pki/base/common/test/com/netscape/cmscore/dbs/RequestRecordDefaultStub.java new file mode 100644 index 000000000..ad0572aab --- /dev/null +++ b/pki/base/common/test/com/netscape/cmscore/dbs/RequestRecordDefaultStub.java @@ -0,0 +1,39 @@ +package com.netscape.cmscore.dbs; + +import com.netscape.certsrv.request.IRequestRecord; +import com.netscape.certsrv.request.RequestId; +import com.netscape.certsrv.base.EBaseException; +import com.netscape.certsrv.dbs.IDBObj; + +import java.util.Enumeration; + +/** + * Default stub for RequestRecord tests. + */ +public class RequestRecordDefaultStub implements IRequestRecord, IDBObj { + public RequestId getRequestId() { + return null; + } + + public Enumeration getAttrNames() { + return null; + } + + public Object get(String name) { + return null; + } + + public void set(String name, Object o) { + } + + public void delete(String name) throws EBaseException { + } + + public Enumeration getElements() { + return null; + } + + public Enumeration getSerializableAttrNames() { + return null; + } +} |