* 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;
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;
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<Serializable, Serializable> _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<Serializable, Serializable> aUserCache) {
+ _userCache = aUserCache;
+ }
+
+ public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
+ _databaseTester = aDatabaseTester;
+ }
+
@Override
protected void tearDown() throws Exception {
_container.stop(_scope);
}
/**
- * 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<Group> 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());
}
-
+
}