PageRenderTime 110ms CodeModel.GetById 97ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/jain-mgcp/stack/src/test/java/org/mobicents/protocols/mgcp/stack/test/TestHarness.java

http://mobicents.googlecode.com/
Java | 159 lines | 88 code | 45 blank | 26 comment | 7 complexity | 4c8987d5ad1072e9d0bdac480f9e8111 MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.mgcp.stack.test;
 24
 25import java.io.FileNotFoundException;
 26import java.io.IOException;
 27import java.io.InputStream;
 28import java.util.Enumeration;
 29import java.util.Properties;
 30
 31import junit.framework.TestCase;
 32import junit.framework.TestResult;
 33
 34import org.apache.log4j.BasicConfigurator;
 35import org.apache.log4j.FileAppender;
 36import org.apache.log4j.Logger;
 37import org.apache.log4j.PropertyConfigurator;
 38import org.apache.log4j.SimpleLayout;
 39
 40public class TestHarness extends TestCase {
 41
 42	protected static final String ABORT_ON_FAIL = "org.mobicents.mgcp.stack.test.ABORT_ON_FAIL";
 43
 44	protected static final String LOG_FILE_NAME = "org.mobicents.mgcp.stack.test.LOG_FILE";
 45
 46	protected static final String LOCAL_ADDRESS = "127.0.0.1";
 47
 48	protected static final int CA_PORT = 2724;
 49
 50	protected static final int MGW_PORT = 2727;
 51
 52	protected static String logFileName = "mgcplog.txt";
 53
 54	protected static boolean abortOnFail = true;
 55
 56	private static boolean testPassed = true;
 57
 58	protected static int testCounter;
 59
 60	private static Logger logger = Logger.getLogger("mgcp.test");
 61
 62	private static String currentMethodName;
 63
 64	private static String currentClassName;
 65
 66	protected TestResult testResult;
 67
 68	// protected static Appender console = new ConsoleAppender(new
 69	// SimpleLayout());
 70
 71	public void init() {
 72		try {
 73			Properties tckProperties = new Properties();
 74
 75			InputStream is = getClass().getResourceAsStream("/test.properties");
 76			System.out.println("Input Stream = " + is);
 77
 78			tckProperties.load(is);
 79
 80			Enumeration props = tckProperties.propertyNames();
 81			while (props.hasMoreElements()) {
 82				String propname = (String) props.nextElement();
 83				System.setProperty(propname, tckProperties.getProperty(propname));
 84			}
 85
 86			String flag = System.getProperties().getProperty(ABORT_ON_FAIL);
 87
 88			String lf = System.getProperties().getProperty(LOG_FILE_NAME);
 89			if (lf != null)
 90				logFileName = lf;
 91			abortOnFail = (flag != null && flag.equalsIgnoreCase("true"));
 92
 93			// JvB: init log4j
 94			// PropertyConfigurator.configure("log4j.properties");
 95
 96			InputStream inStreamLog4j = getClass().getResourceAsStream("/log4j.properties");
 97			Properties propertiesLog4j = new Properties();
 98			try {
 99				propertiesLog4j.load(inStreamLog4j);
100				PropertyConfigurator.configure(propertiesLog4j);
101			} catch (IOException e) {
102				e.printStackTrace();
103				BasicConfigurator.configure();
104			}
105
106			logger.debug("log4j configured");
107
108			// If already created a print writer then just use it.
109			try{
110			if (lf != null)
111				logger.addAppender(new FileAppender(new SimpleLayout(), logFileName));
112			else
113				logger.addAppender(new FileAppender(new SimpleLayout(), "testoutput.txt"));
114			} catch(FileNotFoundException fnfe){
115				
116			}
117		} catch (Exception ex) {
118			throw new RuntimeException(ex);
119		}
120
121	}
122
123	protected TestHarness() {
124		init();
125
126	}
127
128	public TestHarness(String name) {
129		this(name, false); // default: disable auto-dialog
130	}
131
132	protected TestHarness(String name, boolean autoDialog) {
133		super(name);
134		this.testResult = new TestResult();
135		init();
136	}
137
138
139
140	public void logTestCompleted() {
141
142		logger.info(this.getName() + " Completed");
143
144	}
145
146	public void logTestCompleted(String info) {
147		logger.info(this.getName() + ":" + info + " Completed");
148
149	}
150
151	public void setUp() throws Exception {
152		testPassed = true;
153	}
154
155	public void tearDown() throws Exception {
156		assertTrue("Test failed. See log for details.", testPassed);
157	}
158
159}