Skip to content

Initial refactoring by Pazus #72

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 7 commits into from
Feb 22, 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
39 changes: 10 additions & 29 deletions src/main/java/org/utplsql/api/DBHelper.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.utplsql.api;

import oracle.jdbc.OracleTypes;
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
import org.utplsql.api.db.DatabaseInformation;
import org.utplsql.api.db.DefaultDatabaseInformation;

import java.sql.*;
import java.util.Objects;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

/**
* Database utility functions.
Expand Down Expand Up @@ -53,23 +56,9 @@ public static String getCurrentSchema(Connection conn) throws SQLException {
*/
@Deprecated
public static Version getDatabaseFrameworkVersion( Connection conn ) throws SQLException {
Version result = new Version("");
try (PreparedStatement stmt = conn.prepareStatement("select ut_runner.version() from dual"))
{
ResultSet rs = stmt.executeQuery();
DatabaseInformation databaseInformation = new DefaultDatabaseInformation();
return databaseInformation.getUtPlsqlFrameworkVersion(conn);

if ( rs.next() )
result = new Version(rs.getString(1));

rs.close();
} catch ( SQLException e ) {
if ( e.getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE )
throw new UtPLSQLNotInstalledException(e);
else
throw e;
}

return result;
}

/** Returns the Oracle database Version from a given connection object
Expand All @@ -81,16 +70,8 @@ public static Version getDatabaseFrameworkVersion( Connection conn ) throws SQLE
*/
@Deprecated
public static String getOracleDatabaseVersion( Connection conn ) throws SQLException {
String result = null;
try (PreparedStatement stmt = conn.prepareStatement("select version from product_component_version where product like 'Oracle Database%'"))
{
ResultSet rs = stmt.executeQuery();

if ( rs.next() )
result = rs.getString(1);
}

return result;
DatabaseInformation databaseInformation = new DefaultDatabaseInformation();
return databaseInformation.getOracleVersion(conn);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/org/utplsql/api/JavaApiVersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

/** This class is getting updated automatically by the build process.
* Please do not update its constants manually cause they will be overwritten.
Expand All @@ -25,11 +21,9 @@ private JavaApiVersionInfo() { }
static {
try {

try ( InputStream in = JavaApiVersionInfo.class.getClassLoader().getResourceAsStream("utplsql-api.version")) {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try ( InputStream in = JavaApiVersionInfo.class.getClassLoader().getResourceAsStream("utplsql-api.version");
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
MAVEN_PROJECT_VERSION = reader.readLine();

reader.close();
}
}
catch ( IOException e ) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/utplsql/api/TestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.utplsql.api.compatibility.CompatibilityProxy;
import org.utplsql.api.db.DatabaseInformation;
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.DocumentationReporter;
Expand Down
75 changes: 62 additions & 13 deletions src/main/java/org/utplsql/api/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,83 @@
import org.utplsql.api.exception.InvalidVersionException;

import javax.annotation.Nullable;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toMap;

/** Simple class to parse utPLSQL Version-information and provide the separate version-numbers
*
* @author pesse
*/
public class Version implements Comparable<Version> {

public final static Version V3_0_0 = new Version("3.0.0", 3,0,0,null, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the known versions! Maybe we could put the exact build numbers here - in addition to 3.1.3 version

    public final static Version V3_0_2 = new Version("3.0.2", 3,0,2,938, true);
    public final static Version V3_0_3 = new Version("3.0.3", 3,0,3,1266, true);
    public final static Version V3_0_4 = new Version("3.0.4", 3,0,4,1372, true);
    public final static Version V3_1_0 = new Version("3.1.0", 3,1,0,1849, true);
    public final static Version V3_1_1 = new Version("3.1.1", 3,1,1,1868, true);
    public final static Version V3_1_2 = new Version("3.1.2", 3,1,2,2134, true);
    public final static Version V3_1_3 = new Version("3.1.3", 3,1,3,2398, true);
    private final static Map<String, Version> knownVersions =
            Stream.of(V3_0_0, V3_0_1, V3_0_2, V3_0_3, V3_0_4, V3_1_0, V3_1_1, V3_1_2, V3_1_3)

public final static Version V3_0_1 = new Version("3.0.1", 3,0,1,null, true);
public final static Version V3_0_2 = new Version("3.0.2", 3,0,2,null, true);
public final static Version V3_0_3 = new Version("3.0.3", 3,0,3,null, true);
public final static Version V3_0_4 = new Version("3.0.4", 3,0,4,null, true);
public final static Version V3_1_0 = new Version("3.1.0", 3,1,0,null, true);
public final static Version V3_1_1 = new Version("3.1.1", 3,1,1,null, true);
public final static Version V3_1_2 = new Version("3.1.2", 3,1,2,null, true);
private final static Map<String, Version> knownVersions =
Stream.of(V3_0_0, V3_0_1, V3_0_2, V3_0_3, V3_0_4, V3_1_0, V3_1_1, V3_1_2)
.collect(toMap(Version::toString, Function.identity()));

private final String origString;
private final Integer major;
private final Integer minor;
private final Integer bugfix;
private final Integer build;
private final boolean valid;

public Version( String versionString ) {
private Version(String origString, @Nullable Integer major, @Nullable Integer minor, @Nullable Integer bugfix, @Nullable Integer build, boolean valid) {
this.origString = origString;
this.major = major;
this.minor = minor;
this.bugfix = bugfix;
this.build = build;
this.valid = valid;
}

/**
* Use {@link Version#create} factory method instead
* For removal
*/
@Deprecated()
public Version(String versionString) {
assert versionString != null;
this.origString = versionString.trim();
Version dummy = parseVersionString(versionString);

this.origString = dummy.origString;
this.major = dummy.major;
this.minor =dummy.minor;
this.bugfix = dummy.bugfix;
this.build = dummy.build;
this.valid = dummy.valid;
}

Pattern p = Pattern.compile("([0-9]+)\\.?([0-9]+)?\\.?([0-9]+)?\\.?([0-9]+)?");
public static Version create(final String versionString) {
String origString = Objects.requireNonNull(versionString).trim();
Version version = knownVersions.get(origString);
return version != null ? version : parseVersionString(origString);
}

Matcher m = p.matcher(origString);
private static Version parseVersionString(String origString)
{

Integer major = null;
Integer minor = null;
Integer bugfix = null;
Integer build = null;
boolean valid = false;
Pattern p = Pattern.compile("([0-9]+)\\.?([0-9]+)?\\.?([0-9]+)?\\.?([0-9]+)?");

Matcher m = p.matcher(origString);

try {
if (m.find()) {
Expand All @@ -52,30 +101,30 @@ public Version( String versionString ) {
valid = false;
}

this.major = major;
this.minor = minor;
this.bugfix = bugfix;
this.build = build;
this.valid = valid;
return new Version(origString, major, minor, bugfix, build, valid);
}

@Override
public String toString() {
return origString;
}

@Nullable
public Integer getMajor() {
return major;
}

@Nullable
public Integer getMinor() {
return minor;
}

@Nullable
public Integer getBugfix() {
return bugfix;
}

@Nullable
public Integer getBuild() {
return build;
}
Expand All @@ -92,13 +141,13 @@ public String getNormalizedString()
{
if ( isValid() ) {
StringBuilder sb = new StringBuilder();
sb.append(String.valueOf(major));
sb.append(major);
if ( minor != null )
sb.append(".").append(String.valueOf(minor));
sb.append(".").append(minor);
if ( bugfix != null )
sb.append(".").append(String.valueOf(bugfix));
sb.append(".").append(bugfix);
if ( build != null )
sb.append(".").append(String.valueOf(build));
sb.append(".").append(build);

return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class CompatibilityProxy {

private Version databaseVersion;
private boolean compatible = false;
private DatabaseInformation databaseInformation;
private final DatabaseInformation databaseInformation;

public CompatibilityProxy( Connection conn ) throws SQLException {
this(conn, false, null);
Expand Down Expand Up @@ -62,7 +62,7 @@ public CompatibilityProxy( Connection conn, boolean skipCompatibilityCheck, Data
private void doCompatibilityCheckWithDatabase( Connection conn ) throws SQLException
{
databaseVersion = databaseInformation.getUtPlsqlFrameworkVersion(conn);
Version clientVersion = new Version(UTPLSQL_COMPATIBILITY_VERSION);
Version clientVersion = Version.create(UTPLSQL_COMPATIBILITY_VERSION);

if ( databaseVersion == null )
throw new DatabaseNotCompatibleException("Could not get database version", clientVersion, null, null);
Expand All @@ -74,7 +74,7 @@ private void doCompatibilityCheckWithDatabase( Connection conn ) throws SQLExcep
try {
compatible = versionCompatibilityCheck(conn, UTPLSQL_COMPATIBILITY_VERSION, null);
} catch (SQLException e) {
throw new DatabaseNotCompatibleException("Compatibility-check failed with error. Aborting. Reason: " + e.getMessage(), clientVersion, new Version("Unknown"), e);
throw new DatabaseNotCompatibleException("Compatibility-check failed with error. Aborting. Reason: " + e.getMessage(), clientVersion, Version.create("Unknown"), e);
}
} else
compatible = versionCompatibilityCheckPre303(UTPLSQL_COMPATIBILITY_VERSION);
Expand All @@ -85,7 +85,7 @@ private void doCompatibilityCheckWithDatabase( Connection conn ) throws SQLExcep
*/
private void doExpectCompatibility()
{
databaseVersion = new Version(UTPLSQL_API_VERSION);
databaseVersion = Version.create(UTPLSQL_API_VERSION);
compatible = true;
}

Expand Down Expand Up @@ -114,7 +114,7 @@ private boolean versionCompatibilityCheck(Connection conn, String requested, Str
*/
private boolean versionCompatibilityCheckPre303(String requested )
{
Version requestedVersion = new Version(requested);
Version requestedVersion = Version.create(requested);

Objects.requireNonNull(databaseVersion.getMajor(), "Illegal database Version: " + databaseVersion.toString());
return databaseVersion.getMajor().equals(requestedVersion.getMajor())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public enum OptionalFeatures {

OptionalFeatures( String minVersion, String maxVersion )
{
this.minVersion = minVersion != null ? new Version(minVersion) : null;
this.maxVersion = maxVersion != null ? new Version(maxVersion) : null;
this.minVersion = minVersion != null ? Version.create(minVersion) : null;
this.maxVersion = maxVersion != null ? Version.create(maxVersion) : null;
}

public boolean isAvailableFor(Version version ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@

import javax.annotation.Nullable;
import java.sql.*;
import java.util.Objects;

public class DefaultDatabaseInformation implements DatabaseInformation {

@Override
public Version getUtPlsqlFrameworkVersion(Connection conn) throws SQLException {
Version result = new Version("");
Version result = Version.create("");
try (PreparedStatement stmt = conn.prepareStatement("select ut_runner.version() from dual"))
{
ResultSet rs = stmt.executeQuery();

if ( rs.next() )
result = new Version(rs.getString(1));
result = Version.create(rs.getString(1));

rs.close();
} catch ( SQLException e ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public DatabaseNotCompatibleException( String message, Version clientVersion, Ve

public DatabaseNotCompatibleException( Version clientVersion, Version databaseVersion, Throwable cause )
{
this("utPLSQL API (" + String.valueOf(clientVersion) + ") not compatible with database (" + String.valueOf(databaseVersion) + ")", clientVersion, databaseVersion, cause);
this("utPLSQL API (" + clientVersion + ") not compatible with database (" + databaseVersion + ")", clientVersion, databaseVersion, cause);
}

public DatabaseNotCompatibleException( Version clientVersion, Version databaseVersion )
Expand All @@ -35,12 +35,12 @@ public DatabaseNotCompatibleException( Version clientVersion, Version databaseVe

public DatabaseNotCompatibleException( Version databaseVersion, Throwable cause )
{
this(new Version(CompatibilityProxy.UTPLSQL_COMPATIBILITY_VERSION), databaseVersion, cause );
this(Version.create(CompatibilityProxy.UTPLSQL_COMPATIBILITY_VERSION), databaseVersion, cause );
}

public DatabaseNotCompatibleException( Version databaseVersion )
{
this(new Version(CompatibilityProxy.UTPLSQL_COMPATIBILITY_VERSION), databaseVersion, null );
this(Version.create(CompatibilityProxy.UTPLSQL_COMPATIBILITY_VERSION), databaseVersion, null );
}

public Version getClientVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static OutputBuffer getCompatibleOutputBuffer(Version databaseVersion, Re
OracleConnection oraConn = conn.unwrap(OracleConnection.class);

try {
if (databaseVersion.isGreaterOrEqualThan(new Version("3.1.0"))) {
if (databaseVersion.isGreaterOrEqualThan(Version.V3_1_0)) {
if ( hasOutput(reporter, oraConn) ) {
return new DefaultOutputBuffer(reporter);
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/utplsql/api/reporter/CoreReporters.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
*/
public enum CoreReporters {

UT_COVERAGE_HTML_REPORTER(new Version("3.0.0"), null),
UT_DOCUMENTATION_REPORTER(new Version("3.0.0"), null),
UT_TEAMCITY_REPORTER(new Version("3.0.0"), null),
UT_XUNIT_REPORTER(new Version("3.0.0"), null),
UT_COVERALLS_REPORTER(new Version("3.0.0"), null),
UT_COVERAGE_SONAR_REPORTER(new Version("3.0.0"), null),
UT_SONAR_TEST_REPORTER(new Version("3.0.0"), null),
UT_COVERAGE_COBERTURA_REPORTER(new Version("3.1.0"), null);
UT_COVERAGE_HTML_REPORTER(Version.V3_0_0, null),
UT_DOCUMENTATION_REPORTER(Version.V3_0_0, null),
UT_TEAMCITY_REPORTER(Version.V3_0_0, null),
UT_XUNIT_REPORTER(Version.V3_0_0, null),
UT_COVERALLS_REPORTER(Version.V3_0_0, null),
UT_COVERAGE_SONAR_REPORTER(Version.V3_0_0, null),
UT_SONAR_TEST_REPORTER(Version.V3_0_0, null),
UT_COVERAGE_COBERTURA_REPORTER(Version.V3_1_0, null);

private final Version since;
private final Version until;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.function.Consumer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static ReporterInspector create(ReporterFactory reporterFactory, Connection conn

CompatibilityProxy proxy = new CompatibilityProxy(conn);

if (proxy.getDatabaseVersion().isGreaterOrEqualThan(new Version("3.1.0")))
if (proxy.getDatabaseVersion().isGreaterOrEqualThan(Version.V3_1_0))
return new ReporterInspector310(reporterFactory, conn);
else
return new ReporterInspectorPre310(reporterFactory, conn);
Expand Down
Loading