Ignore:
Timestamp:
2007-06-30T22:01:02+02:00 (17 years ago)
Author:
imi
Message:
  • added and fixed some tests
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/org/openstreetmap/josm/plugins/PluginInformationTest.java

    r169 r267  
    11package org.openstreetmap.josm.plugins;
    22
     3import java.io.ByteArrayInputStream;
     4import java.io.ByteArrayOutputStream;
    35import java.io.File;
     6import java.net.URL;
     7import java.net.URLClassLoader;
     8import java.util.Arrays;
     9import java.util.Collection;
     10import java.util.jar.JarInputStream;
    411
    512import junit.framework.TestCase;
    613
     14import org.openstreetmap.josm.Main;
     15import org.openstreetmap.josm.data.Preferences;
     16
    717public 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        }
    827
    928        public void testConstructorExtractsAttributesFromManifest() throws Exception {
    1029                PluginInformation info = new PluginInformation(new File(getClass().getResource("simple.jar").getFile()));
    1130                String s = getClass().getResource(".").getFile();
    12                
    13                 assertEquals(4, info.libraries.size());
    14                 assertEquals(s+"foo", info.libraries.get(1).getFile());
    15                 assertEquals(s+"bar", info.libraries.get(2).getFile());
    16                 assertEquals(s+"C:/Foo%20and%20Bar", info.libraries.get(3).getFile());
    17                
    18                 assertEquals("imi", info.author);
    19                 assertEquals("Simple", info.className);
    20                 assertEquals("Simpler", info.description);
    21                 assertEquals(true, info.early);
     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"));
    22100    }
    23101}
Note: See TracChangeset for help on using the changeset viewer.