Skip to content

Feature/simple logging #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ The file tnsnames.ora must contain valid TNS entries.
-exclude=pckg_list - Comma-separated object list to exclude from the coverage report.
Format: [schema.]package[,[schema.]package ...].
See coverage reporting options in framework documentation.

-q - Does not output the informational messages normally printed to console.
Default: false

-d - Outputs a load of debug information to console
Default: false
```

Parameters -f, -o, -s are correlated. That is parameters -o and -s are controlling outputs for reporter specified by the preceding -f parameter.
Expand Down
66 changes: 34 additions & 32 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,42 @@
</properties>

<dependencies>
<dependency>
<groupId>org.utplsql</groupId>
<artifactId>java-api</artifactId>
<version>3.1.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ucp</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.72</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.25</version>
<groupId>org.utplsql</groupId>
<artifactId>java-api</artifactId>
<version>3.1.3-SNAPSHOT</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ucp</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.72</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand All @@ -62,11 +68,7 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>

</dependencies>

<build>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/utplsql/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public static void main(String[] args) {
}

static int runWithExitCode( String[] args ) {

LoggerConfiguration.configure(LoggerConfiguration.ConfigLevel.NONE);
LocaleInitializer.initLocale();

JCommander jc = new JCommander();
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/org/utplsql/cli/LoggerConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.utplsql.cli;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.LoggerFactory;

class LoggerConfiguration {

public enum ConfigLevel {
BASIC, NONE, DEBUG
}

private LoggerConfiguration() {
throw new UnsupportedOperationException();
}

static void configure(ConfigLevel level) {
switch ( level ) {
case BASIC:
configureInfo();
break;
case NONE:
configureSilent();
break;
case DEBUG:
configureDebug();
break;
}
}

private static void configureSilent() {
setRootLoggerLevel(Level.OFF);
}

private static void configureInfo() {
setRootLoggerLevel(Level.INFO);
muteHikariLogger();
setSingleConsoleAppenderWithLayout("%msg%n");
}

private static void configureDebug() {
setRootLoggerLevel(Level.DEBUG);
setSingleConsoleAppenderWithLayout("%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n");
}

private static void setRootLoggerLevel( Level level ) {
Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(level);
}

private static void muteHikariLogger() {
((Logger) LoggerFactory.getLogger(HikariDataSource.class)).setLevel(Level.OFF);
}

private static void setSingleConsoleAppenderWithLayout( String patternLayout ) {
Logger logger = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

PatternLayoutEncoder ple = new PatternLayoutEncoder();
ple.setPattern(patternLayout);

ple.setContext(lc);
ple.start();

ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
consoleAppender.setEncoder(ple);
consoleAppender.setContext(lc);
consoleAppender.start();

logger.detachAndStopAllAppenders();
logger.setAdditive(false);
logger.addAppender(consoleAppender);
}
}
1 change: 1 addition & 0 deletions src/main/java/org/utplsql/cli/ReportersCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public int run() {
}
catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed | IllegalArgumentException e ) {
System.out.println(e.getMessage());
return 1;
}
catch (Exception e) {
e.printStackTrace();
Expand Down
105 changes: 82 additions & 23 deletions src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import org.utplsql.api.FileMapperOptions;
import org.utplsql.api.KeyValuePair;
import org.utplsql.api.TestRunner;
import org.utplsql.api.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.utplsql.api.*;
import org.utplsql.api.compatibility.CompatibilityProxy;
import org.utplsql.api.db.DefaultDatabaseInformation;
import org.utplsql.api.exception.DatabaseNotCompatibleException;
import org.utplsql.api.exception.SomeTestsFailedException;
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.cli.exception.DatabaseConnectionFailed;
import org.utplsql.cli.log.StringBlockFormatter;

import javax.sql.DataSource;
import java.io.File;
Expand All @@ -34,6 +35,8 @@
@Parameters(separators = "=", commandDescription = "run tests")
public class RunCommand implements ICommand {

private static final Logger logger = LoggerFactory.getLogger(RunCommand.class);

@Parameter(
required = true,
converter = ConnectionInfo.ConnectionStringConverter.class,
Expand Down Expand Up @@ -99,6 +102,15 @@ public class RunCommand implements ICommand {
)
private String excludeObjects = null;

@Parameter(
names = {"-q", "--quiet"},
description = "Does not output the informational messages normally printed to console")
private boolean logSilent = false;

@Parameter(
names = {"-d", "--debug"},
description = "Outputs a load of debug information to console")
private boolean logDebug = false;

private CompatibilityProxy compatibilityProxy;
private ReporterFactory reporterFactory;
Expand All @@ -112,7 +124,22 @@ public List<String> getTestPaths() {
return testPaths;
}

void init() {

LoggerConfiguration.ConfigLevel level = LoggerConfiguration.ConfigLevel.BASIC;
if ( logSilent ) {
level = LoggerConfiguration.ConfigLevel.NONE;
}
else if ( logDebug ) {
level = LoggerConfiguration.ConfigLevel.DEBUG;
}

LoggerConfiguration.configure(level);
}

public int run() {
init();
outputMainInformation();

try {

Expand Down Expand Up @@ -148,25 +175,8 @@ public int run() {

final DataSource dataSource = DataSourceProvider.getDataSource(getConnectionInfo(), getReporterManager().getNumberOfReporters() + 1);

// Do the reporters initialization, so we can use the id to run and gather results.
try (Connection conn = dataSource.getConnection()) {

// Check if orai18n exists if database version is 11g
RunCommandChecker.checkOracleI18nExists(conn);

// First of all do a compatibility check and fail-fast
compatibilityProxy = checkFrameworkCompatibility(conn);
reporterFactory = ReporterFactoryProvider.createReporterFactory(compatibilityProxy);

reporterList = getReporterManager().initReporters(conn, reporterFactory, compatibilityProxy);

} catch (SQLException e) {
if (e.getErrorCode() == 1017 || e.getErrorCode() == 12514) {
throw new DatabaseConnectionFailed(e);
} else {
throw e;
}
}
initDatabase(dataSource);
reporterList = initReporters(dataSource);

// Output a message if --failureExitCode is set but database framework is not capable of
String msg = RunCommandChecker.getCheckFailOnErrorMessage(failureExitCode, compatibilityProxy.getDatabaseVersion());
Expand All @@ -190,6 +200,8 @@ public int run() {
.includeObjects(finalIncludeObjectsList)
.excludeObjects(finalExcludeObjectsList);

logger.info("Running tests now.");
logger.info("--------------------------------------");
testRunner.run(conn);
} catch (SomeTestsFailedException e) {
returnCode[0] = this.failureExitCode;
Expand All @@ -205,6 +217,10 @@ public int run() {

executorService.shutdown();
executorService.awaitTermination(60, TimeUnit.MINUTES);

logger.info("--------------------------------------");
logger.info("All tests done.");

return returnCode[0];
}
catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed e ) {
Expand All @@ -221,6 +237,49 @@ public String getCommand() {
}


private void outputMainInformation() {

StringBlockFormatter formatter = new StringBlockFormatter("utPLCSL cli");
formatter.appendLine(CliVersionInfo.getInfo());
formatter.appendLine(JavaApiVersionInfo.getInfo());
formatter.appendLine("Java-Version: " + System.getProperty("java.version"));
formatter.appendLine("ORACLE_HOME: " + EnvironmentVariableUtil.getEnvValue("ORACLE_HOME"));
formatter.appendLine("NLS_LANG: " + EnvironmentVariableUtil.getEnvValue("NLS_LANG"));
formatter.appendLine("");
formatter.appendLine("Thanks for testing!");

logger.info(formatter.toString());
logger.info("");
}

private void initDatabase(DataSource dataSource) throws SQLException {
try (Connection conn = dataSource.getConnection()) {

// Check if orai18n exists if database version is 11g
RunCommandChecker.checkOracleI18nExists(conn);

// First of all do a compatibility check and fail-fast
compatibilityProxy = checkFrameworkCompatibility(conn);

logger.info("Successfully connected to database. UtPLSQL core: {}", compatibilityProxy.getDatabaseVersion());
logger.info("Oracle-Version: {}", new DefaultDatabaseInformation().getOracleVersion(conn));
}
catch (SQLException e) {
if (e.getErrorCode() == 1017 || e.getErrorCode() == 12514) {
throw new DatabaseConnectionFailed(e);
} else {
throw e;
}
}
}

private List<Reporter> initReporters(DataSource dataSource) throws SQLException {
try (Connection conn = dataSource.getConnection()) {
reporterFactory = ReporterFactoryProvider.createReporterFactory(compatibilityProxy);
return getReporterManager().initReporters(conn, reporterFactory, compatibilityProxy);
}
}

/** Returns FileMapperOptions for the first item of a given param list in a baseDir
*
* @param pathParams
Expand Down
Loading