1 /* DownloadServiceTest.java
2 Copyright (C) 2012 Red Hat, Inc.
3 
4 This file is part of IcedTea.
5 
6 IcedTea is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 2.
9 
10 IcedTea is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING.  If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19 
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library.  Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24 
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module.  An independent module is a module which is not derived from
32 or based on this library.  If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so.  If you do not wish to do so, delete this
35 exception statement from your version.
36  */
37 
38 import java.io.File;
39 import java.net.MalformedURLException;
40 import java.util.ArrayList;
41 import java.util.List;
42 
43 import net.sourceforge.jnlp.ProcessResult;
44 import net.sourceforge.jnlp.ServerAccess;
45 
46 import org.junit.Assert;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 
50 public class DownloadServiceTest {
51     private static final ServerAccess server = new ServerAccess();
52     private final String exitString = "Exiting DownloadService..";
53     private static final List<String> checkCache = new ArrayList<>();
54     private static final List<String> manageJnlpResources = new ArrayList<>();
55     private static final List<String> manageExternalResources = new ArrayList<>();
56 
57     @BeforeClass
initalizeClass()58     public static void initalizeClass() throws MalformedURLException {
59         //Check Cache
60         checkCache.add(server.getJavawsLocation());
61         checkCache.add("-arg");
62         checkCache.add(server.getUrl().toString() + "/");
63         checkCache.add("-arg");
64         checkCache.add("checkCache");
65         checkCache.add("-Xtrustall");
66         checkCache.add(ServerAccess.HEADLES_OPTION);
67         checkCache.add(server.getUrl() + "/DownloadService.jnlp");
68 
69         //Manage Jnlp Resouces
70         manageJnlpResources.add(server.getJavawsLocation());
71         manageJnlpResources.add("-arg");
72         manageJnlpResources.add(server.getUrl().toString() + "/");
73         manageJnlpResources.add("-arg");
74         manageJnlpResources.add("manageJnlpJars");
75         manageJnlpResources.add("-Xtrustall");
76         manageJnlpResources.add(ServerAccess.HEADLES_OPTION);
77         manageJnlpResources.add(server.getUrl() + "/DownloadService.jnlp");
78 
79         //Manage External Resources
80         manageExternalResources.add(server.getJavawsLocation());
81         manageExternalResources.add("-arg");
82         manageExternalResources.add(server.getUrl().toString() + "/");
83         manageExternalResources.add("-arg");
84         manageExternalResources.add("manageExternalJars");
85         manageExternalResources.add("-Xtrustall");
86         manageExternalResources.add(ServerAccess.HEADLES_OPTION);
87         manageExternalResources.add(server.getUrl() + "/DownloadService.jnlp");
88     }
89 
90     /**
91      * Executes reproducer to checks if DownloadServices's cache checks are working correctly.
92      * @return stdout of reproducer.
93      */
runCacheCheckTests()94     private String runCacheCheckTests() throws Exception {
95         //Check cache test
96         ProcessResult processResult = ServerAccess.executeProcess(checkCache);
97         String stdoutCheckCache = processResult.stdout;
98         Assert.assertTrue("CheckCache - DownloadServiceRunner instance did not close as expected, this test may fail.",
99                 stdoutCheckCache.contains(exitString));
100 
101         return stdoutCheckCache;
102     }
103 
104     /**
105      * Executes reproducer to checks if DownloadServices's management of external jars are working correctly.
106      * @return stdout of reproducer.
107      */
runExternalTests()108     private String runExternalTests() throws Exception {
109         ProcessResult processResult = ServerAccess.executeProcess(manageExternalResources);
110         String stdoutExternalResources = processResult.stdout;
111         Assert.assertTrue("ManageExternalResources - DownloadServiceRunner instance did not close as expected, this test may fail.",
112                 stdoutExternalResources.contains(exitString));
113 
114         return stdoutExternalResources;
115     }
116 
117     /**
118      * Executes reproducer to checks if DownloadServices's management of jnlp jars are working correctly.
119      * @return stdout of reproducer.
120      */
runJnlpResourceTests()121     private String runJnlpResourceTests() throws Exception {
122         ProcessResult processResult = ServerAccess.executeProcess(manageJnlpResources);
123         String stdoutJnlpResources = processResult.stdout;
124         Assert.assertTrue("ManageJnlpResources - DownloadServiceRunner instance did not close as expected, this test may fail.",
125                 stdoutJnlpResources.contains(exitString));
126 
127         return stdoutJnlpResources;
128     }
129 
130     @Test
checkIfRequiredResourcesExist()131     public void checkIfRequiredResourcesExist() {
132         //Jnlp files
133         Assert.assertTrue("DownloadService.jnlp is a required resource that's missing.",
134                 new File(server.getDir().getAbsolutePath() + "/DownloadService.jnlp").isFile());
135         Assert.assertTrue("DownloadServiceExtension.jnlp is a required resource that's missing.", new File(server.getDir().getAbsolutePath()
136                 + "/DownloadServiceExtension.jnlp").isFile());
137 
138         //Jar files
139         Assert.assertTrue("DownloadService.jar is a required resource that's missing.",
140                 new File(server.getDir().getAbsolutePath() + "/DownloadService.jar").isFile());
141         Assert.assertTrue("SignedJnlpResource.jar is a required resource that's missing.", new File(server.getDir().getAbsolutePath()
142                 + "/SignedJnlpResource.jar").isFile());
143         Assert.assertTrue("SignedJarResource.jar is a required resource that's missing.",
144                 new File(server.getDir().getAbsolutePath() + "/SignedJarResource.jar").isFile());
145         Assert.assertTrue("MultiJar-NoSignedJnlp.jar is a required resource that's missing.", new File(server.getDir().getAbsolutePath()
146                 + "/MultiJar-NoSignedJnlp.jar").isFile());
147     }
148 
149     @Test
testcheckCaches()150     public void testcheckCaches() throws Exception {
151         String stdoutCheckCache = runCacheCheckTests();
152 
153         //Stdout validations
154         String s = "CHECKCACHE-isPartCached: LaunchPartOne: true";
155         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
156 
157         s = "CHECKCACHE-isPartCached: LaunchPartTwo: true";
158         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
159 
160         s = "CHECKCACHE-isPartCached: NonExistingPart: false";
161         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
162     }
163 
164     @Test
testcheckCachesUsingArray()165     public void testcheckCachesUsingArray() throws Exception {
166         String stdoutCheckCache = runCacheCheckTests();
167 
168         //Stdout validations
169         String s = "CHECKCACHEUSINGMUTIPLEPARTS-isPartCached(Array): ValidLaunchParts: true";
170         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
171 
172         s = "CHECKCACHEUSINGMUTIPLEPARTS-isPartCached(Array): HalfValidLaunchParts: false";
173         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
174 
175         s = "CHECKCACHEUSINGMUTIPLEPARTS-isPartCached(Array): InvalidParts: false";
176         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
177     }
178 
179     @Test
testExtensioncheckCaches()180     public void testExtensioncheckCaches() throws Exception {
181         String stdoutCheckCache = runCacheCheckTests();
182 
183         //Stdout validations
184         String s = "CHECKEXTENSIONCACHE-isExtensionPartCached: ExtensionPartOne: true";
185         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
186 
187         s = "CHECKEXTENSIONCACHE-isExtensionPartCached: NonExistingPart: false";
188         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
189 
190         s = "CHECKEXTENSIONCACHE-isExtensionPartCached: NonExistingUrl: false";
191         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
192     }
193 
194     @Test
testExtensioncheckCachesUsingArray()195     public void testExtensioncheckCachesUsingArray() throws Exception {
196         String stdoutCheckCache = runCacheCheckTests();
197 
198         //Stdout validations
199         String s = "CHECKEXTENSIONCACHEUSINGMUTIPLEPARTS-isExtensionPartCached(Array): ValidExtensionParts: true";
200         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
201 
202         s = "CHECKEXTENSIONCACHEUSINGMUTIPLEPARTS-isExtensionPartCached(Array): HalfValidExtensionParts: false";
203         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
204 
205         s = "CHECKEXTENSIONCACHEUSINGMUTIPLEPARTS-isExtensionPartCached(Array): InvalidParts: false";
206         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutCheckCache.contains(s));
207 
208     }
209 
210     @Test
testExternalResourceChecks()211     public void testExternalResourceChecks() throws Exception {
212         runCacheCheckTests();
213         String stdoutExternalResources = runExternalTests();
214 
215         //Stdout validations
216         //This is automatically cached from the test engine because the .jar exists
217         String s = "CHECKEXTERNALCACHE-isResourceCached: UrlToExternalResource: true";
218         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutExternalResources.contains(s));
219 
220         s = "CHECKEXTERNALCACHE-isResourceCached: NonExistingUrl: false";
221         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutExternalResources.contains(s));
222     }
223 
224     @Test
testRemovePart()225     public void testRemovePart() throws Exception {
226         runCacheCheckTests();
227         String stdoutJnlpResources = runJnlpResourceTests();
228 
229         String s = "REMOVEPART-removePart: LaunchPartOne-BEFORE: true";
230         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
231         s = "REMOVEPART-removePart: LaunchPartOne-AFTER: false";
232         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
233 
234         s = "REMOVEPART-removePart: LaunchPartTwo-BEFORE: true";
235         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
236         s = "REMOVEPART-removePart: LaunchPartTwo-AFTER: false";
237         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
238     }
239 
240     @Test
testRemoveExtensionPart()241     public void testRemoveExtensionPart() throws Exception {
242         runCacheCheckTests();
243         String stdoutJnlpResources = runJnlpResourceTests();
244 
245         //Stdout validations
246         String s = "REMOVEEXTENSIONPART-removeExtensionPart: ExtensionPartOne-BEFORE: true";
247         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
248 
249         s = "REMOVEEXTENSIONPART-removeExtensionPart: ExtensionPartOne-AFTER: false";
250         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
251     }
252 
253     @Test
testRemoveExtensionPartUsingArray()254     public void testRemoveExtensionPartUsingArray() throws Exception {
255         runCacheCheckTests();
256         String stdoutJnlpResources = runJnlpResourceTests();
257 
258         //Stdout validations
259         String s = "REMOVEEXTENSIONUSINGVALIDPARTINARRAY-removeExtensionPart(Array): ValidExtensionParts-BEFORE: true";
260         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
261 
262         s = "REMOVEEXTENSIONUSINGVALIDPARTINARRAY-removeExtensionPart(Array): ValidExtensionParts-AFTER: false";
263         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
264 
265         s = "REMOVEEXTENSIONUSINGHALFVALIDPARTINARRAY-removeExtensionPart(Array): HalfValidExtensionParts-BEFORE: true";
266         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
267 
268         s = "REMOVEEXTENSIONUSINGHALFVALIDPARTINARRAY-removeExtensionPart(Array): HalfValidExtensionParts-AFTER: false";
269         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
270     }
271 
272     @Test
testRemoveExternalResource()273     public void testRemoveExternalResource() throws Exception {
274         runCacheCheckTests();
275         String stdoutExternalResources = runExternalTests();
276 
277         //Stdout validations
278         String s = "REMOVEEXTERNALPART-removeResource: UrlToExternalResource-BEFORE: true";
279         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutExternalResources.contains(s));
280 
281         s = "REMOVEEXTERNALPART-removeResource: UrlToExternalResource-AFTER: false";
282         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutExternalResources.contains(s));
283 
284     }
285 
286     @Test
testLoadPart()287     public void testLoadPart() throws Exception {
288         runCacheCheckTests();
289         String stdoutJnlpResources = runJnlpResourceTests();
290 
291         //Stdout validations
292         //Part 'one'
293         String s = "LOADPART-loadPart: LaunchPartOne-BEFORE: false";
294         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
295         s = "LOADPART-loadPart: LaunchPartOne-AFTER: true";
296         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
297 
298         //Part 'two'
299         s = "LOADPART-loadPart: LaunchPartTwo-BEFORE: false";
300         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
301         s = "LOADPART-loadPart: LaunchPartTwo-AFTER: true";
302         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
303     }
304 
305     @Test
testLoadExtensionPart()306     public void testLoadExtensionPart() throws Exception {
307         runCacheCheckTests();
308         String stdoutJnlpResources = runJnlpResourceTests();
309 
310         //Stdout validations
311         String s = "LOADEXTENSIONPART-loadExtensionPart: ExtensionPartOne-BEFORE: false";
312         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
313 
314         s = "LOADEXTENSIONPART-loadExtensionPart: ExtensionPartOne-AFTER: true";
315         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
316     }
317 
318     @Test
testLoadExtensionPartUsingArray()319     public void testLoadExtensionPartUsingArray() throws Exception {
320         runCacheCheckTests();
321         String stdoutJnlpResources = runJnlpResourceTests();
322 
323         //Stdout validations
324         String s = "LOADEXTENSIONUSINGVALIDPARTINARRAY-loadExtensionPart(Array): ValidExtensionParts-BEFORE: false";
325         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
326 
327         s = "LOADEXTENSIONUSINGVALIDPARTINARRAY-loadExtensionPart(Array): ValidExtensionParts-AFTER: true";
328         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
329 
330         s = "LOADEXTENSIONUSINGHALFVALIDPARTINARRAY-loadExtensionPart(Array): HalfValidExtensionParts-BEFORE: false";
331         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
332 
333         s = "LOADEXTENSIONUSINGHALFVALIDPARTINARRAY-loadExtensionPart(Array): HalfValidExtensionParts-AFTER: true";
334         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
335     }
336 
337     @Test
testLoadExternalResource()338     public void testLoadExternalResource() throws Exception {
339         runCacheCheckTests();
340         String stdoutExternalResources = runExternalTests();
341 
342         //Stdout validations
343         String s = "LOADEXTERNALRESOURCE-loadResource: UrlToExternalResource-BEFORE: false";
344         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutExternalResources.contains(s));
345 
346         s = "LOADEXTERNALRESOURCE-loadResource: UrlToExternalResource-AFTER: true";
347         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutExternalResources.contains(s));
348 
349     }
350 
351     @Test
testRepeatedlyLoadingAndUnloadingJnlpResources()352     public void testRepeatedlyLoadingAndUnloadingJnlpResources() throws Exception {
353         runCacheCheckTests();
354         String stdoutJnlpResources = runJnlpResourceTests();
355 
356         //Stdout validations
357         String s = "MULTIPLEMETHODCALLS - removePart: LaunchPartOne: false";
358         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
359 
360         s = "MULTIPLEMETHODCALLS - loadPart: LaunchPartOne: true";
361         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutJnlpResources.contains(s));
362 
363     }
364 
365     @Test
testRepeatedlyLoadingAndUnloadingExternalResources()366     public void testRepeatedlyLoadingAndUnloadingExternalResources() throws Exception {
367         runCacheCheckTests();
368         String stdoutExternalResources = runExternalTests();
369 
370         //Stdout validations
371         String s = "MULTIPLEMETHODCALLS - removeResource: UrlToExternalResource: false";
372         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutExternalResources.contains(s));
373 
374         s = "MULTIPLEMETHODCALLS - loadResource: UrlToExternalResource: true";
375         Assert.assertTrue("stdout should contain \"" + s + "\" but did not.", stdoutExternalResources.contains(s));
376     }
377 }
378