KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > errorstripe > UnitUtilities


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 the LaTeX module.
16  * The Initial Developer of the Original Software is Jan Lahoda.
17  * Portions created by Jan Lahoda_ are Copyright (C) 2002-2004.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Jan Lahoda.
21  */

22 package org.netbeans.modules.editor.errorstripe;
23
24
25 import java.beans.PropertyVetoException JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.net.URL JavaDoc;
29 import javax.swing.text.Utilities JavaDoc;
30 import junit.framework.Assert;
31 import org.netbeans.junit.NbTestCase;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.LocalFileSystem;
35 import org.openide.filesystems.Repository;
36 import org.openide.filesystems.XMLFileSystem;
37 import org.openide.util.Lookup;
38 import org.openide.util.lookup.Lookups;
39 import org.openide.util.lookup.ProxyLookup;
40 import org.xml.sax.SAXException JavaDoc;
41
42 /**Inspired by org.netbeans.api.project.TestUtil.
43  *
44  * @author Jan Lahoda
45  */

46 public class UnitUtilities extends ProxyLookup {
47     
48     public static UnitUtilities DEFAULT_LOOKUP = null;
49     
50     public UnitUtilities() {
51         Assert.assertNull(DEFAULT_LOOKUP);
52         DEFAULT_LOOKUP = this;
53     }
54     
55     /**
56      * Set the global default lookup with some fixed instances including META-INF/services/*.
57      */

58     public static void setLookup(Object JavaDoc[] instances, ClassLoader JavaDoc cl) {
59         // XXX consider using MockServices
60
DEFAULT_LOOKUP.setLookups(new Lookup[] {
61             Lookups.fixed(instances),
62             Lookups.metaInfServices(cl),
63             Lookups.singleton(cl),
64         });
65     }
66     
67     public static void prepareTest(String JavaDoc[] additionalLayers, Object JavaDoc[] additionalLookupContent) throws IOException JavaDoc, SAXException JavaDoc, PropertyVetoException JavaDoc {
68         URL JavaDoc[] layers = new URL JavaDoc[additionalLayers.length + 1];
69         
70         layers[0] = Utilities JavaDoc.class.getResource("/org/netbeans/modules/editor/errorstripe/resources/layer.xml");
71         
72         for (int cntr = 0; cntr < additionalLayers.length; cntr++) {
73             layers[cntr + 1] = Utilities JavaDoc.class.getResource(additionalLayers[cntr]);
74         }
75         
76         for (int cntr = 0; cntr < layers.length; cntr++) {
77             if (layers[cntr] == null) {
78                 throw new IllegalStateException JavaDoc("layers[" + cntr + "]=null");
79             }
80         }
81         
82         XMLFileSystem system = new XMLFileSystem();
83         system.setXmlUrls(layers);
84         
85         Repository repository = new Repository(system);
86         Object JavaDoc[] lookupContent = new Object JavaDoc[additionalLookupContent.length + 1];
87         
88         System.arraycopy(additionalLookupContent, 0, lookupContent, 1, additionalLookupContent.length);
89         
90         lookupContent[0] = repository;
91         
92         DEFAULT_LOOKUP.setLookup(lookupContent, UnitUtilities.class.getClassLoader());
93     }
94     
95     static {
96         UnitUtilities.class.getClassLoader().setDefaultAssertionStatus(true);
97         System.setProperty("org.openide.util.Lookup", UnitUtilities.class.getName());
98         Assert.assertEquals(UnitUtilities.class, Lookup.getDefault().getClass());
99     }
100     
101     public static void initLookup() {
102         //currently nothing.
103
}
104     
105     /**Copied from org.netbeans.api.project.
106      * Create a scratch directory for tests.
107      * Will be in /tmp or whatever, and will be empty.
108      * If you just need a java.io.File use clearWorkDir + getWorkDir.
109      */

110     public static FileObject makeScratchDir(NbTestCase test) throws IOException JavaDoc {
111         test.clearWorkDir();
112         File JavaDoc root = test.getWorkDir();
113         assert root.isDirectory() && root.list().length == 0;
114         FileObject fo = FileUtil.toFileObject(root);
115         if (fo != null) {
116             // Presumably using masterfs.
117
return fo;
118         } else {
119             // For the benefit of those not using masterfs.
120
LocalFileSystem lfs = new LocalFileSystem();
121             try {
122                 lfs.setRootDirectory(root);
123             } catch (PropertyVetoException JavaDoc e) {
124                 assert false : e;
125             }
126             Repository.getDefault().addFileSystem(lfs);
127             return lfs.getRoot();
128         }
129     }
130     
131 }
132
Popular Tags