1 package com.atlassian.plugin;
2
3 import org.junit.Test;
4
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import static org.hamcrest.core.Is.is;
9 import static org.junit.Assert.assertEquals;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.assertTrue;
13
14 public class TestModuleCompleteKey {
15
16 @Test
17 public void spacesAreStrippedFromKeys() {
18 String pluginKey = " com.acme ";
19 String moduleKey = " tools ";
20
21 ModuleCompleteKey moduleCompleteKey = new ModuleCompleteKey(pluginKey, moduleKey);
22
23 assertEquals(pluginKey.trim(), moduleCompleteKey.getPluginKey());
24 assertEquals(moduleKey.trim(), moduleCompleteKey.getModuleKey());
25 }
26
27 @Test
28 public void spacesAreStrippedFromKeys2() {
29 String moduleCompleteKeyString = " com.acme : tools ";
30
31 ModuleCompleteKey moduleCompleteKey = new ModuleCompleteKey(moduleCompleteKeyString);
32
33 assertEquals("com.acme", moduleCompleteKey.getPluginKey());
34 assertEquals("tools", moduleCompleteKey.getModuleKey());
35 }
36
37 @Test
38 public void pluginKeyAndModuleKeyConstructor() {
39 String pluginKey = "com.acme";
40 String moduleKey = "tools";
41
42 ModuleCompleteKey moduleCompleteKey = new ModuleCompleteKey(pluginKey, moduleKey);
43
44 assertEquals(pluginKey, moduleCompleteKey.getPluginKey());
45 assertEquals(moduleKey, moduleCompleteKey.getModuleKey());
46 }
47
48 @Test
49 public void moduleCompleteKeyCanBeUsedAsMapKey() {
50 Map<ModuleCompleteKey, String> map = new HashMap<>();
51 String expected = "bananas";
52
53 map.put(new ModuleCompleteKey("foo", "bar"), expected);
54
55 assertEquals(expected, map.get(new ModuleCompleteKey("foo", "bar")));
56 }
57
58 @Test
59 public void equals() {
60 assertTrue(new ModuleCompleteKey("foo", "bar").equals(new ModuleCompleteKey("foo", "bar")));
61 assertFalse(new ModuleCompleteKey("foo", "bar").equals(new ModuleCompleteKey("foo", "baz")));
62 assertFalse(new ModuleCompleteKey("fon", "bar").equals(new ModuleCompleteKey("foo", "baz")));
63 }
64
65 @Test
66 public void testToString() {
67 ModuleCompleteKey moduleCompleteKey = new ModuleCompleteKey("foo", "bar");
68
69 assertEquals(moduleCompleteKey.getCompleteKey(), moduleCompleteKey.toString());
70 }
71
72 @Test
73 public void workingKey() {
74 ModuleCompleteKey key = new ModuleCompleteKey("foo:bar");
75
76 assertEquals("foo", key.getPluginKey());
77 assertEquals("bar", key.getModuleKey());
78 assertEquals("foo:bar", key.getCompleteKey());
79 }
80
81 @Test(expected = IllegalArgumentException.class)
82 public void colonInPluginKey() {
83 new ModuleCompleteKey("foo:", "bar");
84 }
85
86 @Test
87 public void colonInModuleKeyDoesNotThrowException() {
88 final ModuleCompleteKey completeKey = new ModuleCompleteKey("foo", ":bar");
89 assertEquals("foo", completeKey.getPluginKey());
90 assertEquals(":bar", completeKey.getModuleKey());
91 }
92
93 @Test(expected = IllegalArgumentException.class)
94 public void badKey1() {
95 new ModuleCompleteKey("foo");
96 }
97
98 @Test(expected = IllegalArgumentException.class)
99 public void badKey2() {
100 new ModuleCompleteKey("foo:");
101 }
102
103 @Test(expected = IllegalArgumentException.class)
104 public void badKey3() {
105 new ModuleCompleteKey(":foo");
106 }
107
108 @Test(expected = IllegalArgumentException.class)
109 public void badKey4() {
110 new ModuleCompleteKey("");
111 }
112
113 @Test(expected = IllegalArgumentException.class)
114 public void badKey5() {
115 new ModuleCompleteKey(null);
116 }
117
118 @Test(expected = IllegalArgumentException.class)
119 public void nullPluginKey() {
120 new ModuleCompleteKey(null, "foo");
121 }
122
123 @Test(expected = IllegalArgumentException.class)
124 public void nullModuleKey() {
125 new ModuleCompleteKey("foo", null);
126 }
127
128 @Test(expected = IllegalArgumentException.class)
129 public void nullPluginKeyAndNullModuleKey() {
130 new ModuleCompleteKey(null, null);
131 }
132
133 @Test(expected = IllegalArgumentException.class)
134 public void emptyPluginKey() throws Exception {
135 new ModuleCompleteKey("", "foo");
136 }
137
138 @Test(expected = IllegalArgumentException.class)
139 public void emptyModuleKey() throws Exception {
140 new ModuleCompleteKey("foo", "");
141 }
142
143 @Test(expected = IllegalArgumentException.class)
144 public void emptyPluginKeyAndEmptyModuleKey() throws Exception {
145 new ModuleCompleteKey("", "");
146 }
147
148 @Test(expected = IllegalArgumentException.class)
149 public void pluginKeyOfSpaces() throws Exception {
150 new ModuleCompleteKey(" ", "foo");
151 }
152
153 @Test(expected = IllegalArgumentException.class)
154 public void moduleKeyOfSpaces() throws Exception {
155 new ModuleCompleteKey("foo", " ");
156 }
157
158 @Test(expected = IllegalArgumentException.class)
159 public void pluginKeyAndModuleKeyOfSpaces() throws Exception {
160 new ModuleCompleteKey(" ", " ");
161 }
162
163 @Test
164 public void pluginModuleKeyCanHaveAColonPresent() {
165 String pluginKey = "com.acme";
166 String moduleKey = "tools:foo";
167 String completeKey = "com.acme:tools:foo";
168
169 ModuleCompleteKey moduleCompleteKey = new ModuleCompleteKey(completeKey);
170
171 assertEquals(pluginKey, moduleCompleteKey.getPluginKey());
172 assertEquals(moduleKey, moduleCompleteKey.getModuleKey());
173 }
174
175 @Test
176 public void pluginKeyFromCompleteKeyNull() {
177 assertThat(ModuleCompleteKey.pluginKeyFromCompleteKey(null), is(""));
178 }
179
180 @Test
181 public void pluginKeyFromCompleteKeyNoSeparator() {
182 assertThat(ModuleCompleteKey.pluginKeyFromCompleteKey("blargh"), is("blargh"));
183 }
184
185 @Test
186 public void pluginKeyFromCompleteKeySeparatorMid() {
187 assertThat(ModuleCompleteKey.pluginKeyFromCompleteKey("bla:rgh"), is("bla"));
188 }
189
190 @Test
191 public void pluginKeyFromCompleteKeySeparatorEnd() {
192 assertThat(ModuleCompleteKey.pluginKeyFromCompleteKey("blargh:"), is("blargh"));
193 }
194
195 @Test
196 public void moduleKeyFromCompleteKeyNull() {
197 assertThat(ModuleCompleteKey.moduleKeyFromCompleteKey(null), is(""));
198 }
199
200 @Test
201 public void moduleKeyFromCompleteKeyNoSeparator() {
202 assertThat(ModuleCompleteKey.moduleKeyFromCompleteKey("blargh"), is(""));
203 }
204
205 @Test
206 public void moduleKeyFromCompleteKeySeparatorMid() {
207 assertThat(ModuleCompleteKey.moduleKeyFromCompleteKey("bla:rgh"), is("rgh"));
208 }
209
210 @Test
211 public void moduleKeyFromCompleteKeySeparatorEnd() {
212 assertThat(ModuleCompleteKey.moduleKeyFromCompleteKey("blargh:"), is(""));
213 }
214 }