1 /* ParallelAppletsTest.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 net.sourceforge.jnlp.ProcessResult;
39 import net.sourceforge.jnlp.browsertesting.BrowserTest;
40 import net.sourceforge.jnlp.browsertesting.Browsers;
41 import net.sourceforge.jnlp.annotations.NeedsDisplay;
42 import net.sourceforge.jnlp.annotations.TestInBrowsers;
43 import org.junit.Assert;
44 
45 import org.junit.Test;
46 
47 public class ParallelAppletsTest extends BrowserTest {
48 
49     @Test
50     @TestInBrowsers(testIn = {Browsers.one})
51     @NeedsDisplay
testParallelAppletsTest1Ex2s()52     public void testParallelAppletsTest1Ex2s() throws Exception {
53         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1EE_x_2s.html");
54         checkSimpleSignedStarted(pr);
55         checkNotInitialised(pr);
56     }
57 
58     @Test
59     @TestInBrowsers(testIn = {Browsers.one})
60     @NeedsDisplay
testParallelAppletsTest1x2E()61     public void testParallelAppletsTest1x2E() throws Exception {
62         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1_x_2EE.html");
63         checkExactCounts(1, 10, pr);
64         checkNotInitialised(pr);
65 
66     }
67 
68     @Test
69     @TestInBrowsers(testIn = {Browsers.one})
70     @NeedsDisplay
testParallelAppletsTest1x2e()71     public void testParallelAppletsTest1x2e() throws Exception {
72         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1_x_2e.html");
73         checkExactCounts(1, 10, pr);
74         checkException(pr);
75     }
76 
77     @Test
78     @TestInBrowsers(testIn = {Browsers.one})
79     @NeedsDisplay
testParallelAppletsTest1ex2s()80     public void testParallelAppletsTest1ex2s() throws Exception {
81         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1e_x_2s.html");
82         checkSimpleSignedStarted(pr);
83         checkException(pr);
84     }
85 
86     @Test
87     @TestInBrowsers(testIn = {Browsers.one})
88     @NeedsDisplay
testParallelAppletsTest1sx2()89     public void testParallelAppletsTest1sx2() throws Exception {
90         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1s_x_2.html");
91         checkAppletStarted(pr);
92         checkSimpleSignedStarted(pr);
93     }
94 
95     @Test
96     @TestInBrowsers(testIn = {Browsers.one})
97     @NeedsDisplay
testParallelAppletsTest1sx2s()98     public void testParallelAppletsTest1sx2s() throws Exception {
99         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1s_x_2s.html");
100         int found=countCounts(SimpleSignedStarted, pr.stdout);
101         assertExactCount(SimpleSignedStarted, 2, found);
102 
103 
104     }
105 
106     @Test
107     @TestInBrowsers(testIn = {Browsers.one})
108     @NeedsDisplay
testParallelAppletsTest1sx2ssk()109     public void testParallelAppletsTest1sx2ssk() throws Exception {
110         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1s_x_2ss.html");
111         checkSimpleSigned2Started(pr);
112         checkSimpleSignedStarted(pr);
113     }
114 
115     @Test
116     @TestInBrowsers(testIn = {Browsers.one})
117     @NeedsDisplay
testParallelAppletsTest1x2sk()118     public void testParallelAppletsTest1x2sk() throws Exception {
119         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1_x_2sk.html");
120         checkExitNotAllowed(pr);
121         checkAtLeastCounts(1, 10, pr);
122         checkExactCounts(2, 5, pr);
123 
124     }
125 
126     @Test
127     @TestInBrowsers(testIn = {Browsers.one})
128     @NeedsDisplay
testParallelAppletsTest1kx2()129     public void testParallelAppletsTest1kx2() throws Exception {
130         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1k_x_2.html");
131         checkExitNotAllowed(pr);
132         checkAtLeastCounts(1, 10, pr);
133         checkExactCounts(2, 5, pr);
134 
135     }
136 
137     @Test
138     @TestInBrowsers(testIn = {Browsers.one})
139     @NeedsDisplay
testParallelAppletsTest1x2()140     public void testParallelAppletsTest1x2() throws Exception {
141         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1_x_2.html");
142         checkExactCounts(2, 10, pr);
143     }
144 
145     @Test
146     @TestInBrowsers(testIn = {Browsers.one})
147     @NeedsDisplay
testParallelAppletsTest1x1()148     public void testParallelAppletsTest1x1() throws Exception {
149         ProcessResult pr = server.executeBrowser("ParallelAppletsTest_1_x_1.html");
150         checkExactCounts(2, 10, pr);
151     }
152     private static final String ACE = "java.security.AccessControlException";
153     private static final String Sexit = "System.exit()";
154     private static final String LE1 = "net.sourceforge.jnlp.LaunchException";
155     private static final String LE2 = "Fatal: Initialization Error";
156     private static final String Cinit = "Could not initialize applet";
157     private static final String CountStub = "counting... ";
158     private static final String SimpleSignedStarted = "AppletTestSigned was started";
159     private static final String SimpleSigned2Started = "AppletTestSigned2 was started";
160     private static final String AppletStarted = "applet was started";
161     private static final String AppletThrowedException = "java.lang.RuntimeException: Correct exception";
162 
163 
164 
165 
checkExitNotAllowed(ProcessResult pr)166     private void checkExitNotAllowed(ProcessResult pr) {
167         Assert.assertTrue("Applets cant call " + Sexit, pr.stderr.matches("(?s).*" + ACE + ".*" + Sexit + ".*"));
168     }
169 
checkNotInitialised(ProcessResult pr)170     private void checkNotInitialised(ProcessResult pr) {
171         Assert.assertTrue("Applets should not be initialised ", pr.stderr.matches("(?s).*" + LE1 + ".*" + LE2 + ".*" + Cinit + ".*"));
172     }
173 
checkSimpleSignedStarted(ProcessResult pr)174     private void checkSimpleSignedStarted(ProcessResult pr) {
175         Assert.assertTrue("Applet's start should be confirmed by " + SimpleSignedStarted, pr.stdout.contains(SimpleSignedStarted));
176     }
checkSimpleSigned2Started(ProcessResult pr)177     private void checkSimpleSigned2Started(ProcessResult pr) {
178         Assert.assertTrue("Applet's start should be confirmed by " + SimpleSigned2Started, pr.stdout.contains(SimpleSigned2Started));
179     }
checkAppletStarted(ProcessResult pr)180     private void checkAppletStarted(ProcessResult pr) {
181         Assert.assertTrue("Applet's start should be confirmed by " + AppletStarted, pr.stdout.contains(AppletStarted));
182     }
checkException(ProcessResult pr)183       private void checkException(ProcessResult pr) {
184          Assert.assertTrue("Applet's exception should be confirmed by " + AppletThrowedException, pr.stderr.contains(AppletThrowedException));
185     }
186 
checkExactCounts(int howManyTimes, int countIdTill, ProcessResult pr)187     private void checkExactCounts(int howManyTimes, int countIdTill, ProcessResult pr) {
188         for (int i = 0; i <= countIdTill; i++) {
189             String countId = CountStub + i+"\n";
190             int found = countCounts(countId, pr.stdout);
191             assertExactCount(countId, howManyTimes, found);
192         }
193 
194     }
195 
assertExactCount(String what, int howManyTimes, int found)196     private void assertExactCount(String what, int howManyTimes, int found) {
197         Assert.assertEquals(what + " was expected exactly " + howManyTimes + " but was found " + found, howManyTimes, found);
198     }
199 
checkAtLeastCounts(int howManyTimes, int countIdTill, ProcessResult pr)200     private void checkAtLeastCounts(int howManyTimes, int countIdTill, ProcessResult pr) {
201         for (int i = 0; i <= countIdTill; i++) {
202             String countId = CountStub + i;
203             int found = countCounts(countId, pr.stdout);
204             Assert.assertTrue(countId + " was expected et least " + howManyTimes + " but was found " + found, found >= howManyTimes);
205         }
206 
207     }
208 
countCounts(String what, String where)209     private int countCounts(String what, String where) {
210         int lastIndex = 0;
211         int count = 0;
212 
213         while (lastIndex != -1) {
214 
215             lastIndex = where.indexOf(what, lastIndex);
216 
217             if (lastIndex != -1) {
218                 count++;
219                 lastIndex += what.length();
220             }
221         }
222         return count;
223 
224     }
225 
226 
227 }
228