1 /* SimpleTest1Test.java
2  Copyright (C) 2011 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.io.FileInputStream;
40 import java.io.IOException;
41 import java.net.MalformedURLException;
42 import java.net.URL;
43 import java.util.Arrays;
44 import java.util.List;
45 import net.sourceforge.jnlp.ProcessResult;
46 import net.sourceforge.jnlp.ServerAccess;
47 import org.junit.Assert;
48 
49 import org.junit.Test;
50 
51 public class SimpleTest1Test {
52 
53     private static final ServerAccess server = new ServerAccess();
54     private static final List<String> strict = Arrays.asList(new String[]{"-strict", ServerAccess.VERBOSE_OPTION});
55 
checkLaunched(ProcessResult pr)56     static void checkLaunched(ProcessResult pr) {
57         checkLaunched(pr, false);
58     }
59 
checkLaunched(ProcessResult pr, boolean negate)60     static void checkLaunched(ProcessResult pr, boolean negate) {
61         checkLaunched(pr, negate, true);
62     }
63 
checkLaunched(ProcessResult pr, boolean negate, boolean checkTermination)64     static void checkLaunched(ProcessResult pr, boolean negate, boolean checkTermination) {
65         String s = "Good simple javaws exapmle";
66         if (negate) {
67             Assert.assertFalse("testSimpletest1lunchOk stdout should NOT contains " + s + " bud did", pr.stdout.contains(s));
68         } else {
69             Assert.assertTrue("testSimpletest1lunchOk stdout should contains " + s + " bud didn't", pr.stdout.contains(s));
70         }
71         String ss = "xception";
72         if (negate) {
73             Assert.assertTrue("testSimpletest1lunchOk stderr should contains " + ss + " but didn't", pr.stderr.contains(ss));
74         } else {
75             //disabled, unnecessary exceptions may occure
76             //Assert.assertFalse("testSimpletest1lunchOk stderr should not contains " + ss + " but did", pr.stderr.contains(ss));
77         }
78         if (checkTermination) {
79             Assert.assertFalse(pr.wasTerminated);
80             if (negate) {
81                 Assert.assertEquals((Integer) 1, pr.returnValue);
82             } else {
83                 Assert.assertEquals((Integer) 0, pr.returnValue);
84             }
85        }
86     }
87 
88     @Test
testSimpletest1lunchOk()89     public void testSimpletest1lunchOk() throws Exception {
90         ProcessResult pr = server.executeJavawsHeadless(null, "/simpletest1.jnlp");
91         checkLaunched(pr);
92     }
93 
94     @Test
testSimpletest1lunchNotOkJnlpStrict()95     public void testSimpletest1lunchNotOkJnlpStrict() throws Exception {
96         ProcessResult pr = server.executeJavawsHeadless(strict, "/simpletest1.jnlp");
97         checkLaunched(pr, true);
98     }
99 
100     @Test
testSimpletest1lunchOkStrictJnlp()101     public void testSimpletest1lunchOkStrictJnlp() throws Exception {
102         String originalResourceName = "simpletest1.jnlp";
103         String newResourceName = "simpletest1_strict.jnlp";
104         createStrictFile(originalResourceName, newResourceName, server.getUrl(""));
105         ProcessResult pr = server.executeJavawsHeadless(null, "/" + newResourceName);
106         checkLaunched(pr);
107     }
108 
109     @Test
testSimpletest1lunchOkStrictJnlpStrict()110     public void testSimpletest1lunchOkStrictJnlpStrict() throws Exception {
111         String originalResourceName = "simpletest1.jnlp";
112         String newResourceName = "simpletest1_strict.jnlp";
113         createStrictFile(originalResourceName, newResourceName, server.getUrl(""));
114         ProcessResult pr = server.executeJavawsHeadless(strict, "/" + newResourceName);
115         checkLaunched(pr);
116     }
117 
createStrictFile(String originalResourceName, String newResourceName, URL codebase)118     private void createStrictFile(String originalResourceName, String newResourceName, URL codebase) throws MalformedURLException, IOException {
119         String originalContent = ServerAccess.getContentOfStream(new FileInputStream(new File(server.getDir(), originalResourceName)));
120         String nwContent1 = originalContent.replaceAll("href=\""+originalResourceName+"\"", "href=\""+newResourceName+"\"");
121         String nwContent = nwContent1.replaceAll("codebase=\".\"", "codebase=\"" + codebase + "\"");
122         ServerAccess.saveFile(nwContent, new File(server.getDir(), newResourceName));
123     }
124 }
125