1 37 package net.sourceforge.cruisecontrol.publishers.sfee; 38 39 import com.vasoftware.sf.soap42.webservices.ClientSoapStubFactory; 40 import com.vasoftware.sf.soap42.webservices.docman.DocumentFolderSoapDO; 41 import com.vasoftware.sf.soap42.webservices.docman.DocumentSoapDO; 42 import com.vasoftware.sf.soap42.webservices.docman.DocumentSoapList; 43 import com.vasoftware.sf.soap42.webservices.docman.DocumentSoapRow; 44 import com.vasoftware.sf.soap42.webservices.docman.IDocumentAppSoap; 45 import com.vasoftware.sf.soap42.webservices.filestorage.IFileStorageAppSoap; 46 import com.vasoftware.sf.soap42.webservices.sfmain.ISourceForgeSoap; 47 import com.vasoftware.sf.soap42.webservices.sfmain.ProjectSoapList; 48 import com.vasoftware.sf.soap42.webservices.sfmain.ProjectSoapRow; 49 import junit.framework.TestCase; 50 import net.sourceforge.cruisecontrol.CruiseControlException; 51 52 import javax.activation.DataHandler ; 53 import javax.activation.DataSource ; 54 import java.io.BufferedReader ; 55 import java.io.ByteArrayInputStream ; 56 import java.io.ByteArrayOutputStream ; 57 import java.io.IOException ; 58 import java.io.InputStream ; 59 import java.io.InputStreamReader ; 60 import java.io.OutputStream ; 61 import java.rmi.RemoteException ; 62 63 public class SfeeDocumentManagerPublisherTest extends TestCase { 64 private static final String SERVER_URL = "http://tapestry.sourceforge.vasoftware.com"; 65 private static final String USERNAME = "foo"; 66 private static final String PASSWORD = "bar"; 67 private static final String PROJECT_NAME = "CC Integration"; 68 private String sessionId; 69 private String projectId; 70 71 public void setUp() throws Exception { 72 SfeeTestUtils testUtil = new SfeeTestUtils(); 73 testUtil.loadSfeeInMemory(SERVER_URL, USERNAME, PASSWORD); 74 testUtil.addProject(PROJECT_NAME); 75 testUtil.createFolders(PROJECT_NAME, "/Root Folder/level1/level2"); 76 77 ISourceForgeSoap soap = (ISourceForgeSoap) ClientSoapStubFactory 78 .getSoapStub(ISourceForgeSoap.class, SERVER_URL); 79 sessionId = soap.login(USERNAME, PASSWORD); 80 projectId = getProjectId(soap, sessionId); 81 } 82 83 public void testSimpleUpload() throws IOException , CruiseControlException { 84 String title = getClass().getName() + System.currentTimeMillis() + "Document.txt"; 85 String description = "This document was created by a unit test at " + System.currentTimeMillis(); 86 String expectedContent = "testing at " + System.currentTimeMillis(); 87 String documentPath = "/Root Folder/level1"; 88 String versionComment = "This is a version created at " + System.currentTimeMillis(); 89 DocumentStatus status = DocumentStatus.FINAL; 90 91 SfeeDocumentManagerPublisher publisher = new SfeeDocumentManagerPublisher(); 92 publisher.setServerURL(SERVER_URL); 93 publisher.setUsername(USERNAME); 94 publisher.setPassword(PASSWORD); 95 publisher.setProjectName(PROJECT_NAME); 96 publisher.setFolder("/Root Folder/level1"); 97 98 publisher.setData(new StringDataSource(expectedContent, title)); 99 publisher.createDocumentName().setValue(title); 100 publisher.createDescription().setValue(description); 101 publisher.createStatus().setValue(status.getName()); 102 publisher.createVersionComment().setValue(versionComment); 103 publisher.setLock(true); 104 105 publisher.validate(); 106 publisher.publish(null); 107 108 109 assertDocumentCreated(publisher, documentPath, title, description, expectedContent, status, versionComment); 110 } 111 112 public void testPublishingSameDocName() throws CruiseControlException, RemoteException { 113 String title = getClass().getName() + System.currentTimeMillis() + "Document.txt"; 114 String description = "This document was created by a unit test at " + System.currentTimeMillis(); 115 String expectedContent = "testing at " + System.currentTimeMillis(); 116 String documentPath = "/Root Folder/level1"; 117 String versionComment = "This is a version created at " + System.currentTimeMillis(); 118 DocumentStatus status = DocumentStatus.FINAL; 119 120 SfeeDocumentManagerPublisher publisher = new SfeeDocumentManagerPublisher(); 121 publisher.setServerURL(SERVER_URL); 122 publisher.setUsername(USERNAME); 123 publisher.setPassword(PASSWORD); 124 publisher.setProjectName(PROJECT_NAME); 125 publisher.setFolder("/Root Folder/level1"); 126 127 publisher.setData(new StringDataSource(expectedContent, title)); 128 publisher.createDocumentName().setValue(title); 129 publisher.createDescription().setValue(description); 130 publisher.createStatus().setValue(status.getName()); 131 publisher.createVersionComment().setValue(versionComment); 132 publisher.setLock(true); 133 134 publisher.validate(); 135 publisher.publish(null); 136 137 assertCurrentDocumentVersion(1, title, documentPath, publisher); 138 139 publisher.publish(null); 140 assertCurrentDocumentVersion(2, title, documentPath, publisher); 141 142 publisher.publish(null); 143 assertCurrentDocumentVersion(3, title, documentPath, publisher); 144 } 145 146 private void assertCurrentDocumentVersion(int versionNumber, String docTitle, String docPath, 147 SfeeDocumentManagerPublisher publisher) throws RemoteException , 148 CruiseControlException { 149 150 DocumentSoapDO foundDoc = findCurrentDocument(publisher, docPath, docTitle); 151 assertEquals("Wrong version number found.", versionNumber, foundDoc.getVersion()); 152 } 153 154 public void testValidation() throws CruiseControlException { 155 SfeeDocumentManagerPublisher publisher = new SfeeDocumentManagerPublisher(); 156 assertInvalid(publisher); 157 158 publisher.setServerURL("foo"); 159 assertInvalid(publisher); 160 161 publisher.setUsername("bar"); 162 assertInvalid(publisher); 163 164 publisher.setPassword("baz"); 165 assertInvalid(publisher); 166 167 publisher.setProjectName("foobar"); 168 assertInvalid(publisher); 169 170 publisher.setFolder("foopath"); 171 assertInvalid(publisher); 172 173 publisher.setData(new StringDataSource("foobarbaz", "footitle")); 174 assertInvalid(publisher); 175 176 publisher.createDocumentName().setValue("biz"); 177 assertInvalid(publisher); 178 179 publisher.createDescription().setValue("wak"); 180 assertInvalid(publisher); 181 182 publisher.createStatus().setValue("bizwak"); 183 assertInvalid(publisher); 184 185 publisher.createStatus().setValue("final"); 186 publisher.validate(); 187 } 188 189 private void assertInvalid(SfeeDocumentManagerPublisher publisher) { 190 try { 191 publisher.validate(); 192 fail("Publisher should not validate"); 193 } catch (CruiseControlException expected) { 194 } 195 } 196 197 public void testGetFolderId() throws CruiseControlException { 198 SfeeDocumentManagerPublisher publisher = new SfeeDocumentManagerPublisher(); 199 publisher.setServerURL(SERVER_URL); 200 publisher.setUsername(USERNAME); 201 publisher.setPassword(PASSWORD); 202 publisher.setProjectName(PROJECT_NAME); 203 204 DocumentFolderSoapDO rootFolder = publisher.findFolder("/Root Folder"); 205 assertNotNull(rootFolder); 206 207 DocumentFolderSoapDO level1Folder = publisher.findFolder("/Root Folder/level1"); 208 assertNotNull(level1Folder); 209 assertEquals(rootFolder.getId(), level1Folder.getParentFolderId()); 210 211 DocumentFolderSoapDO level2Folder = publisher.findFolder("/Root Folder/level1/level2"); 212 assertNotNull(level2Folder); 213 assertEquals(level1Folder.getId(), level2Folder.getParentFolderId()); 214 } 215 216 public void testGetFolderWithInvalidPath() throws CruiseControlException { 217 SfeeDocumentManagerPublisher publisher = new SfeeDocumentManagerPublisher(); 218 publisher.setServerURL(SERVER_URL); 219 publisher.setUsername(USERNAME); 220 publisher.setPassword(PASSWORD); 221 publisher.setProjectName(PROJECT_NAME); 222 223 try { 224 publisher.findFolder("/THIS FOLDER DOESN'T EXIST"); 225 fail("expected an exception"); 226 } catch (CruiseControlException expected) { 227 } 228 229 try { 230 publisher.findFolder("THIS FOLDER DOESN'T EXIST"); 231 fail("expected an exception"); 232 } catch (CruiseControlException expected) { 233 } 234 235 try { 236 publisher.findFolder("/Root Folder/level1/level5"); 237 fail("expected an exception"); 238 } catch (CruiseControlException expected) { 239 } 240 241 try { 242 publisher.findFolder(""); 243 fail("expected an exception"); 244 } catch (CruiseControlException expected) { 245 } 246 247 try { 248 publisher.findFolder(null); 249 fail("expected an exception"); 250 } catch (IllegalArgumentException expected) { 251 } 252 } 253 254 public void testDocumentOrDataSourceRequired() throws CruiseControlException { 255 SfeeDocumentManagerPublisher publisher = new SfeeDocumentManagerPublisher(); 256 publisher.setServerURL("foo"); 257 publisher.setUsername("bar"); 258 publisher.setPassword("baz"); 259 publisher.setProjectName("foobar"); 260 publisher.setFolder("foopath"); 261 publisher.createDocumentName().setValue("biz"); 262 publisher.createDescription().setValue("wak"); 263 publisher.createStatus().setValue("bizwak"); 264 publisher.createStatus().setValue("final"); 265 266 try { 267 publisher.validate(); 268 fail("Expected an exception."); 269 } catch (CruiseControlException e) { 270 assertTrue(e.getMessage().indexOf("Either a document or a datasource must be specified.") >= 0); 271 } 272 273 publisher.setDocument("doesntmatter"); 274 publisher.validate(); 275 } 276 277 public void testVersionCommentAndDocumentNameMayBeNull() throws Exception { 278 String title = getClass().getName() + System.currentTimeMillis() + "Document.txt"; 279 String description = "This document was created by a unit test at " + System.currentTimeMillis(); 280 String expectedContent = "testing at " + System.currentTimeMillis(); 281 String documentPath = "/Root Folder/level1"; 282 DocumentStatus status = DocumentStatus.FINAL; 283 284 SfeeDocumentManagerPublisher publisher = new SfeeDocumentManagerPublisher(); 285 publisher.setServerURL(SERVER_URL); 286 publisher.setUsername(USERNAME); 287 publisher.setPassword(PASSWORD); 288 publisher.setProjectName(PROJECT_NAME); 289 publisher.setFolder("/Root Folder/level1"); 290 291 publisher.setData(new StringDataSource(expectedContent, title)); 292 publisher.createDescription().setValue(description); 293 publisher.createStatus().setValue(status.getName()); 294 publisher.setLock(true); 295 296 publisher.validate(); 297 publisher.publish(null); 298 299 300 assertDocumentCreated(publisher, documentPath, title, description, expectedContent, status, null); 301 } 302 303 private void assertDocumentCreated(SfeeDocumentManagerPublisher publisher, String documentPath, String title, 304 String description, String expectedContent, DocumentStatus status, 305 String versionComment) 306 throws IOException , CruiseControlException { 307 assertNotNull(projectId); 308 assertNotNull(sessionId); 309 IDocumentAppSoap docApp = (IDocumentAppSoap) ClientSoapStubFactory 310 .getSoapStub(IDocumentAppSoap.class, SERVER_URL); 311 312 DocumentSoapDO foundDoc = findCurrentDocument(publisher, documentPath, title); 313 314 assertEquals(USERNAME, foundDoc.getLockedBy()); 315 assertEquals(title, foundDoc.getTitle()); 316 assertEquals(description, foundDoc.getDescription()); 317 assertEquals(status.getName(), foundDoc.getStatus()); 318 assertEquals(versionComment, foundDoc.getVersionComment()); 319 320 String documentFileId = docApp.getDocumentFileId(sessionId, foundDoc.getId(), foundDoc.getCurrentVersion()); 321 IFileStorageAppSoap fileStorageApp = (IFileStorageAppSoap) ClientSoapStubFactory 322 .getSoapStub(IFileStorageAppSoap.class, SERVER_URL); 323 DataHandler handler = fileStorageApp.downloadFile(sessionId, documentFileId); 324 BufferedReader in = new BufferedReader (new InputStreamReader (handler.getInputStream())); 325 String foundContent = in.readLine(); 326 assertEquals(expectedContent, foundContent); 327 } 328 329 private DocumentSoapDO findCurrentDocument(SfeeDocumentManagerPublisher publisher, String documentPath, 330 String title) throws CruiseControlException, 331 RemoteException { 332 333 IDocumentAppSoap docApp = (IDocumentAppSoap) ClientSoapStubFactory 334 .getSoapStub(IDocumentAppSoap.class, SERVER_URL); 335 336 DocumentFolderSoapDO documentFolder = publisher.findFolder(documentPath); 337 DocumentSoapList documentList = docApp.getDocumentList(sessionId, documentFolder.getId(), null); 338 DocumentSoapRow[] documents = documentList.getDataRows(); 339 DocumentSoapDO foundDoc = null; 340 for (int i = 0; i < documents.length; i++) { 341 DocumentSoapRow document = documents[i]; 342 if (document.getTitle().equals(title)) { 343 foundDoc = docApp.getDocumentData(sessionId, document.getId(), document.getCurrentVersion()); 344 } 345 } 346 347 assertNotNull(foundDoc); 348 return foundDoc; 349 } 350 351 private static String getProjectId(ISourceForgeSoap soap, String sessionID) throws RemoteException { 352 ProjectSoapList projectList = soap.getProjectList(sessionID); 353 ProjectSoapRow[] rows = projectList.getDataRows(); 354 String projectID = null; 355 for (int i = 0; i < rows.length; i++) { 356 ProjectSoapRow nextProjectRow = rows[i]; 357 if (nextProjectRow.getTitle().equals(PROJECT_NAME)) { 358 projectID = nextProjectRow.getId(); 359 } 360 } 361 return projectID; 362 } 363 364 public static class StringDataSource implements DataSource { 365 private final String data; 366 private final String name; 367 368 public StringDataSource(String data, String name) { 369 370 this.data = data; 371 this.name = name; 372 } 373 374 public InputStream getInputStream() throws IOException { 375 return new ByteArrayInputStream (data.getBytes()); 376 } 377 378 public OutputStream getOutputStream() throws IOException { 379 return new ByteArrayOutputStream (); 380 } 381 382 public String getContentType() { 383 return "text/plain"; 384 } 385 386 public String getName() { 387 return name; 388 } 389 } 390 } 391 | Popular Tags |