1 16 package com.google.gwt.junit; 17 18 import com.google.gwt.junit.client.TestResults; 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 41 public class JUnitMessageQueue { 42 43 49 private Map clientTestRequests = new HashMap (); 50 51 54 private int currentTestIndex = -1; 55 56 59 private int numClients = 1; 60 61 65 private Object readTestLock = new Object (); 66 67 70 private Object resultsLock = new Object (); 71 72 75 private String testClass; 76 77 80 private String testMethod; 81 82 85 private List testResults = new ArrayList (); 86 87 92 JUnitMessageQueue() { 93 } 94 95 101 JUnitMessageQueue(int numClients) { 102 this.numClients = numClients; 103 } 104 105 114 public String getNextTestName(String clientId, String testClassName, 115 long timeout) { 116 synchronized (readTestLock) { 117 long stopTime = System.currentTimeMillis() + timeout; 118 while (!testIsAvailableFor(clientId, testClassName)) { 119 long timeToWait = stopTime - System.currentTimeMillis(); 120 if (timeToWait < 1) { 121 return null; 122 } 123 try { 124 readTestLock.wait(timeToWait); 125 } catch (InterruptedException e) { 126 return null; 128 } 129 } 130 131 if (!testClassName.equals(testClass)) { 132 return null; 134 } 135 136 bumpClientTestRequest(clientId); 137 return testMethod; 138 } 139 } 140 141 147 public void reportResults(String testClassName, TestResults results) { 148 synchronized (resultsLock) { 149 if (!testClassName.equals(testClass)) { 150 return; 152 } 153 testResults.add(results); 154 } 155 } 156 157 164 List getResults(String testClassName) { 165 assert (testClassName.equals(testClass)); 166 return testResults; 167 } 168 169 176 boolean hasResult(String testClassName) { 177 synchronized (resultsLock) { 178 assert (testClassName.equals(testClass)); 179 return testResults.size() == numClients; 180 } 181 } 182 183 187 boolean haveAllClientsRetrievedCurrentTest() { 188 synchronized (readTestLock) { 189 Collection clientIndices = clientTestRequests.values(); 191 if (clientIndices.size() < numClients) { 192 return false; 193 } 194 for (Iterator it = clientIndices.iterator(); it.hasNext();) { 196 Integer value = (Integer ) it.next(); 197 if (value.intValue() <= currentTestIndex) { 198 return false; 199 } 200 } 201 return true; 202 } 203 } 204 205 212 void setNextTestName(String testClassName, String testName) { 213 synchronized (readTestLock) { 214 testClass = testClassName; 215 testMethod = testName; 216 ++currentTestIndex; 217 testResults = new ArrayList (numClients); 218 readTestLock.notifyAll(); 219 } 220 } 221 222 228 void setNumClients(int numClients) { 229 this.numClients = numClients; 230 } 231 232 private void bumpClientTestRequest(String clientId) { 234 Integer index = (Integer ) clientTestRequests.get(clientId); 235 clientTestRequests.put(clientId, new Integer (index.intValue() + 1)); 236 } 237 238 private boolean testIsAvailableFor(String clientId, String testClassName) { 240 if (!testClassName.equals(testClass)) { 241 return true; 243 } 244 Integer index = (Integer ) clientTestRequests.get(clientId); 245 if (index == null) { 246 index = new Integer (0); 247 clientTestRequests.put(clientId, index); 248 } 249 return index.intValue() == currentTestIndex; 250 } 251 } 252 | Popular Tags |