Skip to content

Commit 14401b5

Browse files
authored
Merge pull request #46 from utPLSQL/feature/version-check
Feature/version check
2 parents 2d66cde + 6c22ad0 commit 14401b5

File tree

9 files changed

+264
-28
lines changed

9 files changed

+264
-28
lines changed

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
sudo: required
22
language: java
33

4+
services:
5+
- docker
6+
47
jdk:
58
- oraclejdk8
69

710
env:
811
global:
12+
- DOCKER_CFG=$HOME/.docker
13+
- DOCKER_REPO="utplsqlv3/oracledb"
914
- CACHE_DIR=$HOME/.cache
1015
- MAVEN_HOME=/usr/local/maven
1116
- MAVEN_CFG=$HOME/.m2
17+
- DB_URL="127.0.0.1:1521:XE"
18+
- DB_USER=app
19+
- DB_PASS=app
20+
- ORACLE_VERSION="11g-r2-xe"
21+
- DOCKER_OPTIONS="--shm-size=1g"
1222

1323
cache:
1424
directories:
25+
- $DOCKER_CFG
1526
- $CACHE_DIR
1627
- $MAVEN_CFG
1728

1829
install:
1930
- bash .travis/maven_cfg.sh
31+
- bash .travis/start_db.sh
32+
- bash .travis/install_utplsql.sh
33+
- bash .travis/install_demo_project.sh
2034

2135
script:
2236
- mvn package -DskipTests

.travis/create_api_user.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -ev
3+
4+
sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA <<EOF
5+
create user api identified by api
6+
quota unlimited on USERS
7+
default tablespace USERS;
8+
grant create session,
9+
create procedure,
10+
create type,
11+
create table,
12+
create sequence,
13+
create view
14+
to api;
15+
grant select any dictionary to api;
16+
exit;
17+
EOF

.travis/install_demo_project.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
set -ev
3+
cd $(dirname $(readlink -f $0))
4+
5+
PROJECT_FILE="utPLSQL-demo-project"
6+
git clone -b develop --single-branch https://github.com/utPLSQL/utPLSQL-demo-project.git
7+
8+
cat > demo_project.sh.tmp <<EOF
9+
sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA <<SQL
10+
create user ${DB_USER} identified by ${DB_PASS} quota unlimited on USERS default tablespace USERS;
11+
grant create session, create procedure, create type, create table, create sequence, create view to ${DB_USER};
12+
grant select any dictionary to ${DB_USER};
13+
exit
14+
SQL
15+
16+
cd ${PROJECT_FILE}
17+
sqlplus -S -L ${DB_USER}/${DB_PASS}@//127.0.0.1:1521/xe <<SQL
18+
whenever sqlerror exit failure rollback
19+
whenever oserror exit failure rollback
20+
21+
@source/award_bonus/employees_test.sql
22+
@source/award_bonus/award_bonus.prc
23+
24+
@source/between_string/betwnstr.fnc
25+
26+
@source/remove_rooms_by_name/rooms.sql
27+
@source/remove_rooms_by_name/remove_rooms_by_name.prc
28+
29+
@test/award_bonus/test_award_bonus.pks
30+
@test/award_bonus/test_award_bonus.pkb
31+
32+
@test/between_string/test_betwnstr.pks
33+
@test/between_string/test_betwnstr.pkb
34+
35+
@test/remove_rooms_by_name/test_remove_rooms_by_name.pks
36+
@test/remove_rooms_by_name/test_remove_rooms_by_name.pkb
37+
38+
exit
39+
SQL
40+
EOF
41+
42+
docker cp ./$PROJECT_FILE $ORACLE_VERSION:/$PROJECT_FILE
43+
docker cp ./demo_project.sh.tmp $ORACLE_VERSION:/demo_project.sh
44+
docker exec $ORACLE_VERSION bash demo_project.sh

.travis/install_utplsql.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
set -ev
3+
cd $(dirname $(readlink -f $0))
4+
5+
# Download the specified version of utPLSQL.
6+
UTPLSQL_VERSION="v3.0.4"
7+
UTPLSQL_FILE="utPLSQL"
8+
curl -L -O "https://github.com/utPLSQL/utPLSQL/releases/download/$UTPLSQL_VERSION/$UTPLSQL_FILE.tar.gz"
9+
10+
# Download develop branch of utPLSQL.
11+
#UTPLSQL_VERSION="develop"
12+
#UTPLSQL_FILE="utPLSQL"
13+
#git clone -b develop --single-branch https://github.com/utPLSQL/utPLSQL.git
14+
# tar -czf $UTPLSQL_FILE.tar.gz $UTPLSQL_FILE && rm -rf $UTPLSQL_FILE
15+
16+
# Create a temporary install script.
17+
cat > install.sh.tmp <<EOF
18+
tar -xzf ${UTPLSQL_FILE}.tar.gz && rm ${UTPLSQL_FILE}.tar.gz
19+
cd ${UTPLSQL_FILE}/source
20+
sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA @install_headless.sql ut3 ut3 users
21+
EOF
22+
23+
# Copy utPLSQL files to the container and install it.
24+
docker cp ./$UTPLSQL_FILE.tar.gz $ORACLE_VERSION:/$UTPLSQL_FILE.tar.gz
25+
# docker cp ./$UTPLSQL_FILE $ORACLE_VERSION:/$UTPLSQL_FILE
26+
docker cp ./install.sh.tmp $ORACLE_VERSION:/install.sh
27+
docker cp ./create_api_user.sh $ORACLE_VERSION:/create_api_user.sh
28+
29+
# Remove temporary files.
30+
# rm $UTPLSQL_FILE.tar.gz
31+
rm -rf $UTPLSQL_FILE
32+
rm install.sh.tmp
33+
34+
# Execute the utPLSQL installation inside the container.
35+
docker exec $ORACLE_VERSION bash install.sh
36+
docker exec $ORACLE_VERSION bash create_api_user.sh

.travis/start_db.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -ev
3+
4+
# If docker credentials are not cached, do the login.
5+
if [ ! -f $DOCKER_CFG/config.json ]; then
6+
docker login -u "$DOCKER_USER" -p "$DOCKER_PASSWORD"
7+
else
8+
echo "Using docker login from cache..."
9+
fi
10+
11+
# Pull the specified db version from docker hub.
12+
docker pull $DOCKER_REPO:$ORACLE_VERSION
13+
docker run -d --name $ORACLE_VERSION $DOCKER_OPTIONS -p 1521:1521 $DOCKER_REPO:$ORACLE_VERSION
14+
docker logs -f $ORACLE_VERSION | grep -m 1 "DATABASE IS READY TO USE!" --line-buffered

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ You can download development versions on [Bintray](https://bintray.com/viniciusa
1616
* [Java SE Runtime Environment 8](http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html)
1717
* When using reporters for Sonar or Coveralls client needs to be invoked from project's root directory.
1818

19+
## Compatibility
20+
The latest CLI is always compatible with all database frameworks of the same major version.
21+
For example CLI-3.0.4 is compatible with database framework 3.0.0-3.0.4 but not with database framework 2.x.
22+
1923
## Usage
2024
utplsql run \<ConnectionURL\> [-p=(ut_path|ut_paths)] [-f=format [-o=output_file] [-s] ...]
2125

@@ -74,6 +78,8 @@ utplsql run \<ConnectionURL\> [-p=(ut_path|ut_paths)] [-f=format [-o=output_file
7478
-c - If specified, enables printing of test results in colors as defined by ANSICONSOLE standards.
7579
Works only on reporeters that support colors (ut_documentation_reporter).
7680
--failure-exit-code - Override the exit code on failure, defaults to 1. You can set it to 0 to always exit with a success status.
81+
-scc - If specified, skips the compatibility-check with the version of the database framework.
82+
If you skip compatibility-check, CLI will expect the most actual framework version
7783
```
7884

7985
Parameters -f, -o, -s are correlated. That is parameters -o and -s are controlling outputs for reporter specified by the preceding -f parameter.

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>org.utplsql</groupId>
66
<artifactId>cli</artifactId>
7-
<version>1.0-SNAPSHOT</version>
7+
<version>3.0.4-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>cli</name>
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.utplsql</groupId>
2222
<artifactId>java-api</artifactId>
23-
<version>1.0-SNAPSHOT</version>
23+
<version>3.0.4-SNAPSHOT</version>
2424
<scope>compile</scope>
2525
</dependency>
2626
<dependency>

src/main/java/org/utplsql/cli/RunCommand.java

Lines changed: 96 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.beust.jcommander.Parameter;
44
import com.beust.jcommander.Parameters;
55
import org.utplsql.api.*;
6+
import org.utplsql.api.exception.DatabaseNotCompatibleException;
67
import org.utplsql.api.exception.SomeTestsFailedException;
78
import org.utplsql.api.reporter.Reporter;
89
import org.utplsql.api.reporter.ReporterFactory;
@@ -21,6 +22,9 @@
2122

2223
/**
2324
* Created by vinicius.moreira on 19/04/2017.
25+
*
26+
* @author vinicious moreira
27+
* @author pesse
2428
*/
2529
@Parameters(separators = "=", commandDescription = "run tests")
2630
public class RunCommand {
@@ -70,6 +74,12 @@ public class RunCommand {
7074
"-name_subexpression=0] - path to project test files")
7175
private List<String> testPathParams = new ArrayList<>();
7276

77+
@Parameter(
78+
names = {"-scc", "--skip-compatibility-check"},
79+
description = "Skips the check for compatibility with database framework. CLI expects the framework to be " +
80+
"most actual. Use this if you use CLI with a development version of utPLSQL-framework")
81+
private boolean skipCompatibilityCheck = false;
82+
7383
public ConnectionInfo getConnectionInfo() {
7484
return connectionInfoList.get(0);
7585
}
@@ -81,36 +91,27 @@ public List<String> getTestPaths() {
8191
public int run() throws Exception {
8292
final ConnectionInfo ci = getConnectionInfo();
8393

94+
final List<Reporter> reporterList;
8495
final List<ReporterOptions> reporterOptionsList = getReporterOptionsList();
8596
final List<String> testPaths = getTestPaths();
86-
final List<Reporter> reporterList = new ArrayList<>();
8797

8898
final File baseDir = new File("").getAbsoluteFile();
8999
final FileMapperOptions[] sourceMappingOptions = {null};
90100
final FileMapperOptions[] testMappingOptions = {null};
91101

92102
final int[] returnCode = {0};
93103

94-
if (!this.sourcePathParams.isEmpty()) {
95-
String sourcePath = this.sourcePathParams.get(0);
96-
List<String> sourceFiles = new FileWalker().getFileList(baseDir, sourcePath);
97-
sourceMappingOptions[0] = getMapperOptions(this.sourcePathParams, sourceFiles);
98-
}
99-
100-
if (!this.testPathParams.isEmpty()) {
101-
String testPath = this.testPathParams.get(0);
102-
List<String> testFiles = new FileWalker().getFileList(baseDir, testPath);
103-
testMappingOptions[0] = getMapperOptions(this.testPathParams, testFiles);
104-
}
104+
sourceMappingOptions[0] = getFileMapperOptionsByParamListItem(this.sourcePathParams, baseDir);
105+
testMappingOptions[0] = getFileMapperOptionsByParamListItem(this.testPathParams, baseDir);
105106

106107
// Do the reporters initialization, so we can use the id to run and gather results.
107108
try (Connection conn = ci.getConnection()) {
108-
for (ReporterOptions ro : reporterOptionsList) {
109-
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
110-
reporter.init(conn);
111-
ro.setReporterObj(reporter);
112-
reporterList.add(reporter);
113-
}
109+
110+
// First of all do a compatibility check and fail-fast
111+
checkFrameworkCompatibility(conn);
112+
113+
reporterList = initReporters(conn, reporterOptionsList);
114+
114115
} catch (SQLException e) {
115116
System.out.println(e.getMessage());
116117
return Cli.DEFAULT_ERROR_CODE;
@@ -128,6 +129,7 @@ public int run() throws Exception {
128129
.testMappingOptions(testMappingOptions[0])
129130
.colorConsole(this.colorConsole)
130131
.failOnErrors(true)
132+
.skipCompatibilityCheck(skipCompatibilityCheck)
131133
.run(conn);
132134
} catch (SomeTestsFailedException e) {
133135
returnCode[0] = this.failureExitCode;
@@ -138,6 +140,44 @@ public int run() throws Exception {
138140
}
139141
});
140142

143+
// Gather each reporter results on a separate thread.
144+
startReporterGatherers(reporterOptionsList, executorService, ci, returnCode);
145+
146+
executorService.shutdown();
147+
executorService.awaitTermination(60, TimeUnit.MINUTES);
148+
return returnCode[0];
149+
}
150+
151+
/** Initializes the reporters so we can use the id to gather results
152+
*
153+
* @param conn Active Connection
154+
* @param reporterOptionsList
155+
* @return List of Reporters
156+
* @throws SQLException
157+
*/
158+
private List<Reporter> initReporters( Connection conn, List<ReporterOptions> reporterOptionsList ) throws SQLException
159+
{
160+
final List<Reporter> reporterList = new ArrayList<>();
161+
162+
for (ReporterOptions ro : reporterOptionsList) {
163+
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
164+
reporter.init(conn);
165+
ro.setReporterObj(reporter);
166+
reporterList.add(reporter);
167+
}
168+
169+
return reporterList;
170+
}
171+
172+
/** Starts a separate thread for each Reporter to gather its results
173+
*
174+
* @param reporterOptionsList
175+
* @param executorService
176+
* @param ci
177+
* @param returnCode
178+
*/
179+
private void startReporterGatherers(List<ReporterOptions> reporterOptionsList, ExecutorService executorService, final ConnectionInfo ci, final int[] returnCode)
180+
{
141181
// Gather each reporter results on a separate thread.
142182
for (ReporterOptions ro : reporterOptionsList) {
143183
executorService.submit(() -> {
@@ -165,10 +205,23 @@ public int run() throws Exception {
165205
}
166206
});
167207
}
208+
}
168209

169-
executorService.shutdown();
170-
executorService.awaitTermination(60, TimeUnit.MINUTES);
171-
return returnCode[0];
210+
/** Returns FileMapperOptions for the first item of a given param list in a baseDir
211+
*
212+
* @param pathParams
213+
* @param baseDir
214+
* @return FileMapperOptions or null
215+
*/
216+
private FileMapperOptions getFileMapperOptionsByParamListItem(List<String> pathParams, File baseDir )
217+
{
218+
if (!pathParams.isEmpty()) {
219+
String sourcePath = pathParams.get(0);
220+
List<String> files = new FileWalker().getFileList(baseDir, sourcePath);
221+
return getMapperOptions(pathParams, files);
222+
}
223+
224+
return null;
172225
}
173226

174227
public List<ReporterOptions> getReporterOptionsList() {
@@ -198,6 +251,28 @@ public List<ReporterOptions> getReporterOptionsList() {
198251
return reporterOptionsList;
199252
}
200253

254+
/** Checks whether cli is compatible with the database framework
255+
*
256+
* @param conn Active Connection
257+
* @throws SQLException
258+
*/
259+
private void checkFrameworkCompatibility(Connection conn) throws SQLException {
260+
261+
if ( !skipCompatibilityCheck ) {
262+
try {
263+
DBHelper.failOnVersionCompatibilityCheckFailed(conn);
264+
} catch (DatabaseNotCompatibleException e) {
265+
System.out.println(e.getMessage());
266+
267+
throw e;
268+
}
269+
}
270+
else {
271+
System.out.println("Skipping Compatibility check with framework version, expecting the latest version " +
272+
"to be installed in database");
273+
}
274+
}
275+
201276
public FileMapperOptions getMapperOptions(List<String> mappingParams, List<String> filePaths) {
202277
FileMapperOptions mapperOptions = new FileMapperOptions(filePaths);
203278

0 commit comments

Comments
 (0)