1 19 package org.openide.loaders; 20 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.io.PrintStream ; 24 import org.openide.ErrorManager; 25 import org.openide.filesystems.FileObject; 26 import org.openide.filesystems.FileUtil; 27 import org.openide.filesystems.Repository; 28 import org.openide.loaders.Environment.Provider; 29 import org.openide.nodes.Node; 30 import org.openide.util.Lookup; 31 import org.openide.util.RequestProcessor; 32 import org.openide.util.lookup.Lookups; 33 import org.xml.sax.SAXException ; 34 35 39 public class XMLDataObjectGetCookieTest extends LoggingTestCaseHid 40 implements Node.Cookie { 41 42 private ErrorManager err; 43 44 public XMLDataObjectGetCookieTest(String s) { 45 super(s); 46 } 47 protected void setUp() throws Exception { 48 clearWorkDir(); 49 50 err = ErrorManager.getDefault().getInstance("TEST-" + getName()); 51 52 registerIntoLookup(ENV); 53 } 54 55 public void testGetTheLookupWhileWaitingBeforeAssigningIt() throws IOException { 56 doTest( 57 "THREAD:t2 MSG:parsedId set to NULL" + 58 "THREAD:t1 MSG:Has already been parsed.*" + 59 "THREAD:t1 MSG:Query for class org.openide.loaders.XMLDataObjectGetCookieTest" 60 ); 61 } 62 63 public void testGetTheLookupWhileWaitingAfterParsing() throws IOException { 64 doTest( 65 "THREAD:t1 MSG:New id.*" + 66 "THREAD:t2 MSG:Going to read parseId.*" 67 ); 68 } 69 70 private void doTest(String switches) throws IOException { 71 FileObject res = FileUtil.createData( 72 Repository.getDefault().getDefaultFileSystem().getRoot(), 73 getName() + "/R.xml" 74 ); 75 76 err.log("file created: " + res); 77 org.openide.filesystems.FileLock l = res.lock(); 78 OutputStream os = res.getOutputStream(l); 79 err.log("stream opened"); 80 PrintStream ps = new PrintStream (os); 81 82 ps.println("<?xml version='1.0' encoding='UTF-8'?>"); 83 ps.println("<!DOCTYPE MIME-resolver PUBLIC '-//NetBeans//DTD MIME Resolver 1.0//EN' 'http://www.netbeans.org/dtds/mime-resolver-1_0.dtd'>"); 84 ps.println("<MIME-resolver>"); 85 ps.println(" <file>"); 86 ps.println(" <ext name='lenka'/>"); 87 ps.println(" <resolver mime='hodna/lenka'/>"); 88 ps.println(" </file>"); 89 ps.println("</MIME-resolver>"); 90 91 err.log("Content written"); 92 os.close(); 93 err.log("Stream closed"); 94 l.releaseLock(); 95 err.log("releaseLock"); 96 97 98 final DataObject obj = DataObject.find(res); 99 100 class Run implements Runnable { 101 public EP cookie; 102 103 public void run () { 104 cookie = (EP) obj.getCookie(EP.class); 105 } 106 } 107 108 Run r1 = new Run(); 109 Run r2 = new Run(); 110 111 112 registerSwitches(switches, 200); 113 114 RequestProcessor.Task t1 = new RequestProcessor("t1").post(r1); 115 RequestProcessor.Task t2 = new RequestProcessor("t2").post(r2); 116 117 t1.waitFinished(); 118 t2.waitFinished(); 119 120 if (r1.cookie == null && r2.cookie == null) { 121 fail("Both cookies are null"); 122 } 123 124 assertEquals("First result is ok", ENV, r1.cookie); 125 assertEquals("Second result is ok", ENV, r2.cookie); 126 } 127 128 129 130 private static Object ENV = new EP(); 131 132 private static final class EP implements Environment.Provider, Node.Cookie { 133 public Lookup getEnvironment(DataObject obj) { 134 assertEquals("Right object: ", XMLDataObject.class, obj.getClass()); 135 XMLDataObject xml = (XMLDataObject)obj; 136 String id = null; 137 try { 138 id = xml.getDocument().getDoctype().getPublicId(); 139 } catch (IOException ex) { 140 ex.printStackTrace(); 141 fail("No exception"); 142 } catch (SAXException ex) { 143 ex.printStackTrace(); 144 fail("No exception"); 145 } 146 assertEquals("-//NetBeans//DTD MIME Resolver 1.0//EN", id); 147 return Lookups.singleton(this); 148 } 149 }; 150 } 151 | Popular Tags |