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