/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. package org.mobicents.protocols.mgcp.stack.test;
  23. import java.io.FileNotFoundException;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.util.Enumeration;
  27. import java.util.Properties;
  28. import junit.framework.TestCase;
  29. import junit.framework.TestResult;
  30. import org.apache.log4j.BasicConfigurator;
  31. import org.apache.log4j.FileAppender;
  32. import org.apache.log4j.Logger;
  33. import org.apache.log4j.PropertyConfigurator;
  34. import org.apache.log4j.SimpleLayout;
  35. public class TestHarness extends TestCase {
  36. protected static final String ABORT_ON_FAIL = "org.mobicents.mgcp.stack.test.ABORT_ON_FAIL";
  37. protected static final String LOG_FILE_NAME = "org.mobicents.mgcp.stack.test.LOG_FILE";
  38. protected static final String LOCAL_ADDRESS = "127.0.0.1";
  39. protected static final int CA_PORT = 2724;
  40. protected static final int MGW_PORT = 2727;
  41. protected static String logFileName = "mgcplog.txt";
  42. protected static boolean abortOnFail = true;
  43. private static boolean testPassed = true;
  44. protected static int testCounter;
  45. private static Logger logger = Logger.getLogger("mgcp.test");
  46. private static String currentMethodName;
  47. private static String currentClassName;
  48. protected TestResult testResult;
  49. // protected static Appender console = new ConsoleAppender(new
  50. // SimpleLayout());
  51. public void init() {
  52. try {
  53. Properties tckProperties = new Properties();
  54. InputStream is = getClass().getResourceAsStream("/test.properties");
  55. System.out.println("Input Stream = " + is);
  56. tckProperties.load(is);
  57. Enumeration props = tckProperties.propertyNames();
  58. while (props.hasMoreElements()) {
  59. String propname = (String) props.nextElement();
  60. System.setProperty(propname, tckProperties.getProperty(propname));
  61. }
  62. String flag = System.getProperties().getProperty(ABORT_ON_FAIL);
  63. String lf = System.getProperties().getProperty(LOG_FILE_NAME);
  64. if (lf != null)
  65. logFileName = lf;
  66. abortOnFail = (flag != null && flag.equalsIgnoreCase("true"));
  67. // JvB: init log4j
  68. // PropertyConfigurator.configure("log4j.properties");
  69. InputStream inStreamLog4j = getClass().getResourceAsStream("/log4j.properties");
  70. Properties propertiesLog4j = new Properties();
  71. try {
  72. propertiesLog4j.load(inStreamLog4j);
  73. PropertyConfigurator.configure(propertiesLog4j);
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. BasicConfigurator.configure();
  77. }
  78. logger.debug("log4j configured");
  79. // If already created a print writer then just use it.
  80. try{
  81. if (lf != null)
  82. logger.addAppender(new FileAppender(new SimpleLayout(), logFileName));
  83. else
  84. logger.addAppender(new FileAppender(new SimpleLayout(), "testoutput.txt"));
  85. } catch(FileNotFoundException fnfe){
  86. }
  87. } catch (Exception ex) {
  88. throw new RuntimeException(ex);
  89. }
  90. }
  91. protected TestHarness() {
  92. init();
  93. }
  94. public TestHarness(String name) {
  95. this(name, false); // default: disable auto-dialog
  96. }
  97. protected TestHarness(String name, boolean autoDialog) {
  98. super(name);
  99. this.testResult = new TestResult();
  100. init();
  101. }
  102. public void logTestCompleted() {
  103. logger.info(this.getName() + " Completed");
  104. }
  105. public void logTestCompleted(String info) {
  106. logger.info(this.getName() + ":" + info + " Completed");
  107. }
  108. public void setUp() throws Exception {
  109. testPassed = true;
  110. }
  111. public void tearDown() throws Exception {
  112. assertTrue("Test failed. See log for details.", testPassed);
  113. }
  114. }