From ec2d2619ee2ec7a4097648a5081337af9d2534ba Mon Sep 17 00:00:00 2001 From: Abraham Elmahrek Date: Thu, 23 Jul 2015 12:13:12 -0700 Subject: [PATCH] SQOOP-2424: Sqoop2: Provide custom TestNG listener in integration tests (Jarek Jarcec Cecho via Abraham Elmahrek) --- .../sqoop/test/testng/SqoopTestListener.java | 97 +++++++++++++++++++ .../resources/integration-tests-suite.xml | 4 + .../test/resources/upgrade-tests-suite.xml | 4 + 3 files changed, 105 insertions(+) create mode 100644 test/src/main/java/org/apache/sqoop/test/testng/SqoopTestListener.java diff --git a/test/src/main/java/org/apache/sqoop/test/testng/SqoopTestListener.java b/test/src/main/java/org/apache/sqoop/test/testng/SqoopTestListener.java new file mode 100644 index 00000000..c86a765f --- /dev/null +++ b/test/src/main/java/org/apache/sqoop/test/testng/SqoopTestListener.java @@ -0,0 +1,97 @@ +/** + * 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. + */ +package org.apache.sqoop.test.testng; + +import org.apache.commons.lang.StringUtils; +import org.testng.ITestResult; +import org.testng.TestListenerAdapter; + +import java.io.FileDescriptor; +import java.io.FileOutputStream; +import java.io.PrintStream; + +/** + * Sqoop specific listener that will print name of each particular test that being run. + * + * Particularly suitable for integration tests that generally takes long time to finish. + */ +public class SqoopTestListener extends TestListenerAdapter { + + /** + * Sqoop tests are generally running with redirectTestOutputToFile=true which means + * that System.out is redirected to file. That is unpleasant as output user is suppose + * to see is not available on the console. Hence instead of System.out we're using + * directly the STDOUT file descriptor. + */ + static PrintStream ps = new PrintStream(new FileOutputStream(FileDescriptor.out)); + + + @Override + public void onTestStart(ITestResult tr) { + ps.flush(); + ps.print(testName(tr)); + ps.print(" running...\n"); + } + + @Override + public void onTestFailure(ITestResult tr) { + ps.flush(); + ps.print("FAILURE"); + ps.print(elapsedTime(tr)); + ps.print("\n\n"); + } + + @Override + public void onTestSkipped(ITestResult tr) { + ps.flush(); + ps.print("SKIPPED"); + ps.print(elapsedTime(tr)); + ps.print("\n\n"); + } + + @Override + public void onTestSuccess(ITestResult tr) { + ps.flush(); + ps.print("SUCCESS"); + ps.print(elapsedTime(tr)); + ps.print("\n\n"); + } + + private String testName(ITestResult tr) { + StringBuilder sb = new StringBuilder("Test ") + .append(tr.getTestClass().getName()) + .append(".") + .append(tr.getName()); + + if(tr.getParameters() != null && tr.getParameters().length > 0) { + sb.append(" with parameters [") + .append(StringUtils.join(tr.getParameters(), ",")) + .append("]"); + } + + return sb.toString(); + } + + private String elapsedTime(ITestResult tr) { + StringBuilder sb = new StringBuilder(" in ") + .append((tr.getEndMillis() - tr.getStartMillis()) / 1000) + .append(" seconds"); + + return sb.toString(); + } +} diff --git a/test/src/test/resources/integration-tests-suite.xml b/test/src/test/resources/integration-tests-suite.xml index f0dd905b..c1053298 100644 --- a/test/src/test/resources/integration-tests-suite.xml +++ b/test/src/test/resources/integration-tests-suite.xml @@ -20,6 +20,10 @@ limitations under the License. + + + + diff --git a/test/src/test/resources/upgrade-tests-suite.xml b/test/src/test/resources/upgrade-tests-suite.xml index 2856556b..3318e67b 100644 --- a/test/src/test/resources/upgrade-tests-suite.xml +++ b/test/src/test/resources/upgrade-tests-suite.xml @@ -20,6 +20,10 @@ limitations under the License. + + + +