aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test-extensions-tests/net/sourceforge/jnlp/ResourcesTest.java
blob: 8d4e8d6d1924ec8265d640a8f2ecd2119be557e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* ResourcesTest.java
Copyright (C) 2011-2012 Red Hat, Inc.

This file is part of IcedTea.

IcedTea is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 2.

IcedTea is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with IcedTea; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
 */
package net.sourceforge.jnlp;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import net.sourceforge.jnlp.browsertesting.Browser;
import net.sourceforge.jnlp.browsertesting.BrowserFactory;
import net.sourceforge.jnlp.browsertesting.BrowserTest;
import net.sourceforge.jnlp.browsertesting.Browsers;
import net.sourceforge.jnlp.annotations.NeedsDisplay;
import net.sourceforge.jnlp.annotations.TestInBrowsers;
import net.sourceforge.jnlp.browsertesting.browsers.LinuxBrowser;
import net.sourceforge.jnlp.annotations.TestInBrowsers;
import org.junit.Assert;

import org.junit.Test;

public class ResourcesTest extends  BrowserTest{


    @Test
    @NeedsDisplay
    public void testNonExisitngBrowserWillNotDeadlock() throws Exception {
        server.setCurrentBrowser(Browsers.none);
        ServerAccess.ProcessResult pr = server.executeBrowser("not_existing_url.html");
        Assert.assertNull(pr.process);
        Assert.assertEquals(pr.stderr, "");
        Assert.assertEquals(pr.stdout, "");
        Assert.assertTrue(pr.wasTerminated);
        Assert.assertTrue(pr.returnValue < 0);
        junit.framework.Assert.assertNotNull(pr.deadlyException);
    }

    @Test
    public void testUnexistingProcessWillFailRecognizedly() throws Exception {
        server.setCurrentBrowser(Browsers.none);
        List<String> al=Arrays.asList(new String[] {"definietly_not_Existing_process"});
        ServerAccess.ProcessResult pr = server.executeProcess(al);
        Assert.assertNull(pr.process);
        Assert.assertEquals(pr.stderr, "");
        Assert.assertEquals(pr.stdout, "");
        Assert.assertTrue(pr.wasTerminated);
        Assert.assertTrue(pr.returnValue < 0);
        junit.framework.Assert.assertNotNull(pr.deadlyException);
    }

    @Test
    public void testGetUrlUponThisInstance() throws MalformedURLException{
        URL u1=server.getUrlUponThisInstance("simple.jsp");
        URL u2=server.getUrlUponThisInstance("/simple.jsp");
        Assert.assertEquals(u1, u2);
    }

    @Test
    @TestInBrowsers(testIn=Browsers.none)
    public void testNonExisitngBrowserWillNotCauseMess() throws Exception {
        ServerAccess.ProcessResult pr = server.executeBrowser("not_existing_url.html");
        Assert.assertNull(pr.process);
        Assert.assertEquals(pr.stderr, "");
        Assert.assertEquals(pr.stdout, "");
        Assert.assertTrue(pr.wasTerminated);
        Assert.assertTrue(pr.returnValue < 0);
        junit.framework.Assert.assertNotNull(pr.deadlyException);
    }

    @Test
    public void testBrowsers2() throws Exception {
        List<Browser> a = BrowserFactory.getFactory().getAllBrowsers();
        Assert.assertNotNull("returned browsers array must not be null", a);
        Assert.assertTrue("at least one browser must be configured", a.size() > 0);
        for (Browser b : a) {
            testBrowser(b);
        }

    }

    @Test
    @TestInBrowsers(testIn = Browsers.all)
    public void testBrowser3() throws Exception {
        testBrowser(server.getCurrentBrowser());
    }

    @Test
    public void testBrowsers1() throws Exception {
        BrowserFactory bf = new BrowserFactory(null);
        int expected = 0;
        Assert.assertTrue("Created from null there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        bf = new BrowserFactory("");
        expected = 0;
        Assert.assertTrue("Created from empty there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        String s = "dsgrdg";
        bf = new BrowserFactory(s);
        expected = 0;
        Assert.assertTrue("Created from nonsense " + s + " there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = "sgrg/jkik";
        bf = new BrowserFactory(s);
        expected = 0;
        Assert.assertTrue("Created from nonsense " + s + " there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = Browsers.firefox + "/jkik";
        bf = new BrowserFactory(s);
        expected = 0;
        Assert.assertTrue("Created from nonsense " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = "sgrg/jkik:sege";
        bf = new BrowserFactory(s);
        expected = 0;
        Assert.assertTrue("Created from  two nonsenses " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = Browsers.firefox.toExec() + ":" + Browsers.firefox;
        bf = new BrowserFactory(s);
        expected = 2;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = Browsers.firefox.toExec();
        bf = new BrowserFactory(s);
        expected = 1;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = "something/somewhere/" + Browsers.firefox.toExec();
        bf = new BrowserFactory(s);
        expected = 1;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = "something/somewhere/" + Browsers.firefox.toExec() + ":" + "something/somewhere/" + Browsers.opera.toExec();
        bf = new BrowserFactory(s);
        expected = 2;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = "something/somewhere/" + Browsers.firefox.toExec() + ":" + "something/somewhere/" + Browsers.opera.toExec() + ":" + Browsers.chromiumBrowser;
        bf = new BrowserFactory(s);
        expected = 3;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = Browsers.firefox.toExec() + ":" + "vfdgf" + ":" + Browsers.googleChrome.toExec();
        bf = new BrowserFactory(s);
        expected = 2;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = Browsers.firefox.toExec() + ":" + Browsers.chromiumBrowser + ":" + Browsers.googleChrome.toExec() + ":" + Browsers.opera + ":" + Browsers.epiphany + ":" + Browsers.midori;
        bf = new BrowserFactory(s);
        expected = 6;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);
        testFullFactory(bf);

        s = "fgfd/" + Browsers.firefox.toExec() + ":" + "/fgfd/" + Browsers.chromiumBrowser + ":" + "fgfd/dfsdf/" + Browsers.googleChrome.toExec() + ":" + "/g/fgfd/" + Browsers.opera + ":" + Browsers.epiphany + ":" + Browsers.midori;
        bf = new BrowserFactory(s);
        expected = 6;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);
        testFullFactory(bf);

        s = Browsers.firefox.toExec() + ":" + ":" + Browsers.googleChrome.toExec() + ":" + Browsers.opera;
        bf = new BrowserFactory(s);
        expected = 3;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = Browsers.firefox.toExec() + ":" + ":" + ":" + Browsers.opera;
        bf = new BrowserFactory(s);
        expected = 2;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = ":" + ":" + Browsers.googleChrome.toExec() + ":";
        bf = new BrowserFactory(s);
        expected = 1;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);

        s = ":" + Browsers.firefox.toExec() + ":bfgbfg/fddf/" + Browsers.googleChrome.toExec() + ":";
        bf = new BrowserFactory(s);
        expected = 2;
        Assert.assertTrue("Created from  " + s + "there must be " + expected + " browsers in factory. Is" + bf.getAllBrowsers().size(), bf.getAllBrowsers().size() == expected);
    }

    @Test
    public void testResourcesExists() throws Exception {
        File[] simpleContent = server.getDir().listFiles(new FileFilter() {

            public boolean accept(File file) {
                if (!file.isDirectory()) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        Assert.assertNotNull(simpleContent);
        Assert.assertTrue(simpleContent.length > 5);

        for (int i = 0; i < simpleContent.length; i++) {
            File file = simpleContent[i];
            ServerAccess.logOutputReprint(file.getName());
            //server port have in fact no usage in converting filename to uri-like-filename.
            //But if there is null, instead if some number, then nullpointer exception is thrown (Integer->int).
            //So I'm using "real" currently used port, instead of some random value.
            URI u = new URI((String) null, (String) null, (String) null, server.getPort(), file.getName(), (String) null, null);
            ServerAccess.logOutputReprint(" ("+u.toString()+")");
            String fname = u.toString();
            if (file.getName().toLowerCase().endsWith(".jnlp")) {
                String c = server.getResourceAsString("/" + fname);
                Assert.assertTrue(c.contains("<"));
                Assert.assertTrue(c.contains(">"));
                Assert.assertTrue(c.contains("jnlp"));
                Assert.assertTrue(c.contains("resources"));
                Assert.assertTrue(c.replaceAll("\\s*", "").contains("</jnlp>"));

            } else {
                byte[] c = server.getResourceAsBytes("/" + fname).toByteArray();
                Assert.assertEquals(c.length, file.length());
            }

        }

    }

    @Test
    @NeedsDisplay
    @TestInBrowsers(testIn = Browsers.one)
    public void testListeners() throws Exception {
        final StringBuilder o1 = new StringBuilder();
        final StringBuilder e1 = new StringBuilder();
        final StringBuilder o2 = new StringBuilder();
        final StringBuilder e2 = new StringBuilder();
        final ContentReaderListener lo = new ContentReaderListener() {

            @Override
            public void charReaded(char ch) {
                o1.append(ch);
            }

            @Override
            public void lineReaded(String s) {
                o2.append(s).append("\n");
            }
        };
        ContentReaderListener le = new ContentReaderListener() {

            @Override
            public void charReaded(char ch) {
                e1.append(ch);
            }

            @Override
            public void lineReaded(String s) {
                e2.append(s).append("\n");
            }
        };
       ServerAccess.ProcessResult pr = server.executeBrowser("not_existing_url.html",lo,le);
       server.setCurrentBrowser(BrowserFactory.getFactory().getFirst().getID());
       Assert.assertNotNull(server.getCurrentBrowsers());
       Assert.assertNotNull(server.getCurrentBrowser());
       Assert.assertEquals(pr.stdout, o1.toString());
       Assert.assertEquals(pr.stderr, e1.toString());
       //the last \n is mandatory as las tline is flushed also when proces dies
       Assert.assertEquals(pr.stdout.replace("\n", ""), o2.toString().replace("\n", ""));
       Assert.assertEquals(pr.stderr.replace("\n", ""), e2.toString().replace("\n", ""));
    }

    private void testFullFactory(BrowserFactory bf) {
        Assert.assertEquals(bf.getBrowser(Browsers.chromiumBrowser).getID(), Browsers.chromiumBrowser);
        Assert.assertEquals(bf.getBrowser(Browsers.googleChrome).getID(), Browsers.googleChrome);
        Assert.assertEquals(bf.getBrowser(Browsers.firefox).getID(), Browsers.firefox);
        Assert.assertEquals(bf.getBrowser(Browsers.opera).getID(), Browsers.opera);
        Assert.assertEquals(bf.getBrowser(Browsers.epiphany).getID(), Browsers.epiphany);
        Assert.assertEquals(bf.getBrowser(Browsers.midori).getID(), Browsers.midori);
    }

    private void testBrowser(Browser browser) throws IOException {
        File defaultPluginDir = null;
        if (browser.getDefaultPluginExpectedLocation() != null) {
            defaultPluginDir = new File(browser.getDefaultPluginExpectedLocation());
        }
        if (defaultPluginDir != null) {
            Assert.assertTrue("browser's plugins  location should exist " + defaultPluginDir.toString() + " for " + browser.getID().toString(), defaultPluginDir.exists());
        }

        File userPluginDir = null;
        if (browser.getUserDefaultPluginExpectedLocation() != null) {
            userPluginDir = new File(browser.getUserDefaultPluginExpectedLocation());
        }
        // userPluginDir (~/.mozilla/plugins/) may not exist if user has not installed any Firefox plugins.

        File[] defaultPlugins = new File[0];
        if (defaultPluginDir != null && defaultPluginDir.isDirectory()) {
            defaultPlugins = defaultPluginDir.listFiles();
        }

        File[] userPlugins = new File[0];
        if (userPluginDir != null && userPluginDir.isDirectory()) {
            userPlugins = userPluginDir.listFiles();
        }

        Assert.assertTrue("at least one of browser's plugins  directory should contains at least one file didn't. For " + browser.getID().toString(), defaultPlugins.length + userPlugins.length > 0);

        defaultPlugins = new File[0];
        if (defaultPluginDir != null && defaultPluginDir.isDirectory()) {
            defaultPlugins = defaultPluginDir.listFiles(new FileFilter() {

                @Override
                public boolean accept(File pathname) {
                    return (pathname.getName().equals(LinuxBrowser.DEFAULT_PLUGIN_NAME));
                }
            });
        }

        userPlugins = new File[0];
        if (userPluginDir != null && userPluginDir.isDirectory()) {
            userPlugins = userPluginDir.listFiles(new FileFilter() {

                @Override
                public boolean accept(File pathname) {
                    return (pathname.getName().equals(LinuxBrowser.DEFAULT_PLUGIN_NAME));
                }
            });
        }

        Assert.assertTrue("browser's plugins  directories should contains exactly one  " + LinuxBrowser.DEFAULT_PLUGIN_NAME + ", but didnt for " + browser.getID().toString(),
                defaultPlugins.length + userPlugins.length == 1);
        String currentPath = server.getJavawsFile().getParentFile().getParentFile().getAbsolutePath();

        File[] plugins;
        if (defaultPlugins.length == 1) {
            plugins = defaultPlugins;
        } else {
            plugins = userPlugins;
        }
        String s = ServerAccess.getContentOfStream(new FileInputStream(plugins[0]), "ASCII");
        Assert.assertTrue("browser's plugins  shoud points to" + currentPath + ", but  didnt",
                s.contains(s));
    }
}