1 17 package org.alfresco.repo.version.common; 18 19 import java.io.Serializable ; 20 import java.util.Collection ; 21 import java.util.Date ; 22 import java.util.HashMap ; 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 38 public class VersionHistoryImplTest extends TestCase 39 { 40 43 private Version rootVersion = null; 44 private Version childVersion1 = null; 45 private Version childVersion2 = null; 46 47 50 protected void setUp() throws Exception 51 { 52 super.setUp(); 53 54 NodeRef nodeRef = new NodeRef(new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "test"), "test"); 56 57 HashMap <String , Serializable > versionProperties1 = new HashMap <String , Serializable >(); 58 versionProperties1.put(VersionModel.PROP_VERSION_LABEL, "1"); 59 versionProperties1.put(VersionModel.PROP_CREATED_DATE, new Date ()); 60 versionProperties1.put("testProperty", "testValue"); 61 this.rootVersion = new VersionImpl(versionProperties1, nodeRef); 62 63 HashMap <String , Serializable > versionProperties2 = new HashMap <String , Serializable >(); 64 versionProperties2.put(VersionModel.PROP_VERSION_LABEL, "2"); 65 versionProperties2.put(VersionModel.PROP_CREATED_DATE, new Date ()); 66 versionProperties2.put("testProperty", "testValue"); 67 this.childVersion1 = new VersionImpl(versionProperties2, nodeRef); 68 69 HashMap <String , Serializable > versionProperties3 = new HashMap <String , Serializable >(); 70 versionProperties3.put(VersionModel.PROP_VERSION_LABEL, "3"); 71 versionProperties3.put(VersionModel.PROP_CREATED_DATE, new Date ()); 72 versionProperties3.put("testProperty", "testValue"); 73 this.childVersion2 = new VersionImpl(versionProperties3, nodeRef); 74 } 75 76 79 public void testConstructor() 80 { 81 testContructorImpl(); 82 } 83 84 89 private VersionHistoryImpl testContructorImpl() 90 { 91 VersionHistoryImpl vh = new VersionHistoryImpl(this.rootVersion); 92 assertNotNull(vh); 93 94 return vh; 95 } 96 97 101 public void testRootVersionSpecified() 102 { 103 try 104 { 105 new VersionHistoryImpl(null); 106 fail(); 107 } 108 catch(VersionServiceException exception) 109 { 110 } 111 } 112 113 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 130 public void testGetAllVersions() 131 { 132 VersionHistoryImpl vh = testAddVersionImpl(); 133 134 Collection <Version> allVersions = vh.getAllVersions(); 135 assertNotNull(allVersions); 136 assertEquals(3, allVersions.size()); 137 } 138 139 144 public void testAddVersion() 145 { 146 testAddVersionImpl(); 147 } 148 149 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 168 169 172 173 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 exception) 195 { 196 fail("Should continue by returning null."); 197 } 198 } 199 200 203 public void testGetSuccessors() 204 { 205 VersionHistoryImpl vh = testAddVersionImpl(); 206 207 Collection <Version> versions1 = vh.getSuccessors(this.rootVersion); 208 assertNotNull(versions1); 209 assertEquals(versions1.size(), 2); 210 211 for (Version version : versions1) 212 { 213 String 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 versions2 = vh.getSuccessors(this.childVersion1); 221 assertNotNull(versions2); 222 assertTrue(versions2.isEmpty()); 223 224 Collection versions3 = vh.getSuccessors(this.childVersion2); 225 assertNotNull(versions3); 226 assertTrue(versions3.isEmpty()); 227 } 228 229 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 |