1 19 20 package org.netbeans.test.web; 21 22 import java.io.BufferedReader ; 23 import java.io.DataOutputStream ; 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import java.net.URL ; 30 import java.net.URLConnection ; 31 import org.netbeans.jemmy.JemmyException; 32 import org.openide.awt.HtmlBrowser; 33 import org.openide.util.Lookup; 34 35 51 public final class TestURLDisplayer extends HtmlBrowser.URLDisplayer { 52 private static TestURLDisplayer instance; 53 private boolean isURLValid = false; 54 private URL url = null; 55 private URLConnection con = null; 56 57 public static synchronized TestURLDisplayer getInstance() { 58 if (instance==null) { 59 instance=registerTestURLDisplayer(); 60 } 61 return instance; 62 } 63 64 public synchronized void showURL(URL u) { 65 url=u; 66 try { 67 con = url.openConnection(); 68 } catch (IOException ex) { 69 System.err.println("Cannot open URL: "+url); 70 ex.printStackTrace(); 71 } 72 final URLConnection fc = con; 74 new Thread () { 75 public void run() { 76 try { 77 fc.getInputStream(); 78 } catch (IOException ex) { 79 System.err.println("Cannot read URL: "+url); 80 ex.printStackTrace(); 81 } 82 } 83 }.start(); 84 85 notifyAll(); 86 isURLValid=true; 87 } 88 89 public synchronized void invalidateURL() { 90 url = null; 91 con = null; 92 isURLValid = false; 93 } 94 95 public synchronized URL waitURL() throws InterruptedException { 96 while (!isURLValid) { 97 wait(60000); 98 if (!isURLValid) { 99 throw new IllegalStateException ("Timeout expired."); 100 } 101 } 102 return url; 103 } 104 105 public String readURL() { 106 if (!isURLValid || url == null) { 107 throw new IllegalStateException ("URL is not valid."); 108 } 109 StringBuffer sb = new StringBuffer (); 110 InputStream is = null; 111 try{ 112 is = con.getInputStream(); 113 BufferedReader reader = new BufferedReader (new InputStreamReader (is)); 114 String line = null; 115 while((line=reader.readLine()) != null){ 116 sb.append(line+"\n"); 117 } 118 }catch(Exception ex){ 119 ex.printStackTrace(); 120 }finally{ 121 try{ 122 is.close(); 123 }catch(Exception ex){ 124 } 126 } 127 return sb.toString(); 128 } 129 130 public URL getURL() { 131 return url; 132 } 133 134 private static TestURLDisplayer registerTestURLDisplayer() { 135 try { 136 File file = new File (System.getProperty("xtest.workdir")+"/sys/tests" + 137 "/META-INF/services/org.openide.awt.HtmlBrowser$URLDisplayer"); 138 DataOutputStream dout = new DataOutputStream (new FileOutputStream (file)); 139 dout.writeBytes( 140 "#-org.netbeans.core.NbTopManager$NbURLDisplayer\n"+ 141 "org.netbeans.test.web.TestURLDisplayer\n"); 142 dout.close(); 143 } catch (IOException ex) { 144 throw new JemmyException("Cannot register URL displayer.", ex); 145 } 146 Object displayer = Lookup.getDefault().lookup(HtmlBrowser.URLDisplayer.class); 147 if (!displayer.getClass().equals(TestURLDisplayer.class)) { 148 throw new JemmyException("URL displayer registration failed"); 149 } 150 return (TestURLDisplayer) displayer; 151 } 152 } 153 | Popular Tags |