KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > app > EarDataNodeTest


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.j2ee.ddloaders.app;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.OutputStreamWriter JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import junit.framework.Assert;
32 import org.netbeans.junit.NbTestCase;
33 import org.openide.actions.OpenAction;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileSystem;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.filesystems.MIMEResolver;
38 import org.openide.filesystems.MultiFileSystem;
39 import org.openide.filesystems.Repository;
40 import org.openide.filesystems.XMLFileSystem;
41 import org.openide.loaders.DataLoader;
42 import org.openide.loaders.DataLoaderPool;
43 import org.openide.loaders.DataObject;
44 import org.openide.util.Enumerations;
45 import org.openide.util.Lookup;
46 import org.openide.util.lookup.Lookups;
47 import org.openide.util.lookup.ProxyLookup;
48
49 /**
50  * Test for {@link EarDataNode}.
51  *
52  * @author Martin Krauskopf
53  */

54 public class EarDataNodeTest extends NbTestCase {
55     
56     // Copied from org.netbeans.api.project.TestUtil:
57
static {
58         // XXX replace with MockServices
59
System.setProperty("org.openide.util.Lookup", Lkp.class.getName());
60         Assert.assertEquals(Lkp.class, Lookup.getDefault().getClass());
61     }
62     
63     public static final class Lkp extends ProxyLookup {
64         private static Lkp DEFAULT;
65         public Lkp() {
66             Assert.assertNull(DEFAULT);
67             DEFAULT = this;
68             setLookup(new Object JavaDoc[0]);
69         }
70         public static void setLookup(Object JavaDoc[] instances) {
71             ClassLoader JavaDoc l = Lkp.class.getClassLoader();
72             DEFAULT.setLookups(new Lookup[] {
73                 Lookups.fixed(instances),
74                 Lookups.metaInfServices(l),
75                 Lookups.singleton(l),
76             });
77         }
78     }
79     
80     public EarDataNodeTest(String JavaDoc testName) throws Exception JavaDoc {
81         super(testName);
82         Lkp.setLookup(new Object JavaDoc[] {
83             new Pool(),
84             new MR(),
85             new Repo()
86         });
87     }
88     
89     public void testGetActions() throws Exception JavaDoc {
90         File JavaDoc ddFile = new File JavaDoc(getWorkDir(), "application.xml");
91         String JavaDoc ddContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
92                 "<application version=\"1.4\" xmlns=\"http://java.sun.com/xml/ns/j2ee\" " +
93                 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
94                 "xsi:schemaLocation=\"http://java.sun.com/xml/ns/j2ee " +
95                 "http://java.sun.com/xml/ns/j2ee/application_1_4.xsd\">" +
96                 "</application>";
97         EarDataNodeTest.dump(ddFile, ddContent);
98         FileObject fo = FileUtil.toFileObject(ddFile);
99         EarDataObject edo = (EarDataObject) DataObject.find(fo);
100         Action JavaDoc[] action = edo.getNodeDelegate().getActions(false);
101         for (int i = 0; i < action.length; i++) {
102             assertFalse("OpenAction is not present yet", action[i] instanceof OpenAction);
103         }
104     }
105     
106     public static void dump(File JavaDoc f, String JavaDoc contents) throws IOException JavaDoc {
107         f.getParentFile().mkdirs();
108         OutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
109         try {
110             Writer JavaDoc w = new OutputStreamWriter JavaDoc(os, "UTF-8");
111             w.write(contents);
112             w.flush();
113         } finally {
114             os.close();
115         }
116     }
117     
118     private static final class Pool extends DataLoaderPool {
119         
120         protected Enumeration JavaDoc loaders() {
121             return Enumerations.singleton(DataLoader.getLoader(EarDataLoader.class));
122         }
123         
124     }
125     
126     private static final class MR extends MIMEResolver {
127         
128         public String JavaDoc findMIMEType(FileObject fo) {
129             return fo.getNameExt().equals("application.xml")
130                     ? EarDataLoader.REQUIRED_MIME_PREFIX_1 : null;
131         }
132         
133     }
134     
135     private static final class Repo extends Repository {
136         
137         public Repo() throws Exception JavaDoc {
138             super(mksystem());
139         }
140         
141         private static FileSystem mksystem() throws Exception JavaDoc {
142             URL JavaDoc layerFile = Repo.class.getClassLoader().getResource(
143                     "org/netbeans/modules/j2ee/ddloaders/resources/layer.xml");
144             assert layerFile != null;
145             MultiFileSystem mfs = new MultiFileSystem(new FileSystem[] {
146                 new XMLFileSystem(layerFile)
147             });
148             return mfs;
149         }
150         
151     }
152     
153 }
154
Popular Tags