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.businessobjects.environment.persistence;
16  
17  import org.figure8.join.util.SpringTestCase;
18  import org.figure8.join.businessobjects.environment.PhysicalEnvironment;
19  /**
20   * JUnit test case for testing PhysicalEnvironmentDao implementation.
21   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
22   * @version $Revision: 1.2 $
23   */
24  public class PhysicalEnvironmentDaoTest extends SpringTestCase{
25  
26     // Static -------------------------------------------------------------------
27  
28     /** Spring configuration files */
29     private String[] configLocations = new String[]{
30        "classpath:/org/figure8/join/businessobjects/environment/persistence/spring.xml"};
31  
32  
33     // Attributes ---------------------------------------------------------------
34  
35     /** The physical environment dao intance to test */
36     protected PhysicalEnvironmentDao dao = null;
37  
38  
39     // Override of SpringTestCase -----------------------------------------------
40  
41     /** Retrieve PhysicalEnv dao after having initialized context */
42     public void setUp(){
43        super.setUp();
44        // Get the dao.
45        dao = (PhysicalEnvironmentDao)context.getBean("physicalEnvironmentDao");
46     }
47  
48  
49     // Public -------------------------------------------------------------------
50  
51     /** Test the creation of a physical environment */
52     public void testPhysicalEnvironmentCreation(){
53        // Save a new environment.
54        PhysicalEnvironment env = new PhysicalEnvironment("creation", "creationName");
55        int size = getPhysicalEnvironmentListSize();
56        dao.save(env);
57        // Assert there's one env created.
58        assertEquals("PhysicalEnvironment is successfully created", size + 1, dao.findAll().size());
59     }
60  
61     /** Test the getByKey() method on dao */
62     public void testFindByKey(){
63        // Save some new environmente.
64        PhysicalEnvironment env1 = new PhysicalEnvironment("key1", "key1Name");
65        PhysicalEnvironment env2 = new PhysicalEnvironment("key2", "key2Name");
66        int size = getPhysicalEnvironmentListSize();
67        dao.save(env1);
68        dao.save(env2);
69        // Assert there's two more env created.
70        assertEquals("PhysicalEnvironment are successfully created", size + 2, dao.findAll().size());
71        // Retrieve and assert.
72        PhysicalEnvironment foundEnv = dao.getPhysicalEnvironment("key2");
73        assertNotNull("Found a PhysicalEnvironment",  foundEnv);
74        assertEquals("Found the correct PhysicalEnvironment", "key2", foundEnv.getKey());
75        assertEquals("Found the correct PhysicalEnvironment", "key2Name", foundEnv.getName());
76     }
77  
78     /** Test the removal of a physical environment */
79     public void testPhysicalEnvironmentRemoval(){
80        // Save a new environment.
81        PhysicalEnvironment env = new PhysicalEnvironment("removal", "removalName");
82        int size = getPhysicalEnvironmentListSize();
83        dao.save(env);
84        // Assert there's one env created.
85        assertEquals("PhysicalEnvironment is successfully created", size + 1, dao.findAll().size());
86        // Remove and assert.
87        dao.remove(env);
88        assertEquals("A PhysicalEnvironment has been successfully removed", size, dao.findAll().size());
89        assertNull("Removed PhysicalEnvironment is no longer available", dao.getPhysicalEnvironment("removal"));
90     }
91  
92  
93     // Protected ----------------------------------------------------------------
94  
95     /** Test the findAll on PhysicalEnvironemnt */
96     protected int getPhysicalEnvironmentListSize(){
97        return dao.findAll().size();
98     }
99  
100 
101    // Implementation of SpringTestCase -----------------------------------------
102 
103    /** @return configLocations inner field */
104    public String[] getConfigLocations(){
105       return configLocations;
106    }
107 }