KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > navigator > UnitTestUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.navigator;
21
22 import java.beans.PropertyVetoException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import javax.swing.text.Utilities JavaDoc;
27 import junit.framework.Assert;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.filesystems.LocalFileSystem;
32 import org.openide.filesystems.Repository;
33 import org.openide.filesystems.XMLFileSystem;
34 import org.openide.util.Lookup;
35 import org.openide.util.lookup.Lookups;
36 import org.openide.util.lookup.ProxyLookup;
37 import org.xml.sax.SAXException JavaDoc;
38
39 /**
40  * Allows tests to install own layers for testing.
41  * Copied from org.netbeans.api.project.TestUtil.
42  *
43  * @author Dafe Simonek
44  */

45 public class UnitTestUtils extends ProxyLookup {
46
47     public static UnitTestUtils DEFAULT_LOOKUP = null;
48
49     /** Creates a new instance of UnitTestUtils */
50     public UnitTestUtils() {
51         Assert.assertNull(DEFAULT_LOOKUP);
52         DEFAULT_LOOKUP = this;
53     }
54     
55     /** Makes global layer from given string resource info */
56     public static void prepareTest(String JavaDoc[] stringLayers)
57                 throws IOException JavaDoc, SAXException JavaDoc, PropertyVetoException JavaDoc {
58         prepareTest(stringLayers, null);
59     }
60     
61     public static void prepareTest (String JavaDoc[] stringLayers, Lookup lkp)
62                 throws IOException JavaDoc, SAXException JavaDoc, PropertyVetoException JavaDoc {
63         URL JavaDoc[] layers = new URL JavaDoc[stringLayers.length];
64         
65         for (int cntr = 0; cntr < layers.length; cntr++) {
66             layers[cntr] = Utilities JavaDoc.class.getResource(stringLayers[cntr]);
67         }
68         
69         XMLFileSystem system = new XMLFileSystem();
70         system.setXmlUrls(layers);
71         
72         Repository repository = new Repository(system);
73         
74         if (lkp == null) {
75             DEFAULT_LOOKUP.setLookup(new Object JavaDoc[] { repository }, UnitTestUtils.class.getClassLoader());
76         } else {
77             DEFAULT_LOOKUP.setLookup(new Object JavaDoc[] { repository }, lkp, UnitTestUtils.class.getClassLoader());
78         }
79     }
80     
81     /**
82      * Set the global default lookup with some fixed instances including META-INF/services/*.
83      */

84     private static void setLookup(Object JavaDoc[] instances, ClassLoader JavaDoc cl) {
85         DEFAULT_LOOKUP.setLookups(new Lookup[] {
86             Lookups.fixed(instances),
87             Lookups.metaInfServices(cl),
88             Lookups.singleton(cl),
89         });
90     }
91     
92     private static void setLookup(Object JavaDoc[] instances, Lookup lkp, ClassLoader JavaDoc cl) {
93         DEFAULT_LOOKUP.setLookups(new Lookup[] {
94             lkp,
95             Lookups.fixed(instances),
96             Lookups.metaInfServices(cl),
97             Lookups.singleton(cl),
98         });
99     }
100     
101     
102     static {
103         UnitTestUtils.class.getClassLoader().setDefaultAssertionStatus(true);
104         System.setProperty("org.openide.util.Lookup", UnitTestUtils.class.getName());
105         Assert.assertEquals(UnitTestUtils.class, Lookup.getDefault().getClass());
106     }
107     
108     public static void initLookup() {
109         //currently nothing.
110
}
111
112 }
113
Popular Tags