1 19 20 package org.openide.loaders; 21 22 import java.util.ArrayList ; 23 import java.util.logging.Level ; 24 import junit.framework.TestFailure; 25 import junit.framework.TestResult; 26 import org.openide.ErrorManager; 27 import org.openide.util.RequestProcessor; 28 29 30 34 public class LoggingControlTest extends LoggingTestCaseHid { 35 36 private ErrorManager err; 37 38 public LoggingControlTest (String name) { 39 super (name); 40 } 41 42 protected void setUp() throws Exception { 43 err = ErrorManager.getDefault().getInstance("TEST-" + getName()); 44 } 45 46 protected Level logLevel() { 47 return Level.ALL; 48 } 49 50 public void testCorrectThreadSwitching() throws Exception { 51 52 class Run implements Runnable { 53 public ArrayList events = new ArrayList (); 54 55 public void run() { 56 events.add("A"); 57 err.log("A"); 58 events.add("B"); 59 err.log("B"); 60 events.add("C"); 61 err.log("C"); 62 } 63 64 public void directly() { 65 err.log("0"); 66 events.add(new Integer (1)); 67 err.log("1"); 68 events.add(new Integer (2)); 69 err.log("2"); 70 events.add(new Integer (3)); 71 err.log("3"); 72 } 73 } 74 75 Run run = new Run(); 76 77 String order = 78 "THREAD:Para MSG:A" + 79 "THREAD:main MSG:0" + 80 "THREAD:main MSG:1" + 81 "THREAD:Para MSG:B" + 82 "THREAD:main MSG:2" + 83 "THREAD:Para MSG:C" + 84 "THREAD:main MSG:3"; 85 registerSwitches(order, 0); 86 87 88 RequestProcessor rp = new RequestProcessor("Para"); 89 RequestProcessor.Task task = rp.post(run); 90 run.directly(); 91 if (!task.waitFinished(10000)) { 92 fail("Runnable deadlocked"); 93 } 94 95 String res = run.events.toString(); 96 97 assertEquals("Really changing the execution according to the provided order: " + res, "[A, 1, B, 2, C, 3]", res); 98 } 99 100 public void testWorksWithRegularExpressionsAsWell() throws Exception { 101 102 class Run implements Runnable { 103 public ArrayList events = new ArrayList (); 104 105 public void run() { 106 events.add("A"); 107 err.log("4329043A"); 108 events.add("B"); 109 err.log("B"); 110 events.add("C"); 111 err.log("CCCC"); 112 } 113 114 public void directly() { 115 err.log("0"); 116 events.add(new Integer (1)); 117 err.log("1"); 118 events.add(new Integer (2)); 119 err.log("2"); 120 events.add(new Integer (3)); 121 err.log("3"); 122 } 123 } 124 125 Run run = new Run(); 126 127 String order = 128 "THREAD:Para MSG:[0-9]*A" + 129 "THREAD:main MSG:0" + 130 "THREAD:main MSG:^1$" + 131 "THREAD:Para MSG:B" + 132 "THREAD:main MSG:2" + 133 "THREAD:Para MSG:C+" + 134 "THREAD:main MSG:3"; 135 registerSwitches(order, 0); 136 137 138 RequestProcessor rp = new RequestProcessor("Para"); 139 RequestProcessor.Task task = rp.post(run); 140 run.directly(); 141 if (!task.waitFinished(10000)) { 142 fail("Runnable deadlocked"); 143 } 144 145 String res = run.events.toString(); 146 147 assertEquals("Really changing the execution according to the provided order: " + res, "[A, 1, B, 2, C, 3]", res); 148 } 149 150 public void testLogMessagesCanRepeat() throws Exception { 151 152 class Run implements Runnable { 153 public ArrayList events = new ArrayList (); 154 155 public void run() { 156 events.add("A"); 157 err.log("A"); 158 events.add("A"); 159 err.log("A"); 160 events.add("A"); 161 err.log("A"); 162 } 163 164 public void directly() { 165 err.log("0"); 166 events.add(new Integer (1)); 167 err.log("1"); 168 events.add(new Integer (2)); 169 err.log("2"); 170 events.add(new Integer (3)); 171 err.log("3"); 172 } 173 } 174 175 Run run = new Run(); 176 177 String order = 178 "THREAD:Para MSG:A" + 179 "THREAD:main MSG:0" + 180 "THREAD:main MSG:^1$" + 181 "THREAD:Para MSG:A" + 182 "THREAD:main MSG:2" + 183 "THREAD:Para MSG:A" + 184 "THREAD:main MSG:3"; 185 registerSwitches(order, 0); 186 187 188 RequestProcessor rp = new RequestProcessor("Para"); 189 RequestProcessor.Task task = rp.post(run); 190 run.directly(); 191 if (!task.waitFinished(10000)) { 192 fail("Runnable deadlocked"); 193 } 194 195 String res = run.events.toString(); 196 197 assertEquals("Really changing the execution according to the provided order: " + res, "[A, 1, A, 2, A, 3]", res); 198 } 199 200 private Exception throwIt; 201 public void testRuntimeExceptionsAlsoGenerateLog() throws Exception { 202 if (throwIt != null) { 203 ErrorManager.getDefault().log("Ahoj"); 204 throw throwIt; 205 } 206 207 LoggingControlTest l = new LoggingControlTest("testRuntimeExceptionsAlsoGenerateLog"); 208 l.throwIt = new NullPointerException (); 209 TestResult res = l.run(); 210 assertEquals("No failures", 0, res.failureCount()); 211 assertEquals("One error", 1, res.errorCount()); 212 213 Object o = res.errors().nextElement(); 214 TestFailure f = (TestFailure)o; 215 216 if (f.exceptionMessage() == null || f.exceptionMessage().indexOf("Ahoj") == -1) { 217 fail("Logged messages shall be in exception message: " + f.exceptionMessage()); 218 } 219 } 220 } 221 | Popular Tags |