1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.plugin; |
---|
16 | |
---|
17 | import java.io.File; |
---|
18 | import java.util.Arrays; |
---|
19 | import java.util.Collection; |
---|
20 | |
---|
21 | import junitx.framework.ArrayAssert; |
---|
22 | |
---|
23 | import org.junit.*; |
---|
24 | |
---|
25 | import de.ugoe.cs.autoquest.plugin.PluginLoader; |
---|
26 | import de.ugoe.cs.autoquest.plugin.PluginLoaderException; |
---|
27 | import de.ugoe.cs.autoquest.plugin.AutoQUESTPlugin; |
---|
28 | import static org.junit.Assert.*; |
---|
29 | |
---|
30 | /** |
---|
31 | * The class <code>PluginLoaderTest</code> contains tests for the class |
---|
32 | * <code>{@link PluginLoader}</code>. |
---|
33 | * |
---|
34 | * @author Steffen Herbold |
---|
35 | * @version 1.0 |
---|
36 | */ |
---|
37 | public class PluginLoaderTest { |
---|
38 | |
---|
39 | @Test |
---|
40 | public void testPluginLoader_1() throws Exception { |
---|
41 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
42 | assertNotNull(loader); |
---|
43 | } |
---|
44 | |
---|
45 | @Test(expected = java.lang.IllegalArgumentException.class) |
---|
46 | public void testPluginLoader_2() throws Exception { |
---|
47 | new PluginLoader(null); |
---|
48 | } |
---|
49 | |
---|
50 | @Test(expected = java.lang.IllegalArgumentException.class) |
---|
51 | public void testPluginLoader_3() throws Exception { |
---|
52 | new PluginLoader(new File("testdata/de.ugoe.cs.autoquest.plugin.PluginLoaderTest/jfcmonitor.jar")); |
---|
53 | } |
---|
54 | |
---|
55 | @Test |
---|
56 | public void testCheckNameConformity_1() throws Exception { |
---|
57 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
58 | String filename = "quest-plugin-jfc-1.0.jar"; |
---|
59 | boolean expected = true; |
---|
60 | |
---|
61 | boolean result = loader.checkNameConformity(filename); |
---|
62 | |
---|
63 | assertEquals(expected, result); |
---|
64 | } |
---|
65 | |
---|
66 | @Test |
---|
67 | public void testCheckNameConformity_2() throws Exception { |
---|
68 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
69 | String filename = "quest-plugin-jf-c-1.0.jar"; |
---|
70 | boolean expected = false; |
---|
71 | |
---|
72 | boolean result = loader.checkNameConformity(filename); |
---|
73 | |
---|
74 | assertEquals(expected, result); |
---|
75 | } |
---|
76 | |
---|
77 | @Test |
---|
78 | public void testCheckNameConformity_3() throws Exception { |
---|
79 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
80 | String filename = "quest-plugin-jfc.jar"; |
---|
81 | boolean expected = false; |
---|
82 | |
---|
83 | boolean result = loader.checkNameConformity(filename); |
---|
84 | |
---|
85 | assertEquals(expected, result); |
---|
86 | } |
---|
87 | |
---|
88 | @Test |
---|
89 | public void testCheckNameConformity_4() throws Exception { |
---|
90 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
91 | String filename = "quest-plugi-jfc-1.0.jar"; |
---|
92 | boolean expected = false; |
---|
93 | |
---|
94 | boolean result = loader.checkNameConformity(filename); |
---|
95 | |
---|
96 | assertEquals(expected, result); |
---|
97 | } |
---|
98 | |
---|
99 | @Test |
---|
100 | public void testCheckNameConformity_5() throws Exception { |
---|
101 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
102 | String filename = "quest-pluginjfc-1.0.jar"; |
---|
103 | boolean expected = false; |
---|
104 | |
---|
105 | boolean result = loader.checkNameConformity(filename); |
---|
106 | |
---|
107 | assertEquals(expected, result); |
---|
108 | } |
---|
109 | |
---|
110 | @Test |
---|
111 | public void testCheckNameConformity_6() throws Exception { |
---|
112 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
113 | String filename = "quest-plugin-jfc-1-0.jar"; |
---|
114 | boolean expected = false; |
---|
115 | |
---|
116 | boolean result = loader.checkNameConformity(filename); |
---|
117 | |
---|
118 | assertEquals(expected, result); |
---|
119 | } |
---|
120 | |
---|
121 | @Test |
---|
122 | public void testCheckNameConformity_7() throws Exception { |
---|
123 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
124 | String filename = "quest-plugin-jfc-1.0.nojar"; |
---|
125 | boolean expected = false; |
---|
126 | |
---|
127 | boolean result = loader.checkNameConformity(filename); |
---|
128 | |
---|
129 | assertEquals(expected, result); |
---|
130 | } |
---|
131 | |
---|
132 | @Test |
---|
133 | public void testCheckNameConformity_8() throws Exception { |
---|
134 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
135 | String filename = null; |
---|
136 | boolean expected = false; |
---|
137 | |
---|
138 | boolean result = loader.checkNameConformity(filename); |
---|
139 | |
---|
140 | assertEquals(expected, result); |
---|
141 | } |
---|
142 | |
---|
143 | @Test |
---|
144 | public void testCheckNameConformity_9() throws Exception { |
---|
145 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
146 | String filename = ""; |
---|
147 | boolean expected = false; |
---|
148 | |
---|
149 | boolean result = loader.checkNameConformity(filename); |
---|
150 | |
---|
151 | assertEquals(expected, result); |
---|
152 | } |
---|
153 | |
---|
154 | @Test |
---|
155 | public void testGetClassPathFromJar_1() throws Exception { |
---|
156 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
157 | File jarFile = new File("testdata/de.ugoe.cs.autoquest.plugin.PluginLoaderTest/jfcmonitor.jar"); |
---|
158 | |
---|
159 | String[] expected = new String[]{ "file:" + jarFile.getParentFile().getAbsolutePath()+"/javahelperlib.jar" }; |
---|
160 | |
---|
161 | String[] result = loader.getClassPathFromJar(jarFile); |
---|
162 | |
---|
163 | ArrayAssert.assertEquivalenceArrays(expected, result); |
---|
164 | } |
---|
165 | |
---|
166 | @Test |
---|
167 | public void testGetClassPathFromJar_2() throws Exception { |
---|
168 | PluginLoader loader = new PluginLoader(new File(".")); |
---|
169 | File jarFile = new File("testdata/de.ugoe.cs.autoquest.plugin.PluginLoaderTest/jmi.jar"); |
---|
170 | |
---|
171 | String[] expected = new String[]{ }; |
---|
172 | |
---|
173 | String[] result = loader.getClassPathFromJar(jarFile); |
---|
174 | |
---|
175 | ArrayAssert.assertEquivalenceArrays(expected, result); |
---|
176 | } |
---|
177 | |
---|
178 | @Test |
---|
179 | public void testLoad_1() throws Exception { |
---|
180 | PluginLoader loader = new PluginLoader(new File("testdata/de.ugoe.cs.autoquest.plugin.PluginLoaderTest")); |
---|
181 | |
---|
182 | loader.load(); |
---|
183 | |
---|
184 | Collection<AutoQUESTPlugin> plugins = loader.getPlugins(); |
---|
185 | |
---|
186 | assertEquals(1, plugins.size()); |
---|
187 | AutoQUESTPlugin plugin = plugins.iterator().next(); |
---|
188 | assertNotNull(plugin); |
---|
189 | assertEquals("Mock Plugin", plugin.getTitle()); |
---|
190 | assertEquals(Arrays.asList(new String[]{"de.ugoe.cs.autoquest.plugin.mock.commands"}), plugin.getCommandPackages()); |
---|
191 | } |
---|
192 | |
---|
193 | @Test |
---|
194 | public void testLoad_2() throws Exception { |
---|
195 | PluginLoader loader = new PluginLoader(new File("testdata/de.ugoe.cs.autoquest.plugin.PluginLoaderTestInvalid_1")); |
---|
196 | |
---|
197 | try { |
---|
198 | loader.load(); |
---|
199 | } catch(PluginLoaderException e) { |
---|
200 | e.getMessage().endsWith("not instance of AutoQUESTPlugin"); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | @Test |
---|
205 | public void testLoad_3() throws Exception { |
---|
206 | PluginLoader loader = new PluginLoader(new File("testdata/de.ugoe.cs.autoquest.plugin.PluginLoaderTestInvalid_2")); |
---|
207 | |
---|
208 | try { |
---|
209 | loader.load(); |
---|
210 | } catch(PluginLoaderException e) { |
---|
211 | e.getMessage().startsWith("No class"); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | @Test |
---|
216 | public void testLoad_4() throws Exception { |
---|
217 | PluginLoader loader = new PluginLoader(new File("testdata/de.ugoe.cs.autoquest.plugin.PluginLoaderTestInvalid_3")); |
---|
218 | |
---|
219 | try { |
---|
220 | loader.load(); |
---|
221 | } catch(PluginLoaderException e) { |
---|
222 | e.getMessage().endsWith("Could not access"); |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | } |
---|