summaryrefslogtreecommitdiffstats
path: root/src/test/java/com/btr/proxy/selector/pac/JavaxPacScriptParserTest.java
blob: ea963f0383a8b06fe174ac4c24c5ef59e9efa14f (plain) (blame)
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
package com.btr.proxy.selector.pac;

import java.net.MalformedURLException;
import java.util.Calendar;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import com.btr.proxy.TestUtil;
import com.btr.proxy.util.ProxyException;

/*****************************************************************************
 * Tests for the javax.script PAC script parser. 
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class JavaxPacScriptParserTest {
	
	/*************************************************************************
	 * Set calendar for date and time base tests.
	 * Current date for all tests  is: 15. December 1994 12:00.00 
	 * its a Thursday 
	 ************************************************************************/
	@BeforeClass
	public static void setup() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, 1994);
		cal.set(Calendar.MONTH, Calendar.DECEMBER);
		cal.set(Calendar.DAY_OF_MONTH, 15);
		cal.set(Calendar.HOUR_OF_DAY, 12);
		cal.set(Calendar.MINUTE, 00);
		cal.set(Calendar.SECOND, 00);
		cal.set(Calendar.MILLISECOND, 00);
		
		// TODO Rossi 26.08.2010 need to fake time 
		// JavaxPacScriptParser.setCurrentTime(cal);
	}

	/*************************************************************************
	 * Cleanup after the tests.
	 ************************************************************************/
	@AfterClass
	public static void teadDown() {
		//JavaxPacScriptParser.setCurrentTime(null);
	}
	
	/*************************************************************************
	 * Test method
	 * @throws ProxyException on proxy detection error.
	 * @throws MalformedURLException on URL erros 
	 ************************************************************************/
	@Test
	public void testScriptExecution() throws ProxyException, MalformedURLException {
		PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("test1.pac")));
		p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
	}

	/*************************************************************************
	 * Test method
	 * @throws ProxyException on proxy detection error.
	 * @throws MalformedURLException on URL erros 
	 ************************************************************************/
	@Test
	public void testCommentsInScript() throws ProxyException, MalformedURLException {
		PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("test2.pac")));
		p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
	}
	
	/*************************************************************************
	 * Test method
	 * @throws ProxyException on proxy detection error.
	 * @throws MalformedURLException on URL erros 
	 ************************************************************************/
	@Test
	@Ignore //Test deactivated because it will not run in Java 1.5 and time based test are unstable 
	public void testScriptWeekDayScript() throws ProxyException, MalformedURLException {
		PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("testWeekDay.pac")));
		p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
	}

	/*************************************************************************
	 * Test method
	 * @throws ProxyException on proxy detection error.
	 * @throws MalformedURLException on URL erros 
	 ************************************************************************/
	@Test
	@Ignore //Test deactivated because it will not run in Java 1.5 and time based test are unstable 
	public void testDateRangeScript() throws ProxyException, MalformedURLException {
		PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("testDateRange.pac")));
		p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
	}

	/*************************************************************************
	 * Test method
	 * @throws ProxyException on proxy detection error.
	 * @throws MalformedURLException on URL erros 
	 ************************************************************************/
	@Test
	@Ignore //Test deactivated because it will not run in Java 1.5 and time based test are unstable 
	public void testTimeRangeScript() throws ProxyException, MalformedURLException {
		PacScriptParser p = new JavaxPacScriptParser(new UrlPacScriptSource(toUrl("testTimeRange.pac")));
		p.evaluate(TestUtil.HTTP_TEST_URI.toString(), "host1.unit-test.invalid");
	}
	
	/*************************************************************************
	 * Helper method to build the url to the given test file
	 * @param testFile the name of the test file.
	 * @return the URL. 
	 * @throws MalformedURLException
	 ************************************************************************/
	
	private String toUrl(String testFile) throws MalformedURLException {
        return JavaxPacScriptParserTest.class.getResource("/" + TestUtil.TEST_DATA_FOLDER + "/pac/" + testFile).toString();
	}
		
}