    1   /* Copyright 2004 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.acl.basic.cache;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.MockApplicationContext;
    21  import org.acegisecurity.acl.basic.AclObjectIdentity;
    22  import org.acegisecurity.acl.basic.BasicAclEntry;
    23  import org.acegisecurity.acl.basic.NamedEntityObjectIdentity;
    24  import org.acegisecurity.acl.basic.SimpleAclEntry;
    25
    26  import net.sf.ehcache.Cache;
    27
    28  import org.springframework.context.ApplicationContext;
    29
    30
    31  /***
    32   * Tests {@link EhCacheBasedAclEntryCache}.
    33   *
    34   * @author Ben Alex
    35   * @version $Id: EhCacheBasedAclEntryCacheTests.java,v 1.3 2005/11/17 00:55:47 benalex Exp $
    36   */
    37  public class EhCacheBasedAclEntryCacheTests extends TestCase {
    38      //~ Static fields/initializers =============================================
    39
    40      private static final AclObjectIdentity OBJECT_100 = new NamedEntityObjectIdentity("OBJECT",
    41              "100");
    42      private static final AclObjectIdentity OBJECT_200 = new NamedEntityObjectIdentity("OBJECT",
    43              "200");
    44      private static final BasicAclEntry OBJECT_100_MARISSA = new SimpleAclEntry("marissa",
    45              OBJECT_100, null, 2);
    46      private static final BasicAclEntry OBJECT_100_SCOTT = new SimpleAclEntry("scott",
    47              OBJECT_100, null, 4);
    48      private static final BasicAclEntry OBJECT_200_PETER = new SimpleAclEntry("peter",
    49              OBJECT_200, null, 4);
    50
    51      //~ Constructors ===========================================================
    52
    53      public EhCacheBasedAclEntryCacheTests() {
    54          super();
    55      }
    56
    57      public EhCacheBasedAclEntryCacheTests(String arg0) {
    58          super(arg0);
    59      }
    60
    61      //~ Methods ================================================================
    62
    63      public final void setUp() throws Exception {
    64          super.setUp();
    65      }
    66
    67      public static void main(String[] args) {
    68          junit.textui.TestRunner.run(EhCacheBasedAclEntryCacheTests.class);
    69      }
    70
    71      public void testCacheOperation() throws Exception {
    72          EhCacheBasedAclEntryCache cache = new EhCacheBasedAclEntryCache();
    73          cache.setCache(getCache());
    74          cache.afterPropertiesSet();
    75
    76          cache.putEntriesInCache(new BasicAclEntry[] {OBJECT_100_SCOTT, OBJECT_100_MARISSA});
    77          cache.putEntriesInCache(new BasicAclEntry[] {OBJECT_200_PETER});
    78
    79          // Check we can get them from cache again
    80          assertEquals(OBJECT_100_SCOTT,
    81              cache.getEntriesFromCache(
    82                  new NamedEntityObjectIdentity("OBJECT", "100"))[0]);
    83          assertEquals(OBJECT_100_MARISSA,
    84              cache.getEntriesFromCache(
    85                  new NamedEntityObjectIdentity("OBJECT", "100"))[1]);
    86          assertEquals(OBJECT_200_PETER,
    87              cache.getEntriesFromCache(
    88                  new NamedEntityObjectIdentity("OBJECT", "200"))[0]);
    89          assertNull(cache.getEntriesFromCache(
    90                  new NamedEntityObjectIdentity("OBJECT", "NOT_IN_CACHE")));
    91      }
    92
    93      public void testStartupDetectsMissingCache() throws Exception {
    94          EhCacheBasedAclEntryCache cache = new EhCacheBasedAclEntryCache();
    95
    96          try {
    97              cache.afterPropertiesSet();
    98              fail("Should have thrown IllegalArgumentException");
    99          } catch (IllegalArgumentException expected) {
    100             assertTrue(true);
    101         }
    102
    103         Cache myCache = getCache();
    104         cache.setCache(myCache);
    105         assertEquals(myCache, cache.getCache());
    106     }
    107
    108     private Cache getCache() {
    109         ApplicationContext ctx = MockApplicationContext.getContext();
    110
    111         return (Cache) ctx.getBean("eHCacheBackend");
    112     }
    113 }