Debian Codegraph @

src/test/java/com/github/markusbernhardt/proxy/selector/pac/JavaxPacScriptParserTest.java

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.selector.pac;
2
3 import java.io.File;
4 import java.net.MalformedURLException;
5 import java.util.Calendar;
6
7 import org.junit.AfterClass;
8 import org.junit.Assert;
9 import org.junit.BeforeClass;
10 import org.junit.Ignore;
11 import org.junit.Test;
12
13 import com.github.markusbernhardt.proxy.TestUtil;
14 import com.github.markusbernhardt.proxy.selector.pac.JavaxPacScriptParser;
15 import com.github.markusbernhardt.proxy.selector.pac.PacScriptParser;
16 import com.github.markusbernhardt.proxy.selector.pac.UrlPacScriptSource;
17 import com.github.markusbernhardt.proxy.util.ProxyException;
18
19 /*****************************************************************************
20 * Tests for the javax.script PAC script parser.
21 *
22 * @author Markus Bernhardt, Copyright 2016
23 * @author Bernd Rosstauscher, Copyright 2009
24 ****************************************************************************/
25
26 public class JavaxPacScriptParserTest {
27
28 /*************************************************************************
29 * Set calendar for date and time base tests. Current date for all tests is:
30 * 15. December 1994 12:00.00 its a Thursday
31 ************************************************************************/
32 @BeforeClass
33 public static void setup() {
34 Calendar cal = Calendar.getInstance();
35 cal.set(Calendar.YEAR, 1994);
36 cal.set(Calendar.MONTH, Calendar.DECEMBER);
37 cal.set(Calendar.DAY_OF_MONTH, 15);
38 cal.set(Calendar.HOUR_OF_DAY, 12);
39 cal.set(Calendar.MINUTE, 00);
40 cal.set(Calendar.SECOND, 00);
41 cal.set(Calendar.MILLISECOND, 00);
42
43 // TODO Rossi 26.08.2010 need to fake time
44 // JavaxPacScriptParser.setCurrentTime(cal);
45 }
46
47 /*************************************************************************
48 * Cleanup after the tests.
49 ************************************************************************/
50 @AfterClass
51 public static void teadDown() {
52 // JavaxPacScriptParser.setCurrentTime(null);
53 }
54
55 /*************************************************************************
56 * Test method
57 *
58 * @throws ProxyException
59 * on proxy detection error.
60 * @throws MalformedURLException
61 * on URL erros
62 ************************************************************************/
63 @Test
64 public void testScriptExecution() throws ProxyException, MalformedURLException {
65 PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("test1.pac")));
66 p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
67 }
68
69 /*************************************************************************
70 * Test method
71 *
72 * @throws ProxyException
73 * on proxy detection error.
74 * @throws MalformedURLException
75 * on URL erros
76 ************************************************************************/
77 @Test
78 public void testCommentsInScript() throws ProxyException, MalformedURLException {
79 PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("test2.pac")));
80 p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
81 }
82
83 /*************************************************************************
84 * Test method
85 *
86 * @throws ProxyException
87 * on proxy detection error.
88 * @throws MalformedURLException
89 * on URL erros
90 ************************************************************************/
91 @Test
92 @Ignore // Test deactivated because it will not run in Java 1.5 and time
93 // based test are unstable
94 public void testScriptWeekDayScript() throws ProxyException, MalformedURLException {
95 PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("testWeekDay.pac")));
96 p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
97 }
98
99 /*************************************************************************
100 * Test method
101 *
102 * @throws ProxyException
103 * on proxy detection error.
104 * @throws MalformedURLException
105 * on URL erros
106 ************************************************************************/
107 @Test
108 @Ignore // Test deactivated because it will not run in Java 1.5 and time
109 // based test are unstable
110 public void testDateRangeScript() throws ProxyException, MalformedURLException {
111 PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("testDateRange.pac")));
112 p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
113 }
114
115 /*************************************************************************
116 * Test method
117 *
118 * @throws ProxyException
119 * on proxy detection error.
120 * @throws MalformedURLException
121 * on URL erros
122 ************************************************************************/
123 @Test
124 @Ignore // Test deactivated because it will not run in Java 1.5 and time
125 // based test are unstable
126 public void testTimeRangeScript() throws ProxyException, MalformedURLException {
127 PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("testTimeRange.pac")));
128 p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
129 }
130
131 /*************************************************************************
132 * Test method
133 *
134 * @throws ProxyException
135 * on proxy detection error.
136 * @throws MalformedURLException
137 * on URL erros
138 ************************************************************************/
139 @Test
140 public void methodsShouldReturnJsStrings() throws ProxyException, MalformedURLException {
141 PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("testReturnTypes.pac")));
142 String actual = p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
143 Assert.assertEquals("number boolean string", actual);
144 }
145
146 /*************************************************************************
147 * Helper method to build the url to the given test file
148 *
149 * @param testFile
150 * the name of the test file.
151 * @return the URL.
152 * @throws MalformedURLException
153 ************************************************************************/
154
155 private String toUrl(String testFile) throws MalformedURLException {
156 return new File(TestUtil.TEST_DATA_FOLDER + "pac", testFile).toURI().toURL().toString();
157 }
158
159 }