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.core.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  import org.figure8.join.businessobjects.artifact.persistence.AssemblyDao;
24  import org.figure8.join.businessobjects.artifact.persistence.DeliverableDao;
25  import org.figure8.join.businessobjects.artifact.persistence.DeliverableTypeDao;
26  
27  import java.util.Date;
28  import java.util.List;
29  import java.util.Collection;
30  import java.util.Iterator;
31  import java.io.StringWriter;
32  import java.io.InputStream;
33  /**
34   * JUnit test case for XmlDatabinder implementations
35   * @author  <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
36   * @version $Revision: 1.1 $
37   */
38  public class XmlDatabinderTest extends SpringTestCase{
39  
40     // Static -------------------------------------------------------------------
41  
42     /** Spring configuration files */
43     private String[] configLocations = new String[]{"classpath:/org/figure8/join/core/persistence/spring.xml"};
44  
45  
46     // Attributes ---------------------------------------------------------------
47  
48     /** A ReleaseDao implementation that we need */
49     protected ReleaseDao releDao = null;
50     /** The DeliverableDao implementation to test */
51     protected DeliverableDao delDao = null;
52     /** A DeliverableTypeDao implementation that we need */
53     protected DeliverableTypeDao typeDao = null;
54     /** The AssemblyDao implementation that we need */
55     protected AssemblyDao assemblyDao = null;
56     /** The GenericHibernateObjectDao that we need */
57     protected GenericHibernateObjectDao dao = null;
58     /** The XmlDatadinder to test */
59     protected XmlDatabinder binder = null;
60  
61  
62     // Override of SpringTestCase -----------------------------------------------
63  
64     /** Retrieve daos after having initialized context */
65     public void setUp(){
66        super.setUp();
67        // Get daos.
68        releDao = (ReleaseDao)context.getBean("releaseDao");
69        delDao = (DeliverableDao)context.getBean("deliverableDao");
70        typeDao = (DeliverableTypeDao)context.getBean("deliverableTypeDao");
71        assemblyDao = (AssemblyDao)context.getBean("assemblyDao");
72        dao = (GenericHibernateObjectDao)context.getBean("genericObjectDao");
73        binder = (XmlDatabinder)context.getBean("xmlDatabinder");
74     }
75  
76  
77     // Public -------------------------------------------------------------------
78  
79     /** Test the export method of XmlDatabinder */
80     public void testWriteToXml(){
81        // Save a release, deliverables and a bunch of assemblies.
82        Release release = new Release("101.0", 101, 0, new Date());
83        DeliverableType foo = new DeliverableType("foo2Type", "Foo 2 Label", "foo2-{0}.{1}", true, true);
84        DeliverableType bar = new DeliverableType("bar2Type", "Bar 2 Label", "bar2-{0}.{1}", true, false);
85        Deliverable foo1 = new Deliverable("01", "comments", "supplier", release, foo);
86        Deliverable bar1 = new Deliverable("01", "comments", "supplier", release, bar);
87        releDao.save(release);
88        typeDao.save(foo);
89        typeDao.save(bar);
90        delDao.save(foo1);
91        delDao.save(bar1);
92        Assembly assembly1 = new Assembly("1.0", "comments1", "fooId", release);
93        assembly1.addDeliverable(foo1);
94        assemblyDao.save(assembly1);
95        Assembly assembly2 = new Assembly("2.0", "comments2", "barId", release);
96        assembly2.addDeliverable(foo1);
97        assembly2.addDeliverable(bar1);
98        assemblyDao.save(assembly2);
99        // Retrieve objects and assert.
100       List objects = dao.findAll();
101       assertEquals("All persistent objects are found", 7, objects.size());
102       // Export objects as Xml.
103       StringWriter writer = new StringWriter();
104       try {binder.writeToXml(writer, dao);}
105       catch (Exception e){
106          fail("No exception should be thrown");
107       }
108       // Output and assert.
109       String export = writer.toString();
110       System.out.println(export);
111       assertNotNull("Export string is not null", export);
112       assertTrue("Export string is not empty", export.length() > 0);
113    }
114 
115    /** Test the import method of XmlDatabinder */
116    public void testLoadFromXml(){
117       Collection entityObjects = null;
118       InputStream is = getClass().getResourceAsStream("entities.xml");
119       // Load entities.
120       try {entityObjects = binder.loadFromXml(is);}
121       catch (Exception e){
122          fail("No exception shoud be thrown");
123       }
124       // Assert.
125       assertNotNull("Imported collection is not null", entityObjects);
126       assertEquals("Imported collection contains 4 entities", 4, entityObjects.size());
127       // Try replicating each object.
128       Iterator iterator = entityObjects.iterator();
129       while (iterator.hasNext()){
130          Object entity = iterator.next();
131          dao.replicate(entity);
132       }
133       /*
134       iterator = entityObjects.iterator();
135       while (iterator.hasNext()){
136          EntityObject entity = (EntityObject)iterator.next();
137          System.err.println(entity.getClass() + " : " + entity.getId());
138          if (entity instanceof Deliverable){
139             System.err.println("\t Type: " + ((Deliverable)entity).getDeliverableType().getId());
140             System.err.println("\t Assemblies: " + ((Assembly)((Deliverable)entity).getAssemblies().get(0)).getId());
141          }
142          if (entity instanceof Assembly){
143             System.err.println("\t Release: " + ((Assembly)entity).getRelease().getId());
144             System.err.println("\t Deliverables: " + ((Deliverable)((Assembly)entity).getDeliverables().values().iterator().next()).getId());
145          }
146       }
147       */
148    }
149 
150    /** Test the import method of XmlDatabinder for entities without ids */
151    public void testLoadFromXmlWithoutId(){
152       Collection entityObjects = null;
153       InputStream is = getClass().getResourceAsStream("entities-woid.xml");
154       // Load entities.
155       try {entityObjects = binder.loadFromXml(is);}
156       catch (Exception e){
157          e.printStackTrace();
158       }
159       // Assert.
160       assertNotNull("Imported collection is not null", entityObjects);
161       assertEquals("Imported collection contains 2 entities", 2, entityObjects.size());
162       // Try replicating each objects.
163       Iterator iterator = entityObjects.iterator();
164       while (iterator.hasNext()){
165          Object entity = iterator.next();
166          dao.replicate(entity);
167       }
168    }
169 
170 
171    // Implementation of SpringTestCase -----------------------------------------
172 
173    /** @return configLocations inner field */
174    public String[] getConfigLocations(){
175       return configLocations;
176    }
177 }