| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
package org.perfectjpattern.core.behavioral.observer; |
| 22 |
|
|
| 23 |
|
import java.util.*; |
| 24 |
|
import java.util.concurrent.*; |
| 25 |
|
|
| 26 |
|
import junit.framework.*; |
| 27 |
|
|
| 28 |
|
import org.perfectjpattern.core.api.behavioral.observer.*; |
| 29 |
|
import org.perfectjpattern.core.behavioral.observer.data.*; |
| 30 |
|
import org.perfectjpattern.core.structural.proxy.*; |
| 31 |
|
import org.slf4j.*; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
@link |
| 36 |
|
|
| 37 |
|
@author |
| 38 |
|
@version |
| 39 |
|
|
| 40 |
|
public |
|
|
|
| 87.9% |
Uncovered Elements: 4 (33) |
Complexity: 3 |
Complexity Density: 0.09 |
|
| 41 |
|
class TestAsynchronousSubject |
| 42 |
|
extends TestCase |
| 43 |
|
{ |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
|
|
|
| 87.5% |
Uncovered Elements: 4 (32) |
Complexity: 3 |
Complexity Density: 0.09 |
1
PASS
|
|
| 47 |
1
|
@SuppressWarnings("unchecked")... |
| 48 |
|
public void |
| 49 |
|
testAsynchronousNotification() |
| 50 |
|
{ |
| 51 |
|
|
| 52 |
1
|
TestStatusObserver myRealObserver1 = new TestStatusObserver(); |
| 53 |
1
|
TestStatusObserver myRealObserver2 = new TestStatusObserver(); |
| 54 |
1
|
FaultyStatusObserver myRealFaultyObserver = new FaultyStatusObserver(); |
| 55 |
|
|
| 56 |
|
|
| 57 |
1
|
IObserver<StatusData> myObserver1 = new SynchronizedProxy< |
| 58 |
|
IObserver>(IObserver.class, myRealObserver1).getComponent(); |
| 59 |
1
|
IObserver<StatusData> myObserver2 = new SynchronizedProxy< |
| 60 |
|
IObserver>(IObserver.class, myRealObserver2).getComponent(); |
| 61 |
1
|
IObserver<StatusData> myFaultyObserver = new SynchronizedProxy< |
| 62 |
|
IObserver>(IObserver.class, myRealFaultyObserver).getComponent(); |
| 63 |
|
|
| 64 |
|
|
| 65 |
1
|
ISubject<StatusData> mySubject = new AsynchronousSubject< |
| 66 |
|
StatusData>(); |
| 67 |
|
|
| 68 |
|
|
| 69 |
1
|
mySubject.attach(myFaultyObserver, myObserver1, myObserver2); |
| 70 |
|
|
| 71 |
1
|
theCountDownLatch = new CountDownLatch(3); |
| 72 |
|
|
| 73 |
|
|
| 74 |
1
|
mySubject.notifyObservers(StatusData.STARTED); |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
1
|
try |
| 79 |
|
{ |
| 80 |
1
|
theCountDownLatch.await(); |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
1
|
Thread.sleep(1000); |
| 86 |
|
} |
| 87 |
|
catch (InterruptedException anException) |
| 88 |
|
{ |
| 89 |
0
|
theLogger.error(anException.getMessage()); |
| 90 |
|
|
| 91 |
0
|
fail(anException.getMessage()); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
1
|
assertEquals("Faulty Observer must have been automatically detached", |
| 96 |
|
2, mySubject.size()); |
| 97 |
|
|
| 98 |
|
|
| 99 |
1
|
mySubject.detach(myObserver1); |
| 100 |
|
|
| 101 |
|
|
| 102 |
1
|
theCountDownLatch = new CountDownLatch(1); |
| 103 |
|
|
| 104 |
|
|
| 105 |
1
|
mySubject.notifyObservers(StatusData.COMPLETED); |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
1
|
try |
| 110 |
|
{ |
| 111 |
1
|
theCountDownLatch.await(); |
| 112 |
|
} |
| 113 |
|
catch (InterruptedException anException) |
| 114 |
|
{ |
| 115 |
0
|
theLogger.error(anException.getMessage()); |
| 116 |
|
|
| 117 |
0
|
fail(anException.getMessage()); |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
|
| 121 |
1
|
assertEquals("myObserver1 did not receive the right events.", 1, |
| 122 |
|
myRealObserver1.getStatus().size()); |
| 123 |
1
|
assertEquals("myObserver1 did not receive the right events.", |
| 124 |
|
StatusData.STARTED, myRealObserver1.getStatus().get(0)); |
| 125 |
|
|
| 126 |
1
|
assertEquals("myObserver2 did not receive the right events.", 2, |
| 127 |
|
myRealObserver2.getStatus().size()); |
| 128 |
1
|
assertEquals("myObserver2 did not receive the right events.", |
| 129 |
|
StatusData.STARTED, myRealObserver2.getStatus().get(0)); |
| 130 |
1
|
assertEquals("myObserver2 did not receive the right events.", |
| 131 |
|
StatusData.COMPLETED, myRealObserver2.getStatus().get(1)); |
| 132 |
|
|
| 133 |
|
|
| 134 |
1
|
Set<String> myThreads = new TreeSet<String>(); |
| 135 |
1
|
myThreads.addAll(myRealObserver1.getThreads()); |
| 136 |
1
|
myThreads.addAll(myRealObserver2.getThreads()); |
| 137 |
|
|
| 138 |
1
|
assertEquals("The number of different threads being used was " + |
| 139 |
|
"different than expected.", 3, myThreads.size()); |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
private static final |
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 150 |
|
class TestStatusObserver |
| 151 |
|
implements IObserver<StatusData> |
| 152 |
|
{ |
| 153 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 154 |
3
|
public void ... |
| 155 |
|
update(StatusData aStatusData) |
| 156 |
|
{ |
| 157 |
3
|
try |
| 158 |
|
{ |
| 159 |
3
|
theStatus.add(aStatusData); |
| 160 |
3
|
theThreads.add(Thread.currentThread().getName()); |
| 161 |
|
} |
| 162 |
|
finally |
| 163 |
|
{ |
| 164 |
3
|
theCountDownLatch.countDown(); |
| 165 |
|
} |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 169 |
5
|
public final List<StatusData>... |
| 170 |
|
getStatus() |
| 171 |
|
{ |
| 172 |
5
|
return theStatus; |
| 173 |
|
} |
| 174 |
|
|
| 175 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 176 |
2
|
public final List<String>... |
| 177 |
|
getThreads() |
| 178 |
|
{ |
| 179 |
2
|
return theThreads; |
| 180 |
|
} |
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
private final List<String> theThreads = new ArrayList<String>(); |
| 186 |
|
private final List<StatusData> theStatus = new ArrayList<StatusData>(); |
| 187 |
|
} |
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
private static final |
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 195 |
|
class FaultyStatusObserver |
| 196 |
|
implements IObserver<StatusData> |
| 197 |
|
{ |
| 198 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 199 |
1
|
public void ... |
| 200 |
|
update(StatusData aStatusData) |
| 201 |
|
{ |
| 202 |
1
|
try |
| 203 |
|
{ |
| 204 |
|
throw new RuntimeException("I am faulty, remember?"); |
| 205 |
|
} |
| 206 |
|
finally |
| 207 |
|
{ |
| 208 |
1
|
theCountDownLatch.countDown(); |
| 209 |
|
} |
| 210 |
|
} |
| 211 |
|
} |
| 212 |
|
|
| 213 |
|
|
| 214 |
|
|
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
private final Logger theLogger = LoggerFactory.getLogger(this.getClass()); |
| 220 |
|
private static CountDownLatch theCountDownLatch; |
| 221 |
|
} |