001/** 002 * Copyright (c) 2010, Regents of the University of Colorado 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions are met: 007 * 008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 011 * 012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 022 * POSSIBILITY OF SUCH DAMAGE. 023 */ 024package org.cleartk.test.util; 025 026import java.io.File; 027import java.util.Arrays; 028import java.util.HashSet; 029import java.util.Set; 030import java.util.logging.Logger; 031 032import org.apache.uima.fit.factory.JCasFactory; 033import org.apache.uima.jcas.JCas; 034import org.junit.Assume; 035import org.junit.Before; 036import org.junit.Rule; 037import org.junit.rules.TemporaryFolder; 038 039/** 040 * <br> 041 * Copyright (c) 2010, Regents of the University of Colorado <br> 042 * All rights reserved. 043 * 044 * @author Philip Ogren 045 */ 046 047public abstract class CleartkTestBase { 048 049 /** 050 * A {@link Logger} instance associated with the class, intended for use by subclasses. 051 */ 052 protected Logger logger; 053 054 /** 055 * The set of names for tests that should be disabled. 056 */ 057 private Set<String> skippedTestNames; 058 059 public CleartkTestBase() { 060 super(); 061 this.logger = Logger.getLogger(this.getClass().getName()); 062 this.skippedTestNames = new HashSet<String>(); 063 String skipTests = System.getProperty(SKIP_TESTS_PROPERTY); 064 if (skipTests != null) { 065 this.skippedTestNames.addAll(Arrays.asList(skipTests.split("\\s*[,]\\s*"))); 066 } 067 } 068 069 /** 070 * The name of the property that can be set to skip some tests. The property value will be parsed 071 * as a comma-separated list of values, where each value can designate one type of test to skip, 072 * e.g. {@value #LONG_TESTS_PROPERTY_VALUE} or {@value #BIG_MEMORY_TESTS_PROPERTY_VALUE}. Example 073 * usage: <br/> 074 * <code>-Dcleartk.skipTests=long,bigMem</code> 075 */ 076 public static final String SKIP_TESTS_PROPERTY = "cleartk.skipTests"; 077 078 /** 079 * Value for the {@link #SKIP_TESTS_PROPERTY} property that indicates that commonly skipped tests 080 * should be disabled. Currently, this will disable at least {@value #LONG_TESTS_PROPERTY_VALUE} 081 * and {@value #BIG_MEMORY_TESTS_PROPERTY_VALUE} tests. It will also typically disable tests that 082 * require binaries that are hard to build (e.g. tk-svmlight, crfsuite). Current value: 083 * {@value #COMMON_TESTS_PROPERTY_VALUE}. 084 */ 085 public static final String COMMON_TESTS_PROPERTY_VALUE = "common"; 086 087 /** 088 * Value for the {@link #SKIP_TESTS_PROPERTY} property that indicates that all tests that can be 089 * skipped are disabled. Current value: {@value #ALL_TESTS_PROPERTY_VALUE}. 090 */ 091 public static final String ALL_TESTS_PROPERTY_VALUE = "all"; 092 093 /** 094 * Subclasses should call this method at the beginning of a test to check whether a specific type 095 * of test is enabled by checking whether the name of that test is contained in the 096 * {@value #SKIP_TESTS_PROPERTY} property. If the name is present, then the test will be skipped. 097 * 098 * Typically, immediately after calling {@link #assumeTestsEnabled}, a test should log a message 099 * created by {@link #createTestEnabledMessage} to explain to a user how to disable the test if 100 * they wish. 101 * 102 * Note that if {@value #SKIP_TESTS_PROPERTY} contains {@value #ALL_TESTS_PROPERTY_VALUE} then the 103 * test will be skipped even if {@link #ALL_TESTS_PROPERTY_VALUE} was not passed as an argument to 104 * this method. 105 */ 106 protected void assumeTestsEnabled(String... testNames) { 107 Assume.assumeTrue(!this.skippedTestNames.contains(ALL_TESTS_PROPERTY_VALUE)); 108 for (String testName : testNames) { 109 Assume.assumeTrue(!this.skippedTestNames.contains(testName)); 110 } 111 } 112 113 /** 114 * Subclasses should call this method to create a message that explains how to disable a test. 115 * Typically such a message should be logged immediately after calling 116 * {@link #assumeTestsEnabled(String...)}. 117 * 118 * @param testName 119 * The name of the test (i.e. the value that should be present in the 120 * {@value #SKIP_TESTS_PROPERTY} property) 121 * @param message 122 * A message describing the requirements of the tests that are run when the named test is 123 * enabled. 124 * @return A message that contains both the given description and an explanation of how to disable 125 * the test. 126 */ 127 public static String createTestEnabledMessage(String testName, String message) { 128 String format = "%s. To skip this test, set -D%s=%s"; 129 return String.format(format, message.replaceAll("[.]$", ""), SKIP_TESTS_PROPERTY, testName); 130 } 131 132 /** 133 * Value for the {@link #SKIP_TESTS_PROPERTY} property that indicates that long-running tests 134 * should be disabled. Current value: {@value #LONG_TESTS_PROPERTY_VALUE}. 135 */ 136 public static final String LONG_TESTS_PROPERTY_VALUE = "long"; 137 138 /** 139 * Subclasses should call this method at the beginning of a test that will take a long time to 140 * run. Immediately after calling this method, they should also call 141 * <code>this.logger.info({@link #LONG_TEST_MESSAGE})</code>. 142 */ 143 protected void assumeLongTestsEnabled() { 144 // note that we can't log the message here as well, or it the log will display the wrong method 145 this.assumeTestsEnabled(COMMON_TESTS_PROPERTY_VALUE, LONG_TESTS_PROPERTY_VALUE); 146 } 147 148 /** 149 * A message indicating that the current test runs for a long time, and giving instructions how to 150 * skip it. Should be logged immediately after calling {@link #assumeLongTestsEnabled()}. 151 */ 152 protected static final String LONG_TEST_MESSAGE = createTestEnabledMessage( 153 LONG_TESTS_PROPERTY_VALUE, 154 "This test takes a long time to run"); 155 156 /** 157 * Value for the {@link #SKIP_TESTS_PROPERTY} property that indicates that tests requiring a lot 158 * of memory should be disabled. Current value: {@value #BIG_MEMORY_TESTS_PROPERTY_VALUE}. 159 */ 160 public static final String BIG_MEMORY_TESTS_PROPERTY_VALUE = "bigMem"; 161 162 /** 163 * Subclasses should call this method at the beginning of a test that will take a long time to 164 * run. Immediately after calling this method, they should also call 165 * <code>this.logger.info({@link #BIG_MEMORY_TEST_MESSAGE})</code>. 166 */ 167 protected void assumeBigMemoryTestsEnabled() { 168 // note that we can't log the message here as well, or it the log will display the wrong method 169 this.assumeTestsEnabled(COMMON_TESTS_PROPERTY_VALUE, BIG_MEMORY_TESTS_PROPERTY_VALUE); 170 } 171 172 /** 173 * A message indicating that the current test requires a lot of memory, and giving instructions 174 * how to skip it. Should be logged immediately after calling 175 * {@link #assumeBigMemoryTestsEnabled()}. 176 */ 177 public static final String BIG_MEMORY_TEST_MESSAGE = createTestEnabledMessage( 178 BIG_MEMORY_TESTS_PROPERTY_VALUE, 179 "This test requires a lot of memory"); 180 181 protected JCas jCas; 182 183 @Rule 184 public TemporaryFolder folder = new TemporaryFolder(); 185 186 protected File outputDirectory; 187 188 protected String outputDirectoryName; 189 190 @Before 191 public void setUp() throws Exception { 192 jCas = JCasFactory.createJCas(); 193 outputDirectory = folder.newFolder("output"); 194 outputDirectoryName = outputDirectory.getPath(); 195 } 196}