Test to ensure that we generate the right meta-inf files BLD200604091800
authorjtulach@netbeans.org
Fri, 07 Apr 2006 12:58:17 +0000
changeset 7042ce0a5adf15ca
parent 7041 6e392c45c384
child 7043 b8e8d2cf3c75
Test to ensure that we generate the right meta-inf files
apisupport.metainfservices/src/org/netbeans/modules/apisupport/metainfservices/ExportAction.java
apisupport.metainfservices/test/unit/src/org/netbeans/modules/apisupport/metainfservices/ExportActionTest.java
     1.1 --- a/apisupport.metainfservices/src/org/netbeans/modules/apisupport/metainfservices/ExportAction.java	Fri Apr 07 12:35:36 2006 +0000
     1.2 +++ b/apisupport.metainfservices/src/org/netbeans/modules/apisupport/metainfservices/ExportAction.java	Fri Apr 07 12:58:17 2006 +0000
     1.3 @@ -111,7 +111,7 @@
     1.4  
     1.5          if (wd.FINISH_OPTION == wd.getValue()) {
     1.6              try {
     1.7 -                createFiles(clazz, wd, target);
     1.8 +                createFiles(clazz.getName(), wd, target);
     1.9              } catch (IOException ex) {
    1.10                  ErrorManager.getDefault().notify(ex);
    1.11              }
    1.12 @@ -119,28 +119,35 @@
    1.13      }
    1.14  
    1.15      @SuppressWarnings("unchecked")
    1.16 -    private void createFiles(final JavaClass clazz, final WizardDescriptor wd, final FileObject target) throws IOException, FileNotFoundException {
    1.17 +    private static void createFiles(String implName, WizardDescriptor wd, FileObject target)
    1.18 +    throws IOException, FileNotFoundException {
    1.19 +        List<String> files = (List<String>)wd.getProperty("files"); // NOI18N
    1.20 +        createFiles(implName, files, target);
    1.21 +    }
    1.22 +
    1.23 +    static void createFiles(String implName, List<String> files, FileObject target)
    1.24 +    throws IOException, FileNotFoundException {
    1.25          // lets apply the files
    1.26 -        List<String> files = (List<String>)wd.getProperty("files"); // NOI18N
    1.27          for (String s : files) {
    1.28              FileObject f = FileUtil.createData(target, s);
    1.29              byte[] exist = new byte[(int)f.getSize()];
    1.30              InputStream is = f.getInputStream();
    1.31              int len = is.read(exist);
    1.32              is.close();
    1.33 -            assert len == exist.length;
    1.34 +            //assert len == exist.length;
    1.35  
    1.36              String content = new String(exist);
    1.37              if (content.length() > 0 && !content.endsWith("\n")) { // NOI18N
    1.38                  content = content + "\n"; // NOI18N
    1.39              }
    1.40  
    1.41 -            content = content + clazz.getName() + "\n"; // NOI18N
    1.42 +            content = content + implName + "\n"; // NOI18N
    1.43  
    1.44              FileLock lock = f.lock();
    1.45              OutputStream os = f.getOutputStream(lock);
    1.46              os.write(content.getBytes());
    1.47              os.close();
    1.48 +            lock.releaseLock();
    1.49          }
    1.50      }
    1.51  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/apisupport.metainfservices/test/unit/src/org/netbeans/modules/apisupport/metainfservices/ExportActionTest.java	Fri Apr 07 12:58:17 2006 +0000
     2.3 @@ -0,0 +1,85 @@
     2.4 +/*
     2.5 + *                 Sun Public License Notice
     2.6 + *
     2.7 + * The contents of this file are subject to the Sun Public License
     2.8 + * Version 1.0 (the "License"). You may not use this file except in
     2.9 + * compliance with the License. A copy of the License is available at
    2.10 + * http://www.sun.com/
    2.11 + *
    2.12 + * The Original Code is NetBeans. The Initial Developer of the Original
    2.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    2.14 + * Microsystems, Inc. All Rights Reserved.
    2.15 + */
    2.16 +package org.netbeans.modules.apisupport.metainfservices;
    2.17 +
    2.18 +import java.net.URL;
    2.19 +import java.net.URLClassLoader;
    2.20 +import java.util.ArrayList;
    2.21 +import java.util.Set;
    2.22 +import org.netbeans.junit.NbTestCase;
    2.23 +import org.openide.filesystems.FileObject;
    2.24 +import org.openide.filesystems.FileSystem;
    2.25 +import org.openide.filesystems.FileUtil;
    2.26 +import org.openide.filesystems.LocalFileSystem;
    2.27 +import org.openide.util.Lookup;
    2.28 +import org.openide.util.lookup.Lookups;
    2.29 +
    2.30 +/**
    2.31 + *
    2.32 + * @author jarda
    2.33 + */
    2.34 +public class ExportActionTest extends NbTestCase {
    2.35 +    
    2.36 +    public ExportActionTest(String testName) {
    2.37 +        super(testName);
    2.38 +    }
    2.39 +
    2.40 +    protected void setUp() throws Exception {
    2.41 +        clearWorkDir();
    2.42 +    }
    2.43 +
    2.44 +    protected void tearDown() throws Exception {
    2.45 +    }
    2.46 +
    2.47 +    public void testGenerateFiles() throws Exception {
    2.48 +        LocalFileSystem lfs = new LocalFileSystem();
    2.49 +        lfs.setRootDirectory(getWorkDir());
    2.50 +
    2.51 +        FileSystem fs = lfs;
    2.52 +        FileObject src = FileUtil.createFolder(fs.getRoot(), "src");
    2.53 +
    2.54 +        ArrayList<String> files = new ArrayList<String>();
    2.55 +        files.add("META-INF/services/java.lang.Object");
    2.56 +        files.add("META-INF/services/java.lang.Runnable");
    2.57 +
    2.58 +        ExportAction.createFiles(R.class.getName(), files, src);
    2.59 +
    2.60 +        URLClassLoader loader = new URLClassLoader(new URL[] { src.getURL() }, getClass().getClassLoader());
    2.61 +        Lookup l = Lookups.metaInfServices(loader);
    2.62 +
    2.63 +        Runnable r = l.lookup(Runnable.class);
    2.64 +
    2.65 +        assertNotNull("Runnable found", r);
    2.66 +        assertEquals("It is my class", R.class, r.getClass());
    2.67 +
    2.68 +
    2.69 +        ExportAction.createFiles(Q.class.getName(), files, src);
    2.70 +
    2.71 +        l = Lookups.metaInfServices(loader);
    2.72 +        Set<Class<? extends Runnable>> all = l.lookupResult(Runnable.class).allClasses();
    2.73 +
    2.74 +        assertEquals(2, all.size());
    2.75 +        assertTrue("Q is there", all.contains(Q.class));
    2.76 +        assertTrue("R is there", all.contains(R.class));
    2.77 +    }
    2.78 +
    2.79 +    public static class R extends Object implements Runnable {
    2.80 +        public void run() {
    2.81 +        }
    2.82 +    }
    2.83 +
    2.84 +    public static class Q implements Runnable {
    2.85 +        public void run() {
    2.86 +        }
    2.87 +    }
    2.88 +}