Skip to content

Commit 95a2952

Browse files
Split prj in multiple files for better structure and testing (#17)
1 parent fb25658 commit 95a2952

9 files changed

+74
-66
lines changed

Diff for: Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ RUN chown -R app:app /usr/src/app
2020

2121
USER app
2222

23-
CMD ["/usr/src/app/pmd.groovy", "--codeFolder=/code", "--configFile=/config.json"]
23+
CMD ["/usr/src/app/pmd", "--codeFolder=/code", "--configFile=/config.json"]

Diff for: fixtures/config.new.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"enabled": true,
33
"channel": "beta",
44
"config": {
5-
"file": "fixtures/rules.xml",
5+
"file": "specified_rules.xml",
66
},
77
"include_paths": [
88
"Main.java"

Diff for: fixtures/config.old.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"enabled": true,
33
"channel": "beta",
4-
"config": "fixtures/rules.xml",
4+
"config": "specified_rules.xml",
55
"include_paths": [
66
"Main.java"
77
]

Diff for: fixtures/rules.xml renamed to fixtures/specified_rules.xml

+16-16
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ The Basic ruleset contains a collection of good practices which should be follow
99
</description>
1010

1111
<rule name="JumbledIncrementer"
12-
language="java"
13-
since="1.0"
12+
language="java"
13+
since="1.0"
1414
message="Avoid modifying an outer loop incrementer in an inner loop for update expression"
1515
class="net.sourceforge.pmd.lang.rule.XPathRule"
1616
externalInfoUrl="${pmd.website.baseurl}/rules/java/basic.html#JumbledIncrementer">
@@ -144,9 +144,9 @@ public class Foo {
144144
synchronized(this) {
145145
if (baz == null) {
146146
baz = new Object();
147-
}
148-
}
149-
}
147+
}
148+
}
149+
}
150150
return baz;
151151
}
152152
}
@@ -386,7 +386,7 @@ BigDecimal bd = new BigDecimal(12); // preferred approach, ok for integer v
386386

387387

388388
<rule name="MisplacedNullCheck"
389-
language="java"
389+
language="java"
390390
since="3.5"
391391
message="The null check here is misplaced; if the variable is null there will be a NullPointerException"
392392
class="net.sourceforge.pmd.lang.rule.XPathRule"
@@ -441,7 +441,7 @@ public class Foo {
441441

442442

443443
<rule name="AvoidThreadGroup"
444-
language="java"
444+
language="java"
445445
since="3.6"
446446
message="Avoid using java.lang.ThreadGroup; it is not thread safe"
447447
class="net.sourceforge.pmd.lang.rule.XPathRule"
@@ -527,10 +527,10 @@ bi4 = new BigInteger(0); // reference BigInteger.ZERO instead
527527
class="net.sourceforge.pmd.lang.java.rule.basic.AvoidUsingOctalValuesRule"
528528
externalInfoUrl="${pmd.website.baseurl}/rules/java/basic.html#AvoidUsingOctalValues">
529529
<description>
530-
<![CDATA[
530+
<![CDATA[
531531
Integer literals should not start with zero since this denotes that the rest of literal will be
532532
interpreted as an octal value.
533-
]]>
533+
]]>
534534
</description>
535535
<priority>3</priority>
536536
<example>
@@ -548,10 +548,10 @@ k = i * j; // set k with 80 not 120
548548
class="net.sourceforge.pmd.lang.java.rule.basic.AvoidUsingHardCodedIPRule"
549549
externalInfoUrl="${pmd.website.baseurl}/rules/java/basic.html#AvoidUsingHardCodedIP">
550550
<description>
551-
<![CDATA[
551+
<![CDATA[
552552
Application with hard-coded IP addresses can become impossible to deploy in some cases.
553553
Externalizing IP adresses is preferable.
554-
]]>
554+
]]>
555555
</description>
556556
<priority>3</priority>
557557
<properties>
@@ -567,7 +567,7 @@ public class Foo {
567567
</rule>
568568

569569
<rule name="CheckResultSet"
570-
language="java"
570+
language="java"
571571
since="4.1"
572572
class="net.sourceforge.pmd.lang.java.rule.basic.CheckResultSetRule"
573573
message="Always check the return of one of the navigation method (next,previous,first,last) of a ResultSet."
@@ -633,7 +633,7 @@ int j = -~7;
633633
</rule>
634634

635635
<rule name="ExtendsObject"
636-
language="java"
636+
language="java"
637637
since="5.0"
638638
message="No need to explicitly extend Object."
639639
class="net.sourceforge.pmd.lang.rule.XPathRule"
@@ -659,10 +659,10 @@ public class Foo extends Object { // not required
659659

660660
<rule name="CheckSkipResult"
661661
language="java"
662-
since="5.0"
662+
since="5.0"
663663
message="Check the value returned by the skip() method of an InputStream to see if the requested number of bytes has been skipped."
664664
class="net.sourceforge.pmd.lang.java.rule.basic.CheckSkipResultRule"
665-
externalInfoUrl="${pmd.website.baseurl}/rules/java/basic.html#CheckSkipResult">
665+
externalInfoUrl="${pmd.website.baseurl}/rules/java/basic.html#CheckSkipResult">
666666
<description>The skip() method may skip a smaller number of bytes than requested. Check the returned value to find out if it was the case or not.</description>
667667
<priority>3</priority>
668668
<example>
@@ -688,7 +688,7 @@ public class Foo {
688688
</rule>
689689

690690
<rule name="AvoidBranchingStatementAsLastInLoop"
691-
since="5.0"
691+
since="5.0"
692692
class="net.sourceforge.pmd.lang.java.rule.basic.AvoidBranchingStatementAsLastInLoopRule"
693693
message="Avoid using a branching statement as the last in a loop."
694694
externalInfoUrl="${pmd.website.baseurl}/rules/java/basic.html#AvoidBranchingStatementAsLastInLoop">

Diff for: pmd

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env sh
2+
3+
libs() {
4+
find $GROOVY_HOME/lib -name "*.jar" | tr "\n" ":"
5+
}
6+
7+
SRC_DIR=/usr/src/app/src
8+
9+
java -cp $SRC_DIR:$(libs) groovy.ui.GroovyMain $SRC_DIR/main.groovy $@

Diff for: pmd.groovy renamed to src/Config.groovy

+7-38
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
#!/usr/bin/env groovy
2-
31
import groovy.json.JsonSlurper
42
import groovy.util.FileNameFinder
53

64
class Config {
5+
static String DEFAULT_RULES = "/usr/src/app/ruleset.xml"
76
def args
87
def appContext
98
def parsedConfig
109
def filesToAnalyze
1110
def filesList
1211

13-
Config(args) {
14-
this.args = args
15-
this.appContext = setupContext()
12+
Config(appContext) {
13+
this.appContext = appContext
1614
this.parsedConfig = new JsonSlurper().parse(new File(appContext.configFile), "UTF-8")
1715
this.filesToAnalyze = filesToAnalyze()
1816
this.filesList = createTempFile()
@@ -27,25 +25,22 @@ class Config {
2725
if(parsedConfig.config) {
2826
def configFile = parsedConfig.config instanceof String ? parsedConfig.config : parsedConfig.config.file
2927

30-
if(fileExists(configFile)) {
31-
return configFile
28+
def specifiedRules = new File(appContext.codeFolder, configFile)
29+
if(specifiedRules.exists()) {
30+
return specifiedRules.absolutePath
3231
} else {
3332
System.err.println "Config file ${configFile} not found"
3433
System.exit(1)
3534
}
3635
}
3736

38-
"/usr/src/app/ruleset.xml"
37+
return DEFAULT_RULES
3938
}
4039

4140
def filesListPath() {
4241
filesList.absolutePath
4342
}
4443

45-
private def fileExists(file) {
46-
new File(file).exists()
47-
}
48-
4944
private def filesToAnalyze() {
5045
def includePaths = parsedConfig.include_paths?.join(" ")
5146
def codeFolder = new File(appContext.codeFolder)
@@ -69,30 +64,4 @@ class Config {
6964
tmp.deleteOnExit()
7065
tmp
7166
}
72-
73-
private def setupContext() {
74-
def cli = new CliBuilder(usage:"${this.class.name}")
75-
cli._(longOpt: "configFile", required: true, args: 1, "Path to config.json file")
76-
cli._(longOpt: "codeFolder", required: true, args: 1, "Path to code folder")
77-
cli.parse(args)
78-
}
7967
}
80-
81-
def execute(command) {
82-
ProcessBuilder builder = new ProcessBuilder(command.split(' '))
83-
def env = builder.environment()
84-
env.put("JAVA_OPTS", "-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=30")
85-
Process process = builder.start()
86-
process.consumeProcessOutput(System.out, System.err)
87-
process.waitFor()
88-
System.exit(process.exitValue())
89-
}
90-
91-
/* ********** MAIN ********** */
92-
93-
def config = new Config(args)
94-
if (config.noFiles()) {
95-
System.exit(0)
96-
}
97-
98-
execute("/usr/src/app/lib/pmd/bin/run.sh pmd -filelist ${config.filesListPath()} -f codeclimate -R ${config.ruleSet()} -failOnViolation false")

Diff for: src/main.groovy

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import groovy.util.*
2+
3+
class Main {
4+
static def execute(command) {
5+
ProcessBuilder builder = new ProcessBuilder(command.split(' '))
6+
def env = builder.environment()
7+
env.put("JAVA_OPTS", "-XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=30")
8+
Process process = builder.start()
9+
process.consumeProcessOutput(System.out, System.err)
10+
process.waitFor()
11+
System.exit(process.exitValue())
12+
}
13+
14+
static def appContext(args) {
15+
def cli = new CliBuilder(usage:"${this.class.name}")
16+
cli._(longOpt: "configFile", required: true, args: 1, "Path to config.json file")
17+
cli._(longOpt: "codeFolder", required: true, args: 1, "Path to code folder")
18+
cli.parse(args)
19+
}
20+
21+
public static void main(args) {
22+
def config = new Config(appContext(args))
23+
if (config.noFiles()) {
24+
System.exit(0)
25+
}
26+
27+
execute("/usr/src/app/lib/pmd/bin/run.sh pmd -filelist ${config.filesListPath()} -f codeclimate -R ${config.ruleSet()} -failOnViolation false")
28+
}
29+
}

Diff for: test.sh

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
set -ex
33

44
BUILD_DIR=build
5-
GROOVY="${GROOVY_HOME}/embeddable/groovy-all-${GROOVY_VERSION}.jar"
6-
JUNIT=$(ls ${GROOVY_HOME}/lib/junit*.jar)
7-
HAMCREST=$(ls ${GROOVY_HOME}/lib/hamcrest*.jar)
5+
6+
libs() {
7+
find $GROOVY_HOME/lib -name "*.jar" | tr "\n" ":"
8+
}
89

910
test_classes() {
1011
find test -name "*.groovy" | sed -E 's#test/(.*).groovy#\1#' | xargs
@@ -16,11 +17,11 @@ clean() {
1617
}
1718

1819
build() {
19-
groovyc test/**.groovy -d $BUILD_DIR
20+
groovyc src/**.groovy test/**.groovy -d $BUILD_DIR
2021
}
2122

2223
run() {
23-
java -cp build:$JUNIT:$HAMCREST:$GROOVY org.junit.runner.JUnitCore $(test_classes)
24+
java -cp build:$(libs) org.junit.runner.JUnitCore $(test_classes)
2425
}
2526

2627
clean

Diff for: test/SanityCheckTest.groovy

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SanityCheckTest {
1717

1818
@Test
1919
void sanityCheck() {
20-
def (proc, out, err) = execute("/usr/src/app/pmd.groovy --codeFolder=/usr/src/app/fixtures --configFile=/usr/src/app/fixtures/config.json")
20+
def (proc, out, err) = execute("/usr/src/app/pmd --codeFolder=/usr/src/app/fixtures --configFile=/usr/src/app/fixtures/config.json")
2121

2222
assert !out.toString().isEmpty()
2323
assert err.toString().isEmpty()
@@ -26,8 +26,8 @@ class SanityCheckTest {
2626

2727
@Test
2828
void checkConfigBackwardCompatibility() {
29-
def (proc, out, _err) = execute("/usr/src/app/pmd.groovy --codeFolder=/usr/src/app/fixtures --configFile=/usr/src/app/fixtures/config.new.json")
30-
def (procOld, outOld, _errOld) = execute("/usr/src/app/pmd.groovy --codeFolder=/usr/src/app/fixtures --configFile=/usr/src/app/fixtures/config.old.json")
29+
def (proc, out, _err) = execute("/usr/src/app/pmd --codeFolder=/usr/src/app/fixtures --configFile=/usr/src/app/fixtures/config.new.json")
30+
def (procOld, outOld, _errOld) = execute("/usr/src/app/pmd --codeFolder=/usr/src/app/fixtures --configFile=/usr/src/app/fixtures/config.old.json")
3131

3232
assert proc.exitValue() == procOld.exitValue()
3333
assert out.toString().equals(outOld.toString())
@@ -36,7 +36,7 @@ class SanityCheckTest {
3636

3737
@Test
3838
void abortOnBadConfig() {
39-
def (proc, out, err) = execute("/usr/src/app/pmd.groovy --codeFolder=/usr/src/app/fixtures --configFile=/usr/src/app/fixtures/config.bad.json")
39+
def (proc, out, err) = execute("/usr/src/app/pmd --codeFolder=/usr/src/app/fixtures --configFile=/usr/src/app/fixtures/config.bad.json")
4040

4141
assert !err.toString().isEmpty()
4242
assert proc.exitValue() != 0

0 commit comments

Comments
 (0)