1 19 20 21 package org.openide.text; 22 23 24 import java.io.IOException ; 25 26 import org.netbeans.junit.NbTestCase; 27 import org.openide.util.Exceptions; 28 29 30 36 public class Starvation37045Test extends NbTestCase 37 implements CloneableEditorSupport.Env { 38 39 private CES support; 40 private String content = ""; 42 private boolean valid = true; 43 private boolean modified = false; 44 45 private String cannotBeModified; 46 private java.util.Date date = new java.util.Date (); 47 private java.util.List propL = new java.util.ArrayList (); 48 private java.beans.VetoableChangeListener vetoL; 49 50 51 public Starvation37045Test (String s) { 52 super(s); 53 } 54 55 protected void setUp () { 56 support = new CES (this, org.openide.util.Lookup.EMPTY); 57 } 58 59 60 public void testTheStarvation37045 () throws Exception { 61 org.openide.util.Task task; 62 63 synchronized (this) { 64 org.openide.util.RequestProcessor.getDefault ().post (support); 65 wait (); 67 68 task = org.openide.util.RequestProcessor.getDefault ().post (support); 70 Thread.sleep (500); 72 73 notify (); 75 } 76 77 for (int i = 0; i < 5; i++) { 79 if (task.isFinished ()) break; 80 Thread.sleep (500); 81 } 82 83 assertTrue ("Should be finished, but there is a starvation", task.isFinished ()); 86 } 87 88 92 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 93 propL.add (l); 94 } 95 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 96 propL.remove (l); 97 } 98 99 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 100 assertNull ("This is the first veto listener", vetoL); 101 vetoL = l; 102 } 103 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 104 assertEquals ("Removing the right veto one", vetoL, l); 105 vetoL = null; 106 } 107 108 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 109 return support; 110 } 111 112 public String getMimeType() { 113 return "text/plain"; 114 } 115 116 public java.util.Date getTime() { 117 return date; 118 } 119 120 public java.io.InputStream inputStream() throws java.io.IOException { 121 throw new OutOfMemoryError ("Ha ha ha"); 122 } 124 public java.io.OutputStream outputStream() throws java.io.IOException { 125 class ContentStream extends java.io.ByteArrayOutputStream { 126 public void close () throws java.io.IOException { 127 super.close (); 128 content = new String (toByteArray ()); 129 } 130 } 131 132 return new ContentStream (); 133 } 134 135 public boolean isValid() { 136 return valid; 137 } 138 139 public boolean isModified() { 140 return modified; 141 } 142 143 public void markModified() throws java.io.IOException { 144 if (cannotBeModified != null) { 145 IOException e = new IOException (); 146 Exceptions.attachLocalizedMessage(e, cannotBeModified); 147 throw e; 148 } 149 150 modified = true; 151 } 152 153 public void unmarkModified() { 154 modified = false; 155 } 156 157 158 private final class CES extends CloneableEditorSupport 159 implements Runnable { 160 private boolean wait = true; 161 162 public CES (Env env, org.openide.util.Lookup l) { 163 super (env, l); 164 } 165 166 protected String messageName() { 167 return "Name"; 168 } 169 170 protected String messageOpened() { 171 return "Opened"; 172 } 173 174 protected String messageOpening() { 175 return "Opening"; 176 } 177 178 protected String messageSave() { 179 return "Save"; 180 } 181 182 protected String messageToolTip() { 183 return "ToolTip"; 184 } 185 186 protected javax.swing.text.EditorKit createEditorKit () { 187 if (wait) { 188 synchronized (Starvation37045Test.this) { 189 wait = false; 190 try { 191 Starvation37045Test.this.notifyAll (); 192 Starvation37045Test.this.wait (); 193 } catch (InterruptedException ex) { 194 ex.printStackTrace(); 195 fail (ex.getMessage ()); 196 } 197 } 198 throw new IllegalStateException ("Let's pretend that I am broken!!!"); 199 } 200 return super.createEditorKit (); 201 } 202 203 public void run () { 204 boolean firstTime = wait; 205 try { 206 edit (); 207 if (firstTime) { 208 fail ("It should throw an exception"); 209 } 210 } catch (IllegalStateException ex) { 211 if (!firstTime) throw ex; 212 assertEquals ("Name of exception is correct", "Let's pretend that I am broken!!!", ex.getMessage ()); 213 } 214 } 215 216 } } 218 | Popular Tags |