Skip to content

Commit b13cd2a

Browse files
authored
fixes #4 (#25)
* Directory or includes are now optional (when the other one is set) * Add new tests * Fix a bug where files paths where relative to the plugin and not the tested project * Fix previous tests
1 parent a4d9545 commit b13cd2a

File tree

17 files changed

+242
-20
lines changed

17 files changed

+242
-20
lines changed

utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/UtPLSQLMojo.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ private FileMapperOptions buildSourcesOptions() throws MojoExecutionException {
208208
if (sources.isEmpty()) {
209209
File defaultSourceDirectory = new File(project.getBasedir(),PluginDefault.SOURCE_DIRECTORY);
210210
if (defaultSourceDirectory.exists()) {
211-
sources.add(PluginDefault.buildDefaultSource(project.getBasedir()));
211+
sources.add(PluginDefault.buildDefaultSource());
212212
} else {
213213
return new FileMapperOptions(new ArrayList<String>());
214214
}
215215
}
216216

217-
List<String> scripts = SQLScannerHelper.findSQLs(project.getBasedir(),sources);
217+
List<String> scripts = SQLScannerHelper.findSQLs(project.getBasedir(),sources,PluginDefault.SOURCE_DIRECTORY, PluginDefault.SOURCE_FILE_PATTERN);
218218
FileMapperOptions fileMapperOptions = new FileMapperOptions(scripts);
219219

220220
if (StringUtils.isNotEmpty(sourcesOwner)) {
@@ -263,15 +263,15 @@ private FileMapperOptions buildTestsOptions() throws MojoExecutionException {
263263
try {
264264
// Check if this element is empty
265265
if (tests.isEmpty()) {
266-
File defaultTestDirecyory = new File(project.getBasedir(),PluginDefault.TEST_DIRECTORY);
267-
if (defaultTestDirecyory.exists()) {
268-
tests.add(PluginDefault.buildDefaultTest(project.getBasedir()));
266+
File defaultTestDirectory = new File(project.getBasedir(),PluginDefault.TEST_DIRECTORY);
267+
if (defaultTestDirectory.exists()) {
268+
tests.add(PluginDefault.buildDefaultTest());
269269
} else {
270270
return new FileMapperOptions(new ArrayList<String>());
271271
}
272272
}
273273

274-
List<String> scripts = SQLScannerHelper.findSQLs(project.getBasedir(),tests);
274+
List<String> scripts = SQLScannerHelper.findSQLs(project.getBasedir(),tests,PluginDefault.TEST_DIRECTORY, PluginDefault.TEST_FILE_PATTERN);
275275
FileMapperOptions fileMapperOptions = new FileMapperOptions(scripts);
276276

277277
if (StringUtils.isNotEmpty(testsOwner)) {

utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/helper/PluginDefault.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public class PluginDefault
2626

2727
// Test Directory
2828
public static final String TEST_DIRECTORY = "src/test/plsql";
29+
30+
/**
31+
* Default source file pattern.
32+
*/
33+
public static final String SOURCE_FILE_PATTERN = "**/*.*";
34+
35+
/**
36+
* Default test file pattern.
37+
*/
38+
public static final String TEST_FILE_PATTERN = "**/*.pkg";
2939

3040
private PluginDefault()
3141
{
@@ -37,25 +47,25 @@ private PluginDefault()
3747
*
3848
* @return a {@link Resource}
3949
*/
40-
public static Resource buildDefaultSource(File baseDir)
50+
public static Resource buildDefaultSource()
4151
{
42-
return buildDirectory(baseDir,SOURCE_DIRECTORY, "**/*.*");
52+
return buildDirectory(SOURCE_DIRECTORY, SOURCE_FILE_PATTERN);
4353
}
4454

4555
/**
4656
* This method returns {@link Resource} for the default {@code test} directory
4757
*
4858
* @return a {@link Resource}
4959
*/
50-
public static Resource buildDefaultTest(File baseDir)
60+
public static Resource buildDefaultTest()
5161
{
52-
return buildDirectory(baseDir, TEST_DIRECTORY, "**/*.pkg");
62+
return buildDirectory(TEST_DIRECTORY, TEST_FILE_PATTERN);
5363
}
5464

55-
private static Resource buildDirectory(File baseDir, String directory, String includes)
65+
private static Resource buildDirectory(String directory, String includes)
5666
{
5767
Resource resource = new Resource();
58-
resource.setDirectory(baseDir.getAbsolutePath()+"/"+directory);
68+
resource.setDirectory(directory);
5969
resource.setIncludes(Arrays.asList(includes));
6070
return resource;
6171
}

utplsql-maven-plugin/src/main/java/org/utplsql/maven/plugin/helper/SQLScannerHelper.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@ private SQLScannerHelper() {
2727
* @param resouces
2828
* @return
2929
*/
30-
public static List<String> findSQLs(File baseDir,List<Resource> resources) {
30+
public static List<String> findSQLs(File baseDir,List<Resource> resources, String defaultDirectory, String defaultFilePattern) {
3131
List<String> founds = new ArrayList<String>();
3232

3333
for (Resource resource : resources) {
34+
if (resource.getDirectory() == null) {
35+
// use default directory if not set.
36+
resource.setDirectory(defaultDirectory);
37+
}
38+
39+
if (resource.getIncludes().isEmpty()) {
40+
// use default file pattern if not set.
41+
resource.getIncludes().add(defaultFilePattern);
42+
}
3443
// Build Scanner
35-
DirectoryScanner scanner = buildScanner(resource);
44+
DirectoryScanner scanner = buildScanner(baseDir.getPath(),resource);
3645
scanner.scan();
3746
for (String basename : scanner.getIncludedFiles()) {
3847
founds.add(baseDir.toURI().relativize(new File(scanner.getBasedir(), basename).toURI()).getPath());
@@ -51,16 +60,16 @@ public static List<String> findSQLs(File baseDir,List<Resource> resources) {
5160
* @param resource
5261
* @return
5362
*/
54-
private static DirectoryScanner buildScanner(Resource resource) {
63+
private static DirectoryScanner buildScanner(String baseDir,Resource resource) {
5564
if (resource != null) {
56-
File baseDir = new File(resource.getDirectory());
57-
if (!baseDir.exists() || !baseDir.isDirectory() || !baseDir.canRead()) {
65+
File fileBaseDir = new File(baseDir,resource.getDirectory());
66+
if (!fileBaseDir.exists() || !fileBaseDir.isDirectory() || !fileBaseDir.canRead()) {
5867
throw new IllegalArgumentException(
5968
format("Invalid <directory> %s in resource. Check your pom.xml", resource.getDirectory()));
6069
}
6170

6271
DirectoryScanner scanner = new DirectoryScanner();
63-
scanner.setBasedir(resource.getDirectory());
72+
scanner.setBasedir(fileBaseDir.getPath());
6473
scanner.setIncludes(resource.getIncludes().toArray(new String[0]));
6574
scanner.setExcludes(resource.getExcludes().toArray(new String[0]));
6675
return scanner;

utplsql-maven-plugin/src/test/java/org/utplsql/maven/plugin/test/UtPLSQLMojoTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,57 @@ public void testSourcesAndTestsParameterDoesNotExistButDefaultDirectoryExists()
158158
assertTrue(tests.getFilePaths().contains("src/test/plsql/f2.pkg"));
159159

160160
}
161+
162+
/**
163+
* testSourcesAndTestsParameterHaveNotDirectoryTag.
164+
*
165+
* Given : a pom.xml with source and test tag not containing a directory tag.
166+
* When : pom is read and buildSourcesOptions / buildTestsOptions are run
167+
* Then : it should find all sources/tests files in default directories
168+
*/
169+
@Test
170+
public void testSourcesAndTestsParameterHaveNotDirectoryTag() throws Exception {
171+
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule.lookupConfiguredMojo(new File("src/test/resources/partialSourceAndTestTag/missingDirectory/"), "test");
172+
Assert.assertNotNull(utplsqlMojo);
173+
174+
// check sources
175+
FileMapperOptions sources = Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions");
176+
assertEquals(2, sources.getFilePaths().size());
177+
assertTrue(sources.getFilePaths().contains("src/main/plsql/f1.sql"));
178+
assertTrue(sources.getFilePaths().contains("src/main/plsql/foo/f2.sql"));
179+
180+
// check tests
181+
FileMapperOptions tests = Whitebox.invokeMethod(utplsqlMojo, "buildTestsOptions");
182+
assertEquals(3, tests.getFilePaths().size());
183+
assertTrue(tests.getFilePaths().contains("src/test/plsql/foo/f1.pkg"));
184+
assertTrue(tests.getFilePaths().contains("src/test/plsql/f2.pkg"));
185+
assertTrue(tests.getFilePaths().contains("src/test/plsql/foo/f1.sql"));
186+
}
187+
188+
/**
189+
* testSourcesAndTestsParameterHaveNotDirectoryTag.
190+
*
191+
* Given : a pom.xml with source and test tag not containing a directory tag.
192+
* When : pom is read and buildSourcesOptions / buildTestsOptions are run
193+
* Then : it should find all sources/tests files in default directories
194+
*/
195+
@Test
196+
public void testSourcesAndTestsParameterHaveNotIncludesTag() throws Exception {
197+
UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo) rule.lookupConfiguredMojo(new File("src/test/resources/partialSourceAndTestTag/missingIncludes/"), "test");
198+
Assert.assertNotNull(utplsqlMojo);
199+
200+
// check sources
201+
FileMapperOptions sources = Whitebox.invokeMethod(utplsqlMojo, "buildSourcesOptions");
202+
assertEquals(2, sources.getFilePaths().size());
203+
assertTrue(sources.getFilePaths().contains("src/main/foo/f1.sql"));
204+
assertTrue(sources.getFilePaths().contains("src/main/foo/foo/f2.sql"));
205+
206+
// check tests
207+
FileMapperOptions tests = Whitebox.invokeMethod(utplsqlMojo, "buildTestsOptions");
208+
assertEquals(2, tests.getFilePaths().size());
209+
assertTrue(tests.getFilePaths().contains("src/test/bar/foo/f1.pkg"));
210+
assertTrue(tests.getFilePaths().contains("src/test/bar/f2.pkg"));
211+
}
212+
213+
161214
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
7+
<groupId>org.utplsql</groupId>
8+
<artifactId>utplsql-maven-plugin-test</artifactId>
9+
<version>3.1.0-SNAPSHOT</version>
10+
11+
<packaging>pom</packaging>
12+
13+
<name>utplsql-maven-plugin Maven Plugin Test</name>
14+
15+
<url>http://utplsql.org</url>
16+
17+
<properties>
18+
<dbUrl>jdbc:oracle:thin:@180.129.3.101:1521:xe</dbUrl>
19+
<dbUser>ut3</dbUser>
20+
<dbPass>XNtxj8eEgA6X6b6f</dbPass>
21+
<project.basedir>/</project.basedir>
22+
</properties>
23+
24+
<build>
25+
26+
<plugins>
27+
<plugin>
28+
<groupId>org.utplsql</groupId>
29+
<artifactId>utplsql-maven-plugin</artifactId>
30+
<version>{project.version}</version>
31+
<goals>
32+
<goal>test</goal>
33+
</goals>
34+
<configuration>
35+
<!-- Mandatory Attributes -->
36+
37+
<ignoreFailure>false</ignoreFailure>
38+
39+
<paths>
40+
<path>:plsql</path>
41+
</paths>
42+
43+
<reporters>
44+
<reporter>
45+
<name>UT_COVERAGE_SONAR_REPORTER</name>
46+
<fileOutput>utplsql/coverage-sonar-reporter.xml</fileOutput>
47+
<consoleOutput>true</consoleOutput>
48+
</reporter>
49+
<reporter>
50+
<name>UT_SONAR_TEST_REPORTER</name>
51+
<fileOutput>utplsql/sonar-test-reporter.xml</fileOutput>
52+
<consoleOutput>true</consoleOutput>
53+
</reporter>
54+
</reporters>
55+
56+
<sources>
57+
<source>
58+
<includes>
59+
<include>**/*sql</include>
60+
</includes>
61+
</source>
62+
</sources>
63+
<tests>
64+
<test>
65+
<includes>
66+
<include>**/*sql</include>
67+
68+
</includes>
69+
</test>
70+
<test>
71+
<includes>
72+
<include>**/*.pkg</include>
73+
</includes>
74+
</test>
75+
</tests>
76+
</configuration>
77+
</plugin>
78+
</plugins>
79+
</build>
80+
</project>

utplsql-maven-plugin/src/test/resources/partialSourceAndTestTag/missingDirectory/src/main/plsql/f1.sql

Whitespace-only changes.

utplsql-maven-plugin/src/test/resources/partialSourceAndTestTag/missingDirectory/src/main/plsql/foo/f2.sql

Whitespace-only changes.

utplsql-maven-plugin/src/test/resources/partialSourceAndTestTag/missingDirectory/src/test/plsql/f2.pkg

Whitespace-only changes.

utplsql-maven-plugin/src/test/resources/partialSourceAndTestTag/missingDirectory/src/test/plsql/foo/f1.pkg

Whitespace-only changes.

utplsql-maven-plugin/src/test/resources/partialSourceAndTestTag/missingDirectory/src/test/plsql/foo/f1.sql

Whitespace-only changes.

0 commit comments

Comments
 (0)