KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > sfee > SfeeDocumentManagerPublisherTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

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 JavaDoc;
53 import javax.activation.DataSource JavaDoc;
54 import java.io.BufferedReader JavaDoc;
55 import java.io.ByteArrayInputStream JavaDoc;
56 import java.io.ByteArrayOutputStream JavaDoc;
57 import java.io.IOException JavaDoc;
58 import java.io.InputStream JavaDoc;
59 import java.io.InputStreamReader JavaDoc;
60 import java.io.OutputStream JavaDoc;
61 import java.rmi.RemoteException JavaDoc;
62
63 public class SfeeDocumentManagerPublisherTest extends TestCase {
64     private static final String JavaDoc SERVER_URL = "http://tapestry.sourceforge.vasoftware.com";
65     private static final String JavaDoc USERNAME = "foo";
66     private static final String JavaDoc PASSWORD = "bar";
67     private static final String JavaDoc PROJECT_NAME = "CC Integration";
68     private String JavaDoc sessionId;
69     private String JavaDoc projectId;
70
71     public void setUp() throws Exception JavaDoc {
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 JavaDoc, CruiseControlException {
84         String JavaDoc title = getClass().getName() + System.currentTimeMillis() + "Document.txt";
85         String JavaDoc description = "This document was created by a unit test at " + System.currentTimeMillis();
86         String JavaDoc expectedContent = "testing at " + System.currentTimeMillis();
87         String JavaDoc documentPath = "/Root Folder/level1";
88         String JavaDoc 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 JavaDoc {
113         String JavaDoc title = getClass().getName() + System.currentTimeMillis() + "Document.txt";
114         String JavaDoc description = "This document was created by a unit test at " + System.currentTimeMillis();
115         String JavaDoc expectedContent = "testing at " + System.currentTimeMillis();
116         String JavaDoc documentPath = "/Root Folder/level1";
117         String JavaDoc 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 JavaDoc docTitle, String JavaDoc docPath,
147                                               SfeeDocumentManagerPublisher publisher) throws RemoteException JavaDoc,
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 JavaDoc 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 JavaDoc {
278         String JavaDoc title = getClass().getName() + System.currentTimeMillis() + "Document.txt";
279         String JavaDoc description = "This document was created by a unit test at " + System.currentTimeMillis();
280         String JavaDoc expectedContent = "testing at " + System.currentTimeMillis();
281         String JavaDoc 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 JavaDoc documentPath, String JavaDoc title,
304                                        String JavaDoc description, String JavaDoc expectedContent, DocumentStatus status,
305                                        String JavaDoc versionComment)
306             throws IOException JavaDoc, 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 JavaDoc documentFileId = docApp.getDocumentFileId(sessionId, foundDoc.getId(), foundDoc.getCurrentVersion());
321         IFileStorageAppSoap fileStorageApp = (IFileStorageAppSoap) ClientSoapStubFactory
322                 .getSoapStub(IFileStorageAppSoap.class, SERVER_URL);
323         DataHandler JavaDoc handler = fileStorageApp.downloadFile(sessionId, documentFileId);
324         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(handler.getInputStream()));
325         String JavaDoc foundContent = in.readLine();
326         assertEquals(expectedContent, foundContent);
327     }
328
329     private DocumentSoapDO findCurrentDocument(SfeeDocumentManagerPublisher publisher, String JavaDoc documentPath,
330                                                String JavaDoc title) throws CruiseControlException,
331             RemoteException JavaDoc {
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 JavaDoc getProjectId(ISourceForgeSoap soap, String JavaDoc sessionID) throws RemoteException JavaDoc {
352         ProjectSoapList projectList = soap.getProjectList(sessionID);
353         ProjectSoapRow[] rows = projectList.getDataRows();
354         String JavaDoc 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 JavaDoc {
365         private final String JavaDoc data;
366         private final String JavaDoc name;
367
368         public StringDataSource(String JavaDoc data, String JavaDoc name) {
369
370             this.data = data;
371             this.name = name;
372         }
373
374         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
375             return new ByteArrayInputStream JavaDoc(data.getBytes());
376         }
377
378         public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
379             return new ByteArrayOutputStream JavaDoc();
380         }
381
382         public String JavaDoc getContentType() {
383             return "text/plain";
384         }
385
386         public String JavaDoc getName() {
387             return name;
388         }
389     }
390 }
391
Popular Tags