Changeset 267 in josm


Ignore:
Timestamp:
2007-06-30T22:01:02+02:00 (18 years ago)
Author:
imi
Message:
  • added and fixed some tests
Files:
5 added
11 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/Main.java

    r266 r267  
    234234                // iterate all plugins and collect all libraries of all plugins:
    235235                List<URL> allPluginLibraries = new ArrayList<URL>();
    236                 for (Collection<PluginInformation> c : p.values()) {
    237                         for (PluginInformation info : c) {
    238                                 allPluginLibraries.addAll(info.getLibraries());
    239                         }
    240                 }
     236                for (Collection<PluginInformation> c : p.values())
     237                        for (PluginInformation info : c)
     238                                allPluginLibraries.addAll(info.libraries);
    241239                // create a classloader for all plugins:
    242240                URL[] jarUrls = new URL[allPluginLibraries.size()];
    243241                jarUrls = allPluginLibraries.toArray(jarUrls);
    244242                URLClassLoader pluginClassLoader = new URLClassLoader(jarUrls, Main.class.getClassLoader());
    245                
    246                
     243
    247244                for (Collection<PluginInformation> c : p.values()) {
    248245                        for (PluginInformation info : c) {
    249246                                try {
    250                                         info.setClassLoader(pluginClassLoader); // set the common classloader
    251                                         Class<?> klass = info.loadClass();
     247                                        Class<?> klass = info.loadClass(pluginClassLoader);
    252248                                        ImageProvider.sources.add(0, klass);
    253249                                        System.out.println("loading "+info.name);
  • src/org/openstreetmap/josm/gui/dialogs/HistoryDialog.java

    r203 r267  
    7272                }
    7373        };
     74
    7475        /**
    7576         * Main table. 3 columns:
     
    8081
    8182        private Map<OsmPrimitive, List<HistoryItem>> cache = new HashMap<OsmPrimitive, List<HistoryItem>>();
    82         private JLabel notLoaded = new JLabel("<html><i><p align=\"center\">"+tr("Click Reload to refresh list")+"</p></i></html>");
     83        private JLabel notLoaded = new JLabel("<html><i>"+tr("Click Reload to refresh list")+"</i></html>");
    8384        private JButton reloadButton = new JButton(tr("Reload"), ImageProvider.get("dialogs/refresh"));
    8485        private JButton revertButton = new JButton(tr("Revert"), ImageProvider.get("dialogs/revert"));
     
    8889                historyPane.setVisible(false);
    8990                notLoaded.setVisible(true);
     91                notLoaded.setHorizontalAlignment(JLabel.CENTER);
    9092
    9193                history.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
     
    131133                reloadButton.setToolTipText(tr("Reload all currently selected objects and refresh the list."));
    132134                reloadButton.putClientProperty("help", "Dialog/History/Reload");
     135               
    133136                revertButton.addActionListener(new ActionListener(){
    134137                        public void actionPerformed(ActionEvent e) {
  • src/org/openstreetmap/josm/plugins/Plugin.java

    r237 r267  
    4040public abstract class Plugin {
    4141
    42         private String name;
     42        String name;
    4343
    4444        public Plugin() {
    45                 URL[] urls = ((URLClassLoader)getClass().getClassLoader()).getURLs();
    46                 name = urls[urls.length-1].toString();
    47                 if (name.toLowerCase().endsWith(".jar")) {
    48                         int lastSlash = name.lastIndexOf('/');
    49                         name = name.substring(lastSlash+1, name.length()-4);
    50                 }
     45                try {
     46                URL[] urls = ((URLClassLoader)getClass().getClassLoader()).getURLs();
     47                name = urls[urls.length-1].toString();
     48                if (name.toLowerCase().endsWith(".jar")) {
     49                        int lastSlash = name.lastIndexOf('/');
     50                        name = name.substring(lastSlash+1, name.length()-4);
     51                }
     52        } catch (RuntimeException e) {
     53                name = "unknown";
     54        }
    5155    }
    5256
    5357        /**
    5458         * @return The name of this plugin. This is the name of the .jar file.
     59         * @deprecated Plugins have to know their name by themself.
    5560         */
    56         public final String getName() {
     61        @Deprecated public final String getName() {
    5762                return name;
    5863        }
    5964        /**
    6065         * @return The directory for the plugin to store all kind of stuff.
     66         * @deprecated Use <code>Main.pref.getPreferencesDir()+"plugins/"+name+"/";</code> instead.
    6167         */
    62         public final String getPluginDir() {
     68        @Deprecated public final String getPluginDir() {
    6369                return Main.pref.getPreferencesDir()+"plugins/"+name+"/";
    6470        }
  • src/org/openstreetmap/josm/plugins/PluginException.java

    r149 r267  
    1111 */
    1212public class PluginException extends RuntimeException {
    13         private final PluginProxy plugin;
    14         private final String name;
     13        public final PluginProxy plugin;
     14        public final String name;
    1515
    1616        public PluginException(PluginProxy plugin, String name, Throwable cause) {
     
    1919                this.name = name;
    2020    }
    21        
    22         public PluginProxy getPlugin() {
    23                 return plugin;
    24         }
    25         public String getName() {
    26                 return name;
    27         }
    2821}
  • src/org/openstreetmap/josm/plugins/PluginInformation.java

    r250 r267  
    66import java.io.InputStream;
    77import java.net.URL;
    8 import java.net.URLClassLoader;
    98import java.util.ArrayList;
    109import java.util.Collection;
     
    3332        public final String author;
    3433        public final int stage;
    35         protected final List<URL> libraries = new ArrayList<URL>();
    36         protected ClassLoader classLoader;
     34        public final List<URL> libraries = new ArrayList<URL>();
    3735
    3836        public final Map<String, String> attr = new TreeMap<String, String>();
     
    8280                                        s = entry.toString();
    8381                                        entry = new StringBuilder();
    84                                         if (!s.startsWith("/") && !s.startsWith("\\") && !s.matches("^.\\:"))
     82                                        if (!s.startsWith("/") && !s.startsWith("\\") && !s.matches("^.\\:") && file != null)
    8583                                                s = file.getParent() + File.separator + s;
    8684                                        libraries.add(new URL(getURLString(s)));
     
    111109         * Load the class of the plugin
    112110         */
    113         public Class<?> loadClass() {
     111        public Class<?> loadClass(ClassLoader classLoader) {
    114112                try {
    115                         Class<?> realClass = Class.forName(className, true, getClassLoader());
     113                        Class<?> realClass = Class.forName(className, true, classLoader);
    116114                        return realClass;
    117115                } catch (Exception e) {
     
    126124        }
    127125
    128         /**
    129          * Returns all libraries the plugin needs (the plugin's jar file itself and all jars declared
    130          * in the manifest's Class-Path section).
    131          * @return the libraries a list of URLs to the libraries.
    132          */
    133         public List<URL> getLibraries() {
    134                 return libraries;
    135         }
    136 
    137         /**
    138      * @return the classLoader
    139      */
    140     public ClassLoader getClassLoader() {
    141                 // if I have no classloader set, create one for me and my libraries:
    142                 if(classLoader == null) {
    143                         URL[] urls = new URL[libraries.size()];
    144                         urls = libraries.toArray(urls);
    145                         classLoader = URLClassLoader.newInstance(urls, getClass().getClassLoader());
    146                 }
    147         return classLoader;
    148     }
    149 
    150         /**
    151      * @param classLoader the classLoader to set
    152      */
    153     public void setClassLoader(ClassLoader classLoader) {
    154         this.classLoader = classLoader;
    155     }
    156        
    157126        /**
    158127         * Try to find a plugin after some criterias. Extract the plugin-information
  • src/org/openstreetmap/josm/plugins/PluginProxy.java

    r238 r267  
    2424                this.plugin = plugin;
    2525                this.info = info;
     26
     27                // setting name of the plugin by reflection
     28                if (plugin instanceof Plugin) {
     29                        try {
     30                        Plugin.class.getDeclaredField("name").set(plugin, info.name);
     31                } catch (Exception e) {
     32                        if (e instanceof RuntimeException)
     33                                throw (RuntimeException)e;
     34                        throw new RuntimeException(e);
     35                }
     36                }
    2637    }
    2738
  • src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java

    r243 r267  
    4343                        // Check for an explicit problem when calling a plugin function
    4444                        if (e instanceof PluginException) {
    45                                 PluginProxy plugin = ((PluginException)e).getPlugin();
     45                                PluginProxy plugin = ((PluginException)e).plugin;
    4646                                if (plugin != null && !plugin.misbehaving) {
    4747                                        JOptionPane.showMessageDialog(Main.parent, tr("The plugin {0} throwed an exception: {1}\nIt may be outdated. Please contact the plugin's autor.\nThis message will not shown again until JOSM is restarted.", plugin.info.name, e.getMessage()));
  • test/org/openstreetmap/josm/data/osm/OsmPrimitiveTest.java

    r146 r267  
    4444        }
    4545
    46         public void testHashCodeReturnsIdIfNotZeroAndNotZeroIfIdIsZero() {
    47                 osm.id = 23;
    48                 assertEquals(23, osm.hashCode());
    49                 osm.id = 0;
    50                 assertTrue(0 != osm.hashCode());
    51         }
    52 
    5346        public void testVisit() {
    5447                osm.visit(new Visitor(){
  • test/org/openstreetmap/josm/plugins/PluginExceptionTest.java

    r153 r267  
    99                PluginException e = new PluginException(new PluginProxy(new String(), null), "42", barEx);
    1010                assertEquals(barEx, e.getCause());
    11                 assertEquals("42", e.getName());
     11                assertEquals("42", e.name);
    1212        }
    1313
  • 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}
  • test/org/openstreetmap/josm/tools/XmlObjectParserTest.java

    r193 r267  
    5252        }
    5353
    54         public void testManyTags() throws Exception {
    55                 StringBuilder b = new StringBuilder("<all>");
    56                 for (int i = 0; i < 50000; ++i) {
    57                         if (Math.random() > 0.5) {
    58                                 b.append("<foo bar='blob");
    59                                 b.append(i);
    60                                 b.append("'/>");
    61                         } else {
    62                                 b.append("<foo><bar>yuppel");
    63                                 b.append(i);
    64                                 b.append("</bar></foo>");
    65                         }
    66                 }
    67                 b.append("</all>");
    68 
    69                 System.gc();
    70                 long memBefore = Runtime.getRuntime().freeMemory();
    71                 parser = createParser(b.toString());
    72                 Thread.sleep(300);
    73                 System.gc();
    74                 long memAfter = Runtime.getRuntime().freeMemory();
    75                 assertTrue("2MB should be more than enough. "+(memAfter-memBefore), memAfter-memBefore < 2*1024*1024);
    76 
    77                 for (int i = 0; i < 50000; ++i) {
    78                         Foo f = (Foo)parser.next();
    79                         assertTrue(f.bar.equals("blob"+i) || f.bar.equals("yuppel"+i));
    80                 }
    81                 assertFalse(parser.hasNext());
    82         }
    83 
    8454        public void testIterable() throws Exception {
    8555                parser = createParser("<xml><foo bar='yo'/><foo bar='yo'/><foo bar='yo'/></xml>");
Note: See TracChangeset for help on using the changeset viewer.