source: josm/test/org/openstreetmap/josm/plugins/PluginInformationTest.java@ 267

Last change on this file since 267 was 267, checked in by imi, 17 years ago
  • added and fixed some tests
File size: 4.0 KB
Line 
1package org.openstreetmap.josm.plugins;
2
3import java.io.ByteArrayInputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.File;
6import java.net.URL;
7import java.net.URLClassLoader;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.jar.JarInputStream;
11
12import junit.framework.TestCase;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.Preferences;
16
17public 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}
Note: See TracBrowser for help on using the repository browser.