1
2
3
4
5 package com.qulice.pmd;
6
7 import com.qulice.spi.Environment;
8 import com.qulice.spi.Violation;
9 import java.io.File;
10 import java.util.Collections;
11 import java.util.stream.Collectors;
12 import org.cactoos.text.TextOf;
13 import org.hamcrest.MatcherAssert;
14 import org.hamcrest.Matchers;
15 import org.junit.jupiter.api.Test;
16
17
18
19
20
21
22
23
24 final class PmdAvoidUsingHardCodedIpTest {
25
26 @Test
27 void firesOnHardCodedIpInProductionClass() throws Exception {
28 final String file = "HasHardCodedIp.java";
29 final Environment.Mock mock = new Environment.Mock();
30 final String name = String.format("src/main/java/foo/%s", file);
31 final Environment env = mock.withFile(
32 name,
33 new TextOf(
34 this.getClass().getResourceAsStream(file)
35 ).asString()
36 );
37 MatcherAssert.assertThat(
38 "AvoidUsingHardCodedIP must fire when a hard-coded IP is used in a production class",
39 new PmdValidator(env).validate(
40 Collections.singletonList(new File(env.basedir(), name))
41 ).stream().map(Violation::name).collect(Collectors.toList()),
42 Matchers.hasItem("AvoidUsingHardCodedIP")
43 );
44 }
45
46 @Test
47 void doesNotFireOnHardCodedIpInTestClass() throws Exception {
48 final String file = "HasHardCodedIpTest.java";
49 final Environment.Mock mock = new Environment.Mock();
50 final String name = String.format("src/test/java/foo/%s", file);
51 final Environment env = mock.withFile(
52 name,
53 new TextOf(
54 this.getClass().getResourceAsStream(file)
55 ).asString()
56 );
57 MatcherAssert.assertThat(
58 "AvoidUsingHardCodedIP must not fire when a hard-coded IP is used inside a unit-test class",
59 new PmdValidator(env).validate(
60 Collections.singletonList(new File(env.basedir(), name))
61 ).stream().map(Violation::name).collect(Collectors.toList()),
62 Matchers.not(Matchers.hasItem("AvoidUsingHardCodedIP"))
63 );
64 }
65 }