KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sourceforge.cruisecontrol.publishers.sfee;
2
3 import com.vasoftware.sf.soap42.webservices.ClientSoapStubFactory;
4 import com.vasoftware.sf.soap42.webservices.filestorage.IFileStorageAppSoap;
5 import com.vasoftware.sf.soap42.webservices.frs.FrsFileSoapList;
6 import com.vasoftware.sf.soap42.webservices.frs.FrsFileSoapRow;
7 import com.vasoftware.sf.soap42.webservices.frs.IFrsAppSoap;
8 import com.vasoftware.sf.soap42.webservices.sfmain.ISourceForgeSoap;
9 import junit.framework.TestCase;
10 import net.sourceforge.cruisecontrol.CruiseControlException;
11 import net.sourceforge.cruisecontrol.Publisher;
12 import net.sourceforge.cruisecontrol.testutil.TestUtil;
13 import net.sourceforge.cruisecontrol.testutil.UnreadableMockFile;
14 import org.apache.tools.ant.util.FileUtils;
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStreamReader JavaDoc;
18 import java.rmi.RemoteException JavaDoc;
19 import javax.activation.DataHandler JavaDoc;
20
21 public class SFEEFrsPublisherTest extends TestCase {
22     private static final String JavaDoc SERVER_URL = "http://tapestry.sourceforge.vasoftware.com";
23     private static final String JavaDoc USERNAME = "foo";
24     private static final String JavaDoc PASSWORD = "bar";
25     private static final String JavaDoc RELEASE_ID = "rel1509";
26
27     public void setUp() {
28         new SfeeTestUtils().loadSfeeInMemory(SERVER_URL, USERNAME, PASSWORD);
29     }
30
31     public void testIsCorrectType() throws Exception JavaDoc {
32         assertTrue(Publisher.class.isAssignableFrom(SfeeFrsPublisher.class));
33     }
34
35     public void testSimpleUpload() throws IOException JavaDoc, CruiseControlException {
36         SfeeFrsPublisher publisher = new SfeeFrsPublisher();
37
38         publisher.setServerURL(SERVER_URL);
39         publisher.setUsername(USERNAME);
40         publisher.setPassword(PASSWORD);
41         publisher.setReleaseID(RELEASE_ID);
42
43         final File tempFile = File.createTempFile(SFEEFrsPublisherTest.class.getName(), "temp");
44         tempFile.deleteOnExit();
45         publisher.setFile(tempFile.getAbsolutePath());
46
47         publisher.validate();
48         //The cruise log information isn't used by this task.
49
publisher.publish(null);
50
51         assertFileExistsInRelease(tempFile.getName(), SERVER_URL, USERNAME, PASSWORD, RELEASE_ID);
52     }
53
54     public void testMissingRequiredFields() throws CruiseControlException {
55         SfeeFrsPublisher publisher = new SfeeFrsPublisher();
56
57         try {
58             publisher.validate();
59             fail("Expected an exception");
60         } catch (CruiseControlException expected) {
61         }
62
63         publisher.setServerURL(SERVER_URL);
64         try {
65             publisher.validate();
66             fail("Expected an exception");
67         } catch (CruiseControlException expected) {
68         }
69
70         publisher.setPassword(PASSWORD);
71         try {
72             publisher.validate();
73             fail("Expected an exception");
74         } catch (CruiseControlException expected) {
75         }
76
77         publisher.setUsername(USERNAME);
78         try {
79             publisher.validate();
80             fail("Expected an exception");
81         } catch (CruiseControlException expected) {
82         }
83
84         publisher.setReleaseID(RELEASE_ID);
85         try {
86             publisher.validate();
87             fail("Expected an exception");
88         } catch (CruiseControlException expected) {
89         }
90
91         publisher.setFile("foo");
92         publisher.validate();
93     }
94
95     public void testPublishNonExistentFile() throws CruiseControlException {
96         SfeeFrsPublisher publisher = new SfeeFrsPublisher();
97
98         publisher.setServerURL(SERVER_URL);
99         publisher.setUsername(USERNAME);
100         publisher.setPassword(PASSWORD);
101         publisher.setReleaseID(RELEASE_ID);
102         publisher.setFile("THISFILEDOESNTEXIST" + System.currentTimeMillis());
103
104         publisher.validate();
105
106         try {
107             publisher.publish(null);
108             fail("Expected an exception because the file doesn't exist.");
109         } catch (CruiseControlException expected) {
110         }
111     }
112
113     public void testPublishUnreadableFile() throws CruiseControlException {
114         //A file that exists, but isn't readable
115
final UnreadableMockFile unreadableFile = new UnreadableMockFile();
116
117         SfeeFrsPublisher publisher = new SfeeFrsPublisher() {
118             protected File getFile() {
119                 return unreadableFile;
120             }
121         };
122         
123         publisher.setFile("mocked out");
124
125         publisher.setServerURL(SERVER_URL);
126         publisher.setUsername(USERNAME);
127         publisher.setPassword(PASSWORD);
128         publisher.setReleaseID(RELEASE_ID);
129
130         publisher.validate();
131
132         try {
133             publisher.publish(null);
134             fail("Expected an exception because the file isn't readable.");
135         } catch (CruiseControlException expected) {
136         }
137
138         assertTrue(unreadableFile.canReadWasCalled());
139     }
140
141     public void testPublishDirectoryInsteadOfFile() throws CruiseControlException {
142         SfeeFrsPublisher publisher = new SfeeFrsPublisher();
143
144         publisher.setServerURL(SERVER_URL);
145         publisher.setUsername(USERNAME);
146         publisher.setPassword(PASSWORD);
147         publisher.setReleaseID(RELEASE_ID);
148         publisher.setFile(System.getProperty("java.io.tmpdir"));
149
150         publisher.validate();
151
152         try {
153             publisher.publish(null);
154             fail("Expected an exception because cannot publish directories.");
155         } catch (CruiseControlException expected) {
156         }
157     }
158
159     public void testUploadFileWithNewName() throws IOException JavaDoc, CruiseControlException {
160         SfeeFrsPublisher publisher = new SfeeFrsPublisher();
161
162         String JavaDoc uploadname = "differentFromLocalFilename" + System.currentTimeMillis();
163         publisher.setServerURL(SERVER_URL);
164         publisher.setUsername(USERNAME);
165         publisher.setPassword(PASSWORD);
166         publisher.setReleaseID(RELEASE_ID);
167         publisher.setUploadname(uploadname);
168
169         final File tempFile = File.createTempFile(SFEEFrsPublisherTest.class.getName(), "temp");
170         tempFile.deleteOnExit();
171         publisher.setFile(tempFile.getAbsolutePath());
172
173         publisher.validate();
174         publisher.publish(null);
175
176         assertFileExistsInRelease(uploadname, SERVER_URL, USERNAME, PASSWORD, RELEASE_ID);
177     }
178
179     private static void assertFileExistsInRelease(String JavaDoc filename, String JavaDoc serverUrl, String JavaDoc username, String JavaDoc password,
180             String JavaDoc releaseId) throws RemoteException JavaDoc {
181         ISourceForgeSoap soap = (ISourceForgeSoap) ClientSoapStubFactory.getSoapStub(ISourceForgeSoap.class, serverUrl);
182         String JavaDoc sessionID = soap.login(username, password);
183
184         IFrsAppSoap frsApp = (IFrsAppSoap) ClientSoapStubFactory.getSoapStub(IFrsAppSoap.class, serverUrl);
185         FrsFileSoapList fileList = frsApp.getFrsFileList(sessionID, releaseId);
186         FrsFileSoapRow[] files = fileList.getDataRows();
187         boolean found = false;
188         for (int i = 0; i < files.length && !found; i++) {
189             FrsFileSoapRow nextFile = files[ i ];
190             if (nextFile.getFilename().equals(filename)) {
191                 found = true;
192             }
193         }
194         assertTrue("File wasn't found in release.", found);
195     }
196
197     public void testUploadShouldOverwrite() throws IOException JavaDoc, CruiseControlException {
198         SfeeFrsPublisher publisher = new SfeeFrsPublisher();
199
200         String JavaDoc uploadname = "fileShouldOverWrite.txt";
201         publisher.setServerURL(SERVER_URL);
202         publisher.setUsername(USERNAME);
203         publisher.setPassword(PASSWORD);
204         publisher.setReleaseID(RELEASE_ID);
205         publisher.setUploadname(uploadname);
206
207         final File tempFile = File.createTempFile(SFEEFrsPublisherTest.class.getName(), "temp");
208         tempFile.deleteOnExit();
209         TestUtil.write("run 1", tempFile);
210         publisher.setFile(tempFile.getAbsolutePath());
211
212         publisher.validate();
213         publisher.publish(null);
214         assertOneFileExistsInRelease(uploadname, SERVER_URL, USERNAME, PASSWORD, RELEASE_ID);
215
216         TestUtil.write("run 2", tempFile);
217         publisher.publish(null);
218         assertOneFileExistsInRelease(uploadname, SERVER_URL, USERNAME, PASSWORD, RELEASE_ID);
219         String JavaDoc contents = getReleaseFileContents(uploadname, SERVER_URL, USERNAME, PASSWORD, RELEASE_ID);
220         assertTrue("File didn't get overwritten", contents.indexOf("run 2") >= 0);
221     }
222
223     private void assertOneFileExistsInRelease(String JavaDoc filename, String JavaDoc serverUrl, String JavaDoc username, String JavaDoc password,
224             String JavaDoc releaseId) throws RemoteException JavaDoc {
225         ISourceForgeSoap soap = (ISourceForgeSoap) ClientSoapStubFactory.getSoapStub(ISourceForgeSoap.class, serverUrl);
226         String JavaDoc sessionID = soap.login(username, password);
227
228         IFrsAppSoap frsApp = (IFrsAppSoap) ClientSoapStubFactory.getSoapStub(IFrsAppSoap.class, serverUrl);
229         FrsFileSoapList fileList = frsApp.getFrsFileList(sessionID, releaseId);
230         FrsFileSoapRow[] files = fileList.getDataRows();
231         int count = 0;
232         for (int i = 0; i < files.length; i++) {
233             FrsFileSoapRow nextFile = files[ i ];
234             if (nextFile.getFilename().equals(filename)) {
235                 count++;
236             }
237         }
238         assertTrue("File was found [" + count + "] times in release. Expected once.", count == 1);
239     }
240
241     private String JavaDoc getReleaseFileContents(String JavaDoc filename, String JavaDoc serverUrl, String JavaDoc username, String JavaDoc password,
242             String JavaDoc releaseId) throws IOException JavaDoc {
243         ISourceForgeSoap soap = (ISourceForgeSoap) ClientSoapStubFactory.getSoapStub(ISourceForgeSoap.class, serverUrl);
244         String JavaDoc sessionID = soap.login(username, password);
245
246         IFrsAppSoap frsApp = (IFrsAppSoap) ClientSoapStubFactory.getSoapStub(IFrsAppSoap.class, serverUrl);
247         FrsFileSoapList fileList = frsApp.getFrsFileList(sessionID, releaseId);
248         FrsFileSoapRow[] files = fileList.getDataRows();
249         boolean found = false;
250         for (int i = 0; i < files.length && !found; i++) {
251             FrsFileSoapRow nextFile = files[ i ];
252             if (nextFile.getFilename().equals(filename)) {
253                 String JavaDoc fileID = nextFile.getId();
254                 IFileStorageAppSoap fileStorageApp = (IFileStorageAppSoap) ClientSoapStubFactory
255                         .getSoapStub(IFileStorageAppSoap.class, serverUrl);
256
257                 DataHandler JavaDoc handler = fileStorageApp.downloadFile(sessionID, frsApp.getFrsFileId(sessionID, fileID));
258                 return FileUtils.readFully(new InputStreamReader JavaDoc(handler.getInputStream()));
259             }
260         }
261
262         return null;
263     }
264
265
266 }
267
Popular Tags