Skip to content

Commit 453e144

Browse files
committed
added test for gradle task
1 parent b654ab3 commit 453e144

File tree

6 files changed

+126
-82
lines changed

6 files changed

+126
-82
lines changed
Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,49 @@
11
buildscript {
22
repositories {
3-
mavenLocal()
3+
flatDir dirs: "../../jcp/target"
44
}
5-
65
dependencies {
7-
classpath group: 'com.igormaznitsa', name: 'jcp', version: '7.0.0-SNAPSHOT'
6+
classpath "com.igormaznitsa:jcp:7.0.0-SNAPSHOT"
87
}
98
}
109

11-
1210
apply plugin: 'java'
1311
apply plugin: 'application'
1412
apply plugin: 'com.igormaznitsa.jcp'
1513

1614
mainClassName = 'hello.world'
1715

16+
sourceCompatibility = JavaVersion.VERSION_1_7
17+
targetCompatibility = JavaVersion.VERSION_1_7
18+
1819
repositories {
20+
mavenLocal()
1921
mavenCentral()
2022
}
2123

2224
dependencies {
23-
def junit4Version = '4.12'
24-
def junitVintageVersion = '5.4.0'
25-
def junitJupiterVersion = '5.4.0'
26-
def junitPlatformVersion = '1.4.0'
27-
28-
testImplementation("org.junit.jupiter:junit-jupiter:${junitJupiterVersion}")
29-
30-
testCompile("junit:junit:${junit4Version}")
31-
testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")
32-
33-
testRuntime("org.junit.platform:junit-platform-launcher:${junitPlatformVersion}")
25+
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.4.0'
3426
}
3527

36-
test {
37-
useJUnitPlatform {
38-
// includeEngines 'junit-jupiter', 'junit-vintage'
39-
// excludeEngines 'custom-engine'
40-
41-
// includeTags 'fast'
42-
excludeTags 'slow'
43-
}
44-
45-
testLogging {
46-
events 'passed', 'skipped', 'failed'
47-
}
28+
jar {
29+
manifest {
30+
attributes(
31+
'Main-Class': 'com.igormaznitsa.jcp.it.gradle.MainTwo'
32+
)
33+
}
4834
}
4935

50-
5136
preprocessSettings {
37+
def targetFolder = 'build/preprocessed/java'
5238

39+
allowWhitespaces = true
40+
41+
sources = ['src/main/java']
42+
target = file(targetFolder)
43+
vars = ['some.test.global': 'Some Test Global Value']
44+
45+
sourceSets.main.java.srcDirs = [targetFolder]
5346
}
5447

55-
compileJava.dependsOn preprocess
48+
compileJava.dependsOn preprocess
49+

jcp-tests/jcp-test-gradle/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@
9191
<gradleVersion>5.0</gradleVersion>
9292
<args>
9393
<arg>--stacktrace</arg>
94+
<arg>--info</arg>
9495
</args>
9596
<tasks>
9697
<task>clean</task>
9798
<task>test</task>
99+
<task>jar</task>
98100
</tasks>
99101
</configuration>
100102
<executions>

jcp-tests/jcp-test-gradle/src/main/java/com/igormaznitsa/jcp/it/gradle/Main.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ public Main(){
1515
final String test = /*$"\""+some.test.global+"\";"$*/ /*-*/ "";
1616

1717
if ("Some Test Global Value".equals(test)){
18-
System.out.println("All ok");
18+
System.out.println("All ok, detected value '/*$some.test.global$*/'");
1919
} else {
2020
throw new Error("Unexpected value: "+test);
2121
}
2222
}
23+
24+
public String getValue(){
25+
return /*$"\""+some.test.global+"\";"$*/ /*-*/ "";
26+
}
27+
28+
public static void main(String [] args) {
29+
//$$new MainTwo();
30+
//#//
31+
new Main();
32+
}
2333
}

jcp-tests/jcp-test-gradle/src/test/java/com/igormaznitsa/jcp/it/gradle/TestMain.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@
66

77
public class TestMain {
88

9-
// @Test
10-
// public void testMain(){
11-
// //$new MainTwo();
12-
// //#//
13-
// new Main();
14-
// String str = /*$"\""+some.test.global.test+"\";"$*/ /*-*/"";
15-
// assertEquals("Some Test", str);
16-
// System.out.println(str);
17-
// }
9+
@Test
10+
public void testMain(){
11+
assertEquals("Some Test", new MainTwo().getValue());
12+
}
1813

1914
}

jcp/src/main/java/com/igormaznitsa/jcp/gradle/JcpPreprocessExtension.java

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44
import org.gradle.api.Project;
55

66
import java.io.File;
7+
import java.nio.charset.Charset;
78
import java.nio.charset.StandardCharsets;
89
import java.util.ArrayList;
9-
import java.util.Arrays;
10-
import java.util.Collections;
1110
import java.util.HashMap;
1211
import java.util.List;
1312
import java.util.Map;
13+
import org.gradle.execution.commandline.TaskConfigurationException;
1414

1515
@Data
1616
public class JcpPreprocessExtension {
1717

1818
/**
19-
* Source root folders for preprocessing, if it is empty then project provided folders will be used.
20-
*
21-
* @since 7.0.0
19+
* Source root folders for preprocessing, if it is empty then project provided
20+
* folders will be used.
2221
*/
2322
private List<String> sources = null;
2423

2524
/**
26-
* End of line string to be used in reprocessed results. It supports java escaping chars.
25+
* End of line string to be used in reprocessed results. It supports java
26+
* escaping chars.
2727
*/
2828
private String eol = null;
2929

@@ -33,15 +33,11 @@ public class JcpPreprocessExtension {
3333
private boolean keepAttributes = false;
3434

3535
/**
36-
* Target folder to place preprocessing result in regular source processing phase.
36+
* Target folder to place preprocessing result in regular source processing
37+
* phase.
3738
*/
3839
private File target = null;
3940

40-
/**
41-
* Target folder to place preprocessing result in test source processing phase.
42-
*/
43-
private File targetTest = null;
44-
4541
/**
4642
* Encoding for text read operations.
4743
*/
@@ -53,19 +49,22 @@ public class JcpPreprocessExtension {
5349
private String targetEncoding = StandardCharsets.UTF_8.name();
5450

5551
/**
56-
* Flag to ignore missing source folders, if false then mojo fail for any missing source folder, if true then missing folder will be ignored.
52+
* Flag to ignore missing source folders, if false then mojo fail for any
53+
* missing source folder, if true then missing folder will be ignored.
5754
*/
5855
private boolean ignoreMissingSources = false;
5956

6057
/**
61-
* List of file extensions to be excluded from preprocessing. By default excluded xml.
58+
* List of file extensions to be excluded from preprocessing. By default
59+
* excluded xml.
6260
*/
63-
private List<String> excludeExtensions = Collections.singletonList("xml");
61+
private List<String> excludeExtensions = null;
6462

6563
/**
66-
* List of file extensions to be included into preprocessing. By default java,txt,htm,html
64+
* List of file extensions to be included into preprocessing. By default
65+
* java,txt,htm,html
6766
*/
68-
private List<String> extensions = new ArrayList<>(Arrays.asList("java", "txt", "htm", "html"));
67+
private List<String> extensions = null;
6968

7069
/**
7170
* Interpretate unknown variable as containing boolean false flag.
@@ -90,18 +89,13 @@ public class JcpPreprocessExtension {
9089
/**
9190
* Set base directory which will be used for relative source paths.
9291
*/
93-
private File baseDir = new File(".");
92+
private File baseDir = null;
9493

9594
/**
9695
* Carefully reproduce last EOL in result files.
9796
*/
9897
private boolean careForLastEol = false;
9998

100-
/**
101-
* Replace source root folders in maven project after preprocessing for following processing.
102-
*/
103-
private boolean replaceSources = true;
104-
10599
/**
106100
* Keep comments in result files.
107101
*/
@@ -113,7 +107,8 @@ public class JcpPreprocessExtension {
113107
private Map<String, String> vars = new HashMap<>();
114108

115109
/**
116-
* List of patterns of folder paths to be excluded from preprocessing, It uses ANT path pattern format.
110+
* List of patterns of folder paths to be excluded from preprocessing, It uses
111+
* ANT path pattern format.
117112
*/
118113
private List<String> excludeFolders = new ArrayList<>();
119114

@@ -123,17 +118,20 @@ public class JcpPreprocessExtension {
123118
private List<String> configFiles = new ArrayList<>();
124119

125120
/**
126-
* Keep preprocessing directives in result files as commented ones, it is useful to not break line numeration in result files.
121+
* Keep preprocessing directives in result files as commented ones, it is
122+
* useful to not break line numeration in result files.
127123
*/
128124
private boolean keepLines = true;
129125

130126
/**
131-
* Turn on support of white spaces in preprocessor directives between '//' and the '#'.
127+
* Turn on support of white spaces in preprocessor directives between '//' and
128+
* the '#'.
132129
*/
133130
private boolean allowWhitespaces = false;
134131

135132
/**
136-
* Preserve indents in lines marked by '//$' and '//$$' directives. Directives will be replaced by white spaces chars.
133+
* Preserve indents in lines marked by '//$' and '//$$' directives. Directives
134+
* will be replaced by white spaces chars.
137135
*/
138136
private boolean preserveIndents = false;
139137

@@ -148,19 +146,48 @@ public class JcpPreprocessExtension {
148146
private boolean skip = false;
149147

150148
/**
151-
* Turn on check of content body compare with existing result file to prevent overwriting, if content is the same then preprocessor will not be writing new result content.
149+
* Turn on check of content body compare with existing result file to prevent
150+
* overwriting, if content is the same then preprocessor will not be writing
151+
* new result content.
152152
*/
153153
private boolean dontOverwriteSameContent = false;
154154

155-
156155
public JcpPreprocessExtension(final Project project) {
156+
if (this.baseDir == null) {
157+
this.baseDir = project.getProjectDir();
158+
project.getLogger().debug("Got basedir from project: " + this.baseDir);
159+
}
157160
}
158161

159-
public void validate(final Project project) {
160-
// if (this.sources == null) {
161-
// throw new GradleException("Sources are not provided");
162-
// }
162+
private void assertCharSet(final String name) {
163+
if (!Charset.isSupported(name)) {
164+
throw new TaskConfigurationException("preprocess", "Unsupported charset: " + name, null);
165+
}
163166
}
164167

168+
public void validate(final Project project) {
169+
if (this.baseDir == null) {
170+
throw new TaskConfigurationException("preprocess", "Basedir must be defined", null);
171+
}
172+
173+
if (!this.baseDir.isDirectory()) {
174+
throw new TaskConfigurationException("preprocess", "Basedir doesn't exist: " + this.baseDir, null);
175+
}
176+
177+
assertCharSet(this.sourceEncoding);
178+
assertCharSet(this.targetEncoding);
179+
180+
if (this.sources == null) {
181+
throw new TaskConfigurationException("preprocess", "Source folders are not deined in 'sources'", null);
182+
}
183+
184+
if (this.sources.isEmpty()) {
185+
throw new TaskConfigurationException("preprocess", "Source folders are empty", null);
186+
}
187+
188+
if (this.target == null) {
189+
throw new TaskConfigurationException("preprocess", "Target folder is not deined in 'target'", null);
190+
}
191+
}
165192

166193
}

0 commit comments

Comments
 (0)