Remove System.out.println and Throwable.printStackTrace from tests

This commit is contained in:
Yannick Welsch 2016-03-15 15:04:34 +01:00
parent e91fd09692
commit f5e6db4090
17 changed files with 115 additions and 98 deletions

View File

@ -166,7 +166,7 @@ public class XContentBuilderTests extends ESTestCase {
byte[] data = bos.bytes().toBytes();
String sData = new String(data, "UTF8");
System.out.println("DATA: " + sData);
assertThat(sData, equalTo("{\"name\":\"something\", source : { test : \"value\" },\"name2\":\"something2\"}"));
}
public void testFieldCaseConversion() throws Exception {

View File

@ -181,7 +181,7 @@ public class NettyHttpServerPipeliningTests extends ESTestCase {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
e.getCause().printStackTrace();
logger.info("Caught exception", e.getCause());
e.getChannel().close();
}
}

View File

@ -1491,7 +1491,7 @@ public class TranslogTests extends ESTestCase {
if (writtenOperations.size() != snapshot.totalOperations()) {
for (int i = 0; i < threadCount; i++) {
if (threadExceptions[i] != null) {
threadExceptions[i].printStackTrace();
logger.info("Translog exception", threadExceptions[i]);
}
}
}

View File

@ -281,7 +281,7 @@ public class IndexingMemoryControllerTests extends ESSingleNodeTestCase {
controller.assertNotThrottled(shard0);
controller.assertThrottled(shard1);
System.out.println("TEST: now index more");
logger.info("--> Indexing more data");
// More indexing to shard0
controller.simulateIndexing(shard0);

View File

@ -54,7 +54,7 @@ import static org.hamcrest.Matchers.nullValue;
*
*/
public class ConcurrentPercolatorIT extends ESIntegTestCase {
public void testSimpleConcurrentPercolator() throws Exception {
public void testSimpleConcurrentPercolator() throws Throwable {
// We need to index a document / define mapping, otherwise field1 doesn't get recognized as number field.
// If we don't do this, then 'test2' percolate query gets parsed as a TermQuery and not a RangeQuery.
// The percolate api doesn't parse the doc if no queries have registered, so it can't lazily create a mapping
@ -143,9 +143,8 @@ public class ConcurrentPercolatorIT extends ESIntegTestCase {
Throwable assertionError = exceptionHolder.get();
if (assertionError != null) {
assertionError.printStackTrace();
throw assertionError;
}
assertThat(assertionError + " should be null", assertionError, nullValue());
}
public void testConcurrentAddingAndPercolating() throws Exception {

View File

@ -218,7 +218,7 @@ public abstract class BasePipelineAggregationTestCase<AF extends PipelineAggrega
AF testAgg = createTestAggregatorFactory();
AggregatorFactories.Builder factoriesBuilder = AggregatorFactories.builder().skipResolveOrder().addPipelineAggregator(testAgg);
String contentString = factoriesBuilder.toString();
System.out.println(contentString);
logger.info("Content string: {}", contentString);
XContentParser parser = XContentFactory.xContent(contentString).createParser(contentString);
QueryParseContext parseContext = new QueryParseContext(queriesRegistry);
parseContext.reset(parser);

View File

@ -27,6 +27,7 @@ import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.termvectors.TermVectorsRequest;
import org.elasticsearch.action.termvectors.TermVectorsResponse;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.termvectors.TermVectorsService;
@ -173,7 +174,7 @@ public class FetchSubPhasePluginIT extends ESIntegTestCase {
}
hitField.values().add(tv);
} catch (IOException e) {
e.printStackTrace();
ESLoggerFactory.getLogger(FetchSubPhasePluginIT.class.getName()).info("Swallowed exception", e);
}
}
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.search.geo;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.locationtech.spatial4j.context.SpatialContext;
import org.locationtech.spatial4j.distance.DistanceUtils;
import org.locationtech.spatial4j.exception.InvalidShapeException;
@ -560,7 +561,7 @@ public class GeoFilterIT extends ESIntegTestCase {
strategy.makeQuery(args);
return true;
} catch (UnsupportedSpatialOperation e) {
e.printStackTrace();
ESLoggerFactory.getLogger(GeoFilterIT.class.getName()).info("Unsupported spatial operation {}", e, relation);
return false;
}
}

View File

@ -400,7 +400,6 @@ public class HighlightBuilderTests extends ESTestCase {
context.reset(parser);
highlightBuilder = HighlightBuilder.PROTOTYPE.fromXContent(context);
assertEquals("expected HighlightBuilder with field", new HighlightBuilder().field(new Field("foo")), highlightBuilder);
System.out.println(Math.log(1/(double)(1+1)) + 1.0);
}
/**

View File

@ -137,8 +137,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
try {
channel.sendResponse(new StringMessageResponse("hello " + request.message));
} catch (IOException e) {
e.printStackTrace();
assertThat(e.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", e);
fail(e.getMessage());
}
}
});
@ -162,8 +162,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
@Override
public void handleException(TransportException exp) {
exp.printStackTrace();
assertThat("got exception instead of a response: " + exp.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", exp);
fail("got exception instead of a response: " + exp.getMessage());
}
});
@ -193,8 +193,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
@Override
public void handleException(TransportException exp) {
exp.printStackTrace();
assertThat("got exception instead of a response: " + exp.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", exp);
fail("got exception instead of a response: " + exp.getMessage());
}
});
@ -218,7 +218,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
threadPool.getThreadContext().putHeader("test.pong.user", "pong_user");
channel.sendResponse(response);
} catch (IOException e) {
assertThat(e.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", e);
fail(e.getMessage());
}
});
final Object context = new Object();
@ -245,7 +246,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
@Override
public void handleException(TransportException exp) {
assertThat("got exception instead of a response: " + exp.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", exp);
fail("got exception instead of a response: " + exp.getMessage());
}
};
StringMessageRequest ping = new StringMessageRequest("ping");
@ -317,8 +319,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
try {
channel.sendResponse(TransportResponse.Empty.INSTANCE, TransportResponseOptions.builder().withCompress(true).build());
} catch (IOException e) {
e.printStackTrace();
assertThat(e.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", e);
fail(e.getMessage());
}
}
});
@ -341,8 +343,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
@Override
public void handleException(TransportException exp) {
exp.printStackTrace();
assertThat("got exception instead of a response: " + exp.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", exp);
fail("got exception instead of a response: " + exp.getMessage());
}
});
@ -364,8 +366,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
try {
channel.sendResponse(new StringMessageResponse("hello " + request.message), TransportResponseOptions.builder().withCompress(true).build());
} catch (IOException e) {
e.printStackTrace();
assertThat(e.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", e);
fail(e.getMessage());
}
}
});
@ -389,8 +391,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
@Override
public void handleException(TransportException exp) {
exp.printStackTrace();
assertThat("got exception instead of a response: " + exp.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", exp);
fail("got exception instead of a response: " + exp.getMessage());
}
});
@ -552,8 +554,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
try {
channel.sendResponse(new StringMessageResponse("hello " + request.message));
} catch (IOException e) {
e.printStackTrace();
assertThat(e.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", e);
fail(e.getMessage());
}
}
});
@ -613,7 +615,7 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
@Override
public void handleException(TransportException exp) {
exp.printStackTrace();
logger.error("Unexpected failure", exp);
fail("got exception instead of a response for " + counter + ": " + exp.getDetailedMessage());
}
});
@ -959,8 +961,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
@Override
public void handleException(TransportException exp) {
exp.printStackTrace();
fail();
logger.error("Unexpected failure", exp);
fail("got exception instead of a response: " + exp.getMessage());
}
@Override
@ -1000,8 +1002,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
@Override
public void handleException(TransportException exp) {
exp.printStackTrace();
fail();
logger.error("Unexpected failure", exp);
fail("got exception instead of a response: " + exp.getMessage());
}
@Override
@ -1044,8 +1046,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
@Override
public void handleException(TransportException exp) {
exp.printStackTrace();
fail();
logger.error("Unexpected failure", exp);
fail("got exception instead of a response: " + exp.getMessage());
}
@Override
@ -1084,8 +1086,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
@Override
public void handleException(TransportException exp) {
exp.printStackTrace();
fail();
logger.error("Unexpected failure", exp);
fail("got exception instead of a response: " + exp.getMessage());
}
@Override

View File

@ -86,8 +86,8 @@ public class NettyScheduledPingTests extends ESTestCase {
try {
channel.sendResponse(TransportResponse.Empty.INSTANCE, TransportResponseOptions.EMPTY);
} catch (IOException e) {
e.printStackTrace();
assertThat(e.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", e);
fail(e.getMessage());
}
}
});
@ -113,8 +113,8 @@ public class NettyScheduledPingTests extends ESTestCase {
@Override
public void handleException(TransportException exp) {
exp.printStackTrace();
assertThat("got exception instead of a response: " + exp.getMessage(), false, equalTo(true));
logger.error("Unexpected failure", exp);
fail("got exception instead of a response: " + exp.getMessage());
}
}).txGet();
}

View File

@ -54,7 +54,7 @@ public class ConcurrentDocumentOperationIT extends ESIntegTestCase {
@Override
public void onFailure(Throwable e) {
e.printStackTrace();
logger.error("Unexpected exception while indexing", e);
failure.set(e);
latch.countDown();
}

View File

@ -354,9 +354,7 @@ public class SimpleVersioningIT extends ESIntegTestCase {
switch (random.nextInt(6)) {
case 0:
// random simple
if (VERBOSE) {
System.out.println("TEST: use random simple ids");
}
logger.info("--> use random simple ids");
ids = new IDSource() {
@Override
public String next() {
@ -366,9 +364,7 @@ public class SimpleVersioningIT extends ESIntegTestCase {
break;
case 1:
// random realistic unicode
if (VERBOSE) {
System.out.println("TEST: use random realistic unicode ids");
}
logger.info("--> use random realistic unicode ids");
ids = new IDSource() {
@Override
public String next() {
@ -378,9 +374,7 @@ public class SimpleVersioningIT extends ESIntegTestCase {
break;
case 2:
// sequential
if (VERBOSE) {
System.out.println("TEST: use seuquential ids");
}
logger.info("--> use sequential ids");
ids = new IDSource() {
int upto;
@ -392,9 +386,7 @@ public class SimpleVersioningIT extends ESIntegTestCase {
break;
case 3:
// zero-pad sequential
if (VERBOSE) {
System.out.println("TEST: use zero-pad seuquential ids");
}
logger.info("--> use zero-padded sequential ids");
ids = new IDSource() {
final int radix = TestUtil.nextInt(random, Character.MIN_RADIX, Character.MAX_RADIX);
final String zeroPad = String.format(Locale.ROOT, "%0" + TestUtil.nextInt(random, 4, 20) + "d", 0);
@ -409,9 +401,7 @@ public class SimpleVersioningIT extends ESIntegTestCase {
break;
case 4:
// random long
if (VERBOSE) {
System.out.println("TEST: use random long ids");
}
logger.info("--> use random long ids");
ids = new IDSource() {
final int radix = TestUtil.nextInt(random, Character.MIN_RADIX, Character.MAX_RADIX);
int upto;
@ -424,9 +414,7 @@ public class SimpleVersioningIT extends ESIntegTestCase {
break;
case 5:
// zero-pad random long
if (VERBOSE) {
System.out.println("TEST: use zero-pad random long ids");
}
logger.info("--> use zero-padded random long ids");
ids = new IDSource() {
final int radix = TestUtil.nextInt(random, Character.MIN_RADIX, Character.MAX_RADIX);
final String zeroPad = String.format(Locale.ROOT, "%015d", 0);
@ -539,9 +527,7 @@ public class SimpleVersioningIT extends ESIntegTestCase {
idPrefix = "";
} else {
idPrefix = TestUtil.randomSimpleString(random);
if (VERBOSE) {
System.out.println("TEST: use id prefix: " + idPrefix);
}
logger.debug("--> use id prefix {}", idPrefix);
}
int numIDs;
@ -564,9 +550,7 @@ public class SimpleVersioningIT extends ESIntegTestCase {
final IDAndVersion[] idVersions = new IDAndVersion[TestUtil.nextInt(random, numIDs / 2, numIDs * (TEST_NIGHTLY ? 8 : 2))];
final Map<String, IDAndVersion> truth = new HashMap<>();
if (VERBOSE) {
System.out.println("TEST: use " + numIDs + " ids; " + idVersions.length + " operations");
}
logger.debug("--> use {} ids; {} operations", numIDs, idVersions.length);
for (int i = 0; i < idVersions.length; i++) {
@ -596,10 +580,9 @@ public class SimpleVersioningIT extends ESIntegTestCase {
idVersions[i] = x;
}
if (VERBOSE) {
for (IDAndVersion idVersion : idVersions) {
System.out.println("id=" + idVersion.id + " version=" + idVersion.version + " delete?=" + idVersion.delete + " truth?=" + (truth.get(idVersion.id) == idVersion));
}
for (IDAndVersion idVersion : idVersions) {
logger.debug("--> id={} version={} delete?={} truth?={}", idVersion.id, idVersion.version, idVersion.delete,
truth.get(idVersion.id) == idVersion);
}
final AtomicInteger upto = new AtomicInteger();
@ -623,8 +606,8 @@ public class SimpleVersioningIT extends ESIntegTestCase {
if (index >= idVersions.length) {
break;
}
if (VERBOSE && index % 100 == 0) {
System.out.println(Thread.currentThread().getName() + ": index=" + index);
if (index % 100 == 0) {
logger.trace("{}: index={}", Thread.currentThread().getName(), index);
}
IDAndVersion idVersion = idVersions[index];
@ -657,18 +640,18 @@ public class SimpleVersioningIT extends ESIntegTestCase {
idVersion.indexFinishTime = System.nanoTime() - startTime;
if (threadRandom.nextInt(100) == 7) {
System.out.println(threadID + ": TEST: now refresh at " + (System.nanoTime() - startTime));
logger.trace("--> {}: TEST: now refresh at {}", threadID, System.nanoTime() - startTime);
refresh();
System.out.println(threadID + ": TEST: refresh done at " + (System.nanoTime() - startTime));
logger.trace("--> {}: TEST: refresh done at {}", threadID, System.nanoTime() - startTime);
}
if (threadRandom.nextInt(100) == 7) {
System.out.println(threadID + ": TEST: now flush at " + (System.nanoTime() - startTime));
logger.trace("--> {}: TEST: now flush at {}", threadID, System.nanoTime() - startTime);
try {
flush();
} catch (FlushNotAllowedEngineException fnaee) {
// OK
}
System.out.println(threadID + ": TEST: flush done at " + (System.nanoTime() - startTime));
logger.trace("--> {}: TEST: flush done at {}", threadID, System.nanoTime() - startTime);
}
}
} catch (Exception e) {
@ -696,16 +679,17 @@ public class SimpleVersioningIT extends ESIntegTestCase {
}
long actualVersion = client().prepareGet("test", "type", id).execute().actionGet().getVersion();
if (actualVersion != expected) {
System.out.println("FAILED: idVersion=" + idVersion + " actualVersion=" + actualVersion);
logger.error("--> FAILED: idVersion={} actualVersion= {}", idVersion, actualVersion);
failed = true;
}
}
if (failed) {
System.out.println("All versions:");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < idVersions.length; i++) {
System.out.println("i=" + i + " " + idVersions[i]);
sb.append("i=").append(i).append(" ").append(idVersions[i]).append(System.lineSeparator());
}
logger.error("All versions: {}", sb);
fail("wrong versions for some IDs");
}
}

View File

@ -316,10 +316,9 @@ public class RandomScoreFunctionTests extends ESIntegTestCase {
}
}
System.out.println();
System.out.println("max repeat: " + maxRepeat);
System.out.println("avg repeat: " + sumRepeat / (double) filled);
System.out.println("distribution: " + filled / (double) count);
logger.info("max repeat: {}", maxRepeat);
logger.info("avg repeat: {}", sumRepeat / (double) filled);
logger.info("distribution: {}", filled / (double) count);
int percentile50 = filled / 2;
int percentile25 = (filled / 4);
@ -333,18 +332,18 @@ public class RandomScoreFunctionTests extends ESIntegTestCase {
}
sum += i * matrix[i];
if (percentile50 == 0) {
System.out.println("median: " + i);
logger.info("median: {}", i);
} else if (percentile25 == 0) {
System.out.println("percentile_25: " + i);
logger.info("percentile_25: {}", i);
} else if (percentile75 == 0) {
System.out.println("percentile_75: " + i);
logger.info("percentile_75: {}", i);
}
percentile50--;
percentile25--;
percentile75--;
}
System.out.println("mean: " + sum / (double) count);
logger.info("mean: {}", sum / (double) count);
}
}

View File

@ -276,7 +276,7 @@ public class DeleteByQueryTests extends ESIntegTestCase {
assertSearchContextsClosed();
}
public void testConcurrentDeleteByQueriesOnDifferentDocs() throws Exception {
public void testConcurrentDeleteByQueriesOnDifferentDocs() throws Throwable {
createIndex("test");
ensureGreen();
@ -324,18 +324,17 @@ public class DeleteByQueryTests extends ESIntegTestCase {
Throwable assertionError = exceptionHolder.get();
if (assertionError != null) {
assertionError.printStackTrace();
throw assertionError;
}
assertThat(assertionError + " should be null", assertionError, nullValue());
refresh();
refresh();
for (int i = 0; i < threads.length; i++) {
assertHitCount(client().prepareSearch("test").setSize(0).setQuery(QueryBuilders.termQuery("field", i)).get(), 0);
}
assertSearchContextsClosed();
}
public void testConcurrentDeleteByQueriesOnSameDocs() throws Exception {
public void testConcurrentDeleteByQueriesOnSameDocs() throws Throwable {
assertAcked(prepareCreate("test").setSettings(Settings.settingsBuilder().put("index.refresh_interval", -1)));
ensureGreen();
@ -386,9 +385,8 @@ public class DeleteByQueryTests extends ESIntegTestCase {
Throwable assertionError = exceptionHolder.get();
if (assertionError != null) {
assertionError.printStackTrace();
throw assertionError;
}
assertThat(assertionError + " should be null", assertionError, nullValue());
assertHitCount(client().prepareSearch("test").setSize(0).get(), 0L);
assertThat(deleted.get(), equalTo(docs));
assertSearchContextsClosed();
@ -445,4 +443,4 @@ public class DeleteByQueryTests extends ESIntegTestCase {
}
});
}
}
}

View File

@ -38,8 +38,6 @@ import org.objectweb.asm.tree.analysis.BasicInterpreter;
import org.objectweb.asm.tree.analysis.BasicValue;
import org.objectweb.asm.tree.analysis.Frame;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitResult;
@ -59,6 +57,7 @@ public class ESLoggerUsageChecker {
public static final List<String> LOGGER_METHODS = Arrays.asList("trace", "debug", "info", "warn", "error");
public static final String IGNORE_CHECKS_ANNOTATION = "org.elasticsearch.common.SuppressLoggerChecks";
@SuppressForbidden(reason = "command line tool")
public static void main(String... args) throws Exception {
System.out.println("checking for wrong usages of ESLogger...");
boolean[] wrongUsageFound = new boolean[1];

View File

@ -0,0 +1,35 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.
*/
package org.elasticsearch.test.loggerusage;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation to suppress forbidden-apis errors inside a whole class, a method, or a field.
* Duplicated from core as main sources of logger-usage project have no dependencies on core
*/
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE })
public @interface SuppressForbidden {
String reason();
}