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.businessfacades.environment;
16  
17  import org.figure8.join.core.DuplicateEntityException;
18  import org.figure8.join.businessobjects.commons.Step;
19  import org.figure8.join.businessobjects.commons.Release;
20  import org.figure8.join.businessobjects.commons.persistence.StepDao;
21  import org.figure8.join.businessobjects.commons.persistence.ReleaseDao;
22  import org.figure8.join.businessobjects.environment.LogicalEnvironment;
23  import org.figure8.join.businessobjects.environment.PhysicalEnvironment;
24  import org.figure8.join.util.SpringTestCase;
25  
26  import java.util.Date;
27  /**
28   * JUnit test case for testing the EnvironmentManager implementation.
29   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
30   * @version Revision$
31   */
32  public class EnvironmentManagerTest extends SpringTestCase{
33  
34     // Static -------------------------------------------------------------------
35  
36     /** Spring configuration files */
37     private String[] configLocations = new String[]{
38        "classpath:/org/figure8/join/businessfacades/environment/spring.xml",
39        "classpath:/org/figure8/join/businessobjects/environment/persistence/spring.xml"};
40  
41  
42     // Attributes ---------------------------------------------------------------
43  
44     /** The needed StepDao impl */
45     protected StepDao stepDao = null;
46     /** The needed ReleaseDao impl */
47     protected ReleaseDao releaseDao = null;
48  
49     /** The EnvironmentManager implementation instance to test. */
50     protected EnvironmentManager environmentManager = null;
51  
52  
53     // Override of SpringTestCase -----------------------------------------------
54  
55     /** Retrieve the needed daos and managed after having initialized context */
56     public void setUp(){
57        super.setUp();
58        // Get the needed daos ...
59        stepDao = (StepDao)context.getBean("stepDao");
60        releaseDao = (ReleaseDao)context.getBean("releaseDao");
61        // ... and the required manager.
62        environmentManager = (EnvironmentManager)context.getBean("environmentManager");
63     }
64  
65  
66     // Public -------------------------------------------------------------------
67  
68     /** Test the logical environment related methods. */
69     public void testLogicalEnvironments(){
70        // Create required items for logical environment.
71        Step step = new Step("first", 1);
72        Release release = new Release("10.0", 10, 0, new Date());
73        try{
74           stepDao.save(step);
75           releaseDao.save(release);
76        }
77        catch (Exception e){
78           fail("No exception should be thrown");
79        }
80        // Save a bunch of logical environments.
81        LogicalEnvironment env1 = new LogicalEnvironment("key1", "label1", "description1", "managerId", step, release);
82        LogicalEnvironment env2 = new LogicalEnvironment("key2", "label2", "description2", "managerId", step, release);
83        try{
84           environmentManager.saveLogicalEnvironment(env1);
85           environmentManager.saveLogicalEnvironment(env2);
86        }
87        catch (Exception e){
88           fail("No exception should be thrown");
89        }
90        // Assert there's 2 logical environments created.
91        assertEquals("Logical environments are successfully created", 2, environmentManager.getLogicalEnvironments().size());
92  
93        // Retrieve a logical environment by its key.
94        LogicalEnvironment foundEnv = environmentManager.getLogicalEnvironment("key1");
95        assertNotNull("Found a logical environment", foundEnv);
96        assertEquals("Found the correct environment", "key1", foundEnv.getKey());
97        assertEquals("Found the correct environment", "label1", foundEnv.getLabel());
98        assertEquals("Found the correct environment", "description1", foundEnv.getDescription());
99        assertEquals("Found the correct environment", "managerId", foundEnv.getManagerId());
100 
101       // Try creating a new environment with same key.
102       boolean exception = false;
103       LogicalEnvironment env3 = new LogicalEnvironment("key2", "label3", "description3", "managerId", step, release);
104       try{
105          environmentManager.saveLogicalEnvironment(env3);
106       }
107       catch (DuplicateEntityException dee){
108          // Check exception.
109          assertEquals("Original entity causes duplicate", "key2", ((LogicalEnvironment)dee.getOriginalEntity()).getKey());
110          exception = true;
111       }
112       assertTrue("DuplicateEntityException was thrown", exception);
113    }
114 
115    /** Test the physical environment related methods. */
116    public void testPhysicalEnvironments(){
117       // Save a bunch of new physical environments.
118       PhysicalEnvironment env1 = new PhysicalEnvironment("key1", "name1");
119       PhysicalEnvironment env2 = new PhysicalEnvironment("key2", "name2");
120       try{
121          environmentManager.savePhysicalEnvironment(env1);
122          environmentManager.savePhysicalEnvironment(env2);
123       }
124       catch (Exception e){
125          fail("No exception should be thrown");
126       }
127       // Assert there's 2 physical environemnts created.
128       assertEquals("Physical environments are successfully created", 2, environmentManager.getPhysicalEnvironments().size());
129 
130       // Retrieve a physical environment by its key.
131       PhysicalEnvironment foundEnv = environmentManager.getPhysicalEnvironment("key1");
132       assertNotNull("Found a physical environment", foundEnv);
133       assertEquals("Found the correct environment", "key1", foundEnv.getKey());
134       assertEquals("Found the correct environment", "name1", foundEnv.getName());
135 
136       // Try creating a new enviroment with same key.
137       boolean exception = false;
138       PhysicalEnvironment env3 = new PhysicalEnvironment("key2", "name3");
139       try{
140          environmentManager.savePhysicalEnvironment(env3);
141       }
142       catch (DuplicateEntityException dee){
143          // Check exception.
144          assertEquals("Original entity causes duplicate", "key2", ((PhysicalEnvironment)dee.getOriginalEntity()).getKey());
145          exception = true;
146       }
147       assertTrue("DuplicateEntityException was thrown", exception);
148    }
149 
150 
151    // Implementation of SpringTestCase -----------------------------------------
152 
153    /** @return An array of Spring config file locations. */
154    public String[] getConfigLocations(){
155       return configLocations;
156    }
157 }