aboutsummaryrefslogtreecommitdiff
path: root/src/batchtools/headless/src/test/java/com/galois/fiveui/CrawlParametersTest.java
blob: f5ffa3556a9ca2e97b9509c4bedaed4448779ff5 (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
package com.galois.fiveui;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.galois.fiveui.CrawlParameters;


public class CrawlParametersTest {

	private static Logger logger = Logger.getLogger("com.galois.fiveui.CrawlParameters");
	
	@BeforeClass
	public static void before() {
		//if (!Logger.getRootLogger().getAllAppenders().hasMoreElements())
		BasicConfigurator.configure();
		logger.info("running unit tests...");
	}
	
	@AfterClass
	public static void after() {
		LogManager.shutdown();
	}
	
	@Test
	public void testConstructorFields() {
		CrawlParameters c;
		try {
			c = new CrawlParameters("0 1 100 foo");
		} catch (Exception e) {
			Assert.assertTrue("failed to parse crawl type string", false);
			return;
		}
		Assert.assertArrayEquals(new int[]{0, 1, 100}, new int[]{c.depth, c.maxFetch, c.politeness});
		Assert.assertEquals("foo", c.match);
	}
	
	@Test
	public void testNone() {
		CrawlParameters c = CrawlParameters.none();
		Assert.assertTrue(c.isNone());
	}
	
	@Test
	public void testMatchFcn() {
		CrawlParameters c;
		try {
			c = new CrawlParameters("0 1 100 foo");
		} catch (Exception e) {
			Assert.assertTrue("failed to parse crawl type string", false);
			return;
		}
		Assert.assertTrue("failed to match foo", c.matchFcn.apply("foo"));
		Assert.assertFalse("failed to not match bar", c.matchFcn.apply("bar"));
	}
	
	@Test
	public void testMatchFcnGlob1() {
		CrawlParameters c;
		try {
			c = new CrawlParameters("0 1 100 foo*");
		} catch (Exception e) {
			Assert.assertTrue("failed to parse crawl type string", false);
			return;
		}
		Assert.assertTrue("failed to match foo", c.matchFcn.apply("foo"));
		Assert.assertFalse("failed to not match bar", c.matchFcn.apply("bar"));
		Assert.assertTrue("failed to match foobar", c.matchFcn.apply("foobar"));
		Assert.assertFalse("failed to not match barfoobar", c.matchFcn.apply("barfoobar"));
	}
	
	@Test
	public void testMatchFcnGlob2() {
		CrawlParameters c;
		try {
			c = new CrawlParameters("0 1 100 http://foo.com/*.html");
		} catch (Exception e) {
			Assert.assertTrue("failed to parse crawl type string", false);
			return;
		}
		Assert.assertTrue("failed to match http://foo.com/index.html", c.matchFcn.apply("http://foo.com/index.html"));
		Assert.assertTrue("failed to match http://foo.com/test/index.html", c.matchFcn.apply("http://foo.com/test/index.html"));
		Assert.assertFalse("failed to not match http://bar.com/index.html", c.matchFcn.apply("http://bar.com/index.html"));
	}
	
	@Test
	public void testMatchFcnGlob3() {
		CrawlParameters c;
		try {
			c = new CrawlParameters("0 1 100 *foo.com*");
		} catch (Exception e) {
			Assert.assertTrue("failed to parse crawl type string", false);
			return;
		}
		Assert.assertTrue("failed to match http://foo.com/index.html", c.matchFcn.apply("http://foo.com/index.html"));
		Assert.assertTrue("failed to match http://foo.com/test/index.html", c.matchFcn.apply("http://foo.com/test/index.html"));
		Assert.assertTrue("failed to match http://bar.foo.com", c.matchFcn.apply("http://bar.foo.com"));
		Assert.assertFalse("failed to not match http://foobar.com", c.matchFcn.apply("http://foobar.com"));
	}
	
}