Skip to content

Commit 7617e78

Browse files
pesseSamuel Nitsche
authored andcommitted
Refactoring aiming for single-responsibility and IOSP
Not yet there, but a bit cleaner. Also added run-Test
1 parent 34a63e0 commit 7617e78

File tree

2 files changed

+82
-22
lines changed

2 files changed

+82
-22
lines changed

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

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,41 +91,27 @@ public List<String> getTestPaths() {
9191
public int run() throws Exception {
9292
final ConnectionInfo ci = getConnectionInfo();
9393

94-
94+
final List<Reporter> reporterList;
9595
final List<ReporterOptions> reporterOptionsList = getReporterOptionsList();
9696
final List<String> testPaths = getTestPaths();
97-
final List<Reporter> reporterList = new ArrayList<>();
9897

9998
final File baseDir = new File("").getAbsoluteFile();
10099
final FileMapperOptions[] sourceMappingOptions = {null};
101100
final FileMapperOptions[] testMappingOptions = {null};
102101

103102
final int[] returnCode = {0};
104103

105-
if (!this.sourcePathParams.isEmpty()) {
106-
String sourcePath = this.sourcePathParams.get(0);
107-
List<String> sourceFiles = new FileWalker().getFileList(baseDir, sourcePath);
108-
sourceMappingOptions[0] = getMapperOptions(this.sourcePathParams, sourceFiles);
109-
}
110-
111-
if (!this.testPathParams.isEmpty()) {
112-
String testPath = this.testPathParams.get(0);
113-
List<String> testFiles = new FileWalker().getFileList(baseDir, testPath);
114-
testMappingOptions[0] = getMapperOptions(this.testPathParams, testFiles);
115-
}
104+
sourceMappingOptions[0] = getFileMapperOptionsByParamListItem(this.sourcePathParams, baseDir);
105+
testMappingOptions[0] = getFileMapperOptionsByParamListItem(this.testPathParams, baseDir);
116106

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

120110
// First of all do a compatibility check and fail-fast
121111
checkFrameworkCompatibility(conn);
122112

123-
for (ReporterOptions ro : reporterOptionsList) {
124-
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
125-
reporter.init(conn);
126-
ro.setReporterObj(reporter);
127-
reporterList.add(reporter);
128-
}
113+
reporterList = initReporters(conn, reporterOptionsList);
114+
129115
} catch (SQLException e) {
130116
System.out.println(e.getMessage());
131117
return Cli.DEFAULT_ERROR_CODE;
@@ -154,6 +140,44 @@ public int run() throws Exception {
154140
}
155141
});
156142

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+
{
157181
// Gather each reporter results on a separate thread.
158182
for (ReporterOptions ro : reporterOptionsList) {
159183
executorService.submit(() -> {
@@ -181,10 +205,23 @@ public int run() throws Exception {
181205
}
182206
});
183207
}
208+
}
184209

185-
executorService.shutdown();
186-
executorService.awaitTermination(60, TimeUnit.MINUTES);
187-
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;
188225
}
189226

190227
public List<ReporterOptions> getReporterOptionsList() {

src/test/java/org/utplsql/cli/RunCommandTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
*/
1313
public class RunCommandTest {
1414

15+
private static String sUrl;
16+
private static String sUser;
17+
private static String sPass;
18+
19+
static {
20+
sUrl = System.getenv("DB_URL") != null ? System.getenv("DB_URL") : "192.168.99.100:1521:XE";
21+
sUser = System.getenv("DB_USER") != null ? System.getenv("DB_USER") : "app";
22+
sPass = System.getenv("DB_PASS") != null ? System.getenv("DB_PASS") : "app";
23+
}
24+
1525
private RunCommand createRunCommand(String... args) {
1626
RunCommand runCmd = new RunCommand();
1727

@@ -96,4 +106,17 @@ public void reporterOptions_TwoReporters() {
96106
Assert.assertTrue(reporterOptions2.outputToScreen());
97107
}
98108

109+
@Test
110+
public void run_Default() {
111+
RunCommand runCmd = createRunCommand(sUser + "/" + sPass + "@" + sUrl);
112+
113+
try {
114+
int result = runCmd.run();
115+
Assert.assertEquals(0, result);
116+
}
117+
catch ( Exception e ) {
118+
Assert.fail(e.getMessage());
119+
}
120+
}
121+
99122
}

0 commit comments

Comments
 (0)