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.artifact.persistence;
16  
17  import org.figure8.join.util.SpringTestCase;
18  import org.figure8.join.businessobjects.commons.Release;
19  import org.figure8.join.businessobjects.commons.persistence.ReleaseDao;
20  import org.figure8.join.businessobjects.artifact.Assembly;
21  import org.figure8.join.businessobjects.artifact.Deliverable;
22  import org.figure8.join.businessobjects.artifact.DeliverableType;
23  
24  import java.util.Date;
25  import java.util.List;
26  import java.util.ArrayList;
27  /**
28   * JUnit test case for testing AssemblyDao implementation.
29   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
30   * @version $Revision: 1.2 $
31   */
32  public class AssemblyDaoTest extends SpringTestCase{
33  
34     // Static -------------------------------------------------------------------
35  
36     /** Spring configuration files */
37     private String[] configLocations = new String[]{
38        "classpath:/org/figure8/join/businessobjects/artifact/persistence/spring.xml"};
39  
40  
41     // Attributes ---------------------------------------------------------------
42  
43     /** A ReleaseDao implementation that we need */
44     protected ReleaseDao releDao = null;
45     /** The DeliverableDao implementation to test */
46     protected DeliverableDao delDao = null;
47     /** A DeliverableTypeDao implementation that we need */
48     protected DeliverableTypeDao typeDao = null;
49     /** The AssemblyDao implementation to test */
50     protected AssemblyDao dao = null;
51     
52     
53     // Override of SpringTestCase -----------------------------------------------
54  
55     /** Retrieve assembly daos after having initialized the context */
56     public void setUp(){
57        super.setUp();
58        // Get daos.
59        releDao = (ReleaseDao)context.getBean("releaseDao");
60        delDao = (DeliverableDao)context.getBean("deliverableDao");
61        typeDao = (DeliverableTypeDao)context.getBean("deliverableTypeDao");
62        dao = (AssemblyDao)context.getBean("assemblyDao");
63     }
64     
65     
66     // Public -------------------------------------------------------------------
67     
68     /** Test the finder methods on assembly */
69     public void testFindBy(){
70        // Save a release and a bunch of assemblies.
71        Release release = new Release("40.0", 40, 0, new Date());
72        Assembly assembly1 = new Assembly("1.0", "comments1", "fooId", release);
73        Assembly assembly2 = new Assembly("2.0", "comments2", "barId", release);
74        int size = getAssemblyListSize();
75        releDao.save(release);
76        dao.save(assembly1);
77        dao.save(assembly2);
78        // Assert there's 2 more assemblies.
79        assertEquals("Assemblies have been successfuylly created", size + 2, dao.findAll().size());
80        // Retrieve assembly using its key.
81        Assembly foundAssembly = dao.getAssembly("assembly-r40.0-v1.0");
82        assertNotNull("Found an Assembly", foundAssembly);
83        assertEquals("Found the correct Assembly", "assembly-r40.0-v1.0", foundAssembly.getKey());
84        assertEquals("Found the correct Assembly", "1.0", foundAssembly.getVersionInfo());
85        assertEquals("Found the correct Assembly", "comments1", foundAssembly.getComments());
86        assertEquals("Found the correct Assembly", "fooId", foundAssembly.getComposerId());
87        // Retrieve assembly using its composer.
88        List foundAssemblies = dao.getAssembliesComposedByUser("barId");
89        assertNotNull("Found a list of Assemblies", foundAssemblies);
90        assertEquals("Found a list with 1 Assembly", 1, foundAssemblies.size());
91        foundAssembly = (Assembly)foundAssemblies.get(0);
92        assertEquals("Found the correct Assembly", "assembly-r40.0-v2.0", foundAssembly.getKey());
93        assertEquals("Found the correct Assembly", "2.0", foundAssembly.getVersionInfo());
94        assertEquals("Found the correct Assembly", "comments2", foundAssembly.getComments());
95        assertEquals("Found the correct Assembly", "barId", foundAssembly.getComposerId());
96        // Retrieve assemblies using their release.
97        foundAssemblies = dao.getAssembliesByRelease(release);
98        assertNotNull("Found a list of Assemblies", foundAssemblies);
99        assertEquals("Found a list with 2 Assemblies", 2, foundAssemblies.size());
100    }
101 
102    /** Test the finder methods on assembly with group functions */
103    public void testFindWith(){
104       // Create release and deliverable types we need.
105       Release release = new Release("50.0", 50, 0, new Date());
106       DeliverableType foo = new DeliverableType("fooType", "Foo Label", "foo-{0}.{1}", true, true);
107       DeliverableType bar = new DeliverableType("barType", "Bar Label", "bar-{0}.{1}", true, false);
108       releDao.save(release);
109       typeDao.save(foo);
110       typeDao.save(bar);
111       // Create bunch of deliverables we need.
112       Deliverable foo1 = new Deliverable("01", "comments", "supplier", release, foo);
113       Deliverable bar1 = new Deliverable("01", "comments", "supplier", release, bar);
114       delDao.save(foo1);
115       delDao.save(bar1);
116       // Now create assemblies for testing deliverables presence.
117       Assembly ass1 = new Assembly("01", "comments", "composer", release);
118       ass1.addDeliverable(foo1);
119       dao.save(ass1);
120       Assembly ass2 = new Assembly("02", "comments", "composer", release);
121       ass2.addDeliverable(foo1);
122       ass2.addDeliverable(bar1);
123       dao.save(ass2);
124       // Retrieve assemblies using deliverables criteria.
125       ArrayList list = new ArrayList();
126       list.add(foo1);
127       List assemblies = dao.getAssembliesWithDeliverables(list);
128       assertTrue("Found assemblies with deliverables", assemblies!=null && !assemblies.isEmpty());
129       assertEquals("Found 2 assemblies with deliverables", 2, assemblies.size());
130       for (int i=0; i<assemblies.size(); i++){
131          Assembly found = (Assembly)assemblies.get(i);
132          assertTrue("Found the correct assemblies" , "assembly-r50.0-v01".equals(found.getKey())
133                  || "assembly-r50.0-v02".equals(found.getKey()));
134       }
135       // Add a new criterion.
136       list.add(bar1);
137       assemblies = dao.getAssembliesWithDeliverables(list);
138       assertTrue("Found assemblies with deliverables", assemblies!=null && !assemblies.isEmpty());
139       assertEquals("Found 1 assembly with deliverables", 1, assemblies.size());
140       assertEquals("Found the correct assembly", "assembly-r50.0-v02", ((Assembly)assemblies.get(0)).getKey() );
141    }
142 
143 
144    // Protected ----------------------------------------------------------------
145 
146    /** Test the findAll on assemblies */
147    protected int getAssemblyListSize(){
148       return dao.findAll().size();
149    }
150 
151 
152    // Implementation of SpringTestCase -----------------------------------------
153 
154    /** @return An array of locations from Spring configuration files. */
155    public String[] getConfigLocations(){
156       return configLocations;
157    }
158 }