1 19 20 package org.netbeans.modules.j2ee.ddloaders.app; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.io.OutputStreamWriter ; 27 import java.io.Writer ; 28 import java.net.URL ; 29 import java.util.Enumeration ; 30 import javax.swing.Action ; 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 54 public class EarDataNodeTest extends NbTestCase { 55 56 static { 58 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 [0]); 69 } 70 public static void setLookup(Object [] instances) { 71 ClassLoader 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 testName) throws Exception { 81 super(testName); 82 Lkp.setLookup(new Object [] { 83 new Pool(), 84 new MR(), 85 new Repo() 86 }); 87 } 88 89 public void testGetActions() throws Exception { 90 File ddFile = new File (getWorkDir(), "application.xml"); 91 String 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 [] 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 f, String contents) throws IOException { 107 f.getParentFile().mkdirs(); 108 OutputStream os = new FileOutputStream (f); 109 try { 110 Writer w = new OutputStreamWriter (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 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 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 { 138 super(mksystem()); 139 } 140 141 private static FileSystem mksystem() throws Exception { 142 URL 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 |