From 8deb16bcf93055b6f35713f93c47f6c4aad222d0 Mon Sep 17 00:00:00 2001 From: Dawid Weiss Date: Wed, 30 May 2012 21:37:03 +0000 Subject: [PATCH] Minor cleanups of LuceneTestCase and reproduce string builder. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1344475 13f79535-47bb-0310-9956-ffa450edef68 --- .../util/junitcompat/TestCodecReported.java | 47 ++++++++++ .../util/junitcompat/WithNestedTests.java | 5 - .../apache/lucene/util/LuceneTestCase.java | 27 ++++-- .../util/RunListenerPrintReproduceInfo.java | 91 ++++++++++--------- 4 files changed, 117 insertions(+), 53 deletions(-) create mode 100644 lucene/core/src/test/org/apache/lucene/util/junitcompat/TestCodecReported.java diff --git a/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestCodecReported.java b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestCodecReported.java new file mode 100644 index 00000000000..6888d734b7c --- /dev/null +++ b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestCodecReported.java @@ -0,0 +1,47 @@ +package org.apache.lucene.util.junitcompat; + +import org.apache.lucene.codecs.Codec; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +public class TestCodecReported extends WithNestedTests { + public TestCodecReported() { + super(true); + } + + public static class Nested1 extends WithNestedTests.AbstractNestedTest { + public static String codecName; + + public void testDummy() { + codecName = Codec.getDefault().getName(); + fail(); + } + } + + @Test + public void testCorrectCodecReported() { + Result r = JUnitCore.runClasses(Nested1.class); + Assert.assertEquals(1, r.getFailureCount()); + Assert.assertTrue(super.getSysErr(), + super.getSysErr().contains("codec=" + Nested1.codecName)); + } +} diff --git a/lucene/core/src/test/org/apache/lucene/util/junitcompat/WithNestedTests.java b/lucene/core/src/test/org/apache/lucene/util/junitcompat/WithNestedTests.java index adf0f94f735..950f7ab3c0d 100644 --- a/lucene/core/src/test/org/apache/lucene/util/junitcompat/WithNestedTests.java +++ b/lucene/core/src/test/org/apache/lucene/util/junitcompat/WithNestedTests.java @@ -26,13 +26,8 @@ import org.apache.lucene.util.TestRuleIgnoreTestSuites; import org.junit.After; import org.junit.Assert; import org.junit.Before; -import org.junit.ClassRule; import org.junit.Rule; -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; -import com.carrotsearch.randomizedtesting.RandomizedRunner; import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; /** diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java index d0ec0bd2aa5..c18f334ae45 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java @@ -28,7 +28,6 @@ import java.util.*; import java.util.concurrent.*; import org.apache.lucene.analysis.Analyzer; -import org.apache.lucene.codecs.Codec; import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.*; @@ -117,6 +116,11 @@ public abstract class LuceneTestCase extends Assert { // ----------------------------------------------------------------- // Test groups and other annotations modifying tests' behavior. // ----------------------------------------------------------------- + + public static final String SYSPROP_NIGHTLY = "tests.nightly"; + public static final String SYSPROP_WEEKLY = "tests.weekly"; + public static final String SYSPROP_AWAITSFIX = "tests.awaitsfix"; + public static final String SYSPROP_SLOW = "tests.slow"; /** * Annotation for tests that should only be run during nightly builds. @@ -124,7 +128,7 @@ public abstract class LuceneTestCase extends Assert { @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) - @TestGroup(enabled = false, sysProperty = "tests.nightly") + @TestGroup(enabled = false, sysProperty = SYSPROP_NIGHTLY) public @interface Nightly {} /** @@ -133,7 +137,7 @@ public abstract class LuceneTestCase extends Assert { @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) - @TestGroup(enabled = false, sysProperty = "tests.weekly") + @TestGroup(enabled = false, sysProperty = SYSPROP_WEEKLY) public @interface Weekly {} /** @@ -142,7 +146,7 @@ public abstract class LuceneTestCase extends Assert { @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) - @TestGroup(enabled = false, sysProperty = "tests.awaitsfix") + @TestGroup(enabled = false, sysProperty = SYSPROP_AWAITSFIX) public @interface AwaitsFix { /** Point to JIRA entry. */ public String bugUrl(); @@ -155,7 +159,7 @@ public abstract class LuceneTestCase extends Assert { @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) - @TestGroup(enabled = false, sysProperty = "tests.slow") + @TestGroup(enabled = false, sysProperty = SYSPROP_SLOW) public @interface Slow {} /** @@ -215,9 +219,18 @@ public abstract class LuceneTestCase extends Assert { /** the line file used by LineFileDocs */ public static final String TEST_LINE_DOCS_FILE = System.getProperty("tests.linedocsfile", DEFAULT_LINE_DOCS_FILE); - /** Whether or not @nightly tests should run. */ - public static final boolean TEST_NIGHTLY = systemPropertyAsBoolean("tests.nightly", false); + /** Whether or not {@link Nightly} tests should run. */ + public static final boolean TEST_NIGHTLY = systemPropertyAsBoolean(SYSPROP_NIGHTLY, false); + /** Whether or not {@link Weekly} tests should run. */ + public static final boolean TEST_WEEKLY = systemPropertyAsBoolean(SYSPROP_WEEKLY, false); + + /** Whether or not {@link AwaitsFix} tests should run. */ + public static final boolean TEST_AWAITSFIX = systemPropertyAsBoolean(SYSPROP_AWAITSFIX, false); + + /** Whether or not {@link Slow} tests should run. */ + public static final boolean TEST_SLOW = systemPropertyAsBoolean(SYSPROP_SLOW, false); + /** Throttling, see {@link MockDirectoryWrapper#setThrottling(Throttling)}. */ public static final Throttling TEST_THROTTLING = TEST_NIGHTLY ? Throttling.SOMETIMES : Throttling.NEVER; diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/RunListenerPrintReproduceInfo.java b/lucene/test-framework/src/java/org/apache/lucene/util/RunListenerPrintReproduceInfo.java index ebc74d8f1a2..4db9e943896 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/RunListenerPrintReproduceInfo.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/RunListenerPrintReproduceInfo.java @@ -1,20 +1,12 @@ package org.apache.lucene.util; -import static org.apache.lucene.util.LuceneTestCase.DEFAULT_LINE_DOCS_FILE; -import static org.apache.lucene.util.LuceneTestCase.JENKINS_LARGE_LINE_DOCS_FILE; -import static org.apache.lucene.util.LuceneTestCase.RANDOM_MULTIPLIER; -import static org.apache.lucene.util.LuceneTestCase.TEST_CODEC; -import static org.apache.lucene.util.LuceneTestCase.TEST_DIRECTORY; -import static org.apache.lucene.util.LuceneTestCase.TEST_LINE_DOCS_FILE; -import static org.apache.lucene.util.LuceneTestCase.TEST_NIGHTLY; -import static org.apache.lucene.util.LuceneTestCase.TEST_POSTINGSFORMAT; -import static org.apache.lucene.util.LuceneTestCase.classEnvRule; +import static org.apache.lucene.util.LuceneTestCase.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; -import org.apache.lucene.codecs.Codec; import org.junit.runner.Description; import org.junit.runner.Result; import org.junit.runner.notification.Failure; @@ -112,9 +104,9 @@ public final class RunListenerPrintReproduceInfo extends RunListener { reportAdditionalFailureInfo(null); } } - + /** print some useful debugging information about the environment */ - static void printDebuggingInformation() { + private static void printDebuggingInformation() { if (classEnvRule != null) { System.err.println("NOTE: test params are: codec=" + classEnvRule.codec + ", sim=" + classEnvRule.similarity + @@ -134,41 +126,58 @@ public final class RunListenerPrintReproduceInfo extends RunListener { System.err.println("NOTE: All tests run in this JVM: " + Arrays.toString(testClassesRun.toArray())); } - // We get here from InterceptTestCaseEvents on the 'failed' event.... - public void reportAdditionalFailureInfo(final String testName) { + private void reportAdditionalFailureInfo(final String testName) { if (TEST_LINE_DOCS_FILE.endsWith(JENKINS_LARGE_LINE_DOCS_FILE)) { - System.err.println("NOTE: download the large Jenkins line-docs file by running 'ant get-jenkins-line-docs' in the lucene directory."); + System.err.println("NOTE: download the large Jenkins line-docs file by running " + + "'ant get-jenkins-line-docs' in the lucene directory."); } - StringBuilder b = new StringBuilder(); - b.append("NOTE: reproduce with: ant test ") - .append("-Dtestcase=").append(RandomizedContext.current().getTargetClass().getSimpleName()); - if (testName != null) { - b.append(" -Dtests.method=").append(testName); + final StringBuilder b = new StringBuilder(); + b.append("NOTE: reproduce with: ant test "); + + // Test case, method, seed. + addVmOpt(b, "testcase", RandomizedContext.current().getTargetClass().getSimpleName()); + addVmOpt(b, "tests.method", testName); + addVmOpt(b, "tests.seed", RandomizedContext.current().getRunnerSeedAsString()); + + // Test groups and multipliers. + if (RANDOM_MULTIPLIER > 1) addVmOpt(b, "tests.multiplier", RANDOM_MULTIPLIER); + if (TEST_NIGHTLY) addVmOpt(b, SYSPROP_NIGHTLY, TEST_NIGHTLY); + if (TEST_WEEKLY) addVmOpt(b, SYSPROP_WEEKLY, TEST_WEEKLY); + if (TEST_SLOW) addVmOpt(b, SYSPROP_SLOW, TEST_SLOW); + if (TEST_AWAITSFIX) addVmOpt(b, SYSPROP_AWAITSFIX, TEST_AWAITSFIX); + + // Codec, postings, directories. + if (!TEST_CODEC.equals("random")) addVmOpt(b, "tests.codec", TEST_CODEC); + if (!TEST_POSTINGSFORMAT.equals("random")) addVmOpt(b, "tests.postingsformat", TEST_POSTINGSFORMAT); + if (!TEST_DIRECTORY.equals("random")) addVmOpt(b, "tests.directory", TEST_DIRECTORY); + + // Environment. + if (!TEST_LINE_DOCS_FILE.equals(DEFAULT_LINE_DOCS_FILE)) addVmOpt(b, "tests.linedocsfile", TEST_LINE_DOCS_FILE); + if (classEnvRule != null) { + addVmOpt(b, "tests.locale", classEnvRule.locale); + addVmOpt(b, "tests.timezone", classEnvRule.timeZone.getID()); } - b.append(" -Dtests.seed=") - .append(RandomizedContext.current().getRunnerSeedAsString()) - .append(reproduceWithExtraParams()); + // Randomize this: LUCENE-4094 + addVmOpt(b, "args", "-Dfile.encoding=" + System.getProperty("file.encoding")); + System.err.println(b.toString()); } - // extra params that were overridden needed to reproduce the command - private static String reproduceWithExtraParams() { - StringBuilder sb = new StringBuilder(); - if (classEnvRule != null) { - if (classEnvRule.locale != null) sb.append(" -Dtests.locale=").append(classEnvRule.locale); - if (classEnvRule.timeZone != null) sb.append(" -Dtests.timezone=").append(classEnvRule.timeZone.getID()); - } - if (!TEST_CODEC.equals("random")) sb.append(" -Dtests.codec=").append(TEST_CODEC); - if (!TEST_POSTINGSFORMAT.equals("random")) sb.append(" -Dtests.postingsformat=").append(TEST_POSTINGSFORMAT); - if (!TEST_DIRECTORY.equals("random")) sb.append(" -Dtests.directory=").append(TEST_DIRECTORY); - if (RANDOM_MULTIPLIER > 1) sb.append(" -Dtests.multiplier=").append(RANDOM_MULTIPLIER); - if (TEST_NIGHTLY) sb.append(" -Dtests.nightly=true"); - if (!TEST_LINE_DOCS_FILE.equals(DEFAULT_LINE_DOCS_FILE)) sb.append(" -Dtests.linedocsfile=" + TEST_LINE_DOCS_FILE); + /** + * Append a VM option (-Dkey=value) to a {@link StringBuilder}. Add quotes if + * spaces or other funky characters are detected. + */ + static void addVmOpt(StringBuilder b, String key, Object value) { + if (value == null) return; - // TODO we can't randomize this yet (it drives ant crazy) but this makes tests reproduce - // in case machines have different default charsets... - sb.append(" -Dargs=\"-Dfile.encoding=" + System.getProperty("file.encoding") + "\""); - return sb.toString(); - } + b.append(" -D").append(key).append("="); + String v = value.toString(); + // Add simplistic quoting. This varies a lot from system to system and between + // shells... ANT should have some code for doing it properly. + if (Pattern.compile("[\\s=']").matcher(v).find()) { + v = '"' + v + '"'; + } + b.append(v); + } }