aboutsummaryrefslogtreecommitdiff
path: root/src/batchtools/headless/src/test/java/com/galois/fiveui/BatchExecutorTest.java
blob: f7f593f492a54da320d5ef658018c5b1dbc268e8 (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
/**
 * Module     : BatchExecutorTest.java
 * Copyright  : (c) 2011-2012, Galois, Inc.
 *
 * Maintainer :
 * Stability  : Provisional
 * Portability: Portable
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.galois.fiveui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.BindException;

import junit.framework.Assert;

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

import com.google.common.collect.ImmutableList;


/**
 * @author bjones
 *
 */
public class BatchExecutorTest {

	private static final String RUN_DESCRIPTION_DIR = "src/test/resources/runDescriptions/";
	private static Logger logger = Logger.getLogger("com.galois.fiveui.BatchExecutorTest");
	private static NanoHTTPD httpServer;
	
	@BeforeClass
	public static void setupTests() {
		BasicConfigurator.configure();
        logger.setLevel(Level.DEBUG);
        Logger root = Logger.getRootLogger();
        root.setLevel(Level.ERROR);
	    // start up local web server for crawl tests
		File dir = new File(".");
		logger.info("Starting NanoHTTPD webserver in " + dir.getAbsolutePath() + " on port 8000 ...");
		try {
			httpServer = new NanoHTTPD(8000, dir);
		} catch (BindException e) {
			logger.debug("assuming that local web server is already running");
		} catch (IOException e1) {
			e1.printStackTrace();
			Assert.assertTrue("failed to start NanoHTTPD in current directory " + dir.getAbsolutePath(), false);
		}
	}
	
	@AfterClass
	public static void teardown() {
		LogManager.shutdown();
		httpServer.stop();
	}
	
	/**
	 * This unit test requires that a webserver be running locally on port 8000,
	 * what it serves does not matter.
	 * 
	 * @throws IOException 
	 * @throws FileNotFoundException
	 */
	@Test
	public void headlessRunTest0() throws IOException {
		String jsonFileName = RUN_DESCRIPTION_DIR + "headlessRunTest0.json";
		testHeadlessRun(jsonFileName);
	}
	
	/**
	 * This unit test requires Internet access to http://www.cnn.com
	 * 
	 * @throws IOException 
	 * @throws FileNotFoundException
	 */
	@Ignore
	public void headlessRunTestCNN() throws IOException {
		String jsonFileName = RUN_DESCRIPTION_DIR + "headlessRunTestCNN.json";
		testHeadlessRun(jsonFileName);
	}
	
	/**
	 * Helper method for headless run unit tests.
	 *  
	 * @param fn filename of a .json file containing a headless run description
	 */
	private static void testHeadlessRun(String fn) {
		boolean flag = true;
		try {
			HeadlessRunDescription descr = HeadlessRunDescription.parse(fn);
			BatchRunner runner = new BatchRunner();
			ImmutableList<Result> results = runner.runHeadless(descr);
			logger.info(results.toString());
		} catch (Exception e) {
			logger.error("testHeadlessRun: exception caught while running a headless run description");
			logger.error(e.toString());
			flag = false;
		} 
		Assert.assertTrue(flag);
	}

}