KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > version > common > VersionHistoryImplTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.version.common;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.alfresco.repo.version.VersionModel;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.cmr.repository.StoreRef;
29 import org.alfresco.service.cmr.version.Version;
30 import org.alfresco.service.cmr.version.VersionDoesNotExistException;
31 import org.alfresco.service.cmr.version.VersionServiceException;
32
33 /**
34  * VersionHistoryImpl Unit Test Class
35  *
36  * @author Roy Wetherall
37  */

38 public class VersionHistoryImplTest extends TestCase
39 {
40     /**
41      * Data used in the tests
42      */

43     private Version rootVersion = null;
44     private Version childVersion1 = null;
45     private Version childVersion2 = null;
46     
47     /**
48      * Set up
49      */

50     protected void setUp() throws Exception JavaDoc
51     {
52         super.setUp();
53         
54         // Create dummy node ref
55
NodeRef nodeRef = new NodeRef(new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "test"), "test");
56         
57         HashMap JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties1 = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
58         versionProperties1.put(VersionModel.PROP_VERSION_LABEL, "1");
59         versionProperties1.put(VersionModel.PROP_CREATED_DATE, new Date JavaDoc());
60         versionProperties1.put("testProperty", "testValue");
61         this.rootVersion = new VersionImpl(versionProperties1, nodeRef);
62         
63         HashMap JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties2 = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
64         versionProperties2.put(VersionModel.PROP_VERSION_LABEL, "2");
65         versionProperties2.put(VersionModel.PROP_CREATED_DATE, new Date JavaDoc());
66         versionProperties2.put("testProperty", "testValue");
67         this.childVersion1 = new VersionImpl(versionProperties2, nodeRef);
68         
69         HashMap JavaDoc<String JavaDoc, Serializable JavaDoc> versionProperties3 = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>();
70         versionProperties3.put(VersionModel.PROP_VERSION_LABEL, "3");
71         versionProperties3.put(VersionModel.PROP_CREATED_DATE, new Date JavaDoc());
72         versionProperties3.put("testProperty", "testValue");
73         this.childVersion2 = new VersionImpl(versionProperties3, nodeRef);
74     }
75
76     /**
77      * Test constructor
78      */

79     public void testConstructor()
80     {
81         testContructorImpl();
82     }
83     
84     /**
85      * Test construtor helper
86      *
87      * @return new version history
88      */

89     private VersionHistoryImpl testContructorImpl()
90     {
91         VersionHistoryImpl vh = new VersionHistoryImpl(this.rootVersion);
92         assertNotNull(vh);
93         
94         return vh;
95     }
96     
97     /**
98      * Exception case - a root version must be specified when creating a
99      * version history object
100      */

101     public void testRootVersionSpecified()
102     {
103         try
104         {
105             new VersionHistoryImpl(null);
106             fail();
107         }
108         catch(VersionServiceException exception)
109         {
110         }
111     }
112
113     /**
114      * Test getRootVersion
115      *
116      *@return root version
117      */

118     public void testGetRootVersion()
119     {
120         VersionHistoryImpl vh = testContructorImpl();
121         
122         Version rootVersion = vh.getRootVersion();
123         assertNotNull(rootVersion);
124         assertEquals(rootVersion, this.rootVersion);
125     }
126     
127     /**
128      * Test getAllVersions
129      */

130     public void testGetAllVersions()
131     {
132         VersionHistoryImpl vh = testAddVersionImpl();
133         
134         Collection JavaDoc<Version> allVersions = vh.getAllVersions();
135         assertNotNull(allVersions);
136         assertEquals(3, allVersions.size());
137     }
138     
139     /**
140      * Test addVersion
141      *
142      * @return version history
143      */

144     public void testAddVersion()
145     {
146         testAddVersionImpl();
147     }
148     
149     /**
150      * Test addVersion helper
151      *
152      * @return version history with version tree built
153      */

154     private VersionHistoryImpl testAddVersionImpl()
155     {
156         VersionHistoryImpl vh = testContructorImpl();
157         Version rootVersion = vh.getRootVersion();
158         
159         vh.addVersion(this.childVersion1, rootVersion);
160         vh.addVersion(this.childVersion2, rootVersion);
161         
162         return vh;
163     }
164     
165     /**
166      * TODO Exception case - add version that has already been added
167      */

168     
169     /**
170      * TODO Exception case - add a version with a duplicate version label
171      */

172     
173     /**
174      * Test getPredecessor
175      */

176     public void testGetPredecessor()
177     {
178         VersionHistoryImpl vh = testAddVersionImpl();
179         
180         Version version1 = vh.getPredecessor(this.childVersion1);
181         assertEquals(version1.getVersionLabel(), this.rootVersion.getVersionLabel());
182         
183         Version version2 = vh.getPredecessor(this.childVersion2);
184         assertEquals(version2.getVersionLabel(), this.rootVersion.getVersionLabel());
185         
186         Version version3 = vh.getPredecessor(this.rootVersion);
187         assertNull(version3);
188         
189         try
190         {
191             Version version4 = vh.getPredecessor(null);
192             assertNull(version4);
193         }
194         catch (Exception JavaDoc exception)
195         {
196             fail("Should continue by returning null.");
197         }
198     }
199     
200     /**
201      * Test getSuccessors
202      */

203     public void testGetSuccessors()
204     {
205         VersionHistoryImpl vh = testAddVersionImpl();
206         
207         Collection JavaDoc<Version> versions1 = vh.getSuccessors(this.rootVersion);
208         assertNotNull(versions1);
209         assertEquals(versions1.size(), 2);
210         
211         for (Version version : versions1)
212         {
213             String JavaDoc versionLabel = version.getVersionLabel();
214             if (!(versionLabel == "2" || versionLabel == "3"))
215             {
216                 fail("There is a version in this collection that should not be here.");
217             }
218         }
219         
220         Collection JavaDoc versions2 = vh.getSuccessors(this.childVersion1);
221         assertNotNull(versions2);
222         assertTrue(versions2.isEmpty());
223         
224         Collection JavaDoc versions3 = vh.getSuccessors(this.childVersion2);
225         assertNotNull(versions3);
226         assertTrue(versions3.isEmpty());
227     }
228     
229     /**
230      * Test getVersion
231      */

232     public void testGetVersion()
233     {
234         VersionHistoryImpl vh = testAddVersionImpl();
235        
236         Version version1 = vh.getVersion("1");
237         assertEquals(version1.getVersionLabel(), this.rootVersion.getVersionLabel());
238         
239         Version version2 = vh.getVersion("2");
240         assertEquals(version2.getVersionLabel(), this.childVersion1.getVersionLabel());
241         
242         Version version3 = vh.getVersion("3");
243         assertEquals(version3.getVersionLabel(), this.childVersion2.getVersionLabel());
244         
245         try
246         {
247             vh.getVersion("invalidLabel");
248             fail("An exception should have been thrown if the version can not be retrieved.");
249         }
250         catch (VersionDoesNotExistException exception)
251         {
252             System.out.println("Error message: " + exception.getMessage());
253         }
254     }
255 }
256
Popular Tags