Skip to content

Commit 24c47c2

Browse files
committed
Merge branch 'gh188_codenarc'
Add support for checking Groovy code by CodeNarc. Unfortunately, I have to put a lot of @SuppressWarnings annotations to suppress some checks that I'm not interesting in. It turned out that I can't suppress them in codenarc.properties file because doesn't support it (gleclaire/codenarc-maven-plugin#14). Also I found one false positive in CodeNarc itself (CodeNarc/CodeNarc#176) that I also suppressed. Fix #188
2 parents 976c437 + 52498c7 commit 24c47c2

22 files changed

+411
-189
lines changed

pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@
378378
<cglib.version>2.2.2</cglib.version>
379379
<checkstyle.plugin.version>2.17</checkstyle.plugin.version>
380380
<clean.plugin.version>3.0.0</clean.plugin.version>
381+
<codenarc.plugin.version>0.22-1</codenarc.plugin.version>
382+
<codenarc.version>0.25.2</codenarc.version>
381383

382384
<!-- Redefine default value from spring-boot-dependencies (https://github.com/spring-projects/spring-boot/blob/v1.4.1.RELEASE/spring-boot-dependencies/pom.xml) -->
383385
<commons-dbcp.version>1.4</commons-dbcp.version>
@@ -778,6 +780,24 @@
778780
</executions>
779781
</plugin>
780782

783+
<!--
784+
Usage:
785+
mvn codenarc:codenarc (checks the sources and generates report in target/site/codenarc.html
786+
-->
787+
<plugin>
788+
<groupId>org.codehaus.mojo</groupId>
789+
<artifactId>codenarc-maven-plugin</artifactId>
790+
<version>${codenarc.plugin.version}</version>
791+
<configuration>
792+
<codeNarcVersion>${codenarc.version}</codeNarcVersion>
793+
<groovyVersion>${groovy.version}</groovyVersion>
794+
<sourceDirectory>src/test/groovy</sourceDirectory>
795+
<rulesetfiles>
796+
rulesets/basic.xml,rulesets/braces.xml,rulesets/concurrency.xml,rulesets/convention.xml,rulesets/design.xml,rulesets/dry.xml,rulesets/enhanced.xml,rulesets/exceptions.xml,rulesets/formatting.xml,rulesets/generic.xml,rulesets/groovyism.xml,rulesets/imports.xml,rulesets/jdbc.xml,rulesets/logging.xml,rulesets/naming.xml,rulesets/security.xml,rulesets/serialization.xml,rulesets/unnecessary.xml,rulesets/unused.xml
797+
</rulesetfiles>
798+
</configuration>
799+
</plugin>
800+
781801
<plugin>
782802
<groupId>org.codehaus.mojo</groupId>
783803
<artifactId>native2ascii-maven-plugin</artifactId>

src/main/scripts/ci/check-build-and-verify.sh

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fi
99

1010
CS_FAIL=
1111
PMD_FAIL=
12+
CODENARC_FAIL=
1213
LICENSE_FAIL=
1314
POM_FAIL=
1415
BOOTLINT_FAIL=
@@ -20,6 +21,11 @@ VERIFY_FAIL=
2021
if [ "$RUN_ONLY_INTEGRATION_TESTS" = 'no' ]; then
2122
mvn --batch-mode checkstyle:check -Dcheckstyle.violationSeverity=warning >cs.log 2>&1 || CS_FAIL=yes
2223
mvn --batch-mode pmd:check >pmd.log 2>&1 || PMD_FAIL=yes
24+
mvn --batch-mode codenarc:codenarc \
25+
-Dcodenarc.maxPriority1Violations=0 \
26+
-Dcodenarc.maxPriority2Violations=0 \
27+
-Dcodenarc.maxPriority3Violations=0 \
28+
>codenarc.log 2>&1 || CODENARC_FAIL=yes
2329
mvn --batch-mode license:check >license.log 2>&1 || LICENSE_FAIL=yes
2430
mvn --batch-mode sortpom:verify -Dsort.verifyFail=stop >pom.log || POM_FAIL=yes
2531
find src -type f -name '*.html' | xargs bootlint >bootlint.log 2>&1 || BOOTLINT_FAIL=yes
@@ -46,6 +52,7 @@ echo
4652
if [ "$RUN_ONLY_INTEGRATION_TESTS" = 'no' ]; then
4753
print_status "$CS_FAIL" 'Run CheckStyle'
4854
print_status "$PMD_FAIL" 'Run PMD'
55+
print_status "$CODENARC_FAIL" 'Run CodeNarc'
4956
print_status "$LICENSE_FAIL" 'Check license headers'
5057
print_status "$POM_FAIL" 'Check sorting of pom.xml'
5158
print_status "$BOOTLINT_FAIL" 'Run bootlint'
@@ -61,6 +68,7 @@ echo
6168
if [ "$RUN_ONLY_INTEGRATION_TESTS" = 'no' ]; then
6269
print_log cs.log 'Run CheckStyle'
6370
print_log pmd.log 'Run PMD'
71+
print_log codenarc.log 'Run CodeNarc'
6472
print_log license.log 'Check license headers'
6573
print_log pom.log 'Check sorting of pom.xml'
6674
print_log bootlint.log 'Run bootlint'
@@ -71,8 +79,8 @@ fi
7179

7280
print_log verify.log 'Run integration tests'
7381

74-
rm -f cs.log pmd.log license.log bootlint.log jasmine.log validator.log test.log verify.log
82+
rm -f cs.log pmd.log codenarc.log license.log bootlint.log jasmine.log validator.log test.log verify.log
7583

76-
if [ -n "$CS_FAIL$PMD_FAIL$LICENSE_FAIL$POM_FAIL$BOOTLINT_FAIL$JASMINE_FAIL$HTML_FAIL$TEST_FAIL$VERIFY_FAIL" ]; then
84+
if [ -n "$CS_FAIL$PMD_FAIL$CODENARC_FAIL$LICENSE_FAIL$POM_FAIL$BOOTLINT_FAIL$JASMINE_FAIL$HTML_FAIL$TEST_FAIL$VERIFY_FAIL" ]; then
7785
exit 1
7886
fi

src/test/groovy/ru/mystamps/web/service/CategoryServiceImplTest.groovy

+20-5
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ import ru.mystamps.web.dao.dto.LinkEntityDto
2727
import ru.mystamps.web.tests.DateUtils
2828
import ru.mystamps.web.util.SlugUtils
2929

30+
@SuppressWarnings(['ClassJavadoc', 'MethodName', 'NoDef', 'NoTabCharacter', 'TrailingWhitespace'])
3031
class CategoryServiceImplTest extends Specification {
3132

32-
private AddCategoryForm form
33-
private Integer userId = 123
33+
private final Integer userId = 123
34+
private final CategoryDao categoryDao = Mock()
35+
private final CategoryService service = new CategoryServiceImpl(categoryDao)
3436

35-
private CategoryDao categoryDao = Mock()
36-
private CategoryService service = new CategoryServiceImpl(categoryDao)
37+
private AddCategoryForm form
3738

3839
def setup() {
3940
form = new AddCategoryForm()
@@ -92,6 +93,7 @@ class CategoryServiceImplTest extends Specification {
9293
actualSlug == expectedSlug
9394
}
9495

96+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
9597
def "add() should pass English category name to dao"() {
9698
given:
9799
String expectedCategoryName = 'Animals'
@@ -105,6 +107,7 @@ class CategoryServiceImplTest extends Specification {
105107
}) >> 20
106108
}
107109

110+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
108111
def "add() should pass Russian category name to dao"() {
109112
given:
110113
String expectedCategoryName = 'Животные'
@@ -127,9 +130,10 @@ class CategoryServiceImplTest extends Specification {
127130
thrown IllegalArgumentException
128131
}
129132

133+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
130134
def "add() should pass slug to dao"() {
131135
given:
132-
String name = "-foo123 test_"
136+
String name = '-foo123 test_'
133137
and:
134138
String slug = SlugUtils.slugify(name)
135139
and:
@@ -143,6 +147,7 @@ class CategoryServiceImplTest extends Specification {
143147
}) >> 40
144148
}
145149

150+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
146151
def "add() should assign created at to current date"() {
147152
when:
148153
service.add(form, userId)
@@ -153,6 +158,7 @@ class CategoryServiceImplTest extends Specification {
153158
}) >> 50
154159
}
155160

161+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
156162
def "add() should assign updated at to current date"() {
157163
when:
158164
service.add(form, userId)
@@ -163,6 +169,7 @@ class CategoryServiceImplTest extends Specification {
163169
}) >> 60
164170
}
165171

172+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
166173
def "add() should assign created by to user"() {
167174
given:
168175
Integer expectedUserId = 10
@@ -175,6 +182,7 @@ class CategoryServiceImplTest extends Specification {
175182
}) >> 70
176183
}
177184

185+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
178186
def "add() should assign updated by to user"() {
179187
given:
180188
Integer expectedUserId = 20
@@ -207,6 +215,7 @@ class CategoryServiceImplTest extends Specification {
207215
}
208216

209217
@Unroll
218+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
210219
def "findAllAsLinkEntities(String) should pass language '#expectedLanguage' to dao"(String expectedLanguage) {
211220
when:
212221
service.findAllAsLinkEntities(expectedLanguage)
@@ -238,6 +247,7 @@ class CategoryServiceImplTest extends Specification {
238247
null | _
239248
}
240249

250+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
241251
def "findOneAsLinkEntity() should pass arguments to dao"() {
242252
given:
243253
String expectedSlug = 'people'
@@ -288,6 +298,7 @@ class CategoryServiceImplTest extends Specification {
288298
thrown IllegalArgumentException
289299
}
290300

301+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
291302
def "countCategoriesOf() should pass arguments to dao"() {
292303
given:
293304
Integer expectedCollectionId = 10
@@ -320,6 +331,7 @@ class CategoryServiceImplTest extends Specification {
320331
result == 2L
321332
}
322333

334+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
323335
def "countByName() should pass category name to dao in lowercase"() {
324336
when:
325337
service.countByName('Sport')
@@ -350,6 +362,7 @@ class CategoryServiceImplTest extends Specification {
350362
result == 2L
351363
}
352364

365+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
353366
def "countByNameRu() should pass category name to dao in lowercase"() {
354367
when:
355368
service.countByNameRu('Спорт')
@@ -371,6 +384,7 @@ class CategoryServiceImplTest extends Specification {
371384
thrown IllegalArgumentException
372385
}
373386

387+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
374388
def "countAddedSince() should invoke dao, pass argument and return result from dao"() {
375389
given:
376390
Date expectedDate = new Date()
@@ -398,6 +412,7 @@ class CategoryServiceImplTest extends Specification {
398412
thrown IllegalArgumentException
399413
}
400414

415+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
401416
def "getStatisticsOf() should pass arguments to dao"() {
402417
given:
403418
Integer expectedCollectionId = 15

src/test/groovy/ru/mystamps/web/service/CollectionServiceImplTest.groovy

+15-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ import ru.mystamps.web.dao.dto.CollectionInfoDto
2626
import ru.mystamps.web.dao.dto.UrlEntityDto
2727
import ru.mystamps.web.util.SlugUtils
2828

29+
@SuppressWarnings(['ClassJavadoc', 'MethodName', 'NoDef', 'NoTabCharacter', 'TrailingWhitespace'])
2930
class CollectionServiceImplTest extends Specification {
30-
private CollectionDao collectionDao = Mock()
31+
32+
private final CollectionDao collectionDao = Mock()
3133

3234
private CollectionService service
3335

@@ -39,27 +41,31 @@ class CollectionServiceImplTest extends Specification {
3941
// Tests for createCollection()
4042
//
4143

44+
@SuppressWarnings('FactoryMethodName')
4245
def "createCollection() should throw exception when owner id is null"() {
4346
when:
4447
service.createCollection(null, 'test-owner-login')
4548
then:
4649
thrown IllegalArgumentException
4750
}
4851

52+
@SuppressWarnings('FactoryMethodName')
4953
def "createCollection() should throw exception when owner login is null"() {
5054
when:
5155
service.createCollection(123, null)
5256
then:
5357
thrown IllegalArgumentException
5458
}
5559

60+
@SuppressWarnings('FactoryMethodName')
5661
def "createCollection() should throw exception when owner login can't be converted to slug"() {
5762
when:
5863
service.createCollection(123, '')
5964
then:
6065
thrown IllegalArgumentException
6166
}
6267

68+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'FactoryMethodName', 'UnnecessaryReturnKeyword'])
6369
def "createCollection() should pass owner id to dao"() {
6470
given:
6571
Integer expectedOwnerId = 123
@@ -72,6 +78,7 @@ class CollectionServiceImplTest extends Specification {
7278
}) >> 100
7379
}
7480

81+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'FactoryMethodName', 'UnnecessaryReturnKeyword'])
7582
def "createCollection() should pass slugified owner login to dao"() {
7683
given:
7784
String ownerLogin = 'Test User'
@@ -104,6 +111,7 @@ class CollectionServiceImplTest extends Specification {
104111
thrown IllegalArgumentException
105112
}
106113

114+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
107115
def "addToCollection() should pass arguments to dao"() {
108116
given:
109117
Integer expectedUserId = 123
@@ -138,7 +146,7 @@ class CollectionServiceImplTest extends Specification {
138146
thrown IllegalArgumentException
139147
}
140148

141-
149+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
142150
def "removeFromCollection() should find collection by user id"() {
143151
given:
144152
Integer expectedUserId = 123
@@ -151,13 +159,14 @@ class CollectionServiceImplTest extends Specification {
151159
}) >> TestObjects.createUrlEntityDto()
152160
}
153161

162+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
154163
def "removeFromCollection() should remove series from collection"() {
155164
given:
156165
Integer expectedSeriesId = 456
157166
and:
158167
UrlEntityDto url = TestObjects.createUrlEntityDto()
159168
and:
160-
Integer expectedCollectionId = url.getId()
169+
Integer expectedCollectionId = url.id
161170
and:
162171
collectionDao.findCollectionUrlEntityByUserId(_ as Integer) >> url
163172
when:
@@ -205,6 +214,7 @@ class CollectionServiceImplTest extends Specification {
205214
0 * collectionDao.isSeriesInUserCollection(_ as Integer, _ as Integer)
206215
}
207216

217+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
208218
def "isSeriesInCollection() should pass arguments to dao"() {
209219
given:
210220
Integer expectedUserId = 123
@@ -257,6 +267,7 @@ class CollectionServiceImplTest extends Specification {
257267
0 | _
258268
}
259269

270+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
260271
def "findRecentlyCreated() should pass arguments to dao"() {
261272
given:
262273
int expectedQuantity = 4
@@ -280,6 +291,7 @@ class CollectionServiceImplTest extends Specification {
280291
thrown IllegalArgumentException
281292
}
282293

294+
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
283295
def "findBySlug() should invoke dao, pass argument and return result from dao"() {
284296
given:
285297
String expectedSlug = 'cuba'

0 commit comments

Comments
 (0)