fix unit test

This commit is contained in:
Olivier Lamy 2014-04-11 15:57:52 +10:00
parent 7039fb8eb6
commit 4f547460cf
4 changed files with 52 additions and 39 deletions

View File

@ -19,7 +19,6 @@ package org.apache.archiva.redback.rbac.jdo;
* under the License.
*/
import net.sf.ehcache.CacheManager;
import org.apache.archiva.redback.common.jdo.test.StoreManagerDebug;
import org.apache.archiva.redback.components.jdo.DefaultConfigurableJdoFactory;
import org.apache.archiva.redback.rbac.RBACManager;
@ -174,7 +173,6 @@ public class JdoRbacManagerTest
public void testGetAssignedPermissionsDeep()
throws RbacManagerException
{
this.clearCache();
super.testGetAssignedPermissionsDeep();
int counter = storeManager.counter();
/* without Level 2 cache: 26 queries */
@ -217,11 +215,5 @@ public class JdoRbacManagerTest
assertEquals( EVENTCOUNT, eventTracker.initCount );
}
protected void clearCache()
{
for ( String cacheName : CacheManager.getInstance().getCacheNames() )
{
CacheManager.getInstance().getCache( cacheName ).removeAll();
}
}
}

View File

@ -19,14 +19,14 @@ package org.apache.archiva.redback.rbac.ldap;
* under the License.
*/
import net.sf.ehcache.CacheManager;
import org.apache.archiva.redback.components.apacheds.ApacheDs;
import org.apache.archiva.redback.policy.PasswordEncoder;
import org.apache.archiva.redback.policy.encoders.SHA1PasswordEncoder;
import org.apache.archiva.redback.rbac.RBACManager;
import org.apache.archiva.redback.rbac.ldap.LdapRbacManager;
import org.apache.archiva.redback.rbac.Role;
import org.apache.archiva.redback.tests.AbstractRbacManagerTestCase;
import org.junit.After;
import org.junit.Before;
import org.springframework.test.annotation.DirtiesContext;
import javax.inject.Inject;
import javax.inject.Named;
@ -40,14 +40,8 @@ import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import org.junit.After;
import org.junit.BeforeClass;
import org.springframework.test.annotation.DirtiesContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -82,13 +76,15 @@ public class LdapRbacManagerTest
throws Exception
{
super.setUp();
CacheManager.getInstance().clearAll();
this.clearCache();
setRbacManager( rbacManager );
assertTrue( getRbacManager() instanceof LdapRbacManager );
rbacManager.setWritableLdap( true );
rbacManager.getRbacImpl().eraseDatabase();
passwordEncoder = new SHA1PasswordEncoder();
usersPerGroup = new HashMap<String, List<String>>( 3 );
@ -125,9 +121,6 @@ public class LdapRbacManagerTest
apacheDs.getAdminContext().createSubcontext( suffix, attributes );
//makeUsers();
//createGroups();
}
@After
@ -270,8 +263,12 @@ public class LdapRbacManagerTest
public void testStoreInitialization()
throws Exception
{
CacheManager.getInstance().clearAll();
//rbacManager.eraseDatabase();
this.clearCache();
for ( Role role : rbacManager.getAllRoles() )
{
rbacManager.removeRole( role );
}
eventTracker.clear();
super.testStoreInitialization();
}

View File

@ -17,16 +17,17 @@ package org.apache.archiva.redback.tests;
*/
import junit.framework.TestCase;
import net.sf.ehcache.CacheManager;
import org.apache.archiva.redback.rbac.Operation;
import org.apache.archiva.redback.rbac.Permission;
import org.apache.archiva.redback.rbac.RBACManager;
import org.apache.archiva.redback.rbac.RbacManagerException;
import org.apache.archiva.redback.rbac.RbacPermanentException;
import org.apache.archiva.redback.rbac.Resource;
import org.apache.archiva.redback.rbac.Role;
import org.apache.archiva.redback.rbac.UserAssignment;
import org.apache.archiva.redback.rbac.Permission;
import org.apache.archiva.redback.rbac.RbacPermanentException;
import org.apache.archiva.redback.tests.utils.RBACDefaults;
import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
@ -39,15 +40,15 @@ import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.springframework.test.annotation.DirtiesContext;
import static org.assertj.core.api.Assertions.assertThat;
/**
* AbstractRbacManagerTestCase
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
*/
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
public abstract class AbstractRbacManagerTestCase
extends TestCase
{
@ -158,6 +159,7 @@ public abstract class AbstractRbacManagerTestCase
public void testStoreInitialization()
throws Exception
{
assertNotNull( rbacManager );
Role role = getAdminRole();
@ -680,8 +682,7 @@ public abstract class AbstractRbacManagerTestCase
assignment = manager.saveUserAssignment( assignment );
assertThat( assignment.getRoleNames() ).isNotNull().isNotEmpty().hasSize( 3 );
assertThat( manager.getAllUserAssignments() ).isNotNull().isNotEmpty().hasSize(
incAssignements( 1 ) );
assertThat( manager.getAllUserAssignments() ).isNotNull().isNotEmpty().hasSize( incAssignements( 1 ) );
assertThat( manager.getAllRoles() ).isNotNull().isNotEmpty().hasSize( 3 );
@ -727,8 +728,7 @@ public abstract class AbstractRbacManagerTestCase
assignment.addRoleName( roleName );
manager.saveUserAssignment( assignment );
assertThat( manager.getAllUserAssignments() ).isNotNull().isNotEmpty().hasSize(
incAssignements( 1 ) );
assertThat( manager.getAllUserAssignments() ).isNotNull().isNotEmpty().hasSize( incAssignements( 1 ) );
assertThat( manager.getAllRoles() ).isNotNull().isNotEmpty().hasSize( 3 );
assertThat( manager.getAllPermissions() ).isNotNull().isNotEmpty().hasSize( 3 );
@ -839,7 +839,9 @@ public abstract class AbstractRbacManagerTestCase
public void testGetAssignedPermissionsDeep()
throws RbacManagerException
{
this.clearCache();
assertNotNull( rbacManager );
rbacManager.eraseDatabase();
rbacDefaults.createDefaults();
// Setup User / Assignment with 1 role.
@ -850,8 +852,11 @@ public abstract class AbstractRbacManagerTestCase
rbacManager.saveUserAssignment( assignment );
assertEquals( incAssignements( 1 ), rbacManager.getAllUserAssignments().size() );
assertEquals( 6, rbacManager.getAllPermissions().size() );
assertEquals( 4, rbacManager.getAllRoles().size() );
List<Permission> permissions = rbacManager.getAllPermissions();
Assertions.assertThat( permissions ).isNotNull().isNotEmpty().hasSize( 6 );
List<Role> roles = rbacManager.getAllRoles();
Assertions.assertThat( roles ).isNotNull().isNotEmpty().hasSize( 4 );
afterSetup();
@ -986,7 +991,8 @@ public abstract class AbstractRbacManagerTestCase
}
@Test
public void testInitialize() throws Exception
public void testInitialize()
throws Exception
{
rbacManager.initialize();
}
@ -998,4 +1004,12 @@ public abstract class AbstractRbacManagerTestCase
{
// do nothing
}
protected void clearCache()
{
for ( String cacheName : CacheManager.getInstance().getCacheNames() )
{
CacheManager.getInstance().getCache( cacheName ).removeAll();
}
}
}

View File

@ -19,8 +19,8 @@ package org.apache.archiva.redback.tests;
* under the License.
*/
import org.apache.archiva.redback.rbac.RBACManagerListener;
import org.apache.archiva.redback.rbac.Permission;
import org.apache.archiva.redback.rbac.RBACManagerListener;
import org.apache.archiva.redback.rbac.Role;
import org.apache.archiva.redback.rbac.UserAssignment;
import org.slf4j.Logger;
@ -33,7 +33,6 @@ import java.util.List;
* RbacManagerEventTracker
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
*
*/
public class RbacManagerEventTracker
implements RBACManagerListener
@ -59,6 +58,17 @@ public class RbacManagerEventTracker
lastDbFreshness = Boolean.valueOf( freshdb );
}
public void clear()
{
this.addedRoleNames = new ArrayList<String>();
this.removedRoleNames = new ArrayList<String>();
this.addedPermissionNames = new ArrayList<String>();
this.removedPermissionNames = new ArrayList<String>();
}
public void rbacPermissionRemoved( Permission permission )
{
log( "Permission Removed: " + permission.getName() );