From a463ceba6a90580875dc7cff4674f461a3db773f Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 13 May 2008 20:53:43 +0000 Subject: [PATCH] HibernateUserSet now tested using DatabaseTesterComponent. Dependence of InMemoryUserSetTest on SpringTestCase removed. --- .../wamblee/usermgt/InMemoryUserSetTest.java | 8 +- .../hibernate/HibernateUserSetTest.java | 223 ++++++++++-------- 2 files changed, 136 insertions(+), 95 deletions(-) diff --git a/security/src/test/java/org/wamblee/usermgt/InMemoryUserSetTest.java b/security/src/test/java/org/wamblee/usermgt/InMemoryUserSetTest.java index 4154d6e4..23cfb24e 100644 --- a/security/src/test/java/org/wamblee/usermgt/InMemoryUserSetTest.java +++ b/security/src/test/java/org/wamblee/usermgt/InMemoryUserSetTest.java @@ -19,6 +19,8 @@ package org.wamblee.usermgt; import java.sql.SQLException; import java.util.Set; +import junit.framework.TestCase; + import org.wamblee.persistence.hibernate.HibernateMappingFiles; import org.wamblee.security.encryption.Md5HexMessageDigester; import org.wamblee.test.spring.SpringTestCase; @@ -29,7 +31,7 @@ import org.wamblee.usermgt.UserMgtException.Reason; * Tests the inmemory user set. Intended to be subclassed for other * implementations of user set. */ -public class InMemoryUserSetTest extends SpringTestCase { +public class InMemoryUserSetTest extends TestCase { protected static final String PASSWORD = "abc123"; @@ -39,12 +41,12 @@ public class InMemoryUserSetTest extends SpringTestCase { private Group _group; public InMemoryUserSetTest() { - super(SpringConfigFiles.class, HibernateMappingFiles.class); + super(); } protected InMemoryUserSetTest(Class aSprings, Class aMappings) { - super(aSprings, aMappings); + super(); } /** diff --git a/security/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserSetTest.java b/security/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserSetTest.java index f7fc0c0f..9ba3a58e 100644 --- a/security/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserSetTest.java +++ b/security/src/test/java/org/wamblee/usermgt/hibernate/HibernateUserSetTest.java @@ -12,10 +12,11 @@ * 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 org.wamblee.usermgt.hibernate; +import java.io.Serializable; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Set; @@ -23,11 +24,12 @@ import java.util.Set; import javax.sql.DataSource; import org.wamblee.cache.Cache; +import org.wamblee.cache.EhCache; import org.wamblee.general.BeanKernel; import org.wamblee.system.adapters.DefaultContainer; import org.wamblee.system.adapters.ObjectConfiguration; -import org.wamblee.system.core.Component; import org.wamblee.system.core.Scope; +import org.wamblee.system.spring.DatabaseTesterComponent; import org.wamblee.usermgt.Group; import org.wamblee.usermgt.GroupSet; import org.wamblee.usermgt.InMemoryUserSetTest; @@ -38,63 +40,86 @@ import org.wamblee.usermgt.UsermgtHibernateMappingFiles; import org.wamblee.usermgt.UsermgtSpringConfigFiles; /** - * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet} - * + * Tests for {@link org.wamblee.usermgt.hibernate.HibernateGroupSet} + * * @author Erik Brakkee */ public class HibernateUserSetTest extends InMemoryUserSetTest { - + private static final String USER_TABLE = "USERS"; - private static final String GROUP_TABLE = "GROUPS"; - - private static final String USER_QUERY = "select * from " + USER_TABLE + " where name = ?"; - private static final String GROUP_QUERY = "select * from " + GROUP_TABLE + " where name = ?"; + private static final String GROUP_TABLE = "GROUPS"; - public HibernateUserSetTest() { - super(UsermgtSpringConfigFiles.class, UsermgtHibernateMappingFiles.class); + private static final String USER_QUERY = "select * from " + USER_TABLE + + " where name = ?"; + private static final String GROUP_QUERY = "select * from " + GROUP_TABLE + + " where name = ?"; + + public HibernateUserSetTest() { + super(UsermgtSpringConfigFiles.class, + UsermgtHibernateMappingFiles.class); } - + private DefaultContainer _container; private Scope _scope; - + private DataSource _datasource; - private UserSet _userset; - private GroupSet _groupset; - - /* (non-Javadoc) + private UserSet _userset; + private GroupSet _groupset; + private EhCache _userCache; + private DatabaseTesterComponent _databaseTester; + + /* + * (non-Javadoc) + * * @see org.wamblee.usermgt.InMemoryUserSetTest#setUp() */ @Override protected void setUp() throws Exception { - super.setUp(); - + _container = new DefaultContainer("top"); _container.addComponent(new ExternalDatasourceComponent("datasource")); - _container.addComponent("mappingFiles", new UsermgtHibernateMappingFiles()); + _container.addComponent("mappingFiles", + new UsermgtHibernateMappingFiles()); _container.addComponent(new HibernateComponent("hibernate")); - _container.addComponent(new UserGroupRepositoryComponent("usersgroups")); - - ObjectConfiguration config = new ObjectConfiguration(HibernateUserSetTest.class); - config.getSetterConfig().clear().add("datasource").add("userset").add("groupset"); + _container + .addComponent(new UserGroupRepositoryComponent("usersgroups")); + _container + .addComponent("databaseTester", DatabaseTesterComponent.class); + + ObjectConfiguration config = new ObjectConfiguration( + HibernateUserSetTest.class); + config.getSetterConfig().clear().add("datasource").add("userset").add( + "groupset").add("databaseTester").add("userCache"); _container.addComponent("testcase", this, config); - + _scope = _container.start(); - + clearUserCache(); + _databaseTester.cleanDatabase(); + + super.setUp(); } - - public void setDatasource(DataSource aDatasource) { - _datasource = aDatasource; + + public void setDatasource(DataSource aDatasource) { + _datasource = aDatasource; } - + public void setUserset(UserSet aUserset) { _userset = aUserset; } - + public void setGroupset(GroupSet aGroupset) { _groupset = aGroupset; } - + + public void setUserCache(EhCache aUserCache) { + _userCache = aUserCache; + } + + public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) { + _databaseTester = aDatabaseTester; + } + @Override protected void tearDown() throws Exception { _container.stop(_scope); @@ -102,127 +127,141 @@ public class HibernateUserSetTest extends InMemoryUserSetTest { } /** - * Clears the user cache. + * Clears the user cache. */ private void clearUserCache() { - BeanKernel.getBeanFactory().find("userCache", Cache.class).clear(); + _userCache.clear(); } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int) */ @Override protected void checkUserCount(int aSize) throws SQLException { - super.flush(); + _databaseTester.flush(); super.checkUserCount(aSize); - assertEquals(aSize, getTableSize(USER_TABLE)); + assertEquals(aSize, _databaseTester.getTableSize(USER_TABLE)); } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String) */ @Override protected void checkUserExists(String aUser) throws SQLException { - flush(); - ResultSet result = executeQuery(USER_QUERY, aUser); - assertEquals(1, countResultSet(result)); + _databaseTester.flush(); + ResultSet result = _databaseTester.executeQuery(USER_QUERY, aUser); + assertEquals(1, _databaseTester.countResultSet(result)); } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String) */ @Override protected void checkUserNotExists(String aUser) throws SQLException { - flush(); - ResultSet result = executeQuery(USER_QUERY, aUser); - assertEquals(0, countResultSet(result)); + _databaseTester.flush(); + ResultSet result = _databaseTester.executeQuery(USER_QUERY, aUser); + assertEquals(0, _databaseTester.countResultSet(result)); } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int) */ @Override protected void checkGroupCount(int aSize) throws SQLException { - super.flush(); - assertEquals(aSize, getTableSize(GROUP_TABLE)); + _databaseTester.flush(); + assertEquals(aSize, _databaseTester.getTableSize(GROUP_TABLE)); } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupExists(java.lang.String) */ @Override protected void checkGroupExists(String aGroup) throws SQLException { - flush(); - - ResultSet result = executeQuery(GROUP_QUERY, aGroup); - assertEquals(1, countResultSet(result)); + _databaseTester.flush(); + + ResultSet result = _databaseTester.executeQuery(GROUP_QUERY, aGroup); + assertEquals(1, _databaseTester.countResultSet(result)); } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupNotExists(java.lang.String) */ @Override protected void checkGroupNotExists(String aGroup) throws SQLException { - flush(); - ResultSet result = executeQuery(GROUP_QUERY, aGroup); - assertEquals(0, countResultSet(result)); + _databaseTester.flush(); + ResultSet result = _databaseTester.executeQuery(GROUP_QUERY, aGroup); + assertEquals(0, _databaseTester.countResultSet(result)); } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.wamblee.usermgt.InMemoryGroupSetTest#createGroupSet() */ @Override protected UserSet createUserSet() { - return BeanKernel.getBeanFactory().find(UserSet.class); + return _userset; } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.wamblee.usermgt.InMemoryUserSetTest#createGroupSet() */ @Override protected GroupSet createGroupSet() { - return BeanKernel.getBeanFactory().find(GroupSet.class); + return _groupset; } /** - * Reproduction of a bug. - * Create a user which is in group1 - * Add it to a second group group2. - * Remove the user from group1. - * Verify the user is in group2. + * Reproduction of a bug. Create a user which is in group1 Add it to a + * second group group2. Remove the user from group1. Verify the user is in + * group2. */ public void testVerifyAddRemove() throws SQLException, UserMgtException { - cleanDatabase(); // just to be sure. - GroupSet groups = getGroups(); - assertEquals(0, groups.size()); + _databaseTester.cleanDatabase(); // just to be sure. + GroupSet groups = getGroups(); + assertEquals(0, groups.size()); Group group1 = createGroup("group1"); Group group2 = createGroup("group2"); - groups.add(group1); - groups.add(group2); - checkGroupExists("group1"); + groups.add(group1); + groups.add(group2); + checkGroupExists("group1"); checkGroupExists("group2"); - + User user = createUser("user", PASSWORD, group1); - getUsers().add(user); + getUsers().add(user); checkUserExists("user"); - + addUserToGroup(user, group2); getUsers().userModified(user); - clearUserCache(); + clearUserCache(); User user2 = getUsers().find("user"); Set userGroups = user2.getGroups(); - assertTrue(user2.isInGroup("group1")); + assertTrue(user2.isInGroup("group1")); assertTrue(user2.isInGroup("group2")); assertEquals(2, userGroups.size()); - - removeUserFromGroup(user, group1); - getUsers().userModified(user); - clearUserCache(); - user2 = getUsers().find("user"); + + removeUserFromGroup(user, group1); + getUsers().userModified(user); + clearUserCache(); + user2 = getUsers().find("user"); userGroups = user2.getGroups(); - assertFalse(user2.isInGroup("group1")); + assertFalse(user2.isInGroup("group1")); assertTrue(user2.isInGroup("group2")); assertEquals(1, userGroups.size()); } - + } -- 2.31.1