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
25 final class PmdAvoidUsingHardCodedIpTest {
26
27 @Test
28 void firesOnHardCodedIpInProductionClass() throws Exception {
29 final String file = "HasHardCodedIp.java";
30 final Environment.Mock mock = new Environment.Mock();
31 final String name = String.format("src/main/java/foo/%s", file);
32 final Environment env = mock.withFile(
33 name,
34 new TextOf(
35 this.getClass().getResourceAsStream(file)
36 ).asString()
37 );
38 MatcherAssert.assertThat(
39 "AvoidUsingHardCodedIP must fire when a hard-coded IP is used in a production class",
40 new PmdValidator(env).validate(
41 Collections.singletonList(new File(env.basedir(), name))
42 ).stream().map(Violation::name).collect(Collectors.toList()),
43 Matchers.hasItem("AvoidUsingHardCodedIP")
44 );
45 }
46
47 @Test
48 void doesNotFireOnHardCodedIpInTestClass() throws Exception {
49 final String file = "HasHardCodedIpTest.java";
50 final Environment.Mock mock = new Environment.Mock();
51 final String name = String.format("src/test/java/foo/%s", file);
52 final Environment env = mock.withFile(
53 name,
54 new TextOf(
55 this.getClass().getResourceAsStream(file)
56 ).asString()
57 );
58 MatcherAssert.assertThat(
59 "AvoidUsingHardCodedIP must not fire when a hard-coded IP is used inside a unit-test class",
60 new PmdValidator(env).validate(
61 Collections.singletonList(new File(env.basedir(), name))
62 ).stream().map(Violation::name).collect(Collectors.toList()),
63 Matchers.not(Matchers.hasItem("AvoidUsingHardCodedIP"))
64 );
65 }
66 }