aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleKeyTest.java
blob: 9996f1019df55fbe73fa6647ccde4ee2ea38adf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * SonarQube
 * Copyright (C) 2009-2019 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.api.rule;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

public class RuleKeyTest {

  @Test
  public void testOf() throws Exception {
    RuleKey key = RuleKey.of("squid", "NullDeref");
    assertThat(key.repository()).isEqualTo("squid");
    assertThat(key.rule()).isEqualTo("NullDeref");
  }

  @Test
  public void key_can_contain_colons() {
    RuleKey key = RuleKey.of("squid", "Key:With:Some::Colons");
    assertThat(key.repository()).isEqualTo("squid");
    assertThat(key.rule()).isEqualTo("Key:With:Some::Colons");
  }

  @Test
  public void repository_must_not_be_null() {
    try {
      RuleKey.of(null, "NullDeref");
      fail();
    } catch (IllegalArgumentException e) {
      assertThat(e).hasMessage("Repository must be set");
    }
  }

  @Test
  public void repository_must_not_be_empty() {
    try {
      RuleKey.of("", "NullDeref");
      fail();
    } catch (IllegalArgumentException e) {
      assertThat(e).hasMessage("Repository must be set");
    }
  }

  @Test
  public void rule_must_not_be_null() {
    try {
      RuleKey.of("squid", null);
      fail();
    } catch (IllegalArgumentException e) {
      assertThat(e).hasMessage("Rule must be set");
    }
  }

  @Test
  public void rule_must_not_be_empty() {
    try {
      RuleKey.of("squid", "");
      fail();
    } catch (IllegalArgumentException e) {
      assertThat(e).hasMessage("Rule must be set");
    }
  }

  @Test
  public void encode_and_decode_string() {
    RuleKey key = RuleKey.of("squid", "NullDeref");
    String serialized = key.toString();
    assertThat(serialized).isEqualTo("squid:NullDeref");
    RuleKey parsed = RuleKey.parse(serialized);
    assertThat(parsed.repository()).isEqualTo("squid");
    assertThat(parsed.rule()).isEqualTo("NullDeref");
    assertThat(parsed.toString()).isEqualTo("squid:NullDeref");
  }

  @Test
  public void parse_key_with_colons() {
    RuleKey key = RuleKey.parse("squid:Key:With:Some::Colons");
    assertThat(key.repository()).isEqualTo("squid");
    assertThat(key.rule()).isEqualTo("Key:With:Some::Colons");
  }

  @Test
  public void not_accept_bad_format() {
    try {
      RuleKey.parse("foo");
      fail();
    } catch (IllegalArgumentException e) {
      assertThat(e).hasMessage("Invalid rule key: foo");
    }
  }

  @Test
  public void test_equals_and_hashcode() throws Exception {
    RuleKey key1 = RuleKey.of("squid", "NullDeref");
    RuleKey key2 = RuleKey.of("squid", "NullDeref");
    RuleKey key3 = RuleKey.of("squid", "Other");

    assertThat(key1).isEqualTo(key1);
    assertThat(key1).isEqualTo(key2);
    assertThat(key1).isNotEqualTo(key3);
    assertThat(key1).isNotEqualTo(null);
    assertThat(key1.hashCode()).isEqualTo(key1.hashCode());
    assertThat(key1.hashCode()).isEqualTo(key2.hashCode());
  }

  @Test
  public void test_compareTo() {
    RuleKey aa = RuleKey.of("A", "A");
    RuleKey ab = RuleKey.of("A", "B");

    assertThat(ab).isGreaterThan(aa);
    assertThat(aa).isLessThan(ab);
    assertThat(aa).isNotEqualTo(ab);
    assertThat(ab).isNotEqualTo(aa);
    assertThat(aa).isEqualTo(aa);
    assertThat(ab).isEqualTo(ab);
  }
}