1 | package org.openstreetmap.josm.plugins;
|
---|
2 |
|
---|
3 | import java.io.ByteArrayInputStream;
|
---|
4 | import java.io.ByteArrayOutputStream;
|
---|
5 | import java.io.File;
|
---|
6 | import java.net.URL;
|
---|
7 | import java.net.URLClassLoader;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.jar.JarInputStream;
|
---|
11 |
|
---|
12 | import junit.framework.TestCase;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.Preferences;
|
---|
16 |
|
---|
17 | public class PluginInformationTest extends TestCase {
|
---|
18 |
|
---|
19 | @Override protected void setUp() throws Exception {
|
---|
20 | super.setUp();
|
---|
21 | Main.pref = new Preferences(){
|
---|
22 | @Override public Collection<String> getAllPossiblePreferenceDirs() {
|
---|
23 | return Arrays.asList(new String[]{getClass().getResource("..").getFile()});
|
---|
24 | }
|
---|
25 | };
|
---|
26 | }
|
---|
27 |
|
---|
28 | public void testConstructorExtractsAttributesFromManifest() throws Exception {
|
---|
29 | PluginInformation info = new PluginInformation(new File(getClass().getResource("simple.jar").getFile()));
|
---|
30 | String s = getClass().getResource(".").getFile();
|
---|
31 | assertEquals(4, info.libraries.size());
|
---|
32 | assertEquals(s+"foo", info.libraries.get(1).getFile());
|
---|
33 | assertEquals(s+"bar", info.libraries.get(2).getFile());
|
---|
34 | assertEquals(s+"C:/Foo%20and%20Bar", info.libraries.get(3).getFile());
|
---|
35 |
|
---|
36 | assertEquals("imi", info.author);
|
---|
37 | assertEquals("Simple", info.className);
|
---|
38 | assertEquals("Simpler", info.description);
|
---|
39 | assertEquals(true, info.early);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void testConstructorRequiresJarWithManifest() throws Exception {
|
---|
43 | try {
|
---|
44 | new PluginInformation(new File(getClass().getResource("no_manifest.jar").getFile()));
|
---|
45 | fail("Exception because missing manifest excpected");
|
---|
46 | } catch (PluginException e) {
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void testConstructorWithInputStream() throws Exception {
|
---|
51 | JarInputStream f = new JarInputStream(getClass().getResourceAsStream("simple.jar"));
|
---|
52 | ByteArrayOutputStream out = new ByteArrayOutputStream();
|
---|
53 | f.getManifest().write(out);
|
---|
54 |
|
---|
55 | PluginInformation info = new PluginInformation(null, "simple", new ByteArrayInputStream(out.toByteArray()));
|
---|
56 | assertEquals("Only the 3 external classpaths are added (as we are using bootstrap classpath for plugin",
|
---|
57 | 3, info.libraries.size());
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void testLoadClassInstantiatePlugin() throws Exception {
|
---|
61 | PluginInformation info = new PluginInformation(new File(getClass().getResource("working.jar").getFile()));
|
---|
62 | ClassLoader cl = new URLClassLoader(new URL[]{getClass().getResource("working.jar")});
|
---|
63 | assertNotNull(info.load(info.loadClass(cl)));
|
---|
64 | }
|
---|
65 |
|
---|
66 | // This is so the bugtracker always detect coding problems as "plugin problems"
|
---|
67 | public void testLoadThrowsPluginExceptionOnRuntimeException() throws Exception {
|
---|
68 | PluginInformation info = new PluginInformation(new File(getClass().getResource("working.jar").getFile()));
|
---|
69 | try {
|
---|
70 | info.load(null);
|
---|
71 | fail("Exception excpected because null-Class");
|
---|
72 | } catch (PluginException e) {
|
---|
73 | }
|
---|
74 | try {
|
---|
75 | info.loadClass(null);
|
---|
76 | fail("Exception excpected because null-ClassLoader");
|
---|
77 | } catch (PluginException e) {
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void testFindPluginReturnsInformationFromBootstrapClasspath() throws Exception {
|
---|
82 | PluginInformation info = PluginInformation.findPlugin("test_simple");
|
---|
83 | assertEquals("Simpler", info.description);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void testFindPluginReturnsFromPreferencesDirs() throws Exception {
|
---|
87 | PluginInformation info = PluginInformation.findPlugin("simple");
|
---|
88 | assertEquals("Simpler", info.description);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void testFindPluginForUnknownReturnsNull() throws Exception {
|
---|
92 | assertNull(PluginInformation.findPlugin("asdf"));
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void testPluginLocationsReturnModifiedPreferenceLocations() throws Exception {
|
---|
96 | setUp();
|
---|
97 | Collection<String> locations = PluginInformation.getPluginLocations();
|
---|
98 | assertEquals(1, locations.size());
|
---|
99 | assertTrue(locations.iterator().next().endsWith("/plugins"));
|
---|
100 | }
|
---|
101 | }
|
---|