1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.services.cache;
16  
17  import org.figure8.join.core.setup.BootstrapException;
18  import org.figure8.join.businessfacades.artifact.ArtifactManager;
19  import org.figure8.join.businessfacades.commons.IntegrationProcessManager;
20  import org.figure8.join.businessfacades.security.PermissionManager;
21  
22  import org.easymock.MockControl;
23  
24  import junit.framework.Test;
25  import junit.framework.TestCase;
26  import junit.framework.TestSuite;
27  
28  import java.util.ArrayList;
29  /**
30   * JUnit test case for testing interaction from CacheLoader pow.
31   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
32   * @version $Revision: 1.2 $$
33   */
34  public class CacheLoaderMockTest extends TestCase{
35  
36     // Attributes ---------------------------------------------------------------
37  
38     /** Control for mock cache manager */
39     private MockControl cacheControl = null;
40     /** CacheManager mock implementation */
41     private CacheManager cacheManager = null;
42  
43     /** Control for mock artifact manager */
44     private MockControl artifactControl = null;
45     /** ArtifactManager mock implementation */
46     private ArtifactManager artifactManager = null;
47  
48     /** Control for mock process manager */
49     private MockControl processControl = null;
50     /** IntegrationProcessManager mock implementation */
51     private IntegrationProcessManager processManager = null;
52  
53     /** Control for mock permission manager */
54     private MockControl permissionControl = null;
55     /** PermissionManager mock implementation */
56     private PermissionManager permissionManager = null;
57  
58  
59     // Constructors -------------------------------------------------------------
60  
61     /** Default constructor. Build a test case using a name. */
62     public CacheLoaderMockTest(String testName){
63        super(testName);
64     }
65  
66  
67     // Override of TestCase -----------------------------------------------------
68  
69     /**
70      * Make this class a TestSuite.
71      * @return A TestSuite containing this TestCase.
72      */
73     public static Test suite(){
74        TestSuite suite = new TestSuite(CacheLoaderMockTest.class);
75        return suite;
76     }
77     
78     
79     // Public -------------------------------------------------------------------
80  
81     /** Creates mock objects and their associated EasyMock controls */
82     public void setUp(){
83        // Mock a cache manager.
84        cacheControl = MockControl.createControl(CacheManager.class);
85        cacheManager = (CacheManager)cacheControl.getMock();
86        // Mock an artifact manager.
87        artifactControl = MockControl.createControl(ArtifactManager.class);
88        artifactManager = (ArtifactManager)artifactControl.getMock();
89        // Mock a process manager.
90        processControl = MockControl.createControl(IntegrationProcessManager.class);
91        processManager = (IntegrationProcessManager)processControl.getMock();
92        // Mock a permission manager.
93        permissionControl = MockControl.createControl(PermissionManager.class);
94        permissionManager = (PermissionManager)permissionControl.getMock();
95     }
96  
97     /** Test the loadCaches() method */
98     public void testLoadCaches(){
99        CacheLoader loader = new CacheLoader();
100       // Set mock objects to loader.
101       loader.setCacheManager(cacheManager);
102       loader.setArtifactManager(artifactManager);
103       loader.setPermissionManager(permissionManager);
104       loader.setIntegrationProcessManager(processManager);
105 
106       // Set expectations on mocks : eternal caches should be accessed.
107       cacheControl.expectAndReturn(
108             cacheManager.getCache(EternalCacheKeys.STEP_ID_TO_STEP_KEY),
109             new EhCache(EternalCacheKeys.STEP_ID_TO_STEP_KEY, null, null),
110             1);
111       cacheControl.expectAndReturn(
112             cacheManager.getCache(EternalCacheKeys.ROLE_NAME_TO_ROLE_KEY),
113             new EhCache(EternalCacheKeys.ROLE_NAME_TO_ROLE_KEY, null, null),
114             1);
115       cacheControl.expectAndReturn(
116             cacheManager.getCache(EternalCacheKeys.DELTYPE_KEY_TO_DELTYPE_KEY),
117             new EhCache(EternalCacheKeys.DELTYPE_KEY_TO_DELTYPE_KEY, null, null),
118             1);
119       cacheControl.expectAndReturn(
120             cacheManager.getCache(EternalCacheKeys.RELEASE_NAME_TO_RELEASE_KEY),
121             new EhCache(EternalCacheKeys.RELEASE_NAME_TO_RELEASE_KEY, null, null),
122             1);
123       cacheControl.replay();
124 
125       // Deliverable types should be retrieved.
126       artifactControl.expectAndReturn(artifactManager.getDeliverableTypes(), new ArrayList(), 1);
127       artifactControl.replay();
128       // Steps and Releases shoud be retrieved too.
129       processControl.expectAndReturn(processManager.getSteps(), new ArrayList(), 1);
130       processControl.expectAndReturn(processManager.getReleases(), new ArrayList(), 1);
131       processControl.replay();
132       // Roles should be retrieved too.
133       permissionControl.expectAndReturn(permissionManager.getRoles(), new ArrayList(), 1);
134       permissionControl.replay();
135 
136       try{
137          // Try loading caches.
138          loader.loadCaches();
139       }
140       catch (BootstrapException be){
141          // Bootstrap exception is normal during registration of cache accessor.
142          assertTrue("BootstrapException during registration", be.getMessage().startsWith("Exception while registring the cache accessor"));
143          //fail("Caches cannot be loaded due to BootstrapException: " + be.getMessage());
144       }
145    }
146 }