Author: desruisseaux
Date: Thu Feb 21 11:04:38 2013
New Revision: 1448579
URL: http://svn.apache.org/r1448579
Log:
Unconditionally send output to TestCase.out (which is now never null), in order
to allow us to print the content in case of test failure.
This is sometime very useful information in case of test failures that happen
randomly.
Modified:
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/Performance.java
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestCase.java
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestConfiguration.java
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestRunner.java
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/package-info.java
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/RangeSetTest.java
Modified:
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/Performance.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/Performance.java?rev=1448579&r1=1448578&r2=1448579&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/Performance.java
(original)
+++
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/Performance.java
Thu Feb 21 11:04:38 2013
@@ -24,10 +24,10 @@ import java.lang.annotation.Target;
/**
- * Annotates methods that are used only for testing the performance of a class.
- * The annotated methods are not executed in normal SIS build. They are rather
- * executed manually before and after an implementation change, in order to
test
- * the impact on performance.
+ * Annotates methods that can be used for testing the performance of a class.
+ * The annotated methods may or may not be executed in normal SIS builds.
+ * Some methods may be executed only manually before and after an
implementation change,
+ * in order to test the impact on performance.
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-3.17)
Modified:
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestCase.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestCase.java?rev=1448579&r1=1448578&r2=1448579&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestCase.java
(original)
+++
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestCase.java
Thu Feb 21 11:04:38 2013
@@ -24,10 +24,7 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
-
import org.apache.sis.util.logging.Logging;
-
-import org.junit.After;
import org.junit.runner.RunWith;
import static org.apache.sis.test.TestConfiguration.VERBOSE_OUTPUT_KEY;
@@ -37,27 +34,20 @@ import static org.apache.sis.test.TestCo
/**
* Base class of Apache SIS tests (except the ones that extend GeoAPI tests).
* This base class provides an {@link #out} field that sub-classes can use
- * <strong>if non-null</strong> for printing debugging information. This
{@code out}
- * field shall be used instead of {@link System#out} for the following reasons:
+ * for printing debugging information. This {@code out} field shall be used
+ * instead of {@link System#out} for the following reasons:
*
* <ul>
- * <li>It is {@code null} by default and enabled only if a system property
is set as
- * described in the {@linkplain org.apache.sis.test package javadoc}. This
allows more
- * quiet (and sometime faster) Maven executions for those who are not SIS
developers.</li>
- * <li>The outputs are collected and printed only after each test completion.
+ * <li>By default, the contents sent to {@code out} are ignored for
successful tests
+ * and printed only when a test fail. This avoid polluting the console
output during
+ * successful Maven builds, and print only the information related to
failed tests.</li>
+ * <li>Printing information for all tests can be enabled if a system
property is set as
+ * described in the {@linkplain org.apache.sis.test package javadoc}.</li>
+ * <li>The outputs are collected and printed only after test completion.
* This avoid the problem of logging messages interleaved with the output.
* If such interleaving is really wanted, then use the logging framework
instead.</li>
* </ul>
*
- * Usage example:
- *
- * {@preformat java
- * if (out != null) {
- * // Performs here some potentially costly calculation to be printed.
- * out.println("Write here some information of particular interest.");
- * }
- * }
- *
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-3.16)
* @version 0.3
@@ -82,11 +72,12 @@ public abstract strictfp class TestCase
public static final boolean PENDING_NEXT_GEOAPI_RELEASE = false;
/**
- * If non-null, the output writer where to print debugging information.
- * This field is non-null if the {@value
org.apache.sis.test.TestConfiguration#VERBOSE_OUTPUT_KEY}
- * system property is set to {@code true}. This writer will use the system
default encoding, unless
- * the {@value org.apache.sis.test.TestConfiguration#OUTPUT_ENCODING_KEY}
system property has been
- * set to a different value.
+ * The output writer where to print debugging information (never {@code
null}).
+ * Texts sent to this printer will be show only if the test fails, or if
the
+ * {@value org.apache.sis.test.TestConfiguration#VERBOSE_OUTPUT_KEY}
system property
+ * is set to {@code true}. This writer will use the system default
encoding, unless
+ * the {@value org.apache.sis.test.TestConfiguration#OUTPUT_ENCODING_KEY}
system
+ * property has been set to a different value.
*
* @see org.apache.sis.test
* @see #flushVerboseOutput()
@@ -94,20 +85,22 @@ public abstract strictfp class TestCase
public static final PrintWriter out;
/**
- * The buffer which is backing the {@linkplain #out} stream, or {@code
null} if none.
+ * The buffer which is backing the {@linkplain #out} stream.
*/
private static final StringWriter buffer;
/**
+ * {@code true} if the {@value
org.apache.sis.test.TestConfiguration#VERBOSE_OUTPUT_KEY}
+ * system property is set to {@code true}.
+ */
+ static final boolean verbose;
+
+ /**
* Sets the {@link #out} writer and its underlying {@link #buffer}.
*/
static {
- if (Boolean.getBoolean(VERBOSE_OUTPUT_KEY)) {
- out = new PrintWriter(buffer = new StringWriter());
- } else {
- buffer = null;
- out = null;
- }
+ verbose = Boolean.getBoolean(VERBOSE_OUTPUT_KEY);
+ out = new PrintWriter(buffer = new StringWriter());
}
/**
@@ -144,21 +137,29 @@ public abstract strictfp class TestCase
}
/**
- * If verbose output is enabled, flushes the {@link #out} stream after
each test.
- * The stream will be flushed to the {@linkplain System#console() console}
if
- * available, or to the {@linkplain System#out standard output stream}
otherwise.
+ * Invoked by {@link TestRunner} in order to clear the buffer before a new
test begin.
+ * This is necessary when the previous test succeeded and the {@link
#verbose} flag is
+ * {@code false}, since the {@link #flushOutput()} method has not been
invoked in such
+ * case.
+ */
+ static void clearBuffer() {
+ synchronized (buffer) { // This is the lock used by the 'out'
PrintWriter.
+ out.flush();
+ buffer.getBuffer().setLength(0);
+ }
+ }
+
+ /**
+ * Invoked by {@link TestRunner} in order to flush the {@link #out} stream.
+ * The stream content will be flushed to the {@linkplain System#console()
console}
+ * if available, or to the {@linkplain System#out standard output stream}
otherwise.
+ * This method clears the stream buffer.
*
- * <p>This method is invoked automatically by JUnit and doesn't need to be
invoked
- * explicitely, unless the developer wants to flush the output at some
specific
- * point.</p>
+ * @param success {@code true} if this method is invoked on build success,
*/
- @After
- public void flushVerboseOutput() {
+ static void flushOutput() {
System.out.flush();
System.err.flush();
- if (out == null) {
- return;
- }
synchronized (buffer) { // This is the lock used by the 'out'
PrintWriter.
out.flush();
/*
Modified:
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestConfiguration.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestConfiguration.java?rev=1448579&r1=1448578&r2=1448579&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestConfiguration.java
(original)
+++
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestConfiguration.java
Thu Feb 21 11:04:38 2013
@@ -31,7 +31,7 @@ public final strictfp class TestConfigur
/**
* The {@value} system property for enabling verbose outputs.
* If this {@linkplain System#getProperties() system property} is set to
{@code true},
- * then the {@link TestCase#out} field will be set to a non-null value.
+ * then the content sent to the {@link TestCase#out} field will be printed
after each test.
*/
public static final String VERBOSE_OUTPUT_KEY =
"org.apache.sis.test.verbose";
Modified:
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestRunner.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestRunner.java?rev=1448579&r1=1448578&r2=1448579&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestRunner.java
(original)
+++
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestRunner.java
Thu Feb 21 11:04:38 2013
@@ -93,16 +93,49 @@ public final class TestRunner extends Bl
* The listener to use for keeping trace of methods that failed.
*/
final RunListener listener = new RunListener() {
+ /**
+ * Clears the buffer if it was not already done by {@link
#testFinished(Description)}.
+ */
+ @Override
+ public void testStarted(final Description description) {
+ if (!TestCase.verbose) {
+ TestCase.clearBuffer();
+ }
+ }
+
+ /**
+ * Prints output only in verbose mode.
+ * Otherwise silently discard the output.
+ */
+ @Override
+ public void testFinished(final Description description) {
+ if (TestCase.verbose) {
+ TestCase.flushOutput();
+ }
+ }
+
+ /**
+ * Remember that a test failed, and prints output if it was not
already done
+ */
@Override
public void testFailure(final Failure failure) {
addDependencyFailure(failure.getDescription().getMethodName());
+ if (!TestCase.verbose) {
+ TestCase.flushOutput();
+ }
}
+ /**
+ * Silently record skipped test as if it failed, without printing the
output.
+ */
@Override
public void testAssumptionFailure(final Failure failure) {
addDependencyFailure(failure.getDescription().getMethodName());
}
+ /**
+ * Silently record ignored test as if it failed, without printing the
output.
+ */
@Override
public void testIgnored(final Description description) {
addDependencyFailure(description.getMethodName());
@@ -110,13 +143,13 @@ public final class TestRunner extends Bl
};
/**
- * Creates a {@code Corollaries} to run {@code klass}.
+ * Creates a new test runner for the given class.
*
- * @param klass The class to run.
+ * @param testClass The class to run.
* @throws InitializationError If the test class is malformed.
*/
- public TestRunner(final Class<?> klass) throws InitializationError {
- super(klass);
+ public TestRunner(final Class<?> testClass) throws InitializationError {
+ super(testClass);
}
/**
@@ -253,7 +286,7 @@ public final class TestRunner extends Bl
/**
* Returns the {@link Statement} which will execute all the tests in the
class given
- * to the constructor.
+ * to the {@linkplain #TestRunner(Class) constructor}.
*
* @param notifier The object to notify about test results.
* @return The statement to execute for running the tests.
Modified:
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java?rev=1448579&r1=1448578&r2=1448579&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java
(original)
+++
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java
Thu Feb 21 11:04:38 2013
@@ -79,14 +79,16 @@ public final strictfp class TestUtilitie
}
/**
- * Prints the given title to {@link TestCase#out} in a box. This method is
invoked for
- * writing a clear visual separator between the verbose output of
different test cases.
+ * If verbose output are enabled, prints the given title to {@link
TestCase#out} in a box.
+ * This method is invoked for writing a clear visual separator between the
verbose output
+ * of different test cases. This method does nothing if verbose output is
not enabled,
+ * because only the output of failed tests should be printed in such case.
*
* @param title The title to write.
*/
public static void printSeparator(final String title) {
- final PrintWriter out = TestCase.out;
- if (out != null) {
+ if (TestCase.verbose) {
+ final PrintWriter out = TestCase.out;
final boolean isAnsiSupported = X364.isAnsiSupported();
if (isAnsiSupported) {
out.print(X364.FOREGROUND_CYAN.sequence());
Modified:
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/package-info.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/package-info.java?rev=1448579&r1=1448578&r2=1448579&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/package-info.java
(original)
+++
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/package-info.java
Thu Feb 21 11:04:38 2013
@@ -17,7 +17,7 @@
/**
* Tools for SIS tests. This package defines a base class, {@link
org.apache.sis.test.TestCase},
- * which is extended directly or indirectly by many (but not all) SIS tests.
+ * which is extended directly or indirectly by most (but not all) SIS tests.
* This package defines also an {@link org.apache.sis.test.Assert} class which
extend the GeoAPI
* {@link org.opengis.test.Assert} (which itself extends the JUnit {@link
org.junit.Assert} class)
* with the addition of assertion methods commonly used in SIS tests.
Modified:
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java?rev=1448579&r1=1448578&r2=1448579&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java
(original)
+++
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/CacheTest.java
Thu Feb 21 11:04:38 2013
@@ -31,6 +31,7 @@ import org.apache.sis.test.TestUtilities
import org.apache.sis.test.TestCase;
import org.apache.sis.test.DependsOn;
import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.Performance;
import org.junit.Test;
import static java.lang.StrictMath.*;
@@ -203,6 +204,7 @@ public final strictfp class CacheTest ex
* @throws InterruptedException If the test has been interrupted.
*/
@Test
+ @Performance
@DependsOnMethod("testThreadBlocking")
public void stress() throws InterruptedException {
final int count = 10000;
@@ -269,34 +271,31 @@ public final strictfp class CacheTest ex
* properly tuned, most values should be non-zero.
*/
final PrintWriter out = CacheTest.out;
- if (out != null) {
- TestUtilities.printSeparator("CacheTest.stress() - testing
concurrent accesses");
- out.print("There is "); out.print(threads.length); out.print("
threads, each of them"
- + " fetching or creating "); out.print(count);
out.println(" values.");
- out.println("Number of times a new value has been created, for
each thread:");
- for (int i=0; i<threads.length;) {
- final String n = String.valueOf(threads[i++].addCount);
- out.print(CharSequences.spaces(6 - n.length()));
- out.print(n);
- if ((i % 10) == 0) {
- out.println();
- }
+ TestUtilities.printSeparator("CacheTest.stress() - testing concurrent
accesses");
+ out.print("There is "); out.print(threads.length); out.print("
threads, each of them"
+ + " fetching or creating "); out.print(count); out.println("
values.");
+ out.println("Number of times a new value has been created, for each
thread:");
+ for (int i=0; i<threads.length;) {
+ final String n = String.valueOf(threads[i++].addCount);
+ out.print(CharSequences.spaces(6 - n.length()));
+ out.print(n);
+ if ((i % 10) == 0) {
+ out.println();
}
- out.println();
- out.println("Now observe how the background thread cleans the
cache.");
- long time = System.nanoTime();
- for (int i=0; i<10; i++) {
- final long t = System.nanoTime();
- out.printf("Cache size: %4d (after %3d ms)%n", cache.size(),
round((t - time) / 1E+6));
- time = t;
- Thread.sleep(250);
- if (i >= 2) {
- System.gc();
- }
+ }
+ out.println();
+ out.println("Now observe how the background thread cleans the cache.");
+ long time = System.nanoTime();
+ for (int i=0; i<10; i++) {
+ final long t = System.nanoTime();
+ out.printf("Cache size: %4d (after %3d ms)%n", cache.size(),
round((t - time) / 1E+6));
+ time = t;
+ Thread.sleep(250);
+ if (i >= 2) {
+ System.gc();
}
- out.println();
- out.flush();
}
+ out.println();
/*
* Gets the statistics of key values after garbage collection. The
mean value should
* be higher, because oldest values (which should have been garbage
collected first)
@@ -304,18 +303,15 @@ public final strictfp class CacheTest ex
* before to perform the actual check in order to allow the developer
to have more
* information in case of failure.
*/
- System.gc();
final Statistics afterGC = validateStressEntries("After GC", cache);
- if (out != null) {
- out.println("Statistics on the keys before and after garbage
collection.");
- out.println("The minimum and the mean values should be greater
after GC.");
- final StatisticsFormat format = StatisticsFormat.getInstance();
- format.setBorderWidth(1);
- try {
- format.format(new Statistics[] {beforeGC, afterGC}, out);
- } catch (IOException e) {
- throw new AssertionError(e);
- }
+ out.println("Statistics on the keys before and after garbage
collection.");
+ out.println("The minimum and the mean values should be greater after
GC.");
+ final StatisticsFormat format = StatisticsFormat.getInstance();
+ format.setBorderWidth(1);
+ try {
+ format.format(new Statistics[] {beforeGC, afterGC}, out);
+ } catch (IOException e) {
+ throw new AssertionError(e);
}
assertTrue("Mean key value should be greater after garbage
collection.", afterGC.mean() >= beforeGC.mean());
}
Modified:
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/RangeSetTest.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/RangeSetTest.java?rev=1448579&r1=1448578&r2=1448579&view=diff
==============================================================================
---
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/RangeSetTest.java
(original)
+++
sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/RangeSetTest.java
Thu Feb 21 11:04:38 2013
@@ -239,10 +239,7 @@ public final strictfp class RangeSetTest
set.remove(lower, upper);
}
}
- if (out != null) {
- final long end = System.nanoTime();
- out.println((end - start) / 1E9 + " " + set.size());
- }
+ out.println((System.nanoTime() - start) / 1E9 + " " + set.size());
Thread.sleep(1000);
}
}