This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 128339
Collapse All | Expand All

(-)a/o.n.bootstrap/src/org/netbeans/ProxyClassLoader.java (-1 / +3 lines)
Lines 75-80 public class ProxyClassLoader extends Cl Link Here
75
75
76
    private static final Logger LOGGER = Logger.getLogger(ProxyClassLoader.class.getName());
76
    private static final Logger LOGGER = Logger.getLogger(ProxyClassLoader.class.getName());
77
    private static final boolean LOG_LOADING;
77
    private static final boolean LOG_LOADING;
78
    private static final ClassLoader TOP_CL = MainImpl.class.getClassLoader();
78
79
79
    static {
80
    static {
80
        boolean prop1 = System.getProperty("org.netbeans.ProxyClassLoader.level") != null;
81
        boolean prop1 = System.getProperty("org.netbeans.ProxyClassLoader.level") != null;
Lines 90-96 public class ProxyClassLoader extends Cl Link Here
90
    private final boolean transitive;
91
    private final boolean transitive;
91
92
92
    /** The base class loader that is before all ProxyClassLoaders. */
93
    /** The base class loader that is before all ProxyClassLoaders. */
93
    private ClassLoader systemCL = ClassLoader.getSystemClassLoader();
94
    private ClassLoader systemCL = TOP_CL;
94
 
95
 
95
    /** A shared map of all packages known by all classloaders. Also covers META-INF based resources.
96
    /** A shared map of all packages known by all classloaders. Also covers META-INF based resources.
96
     * It contains two kinds of keys: dot-separated package names and slash-separated
97
     * It contains two kinds of keys: dot-separated package names and slash-separated
Lines 112-117 public class ProxyClassLoader extends Cl Link Here
112
     *                   automatically search through its parent list
113
     *                   automatically search through its parent list
113
     */
114
     */
114
    public ProxyClassLoader(ClassLoader[] parents, boolean transitive) {
115
    public ProxyClassLoader(ClassLoader[] parents, boolean transitive) {
116
        super(TOP_CL);
115
        this.transitive = transitive;
117
        this.transitive = transitive;
116
        
118
        
117
        this.parents = coalesceParents(parents);
119
        this.parents = coalesceParents(parents);
(-)264b7c1732cb (+190 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5
 * 
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 * 
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 * 
35
 * Contributor(s):
36
 * 
37
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.junit;
41
42
import java.io.File;
43
import java.io.IOException;
44
import java.lang.reflect.Method;
45
import java.net.MalformedURLException;
46
import java.net.URISyntaxException;
47
import java.net.URL;
48
import java.net.URLClassLoader;
49
import java.util.ArrayList;
50
import java.util.Collection;
51
import java.util.Enumeration;
52
import java.util.LinkedHashSet;
53
import java.util.List;
54
import junit.framework.Assert;
55
import junit.framework.TestCase;
56
import junit.framework.TestResult;
57
import org.openide.util.Exceptions;
58
import org.openide.util.Lookup;
59
60
/**
61
 *
62
 * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
63
 */
64
public class NbModuleSuite extends NbTestSuite {
65
    private Class<?> clazz;
66
67
    public NbModuleSuite(Class<?> aClass) {
68
        super();
69
        clazz = aClass;
70
    }
71
72
    @Override
73
    public void run(TestResult result) {
74
        try {
75
            runInRuntimeContainer(result);
76
        } catch (Exception ex) {
77
            result.addError(this, ex);
78
        }
79
    }
80
    
81
    private void runInRuntimeContainer(TestResult result) throws Exception {
82
        File platform = findPlatform();
83
        File[] boot = new File(platform, "lib").listFiles();
84
        List<URL> bootCP = new ArrayList<URL>();
85
        for (int i = 0; i < boot.length; i++) {
86
            URL u = boot[i].toURL();
87
            if (u.toExternalForm().endsWith(".jar")) {
88
                bootCP.add(u);
89
            }
90
        }
91
        // loader that does not see our current classloader
92
        ClassLoader parent = ClassLoader.getSystemClassLoader().getParent();
93
        Assert.assertNotNull("Parent", parent);
94
        URLClassLoader loader = new URLClassLoader(bootCP.toArray(new URL[0]), parent);
95
        Class<?> main = loader.loadClass("org.netbeans.Main"); // NOI18N
96
        Assert.assertEquals("Loaded by our classloader", loader, main.getClassLoader());
97
        Method m = main.getDeclaredMethod("main", String[].class); // NOI18N
98
        
99
        System.setProperty("java.util.logging.config", "-");
100
        System.setProperty("netbeans.home", platform.getPath());
101
        
102
        File ud = new File(new File(Manager.getWorkDirPath()), "userdir");
103
        ud.mkdirs();
104
        NbTestCase.deleteSubFiles(ud);
105
        
106
        System.setProperty("netbeans.user", ud.getPath());
107
        
108
        List<String> args = new ArrayList<String>();
109
        args.add("--nosplash");
110
        m.invoke(null, (Object)args.toArray(new String[0]));
111
        
112
        ClassLoader global = Lookup.getDefault().lookup(ClassLoader.class);
113
        Assert.assertNotNull("Global classloader is initialized", global);
114
        
115
        URL[] testCP = preparePath(clazz);
116
        JunitLoader testLoader = new JunitLoader(testCP, global, NbTestSuite.class.getClassLoader());
117
        Class<?> sndClazz = testLoader.loadClass(clazz.getName());
118
119
        new NbTestSuite(sndClazz).run(result);
120
    }
121
    
122
    private URL[] preparePath(Class<?>... classes) {
123
        Collection<URL> cp = new LinkedHashSet<URL>();
124
        for (Class c : classes) {
125
            URL test = c.getProtectionDomain().getCodeSource().getLocation();
126
            Assert.assertNotNull("URL found for " + c, test);
127
            cp.add(test);
128
        }
129
        return cp.toArray(new URL[0]);
130
    }
131
132
    
133
    private File findPlatform() {
134
        try {
135
            File util = new File(Lookup.class.getProtectionDomain().getCodeSource().getLocation().toURI());
136
            Assert.assertTrue("Util exists: " + util, util.exists());
137
138
            return util.getParentFile().getParentFile();
139
        } catch (URISyntaxException ex) {
140
            Assert.fail("Cannot find utilities JAR");
141
            return null;
142
        }
143
    }
144
    
145
    private static final class JunitLoader extends URLClassLoader {
146
        private final ClassLoader junit;
147
148
        public JunitLoader(URL[] urls, ClassLoader parent, ClassLoader junit) {
149
            super(urls, parent);
150
            this.junit = junit;
151
        }
152
153
        @Override
154
        protected Class<?> findClass(String name) throws ClassNotFoundException {
155
            if (isUnit(name)) {
156
                return junit.loadClass(name);
157
            }
158
            return super.findClass(name);
159
        }
160
161
        @Override
162
        public URL findResource(String name) {
163
            if (isUnit(name)) {
164
                return junit.getResource(name);
165
            }
166
            return super.findResource(name);
167
        }
168
169
        @Override
170
        public Enumeration<URL> findResources(String name) throws IOException {
171
            if (isUnit(name)) {
172
                return junit.getResources(name);
173
            }
174
            return super.findResources(name);
175
        }
176
        
177
        private final boolean isUnit(String res) {
178
            if (res.startsWith("junit")) {
179
                return true;
180
            }
181
            if (res.startsWith("org.junit") || res.startsWith("org/junit")) {
182
                return true;
183
            }
184
            if (res.startsWith("org.netbeans.junit") || res.startsWith("org/netbeans/junit")) {
185
                return true;
186
            }
187
            return false;
188
        }
189
    }
190
}
(-)a/nbjunit/src/org/netbeans/junit/NbTestCase.java (-2 / +2 lines)
Lines 732-738 public abstract class NbTestCase extends Link Here
732
    }
732
    }
733
    
733
    
734
    // private method for deleting a file/directory (and all its subdirectories/files)
734
    // private method for deleting a file/directory (and all its subdirectories/files)
735
    private void deleteFile(File file) throws IOException {
735
    private static void deleteFile(File file) throws IOException {
736
        if (file.isDirectory()) {
736
        if (file.isDirectory()) {
737
            // file is a directory - delete sub files first
737
            // file is a directory - delete sub files first
738
            File files[] = file.listFiles();
738
            File files[] = file.listFiles();
Lines 750-756 public abstract class NbTestCase extends Link Here
750
    }
750
    }
751
    
751
    
752
    // private method for deleting every subfiles/subdirectories of a file object
752
    // private method for deleting every subfiles/subdirectories of a file object
753
    private void deleteSubFiles(File file) throws IOException {
753
    static void deleteSubFiles(File file) throws IOException {
754
        if (file.isDirectory()) {
754
        if (file.isDirectory()) {
755
            File files[] = file.listFiles();
755
            File files[] = file.listFiles();
756
            for (int i = 0; i < files.length; i++) {
756
            for (int i = 0; i < files.length; i++) {
(-)264b7c1732cb (+17 lines)
Added Link Here
1
package org.netbeans.junit;
2
3
import junit.framework.TestCase;
4
5
public class NbModuleSuiteHelper extends TestCase {
6
    static {
7
        System.err.println("here");
8
    }
9
10
    public NbModuleSuiteHelper(String t) {
11
        super(t);
12
    }
13
14
    public void testOne() {
15
        System.setProperty("t.one", "OK");
16
    }
17
}
(-)264b7c1732cb (+74 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5
 * 
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 * 
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 * 
35
 * Contributor(s):
36
 * 
37
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.junit;
41
42
import junit.framework.TestCase;
43
import junit.framework.TestResult;
44
45
/**
46
 *
47
 * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
48
 */
49
public class NbModuleSuiteTest extends TestCase {
50
    
51
    public NbModuleSuiteTest(String testName) {
52
        super(testName);
53
    }            
54
55
    @Override
56
    protected void setUp() throws Exception {
57
        super.setUp();
58
    }
59
60
    @Override
61
    protected void tearDown() throws Exception {
62
        super.tearDown();
63
    }
64
65
    /**
66
     * Test of run method, of class NbModuleSuite.
67
     */
68
    public void testRun() {
69
        NbModuleSuite instance = new NbModuleSuite(NbModuleSuiteHelper.class);
70
        junit.textui.TestRunner.run(instance);
71
        
72
        assertEquals("OK", System.getProperty("t.one"));
73
    }
74
}
(-)a/nbjunit/test/unit/src/org/netbeans/junit/NbModuleSuiteHelper.java (-17 lines)
Removed Link Here
1
package org.netbeans.junit;
2
3
import junit.framework.TestCase;
4
5
public class NbModuleSuiteHelper extends TestCase {
6
    static {
7
        System.err.println("here");
8
    }
9
10
    public NbModuleSuiteHelper(String t) {
11
        super(t);
12
    }
13
14
    public void testOne() {
15
        System.setProperty("t.one", "OK");
16
    }
17
}
(-)a/nbjunit/test/unit/src/org/netbeans/junit/NbModuleSuiteTest.java (-1 / +12 lines)
Lines 66-74 public class NbModuleSuiteTest extends T Link Here
66
     * Test of run method, of class NbModuleSuite.
66
     * Test of run method, of class NbModuleSuite.
67
     */
67
     */
68
    public void testRun() {
68
    public void testRun() {
69
        NbModuleSuite instance = new NbModuleSuite(NbModuleSuiteHelper.class);
69
        NbModuleSuite instance = new NbModuleSuite(T.class);
70
        junit.textui.TestRunner.run(instance);
70
        junit.textui.TestRunner.run(instance);
71
        
71
        
72
        assertEquals("OK", System.getProperty("t.one"));
72
        assertEquals("OK", System.getProperty("t.one"));
73
    }
73
    }
74
    
75
    public static class T extends TestCase {
76
        public T(String t) {
77
            super(t);
78
        }
79
80
        public void testOne() {
81
            System.setProperty("t.one", "OK");
82
        }
83
    }
84
    
74
}
85
}
(-)a/nbjunit/src/org/netbeans/junit/NbModuleSuite.java (+88 lines)
Lines 40-46 package org.netbeans.junit; Link Here
40
package org.netbeans.junit;
40
package org.netbeans.junit;
41
41
42
import java.io.File;
42
import java.io.File;
43
import java.io.FileInputStream;
44
import java.io.FileOutputStream;
43
import java.io.IOException;
45
import java.io.IOException;
46
import java.io.InputStream;
44
import java.lang.reflect.Method;
47
import java.lang.reflect.Method;
45
import java.net.MalformedURLException;
48
import java.net.MalformedURLException;
46
import java.net.URISyntaxException;
49
import java.net.URISyntaxException;
Lines 49-56 import java.util.ArrayList; Link Here
49
import java.util.ArrayList;
52
import java.util.ArrayList;
50
import java.util.Collection;
53
import java.util.Collection;
51
import java.util.Enumeration;
54
import java.util.Enumeration;
55
import java.util.HashSet;
52
import java.util.LinkedHashSet;
56
import java.util.LinkedHashSet;
53
import java.util.List;
57
import java.util.List;
58
import java.util.Set;
59
import java.util.TreeSet;
60
import java.util.regex.Matcher;
61
import java.util.regex.Pattern;
54
import junit.framework.Assert;
62
import junit.framework.Assert;
55
import junit.framework.TestCase;
63
import junit.framework.TestCase;
56
import junit.framework.TestResult;
64
import junit.framework.TestResult;
Lines 105-110 public class NbModuleSuite extends NbTes Link Here
105
        
113
        
106
        System.setProperty("netbeans.user", ud.getPath());
114
        System.setProperty("netbeans.user", ud.getPath());
107
        
115
        
116
        TreeSet<String> modules = new TreeSet<String>();
117
        modules.addAll(findEnabledModules(NbTestSuite.class.getClassLoader()));
118
        modules.add("org.openide.filesystems");
119
        modules.add("org.openide.modules");
120
        modules.add("org.openide.util");
121
        modules.add("org.netbeans.core.startup");
122
        modules.add("org.netbeans");
123
        turnModules(ud, modules, platform);
124
        
108
        List<String> args = new ArrayList<String>();
125
        List<String> args = new ArrayList<String>();
109
        args.add("--nosplash");
126
        args.add("--nosplash");
110
        m.invoke(null, (Object)args.toArray(new String[0]));
127
        m.invoke(null, (Object)args.toArray(new String[0]));
Lines 140-145 public class NbModuleSuite extends NbTes Link Here
140
            Assert.fail("Cannot find utilities JAR");
157
            Assert.fail("Cannot find utilities JAR");
141
            return null;
158
            return null;
142
        }
159
        }
160
    }
161
    
162
    private static Pattern CODENAME = Pattern.compile("OpenIDE-Module: *([^/$ \n\r]*)[/]?[0-9]*", Pattern.MULTILINE);
163
    /** Looks for all modules on classpath of given loader and builds 
164
     * their list from them.
165
     */
166
    static Set<String> findEnabledModules(ClassLoader loader) throws IOException {
167
        Set<String> cnbs = new TreeSet<String>();
168
        
169
        Enumeration<URL> en = loader.getResources("META-INF/MANIFEST.MF");
170
        while (en.hasMoreElements()) {
171
            URL url = en.nextElement();
172
            String manifest = asString(url.openStream(), true);
173
            Matcher m = CODENAME.matcher(manifest);
174
            if (m.find()) {
175
                cnbs.add(m.group(1));
176
            }
177
        }
178
179
        return cnbs;
180
    }
181
    
182
    private static String asString(InputStream is, boolean close) throws IOException {
183
        byte[] arr = new byte[is.available()];
184
        int len = is.read(arr);
185
        if (len != arr.length) {
186
            throw new IOException("Not fully read: " + arr.length + " was " + len);
187
        }
188
        if (close) {
189
            is.close();
190
        }
191
        return new String(arr, "UTF-8"); // NOI18N
143
    }
192
    }
144
    
193
    
145
    private static final class JunitLoader extends URLClassLoader {
194
    private static final class JunitLoader extends URLClassLoader {
Lines 187-190 public class NbModuleSuite extends NbTes Link Here
187
            return false;
236
            return false;
188
        }
237
        }
189
    }
238
    }
239
240
    private static Pattern ENABLED = Pattern.compile("<param name=[\"']enabled[\"']>([^<]*)</param>", Pattern.MULTILINE);
241
    
242
    private static void turnModules(File ud, TreeSet<String> modules, File... clusterDirs) throws IOException {
243
        File config = new File(new File(ud, "config"), "Modules");
244
        config.mkdirs();
245
        
246
        for (File c : clusterDirs) {
247
            File modulesDir = new File(new File(c, "config"), "Modules");
248
            for (File m : modulesDir.listFiles()) {
249
                String n = m.getName();
250
                if (n.endsWith(".xml")) {
251
                    n = n.substring(0, n.length() - 4);
252
                }
253
                n = n.replace('-', '.');
254
                
255
                String xml = asString(new FileInputStream(m), true);
256
                Matcher matcherEnabled = ENABLED.matcher(xml);
257
             //   Matcher matcherEager = EAGER.matcher(xml);
258
                
259
                boolean enabled = matcherEnabled.find() && "true".equals(matcherEnabled.group(1));
260
                
261
                if (modules.contains(n) != enabled) {
262
                    String out = 
263
                        xml.substring(0, matcherEnabled.start(1)) +
264
                        (enabled ? "false" : "true") +
265
                        xml.substring(matcherEnabled.end(1));
266
                    writeModule(new File(config, m.getName()), out);
267
                }
268
            }
269
        }
270
    }
271
272
    private static void writeModule(File file, String xml) throws IOException {
273
        FileOutputStream os = new FileOutputStream(file);
274
        os.write(xml.getBytes("UTF-8"));
275
        os.close();
276
    }
190
}
277
}
278
(-)a/nbjunit/test/unit/src/org/netbeans/junit/NbModuleSuiteTest.java (+10 lines)
Lines 39-44 Link Here
39
39
40
package org.netbeans.junit;
40
package org.netbeans.junit;
41
41
42
import java.util.Set;
42
import junit.framework.TestCase;
43
import junit.framework.TestCase;
43
import junit.framework.TestResult;
44
import junit.framework.TestResult;
44
45
Lines 72-77 public class NbModuleSuiteTest extends T Link Here
72
        assertEquals("OK", System.getProperty("t.one"));
73
        assertEquals("OK", System.getProperty("t.one"));
73
    }
74
    }
74
    
75
    
76
    public void testModulesForCL() throws Exception {
77
        Set<String> s = NbModuleSuite.findEnabledModules(ClassLoader.getSystemClassLoader());
78
        assertEquals("Three modules: " + s, 3, s.size());
79
        
80
        assertTrue("Util: " + s, s.contains("org.openide.util"));
81
        assertTrue("nbjunit: " + s, s.contains("org.netbeans.modules.nbjunit"));
82
        assertTrue("insanse: " + s, s.contains("org.netbeans.insane"));
83
    }
84
    
75
    public static class T extends TestCase {
85
    public static class T extends TestCase {
76
        public T(String t) {
86
        public T(String t) {
77
            super(t);
87
            super(t);
(-)a/nbjunit/src/org/netbeans/junit/NbModuleSuite.java (-12 / +50 lines)
Lines 45-58 import java.io.IOException; Link Here
45
import java.io.IOException;
45
import java.io.IOException;
46
import java.io.InputStream;
46
import java.io.InputStream;
47
import java.lang.reflect.Method;
47
import java.lang.reflect.Method;
48
import java.net.MalformedURLException;
49
import java.net.URISyntaxException;
48
import java.net.URISyntaxException;
50
import java.net.URL;
49
import java.net.URL;
51
import java.net.URLClassLoader;
50
import java.net.URLClassLoader;
52
import java.util.ArrayList;
51
import java.util.ArrayList;
53
import java.util.Collection;
52
import java.util.Collection;
54
import java.util.Enumeration;
53
import java.util.Enumeration;
55
import java.util.HashSet;
56
import java.util.LinkedHashSet;
54
import java.util.LinkedHashSet;
57
import java.util.List;
55
import java.util.List;
58
import java.util.Set;
56
import java.util.Set;
Lines 60-68 import java.util.regex.Matcher; Link Here
60
import java.util.regex.Matcher;
58
import java.util.regex.Matcher;
61
import java.util.regex.Pattern;
59
import java.util.regex.Pattern;
62
import junit.framework.Assert;
60
import junit.framework.Assert;
63
import junit.framework.TestCase;
64
import junit.framework.TestResult;
61
import junit.framework.TestResult;
65
import org.openide.util.Exceptions;
66
import org.openide.util.Lookup;
62
import org.openide.util.Lookup;
67
63
68
/**
64
/**
Lines 70-80 import org.openide.util.Lookup; Link Here
70
 * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
66
 * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
71
 */
67
 */
72
public class NbModuleSuite extends NbTestSuite {
68
public class NbModuleSuite extends NbTestSuite {
73
    private Class<?> clazz;
69
    private final Class<?> clazz;
70
    private final String clusterRegExp;
74
71
75
    public NbModuleSuite(Class<?> aClass) {
72
    public NbModuleSuite(Class<?> aClass) {
73
        this(aClass, ".*");
74
    }
75
    
76
    public NbModuleSuite(Class<?> aClass, String clusterRegExp) {
76
        super();
77
        super();
77
        clazz = aClass;
78
        this.clazz = aClass;
79
        this.clusterRegExp = clusterRegExp;
78
    }
80
    }
79
81
80
    @Override
82
    @Override
Lines 118-126 public class NbModuleSuite extends NbTes Link Here
118
        modules.add("org.openide.filesystems");
120
        modules.add("org.openide.filesystems");
119
        modules.add("org.openide.modules");
121
        modules.add("org.openide.modules");
120
        modules.add("org.openide.util");
122
        modules.add("org.openide.util");
123
        modules.remove("org.netbeans.insane");
121
        modules.add("org.netbeans.core.startup");
124
        modules.add("org.netbeans.core.startup");
122
        modules.add("org.netbeans");
125
        modules.add("org.netbeans.bootstrap");
123
        turnModules(ud, modules, platform);
126
        turnModules(ud, modules, platform);
127
128
        StringBuilder sb = new StringBuilder();
129
        String sep = "";
130
        for (File f : findClusters()) {
131
            turnModules(ud, modules, f);
132
            sb.append(sep);
133
            sb.append(f.getPath());
134
            sep = File.pathSeparator;
135
        }
136
        System.setProperty("netbeans.dirs", sb.toString());
124
        
137
        
125
        List<String> args = new ArrayList<String>();
138
        List<String> args = new ArrayList<String>();
126
        args.add("--nosplash");
139
        args.add("--nosplash");
Lines 157-162 public class NbModuleSuite extends NbTes Link Here
157
            Assert.fail("Cannot find utilities JAR");
170
            Assert.fail("Cannot find utilities JAR");
158
            return null;
171
            return null;
159
        }
172
        }
173
    }
174
    
175
    private File[] findClusters() {
176
        List<File> clusters = new ArrayList<File>();
177
        File plat = findPlatform();
178
        
179
        for (File f : plat.getParentFile().listFiles()) {
180
            if (f.equals(plat)) {
181
                continue;
182
            }
183
            if (!f.getName().matches(clusterRegExp)) {
184
                continue;
185
            }
186
            File m = new File(new File(f, "config"), "Modules");
187
            if (m.exists()) {
188
                clusters.add(f);
189
            }
190
        }
191
        return clusters.toArray(new File[0]);
160
    }
192
    }
161
    
193
    
162
    private static Pattern CODENAME = Pattern.compile("OpenIDE-Module: *([^/$ \n\r]*)[/]?[0-9]*", Pattern.MULTILINE);
194
    private static Pattern CODENAME = Pattern.compile("OpenIDE-Module: *([^/$ \n\r]*)[/]?[0-9]*", Pattern.MULTILINE);
Lines 259-269 public class NbModuleSuite extends NbTes Link Here
259
                boolean enabled = matcherEnabled.find() && "true".equals(matcherEnabled.group(1));
291
                boolean enabled = matcherEnabled.find() && "true".equals(matcherEnabled.group(1));
260
                
292
                
261
                if (modules.contains(n) != enabled) {
293
                if (modules.contains(n) != enabled) {
262
                    String out = 
294
                    assert matcherEnabled.groupCount() == 1 : "Groups: " + matcherEnabled.groupCount() + " for:\n" + xml;
263
                        xml.substring(0, matcherEnabled.start(1)) +
295
264
                        (enabled ? "false" : "true") +
296
                    try {
265
                        xml.substring(matcherEnabled.end(1));
297
                        String out = 
266
                    writeModule(new File(config, m.getName()), out);
298
                            xml.substring(0, matcherEnabled.start(1)) +
299
                            (enabled ? "false" : "true") +
300
                            xml.substring(matcherEnabled.end(1));
301
                        writeModule(new File(config, m.getName()), out);
302
                    } catch (IllegalStateException ex) {
303
                        throw (IOException)new IOException("Unparseable:\n" + xml).initCause(ex);
304
                    }
267
                }
305
                }
268
            }
306
            }
269
        }
307
        }
(-)a/nbjunit/test/unit/src/org/netbeans/junit/NbModuleSuiteTest.java (-1 / +1 lines)
Lines 67-73 public class NbModuleSuiteTest extends T Link Here
67
     * Test of run method, of class NbModuleSuite.
67
     * Test of run method, of class NbModuleSuite.
68
     */
68
     */
69
    public void testRun() {
69
    public void testRun() {
70
        NbModuleSuite instance = new NbModuleSuite(T.class);
70
        NbModuleSuite instance = new NbModuleSuite(T.class, "");
71
        junit.textui.TestRunner.run(instance);
71
        junit.textui.TestRunner.run(instance);
72
        
72
        
73
        assertEquals("OK", System.getProperty("t.one"));
73
        assertEquals("OK", System.getProperty("t.one"));
(-)a/nbjunit/apichanges.xml (-1 / +22 lines)
Lines 55-65 made subject to such option by the copyr Link Here
55
    <!-- ACTUAL CHANGES BEGIN HERE: -->
55
    <!-- ACTUAL CHANGES BEGIN HERE: -->
56
56
57
<changes>
57
<changes>
58
    <change id="NbModuleSuite">
59
        <api name="nbjunit"/>
60
        <summary>Support class to allow excution of unit tests inside of NetBeans Runtime Container</summary>
61
        <version major="1" minor="46"/>
62
        <date day="29" month="2" year="2008"/>
63
        <author login="jtulach"/>
64
        <compatibility addition="yes"/>
65
        <description>
66
            <p>
67
                It always used to be hard to setup the right environment for test
68
                execution. In case this NetBeans this is even harder. The module
69
                system is ready to work without classpath isolation, however 
70
                not every feature is available and every behaviour stays the
71
                same. That is why there is now the new <code>NbModuleSuite</code>
72
                support class that allows to really start the whole NetBeans 
73
                Runtime Container, satisfy all the dependencies and only then 
74
                load the testing class and execute it.
75
            </p>
76
        </description>
77
        <class package="org.netbeans.junit" name="NbModuleSuite"/>
78
    </change>
58
    <change id="CollectData">
79
    <change id="CollectData">
59
        <api name="nbjunit"/>
80
        <api name="nbjunit"/>
60
        <summary>Support for Garbage Collecting of Log Message Arguments</summary>
81
        <summary>Support for Garbage Collecting of Log Message Arguments</summary>
61
        <version major="1" minor="44"/>
82
        <version major="1" minor="44"/>
62
        <date day="14" month="12" year="2006"/>
83
        <date day="14" month="12" year="2007"/>
63
        <author login="jtulach"/>
84
        <author login="jtulach"/>
64
        <compatibility addition="yes"/>
85
        <compatibility addition="yes"/>
65
        <description>
86
        <description>
(-)a/nbjunit/manifest.mf (-1 / +1 lines)
Lines 2-5 OpenIDE-Module: org.netbeans.modules.nbj Link Here
2
OpenIDE-Module: org.netbeans.modules.nbjunit/1
2
OpenIDE-Module: org.netbeans.modules.nbjunit/1
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/junit/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/junit/Bundle.properties
4
OpenIDE-Module-Layer: org/netbeans/junit/layer.xml
4
OpenIDE-Module-Layer: org/netbeans/junit/layer.xml
5
OpenIDE-Module-Specification-Version: 1.45
5
OpenIDE-Module-Specification-Version: 1.46
(-)a/nbjunit/src/org/netbeans/junit/NbModuleSuite.java (-214 / +269 lines)
Lines 58-316 import java.util.regex.Matcher; Link Here
58
import java.util.regex.Matcher;
58
import java.util.regex.Matcher;
59
import java.util.regex.Pattern;
59
import java.util.regex.Pattern;
60
import junit.framework.Assert;
60
import junit.framework.Assert;
61
import junit.framework.Test;
61
import junit.framework.TestResult;
62
import junit.framework.TestResult;
62
import org.openide.util.Lookup;
63
import org.openide.util.Lookup;
63
64
64
/**
65
/**
66
 * Wraps a test class with proper NetBeans Runtime Container environment.
67
 * This allows to execute tests in a very similar environment to the 
68
 * actual invocation in the NetBeans IDE. To use write your test as
69
 * you are used to and add suite static method:
70
 * <pre>
71
 * public class YourTest extends NbTestCase {
72
 *   public YourTest(String s) { super(s); }
73
 * 
74
 *   public static Test suite() {
75
 *     return NbModuleSuite.create(YourTest.class);
76
 *   }
77
 * 
78
 *   public void testXYZ() { ... }
79
 *   public void testABC() { ... }
80
 * }
81
 * </pre>
65
 *
82
 *
66
 * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
83
 * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
67
 */
84
 */
68
public class NbModuleSuite extends NbTestSuite {
85
public final class NbModuleSuite extends Object {
69
    private final Class<?> clazz;
70
    private final String clusterRegExp;
71
86
72
    public NbModuleSuite(Class<?> aClass) {
87
    /** Wraps the provided class into a test that set ups properly the
73
        this(aClass, ".*");
88
     * testing environment. The set of enabled modules is going to be
74
    }
89
     * determined from the actual classpath of a module, which is common
75
    
90
     * when in all NetBeans tests. All other modules are kept disabled.
76
    public NbModuleSuite(Class<?> aClass, String clusterRegExp) {
91
     * <p>
77
        super();
92
     * 
78
        this.clazz = aClass;
93
     * Warning: because the NetBeans Runtime Environment
79
        this.clusterRegExp = clusterRegExp;
94
     * plays various tricks with classloaders, the class provided as argument
95
     * is just a <quote>template</quote>, the one that will really be executed
96
     * is loaded by another classloader. As such it is not recommended to
97
     * do any static initializations in the class, as that would be run twice,
98
     * in different modes and could cause quite a big mishmash.
99
     * 
100
     * @param clazz the class with bunch of testXYZ methods
101
     * @return test that starts the NetBeans Runtime Container and then runs the 
102
     */
103
    public static Test create(Class<? extends Test> clazz) {
104
        return new S(clazz);
80
    }
105
    }
81
106
82
    @Override
107
    /** Factory method to create wrapper test that knows how to setup proper
83
    public void run(TestResult result) {
108
     * NetBeans Runtime Container environment. In addition to other factory 
84
        try {
109
     * methods, it allows one limit the clusters that shall be made available.
85
            runInRuntimeContainer(result);
110
     * For example <code>ide.*|java.*</code> will start the container just
86
        } catch (Exception ex) {
111
     * with platform, ide and java clusters.
87
            result.addError(this, ex);
112
     * 
88
        }
113
     * 
89
    }
114
     * @param clazz the class with bunch of testXYZ methods
90
    
115
     * @param clustersRegExp regexp to apply to name of cluster to find out if it is supposed to be included
91
    private void runInRuntimeContainer(TestResult result) throws Exception {
116
     *    in the runtime container setup or not
92
        File platform = findPlatform();
117
     * @return runtime container ready test
93
        File[] boot = new File(platform, "lib").listFiles();
118
     */
94
        List<URL> bootCP = new ArrayList<URL>();
119
    public static Test create(Class<? extends Test> clazz, String clustersRegExp) {
95
        for (int i = 0; i < boot.length; i++) {
120
        return new S(clazz);
96
            URL u = boot[i].toURL();
97
            if (u.toExternalForm().endsWith(".jar")) {
98
                bootCP.add(u);
99
            }
100
        }
101
        // loader that does not see our current classloader
102
        ClassLoader parent = ClassLoader.getSystemClassLoader().getParent();
103
        Assert.assertNotNull("Parent", parent);
104
        URLClassLoader loader = new URLClassLoader(bootCP.toArray(new URL[0]), parent);
105
        Class<?> main = loader.loadClass("org.netbeans.Main"); // NOI18N
106
        Assert.assertEquals("Loaded by our classloader", loader, main.getClassLoader());
107
        Method m = main.getDeclaredMethod("main", String[].class); // NOI18N
108
        
109
        System.setProperty("java.util.logging.config", "-");
110
        System.setProperty("netbeans.home", platform.getPath());
111
        
112
        File ud = new File(new File(Manager.getWorkDirPath()), "userdir");
113
        ud.mkdirs();
114
        NbTestCase.deleteSubFiles(ud);
115
        
116
        System.setProperty("netbeans.user", ud.getPath());
117
        
118
        TreeSet<String> modules = new TreeSet<String>();
119
        modules.addAll(findEnabledModules(NbTestSuite.class.getClassLoader()));
120
        modules.add("org.openide.filesystems");
121
        modules.add("org.openide.modules");
122
        modules.add("org.openide.util");
123
        modules.remove("org.netbeans.insane");
124
        modules.add("org.netbeans.core.startup");
125
        modules.add("org.netbeans.bootstrap");
126
        turnModules(ud, modules, platform);
127
128
        StringBuilder sb = new StringBuilder();
129
        String sep = "";
130
        for (File f : findClusters()) {
131
            turnModules(ud, modules, f);
132
            sb.append(sep);
133
            sb.append(f.getPath());
134
            sep = File.pathSeparator;
135
        }
136
        System.setProperty("netbeans.dirs", sb.toString());
137
        
138
        List<String> args = new ArrayList<String>();
139
        args.add("--nosplash");
140
        m.invoke(null, (Object)args.toArray(new String[0]));
141
        
142
        ClassLoader global = Lookup.getDefault().lookup(ClassLoader.class);
143
        Assert.assertNotNull("Global classloader is initialized", global);
144
        
145
        URL[] testCP = preparePath(clazz);
146
        JunitLoader testLoader = new JunitLoader(testCP, global, NbTestSuite.class.getClassLoader());
147
        Class<?> sndClazz = testLoader.loadClass(clazz.getName());
148
149
        new NbTestSuite(sndClazz).run(result);
150
    }
151
    
152
    private URL[] preparePath(Class<?>... classes) {
153
        Collection<URL> cp = new LinkedHashSet<URL>();
154
        for (Class c : classes) {
155
            URL test = c.getProtectionDomain().getCodeSource().getLocation();
156
            Assert.assertNotNull("URL found for " + c, test);
157
            cp.add(test);
158
        }
159
        return cp.toArray(new URL[0]);
160
    }
121
    }
161
122
162
    
123
    static final class S extends NbTestSuite {
163
    private File findPlatform() {
124
        private final Class<?> clazz;
164
        try {
125
        private final String clusterRegExp;
165
            File util = new File(Lookup.class.getProtectionDomain().getCodeSource().getLocation().toURI());
166
            Assert.assertTrue("Util exists: " + util, util.exists());
167
126
168
            return util.getParentFile().getParentFile();
127
        public S(Class<?> aClass) {
169
        } catch (URISyntaxException ex) {
128
            this(aClass, ".*");
170
            Assert.fail("Cannot find utilities JAR");
171
            return null;
172
        }
129
        }
173
    }
130
174
    
131
        public S(Class<?> aClass, String clusterRegExp) {
175
    private File[] findClusters() {
132
            super();
176
        List<File> clusters = new ArrayList<File>();
133
            this.clazz = aClass;
177
        File plat = findPlatform();
134
            this.clusterRegExp = clusterRegExp;
178
        
179
        for (File f : plat.getParentFile().listFiles()) {
180
            if (f.equals(plat)) {
181
                continue;
182
            }
183
            if (!f.getName().matches(clusterRegExp)) {
184
                continue;
185
            }
186
            File m = new File(new File(f, "config"), "Modules");
187
            if (m.exists()) {
188
                clusters.add(f);
189
            }
190
        }
135
        }
191
        return clusters.toArray(new File[0]);
136
192
    }
137
        @Override
193
    
138
        public void run(TestResult result) {
194
    private static Pattern CODENAME = Pattern.compile("OpenIDE-Module: *([^/$ \n\r]*)[/]?[0-9]*", Pattern.MULTILINE);
139
            try {
195
    /** Looks for all modules on classpath of given loader and builds 
140
                runInRuntimeContainer(result);
196
     * their list from them.
141
            } catch (Exception ex) {
197
     */
142
                result.addError(this, ex);
198
    static Set<String> findEnabledModules(ClassLoader loader) throws IOException {
199
        Set<String> cnbs = new TreeSet<String>();
200
        
201
        Enumeration<URL> en = loader.getResources("META-INF/MANIFEST.MF");
202
        while (en.hasMoreElements()) {
203
            URL url = en.nextElement();
204
            String manifest = asString(url.openStream(), true);
205
            Matcher m = CODENAME.matcher(manifest);
206
            if (m.find()) {
207
                cnbs.add(m.group(1));
208
            }
143
            }
209
        }
144
        }
210
145
211
        return cnbs;
146
        private void runInRuntimeContainer(TestResult result) throws Exception {
212
    }
147
            File platform = findPlatform();
213
    
148
            File[] boot = new File(platform, "lib").listFiles();
214
    private static String asString(InputStream is, boolean close) throws IOException {
149
            List<URL> bootCP = new ArrayList<URL>();
215
        byte[] arr = new byte[is.available()];
150
            for (int i = 0; i < boot.length; i++) {
216
        int len = is.read(arr);
151
                URL u = boot[i].toURL();
217
        if (len != arr.length) {
152
                if (u.toExternalForm().endsWith(".jar")) {
218
            throw new IOException("Not fully read: " + arr.length + " was " + len);
153
                    bootCP.add(u);
219
        }
154
                }
220
        if (close) {
155
            }
221
            is.close();
156
            // loader that does not see our current classloader
222
        }
157
            ClassLoader parent = ClassLoader.getSystemClassLoader().getParent();
223
        return new String(arr, "UTF-8"); // NOI18N
158
            Assert.assertNotNull("Parent", parent);
224
    }
159
            URLClassLoader loader = new URLClassLoader(bootCP.toArray(new URL[0]), parent);
225
    
160
            Class<?> main = loader.loadClass("org.netbeans.Main"); // NOI18N
226
    private static final class JunitLoader extends URLClassLoader {
161
            Assert.assertEquals("Loaded by our classloader", loader, main.getClassLoader());
227
        private final ClassLoader junit;
162
            Method m = main.getDeclaredMethod("main", String[].class); // NOI18N
228
163
229
        public JunitLoader(URL[] urls, ClassLoader parent, ClassLoader junit) {
164
            System.setProperty("java.util.logging.config", "-");
230
            super(urls, parent);
165
            System.setProperty("netbeans.home", platform.getPath());
231
            this.junit = junit;
166
167
            File ud = new File(new File(Manager.getWorkDirPath()), "userdir");
168
            ud.mkdirs();
169
            NbTestCase.deleteSubFiles(ud);
170
171
            System.setProperty("netbeans.user", ud.getPath());
172
173
            TreeSet<String> modules = new TreeSet<String>();
174
            modules.addAll(findEnabledModules(NbTestSuite.class.getClassLoader()));
175
            modules.add("org.openide.filesystems");
176
            modules.add("org.openide.modules");
177
            modules.add("org.openide.util");
178
            modules.remove("org.netbeans.insane");
179
            modules.add("org.netbeans.core.startup");
180
            modules.add("org.netbeans.bootstrap");
181
            turnModules(ud, modules, platform);
182
183
            StringBuilder sb = new StringBuilder();
184
            String sep = "";
185
            for (File f : findClusters()) {
186
                turnModules(ud, modules, f);
187
                sb.append(sep);
188
                sb.append(f.getPath());
189
                sep = File.pathSeparator;
190
            }
191
            System.setProperty("netbeans.dirs", sb.toString());
192
193
            List<String> args = new ArrayList<String>();
194
            args.add("--nosplash");
195
            m.invoke(null, (Object)args.toArray(new String[0]));
196
197
            ClassLoader global = Lookup.getDefault().lookup(ClassLoader.class);
198
            Assert.assertNotNull("Global classloader is initialized", global);
199
200
            URL[] testCP = preparePath(clazz);
201
            JunitLoader testLoader = new JunitLoader(testCP, global, NbTestSuite.class.getClassLoader());
202
            Class<?> sndClazz = testLoader.loadClass(clazz.getName());
203
204
            new NbTestSuite(sndClazz).run(result);
232
        }
205
        }
233
206
234
        @Override
207
        private URL[] preparePath(Class<?>... classes) {
235
        protected Class<?> findClass(String name) throws ClassNotFoundException {
208
            Collection<URL> cp = new LinkedHashSet<URL>();
236
            if (isUnit(name)) {
209
            for (Class c : classes) {
237
                return junit.loadClass(name);
210
                URL test = c.getProtectionDomain().getCodeSource().getLocation();
211
                Assert.assertNotNull("URL found for " + c, test);
212
                cp.add(test);
238
            }
213
            }
239
            return super.findClass(name);
214
            return cp.toArray(new URL[0]);
240
        }
215
        }
241
216
242
        @Override
217
243
        public URL findResource(String name) {
218
        private File findPlatform() {
244
            if (isUnit(name)) {
219
            try {
245
                return junit.getResource(name);
220
                File util = new File(Lookup.class.getProtectionDomain().getCodeSource().getLocation().toURI());
221
                Assert.assertTrue("Util exists: " + util, util.exists());
222
223
                return util.getParentFile().getParentFile();
224
            } catch (URISyntaxException ex) {
225
                Assert.fail("Cannot find utilities JAR");
226
                return null;
246
            }
227
            }
247
            return super.findResource(name);
248
        }
228
        }
249
229
250
        @Override
230
        private File[] findClusters() {
251
        public Enumeration<URL> findResources(String name) throws IOException {
231
            List<File> clusters = new ArrayList<File>();
252
            if (isUnit(name)) {
232
            File plat = findPlatform();
253
                return junit.getResources(name);
233
234
            for (File f : plat.getParentFile().listFiles()) {
235
                if (f.equals(plat)) {
236
                    continue;
237
                }
238
                if (!f.getName().matches(clusterRegExp)) {
239
                    continue;
240
                }
241
                File m = new File(new File(f, "config"), "Modules");
242
                if (m.exists()) {
243
                    clusters.add(f);
244
                }
254
            }
245
            }
255
            return super.findResources(name);
246
            return clusters.toArray(new File[0]);
256
        }
247
        }
257
        
248
258
        private final boolean isUnit(String res) {
249
        private static Pattern CODENAME = Pattern.compile("OpenIDE-Module: *([^/$ \n\r]*)[/]?[0-9]*", Pattern.MULTILINE);
259
            if (res.startsWith("junit")) {
250
        /** Looks for all modules on classpath of given loader and builds 
260
                return true;
251
         * their list from them.
252
         */
253
        static Set<String> findEnabledModules(ClassLoader loader) throws IOException {
254
            Set<String> cnbs = new TreeSet<String>();
255
256
            Enumeration<URL> en = loader.getResources("META-INF/MANIFEST.MF");
257
            while (en.hasMoreElements()) {
258
                URL url = en.nextElement();
259
                String manifest = asString(url.openStream(), true);
260
                Matcher m = CODENAME.matcher(manifest);
261
                if (m.find()) {
262
                    cnbs.add(m.group(1));
263
                }
261
            }
264
            }
262
            if (res.startsWith("org.junit") || res.startsWith("org/junit")) {
265
263
                return true;
266
            return cnbs;
267
        }
268
269
        private static String asString(InputStream is, boolean close) throws IOException {
270
            byte[] arr = new byte[is.available()];
271
            int len = is.read(arr);
272
            if (len != arr.length) {
273
                throw new IOException("Not fully read: " + arr.length + " was " + len);
264
            }
274
            }
265
            if (res.startsWith("org.netbeans.junit") || res.startsWith("org/netbeans/junit")) {
275
            if (close) {
266
                return true;
276
                is.close();
267
            }
277
            }
268
            return false;
278
            return new String(arr, "UTF-8"); // NOI18N
269
        }
279
        }
270
    }
271
280
272
    private static Pattern ENABLED = Pattern.compile("<param name=[\"']enabled[\"']>([^<]*)</param>", Pattern.MULTILINE);
281
        private static final class JunitLoader extends URLClassLoader {
273
    
282
            private final ClassLoader junit;
274
    private static void turnModules(File ud, TreeSet<String> modules, File... clusterDirs) throws IOException {
283
275
        File config = new File(new File(ud, "config"), "Modules");
284
            public JunitLoader(URL[] urls, ClassLoader parent, ClassLoader junit) {
276
        config.mkdirs();
285
                super(urls, parent);
277
        
286
                this.junit = junit;
278
        for (File c : clusterDirs) {
287
            }
279
            File modulesDir = new File(new File(c, "config"), "Modules");
288
280
            for (File m : modulesDir.listFiles()) {
289
            @Override
281
                String n = m.getName();
290
            protected Class<?> findClass(String name) throws ClassNotFoundException {
282
                if (n.endsWith(".xml")) {
291
                if (isUnit(name)) {
283
                    n = n.substring(0, n.length() - 4);
292
                    return junit.loadClass(name);
284
                }
293
                }
285
                n = n.replace('-', '.');
294
                return super.findClass(name);
286
                
295
            }
287
                String xml = asString(new FileInputStream(m), true);
288
                Matcher matcherEnabled = ENABLED.matcher(xml);
289
             //   Matcher matcherEager = EAGER.matcher(xml);
290
                
291
                boolean enabled = matcherEnabled.find() && "true".equals(matcherEnabled.group(1));
292
                
293
                if (modules.contains(n) != enabled) {
294
                    assert matcherEnabled.groupCount() == 1 : "Groups: " + matcherEnabled.groupCount() + " for:\n" + xml;
295
296
296
                    try {
297
            @Override
297
                        String out = 
298
            public URL findResource(String name) {
298
                            xml.substring(0, matcherEnabled.start(1)) +
299
                if (isUnit(name)) {
299
                            (enabled ? "false" : "true") +
300
                    return junit.getResource(name);
300
                            xml.substring(matcherEnabled.end(1));
301
                }
301
                        writeModule(new File(config, m.getName()), out);
302
                return super.findResource(name);
302
                    } catch (IllegalStateException ex) {
303
            }
303
                        throw (IOException)new IOException("Unparseable:\n" + xml).initCause(ex);
304
305
            @Override
306
            public Enumeration<URL> findResources(String name) throws IOException {
307
                if (isUnit(name)) {
308
                    return junit.getResources(name);
309
                }
310
                return super.findResources(name);
311
            }
312
313
            private final boolean isUnit(String res) {
314
                if (res.startsWith("junit")) {
315
                    return true;
316
                }
317
                if (res.startsWith("org.junit") || res.startsWith("org/junit")) {
318
                    return true;
319
                }
320
                if (res.startsWith("org.netbeans.junit") || res.startsWith("org/netbeans/junit")) {
321
                    return true;
322
                }
323
                return false;
324
            }
325
        }
326
327
        private static Pattern ENABLED = Pattern.compile("<param name=[\"']enabled[\"']>([^<]*)</param>", Pattern.MULTILINE);
328
329
        private static void turnModules(File ud, TreeSet<String> modules, File... clusterDirs) throws IOException {
330
            File config = new File(new File(ud, "config"), "Modules");
331
            config.mkdirs();
332
333
            for (File c : clusterDirs) {
334
                File modulesDir = new File(new File(c, "config"), "Modules");
335
                for (File m : modulesDir.listFiles()) {
336
                    String n = m.getName();
337
                    if (n.endsWith(".xml")) {
338
                        n = n.substring(0, n.length() - 4);
339
                    }
340
                    n = n.replace('-', '.');
341
342
                    String xml = asString(new FileInputStream(m), true);
343
                    Matcher matcherEnabled = ENABLED.matcher(xml);
344
                 //   Matcher matcherEager = EAGER.matcher(xml);
345
346
                    boolean enabled = matcherEnabled.find() && "true".equals(matcherEnabled.group(1));
347
348
                    if (modules.contains(n) != enabled) {
349
                        assert matcherEnabled.groupCount() == 1 : "Groups: " + matcherEnabled.groupCount() + " for:\n" + xml;
350
351
                        try {
352
                            String out = 
353
                                xml.substring(0, matcherEnabled.start(1)) +
354
                                (enabled ? "false" : "true") +
355
                                xml.substring(matcherEnabled.end(1));
356
                            writeModule(new File(config, m.getName()), out);
357
                        } catch (IllegalStateException ex) {
358
                            throw (IOException)new IOException("Unparseable:\n" + xml).initCause(ex);
359
                        }
304
                    }
360
                    }
305
                }
361
                }
306
            }
362
            }
307
        }
363
        }
308
    }
309
364
310
    private static void writeModule(File file, String xml) throws IOException {
365
        private static void writeModule(File file, String xml) throws IOException {
311
        FileOutputStream os = new FileOutputStream(file);
366
            FileOutputStream os = new FileOutputStream(file);
312
        os.write(xml.getBytes("UTF-8"));
367
            os.write(xml.getBytes("UTF-8"));
313
        os.close();
368
            os.close();
314
    }
369
        }
370
    } // end of S
315
}
371
}
316
(-)a/nbjunit/test/unit/src/org/netbeans/junit/NbModuleSuiteTest.java (-3 / +3 lines)
Lines 40-47 package org.netbeans.junit; Link Here
40
package org.netbeans.junit;
40
package org.netbeans.junit;
41
41
42
import java.util.Set;
42
import java.util.Set;
43
import junit.framework.Test;
43
import junit.framework.TestCase;
44
import junit.framework.TestCase;
44
import junit.framework.TestResult;
45
45
46
/**
46
/**
47
 *
47
 *
Lines 67-80 public class NbModuleSuiteTest extends T Link Here
67
     * Test of run method, of class NbModuleSuite.
67
     * Test of run method, of class NbModuleSuite.
68
     */
68
     */
69
    public void testRun() {
69
    public void testRun() {
70
        NbModuleSuite instance = new NbModuleSuite(T.class, "");
70
        Test instance = NbModuleSuite.create(T.class, "");
71
        junit.textui.TestRunner.run(instance);
71
        junit.textui.TestRunner.run(instance);
72
        
72
        
73
        assertEquals("OK", System.getProperty("t.one"));
73
        assertEquals("OK", System.getProperty("t.one"));
74
    }
74
    }
75
    
75
    
76
    public void testModulesForCL() throws Exception {
76
    public void testModulesForCL() throws Exception {
77
        Set<String> s = NbModuleSuite.findEnabledModules(ClassLoader.getSystemClassLoader());
77
        Set<String> s = NbModuleSuite.S.findEnabledModules(ClassLoader.getSystemClassLoader());
78
        assertEquals("Three modules: " + s, 3, s.size());
78
        assertEquals("Three modules: " + s, 3, s.size());
79
        
79
        
80
        assertTrue("Util: " + s, s.contains("org.openide.util"));
80
        assertTrue("Util: " + s, s.contains("org.openide.util"));

Return to bug 128339