|  | @@ -0,0 +1,82 @@
 | 
	
		
			
				|  |  | +/* Copyright 2004 Acegi Technology Pty Limited
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * Licensed under the Apache License, Version 2.0 (the "License");
 | 
	
		
			
				|  |  | + * you may not use this file except in compliance with the License.
 | 
	
		
			
				|  |  | + * You may obtain a copy of the License at
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + *     http://www.apache.org/licenses/LICENSE-2.0
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * Unless required by applicable law or agreed to in writing, software
 | 
	
		
			
				|  |  | + * distributed under the License is distributed on an "AS IS" BASIS,
 | 
	
		
			
				|  |  | + * 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 net.sf.acegisecurity.providers.x509.cache;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import junit.framework.TestCase;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import net.sf.acegisecurity.providers.dao.User;
 | 
	
		
			
				|  |  | +import net.sf.acegisecurity.providers.x509.X509TestUtils;
 | 
	
		
			
				|  |  | +import net.sf.acegisecurity.MockApplicationContext;
 | 
	
		
			
				|  |  | +import net.sf.acegisecurity.UserDetails;
 | 
	
		
			
				|  |  | +import net.sf.acegisecurity.GrantedAuthority;
 | 
	
		
			
				|  |  | +import net.sf.acegisecurity.GrantedAuthorityImpl;
 | 
	
		
			
				|  |  | +import net.sf.ehcache.Cache;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import org.springframework.context.ApplicationContext;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.security.cert.X509Certificate;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * @author Luke Taylor
 | 
	
		
			
				|  |  | + * @version $Id$
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +public class EhCacheBasedX509UserCacheTests extends TestCase {
 | 
	
		
			
				|  |  | +    //~ Constructors ===========================================================
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public EhCacheBasedX509UserCacheTests() {
 | 
	
		
			
				|  |  | +        super();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public EhCacheBasedX509UserCacheTests(String arg0) {
 | 
	
		
			
				|  |  | +        super(arg0);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //~ Methods ================================================================
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public final void setUp() throws Exception {
 | 
	
		
			
				|  |  | +        super.setUp();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public void testCacheOperation() throws Exception {
 | 
	
		
			
				|  |  | +        EhCacheBasedX509UserCache cache = new EhCacheBasedX509UserCache();
 | 
	
		
			
				|  |  | +        cache.setCache(getCache());
 | 
	
		
			
				|  |  | +        cache.afterPropertiesSet();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Check it gets stored in the cache
 | 
	
		
			
				|  |  | +        cache.putUserInCache(X509TestUtils.buildTestCertificate(), getUser());
 | 
	
		
			
				|  |  | +        assertEquals(getUser().getPassword(),
 | 
	
		
			
				|  |  | +            cache.getUserFromCache(X509TestUtils.buildTestCertificate()).getPassword());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Check it gets removed from the cache
 | 
	
		
			
				|  |  | +        cache.removeUserFromCache(X509TestUtils.buildTestCertificate());
 | 
	
		
			
				|  |  | +        assertNull(cache.getUserFromCache(X509TestUtils.buildTestCertificate()));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Check it doesn't return values for null user
 | 
	
		
			
				|  |  | +        assertNull(cache.getUserFromCache(null));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private Cache getCache() {
 | 
	
		
			
				|  |  | +        ApplicationContext ctx = MockApplicationContext.getContext();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return (Cache) ctx.getBean("eHCacheBackend");
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private UserDetails getUser() {
 | 
	
		
			
				|  |  | +        return new User("marissa", "password", true, true, true, true,
 | 
	
		
			
				|  |  | +            new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
 | 
	
		
			
				|  |  | +                    "ROLE_TWO")});
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |