| 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.extras.delegate; |
| 22 |
|
|
| 23 |
|
import java.lang.reflect.*; |
| 24 |
|
|
| 25 |
|
import junit.framework.*; |
| 26 |
|
|
| 27 |
|
import org.perfectjpattern.core.api.extras.delegate.*; |
| 28 |
|
import org.slf4j.*; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
@link |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
@see |
| 38 |
|
@see |
| 39 |
|
@see |
| 40 |
|
@see |
| 41 |
|
|
| 42 |
|
@author |
| 43 |
|
@author |
| 44 |
|
|
| 45 |
|
@author |
| 46 |
|
@version |
| 47 |
|
|
| 48 |
|
@SuppressWarnings("unchecked") |
| 49 |
|
public |
|
|
|
| 97.8% |
Uncovered Elements: 2 (90) |
Complexity: 15 |
Complexity Density: 0.21 |
|
| 50 |
|
class TestDelegator |
| 51 |
|
extends TestCase |
| 52 |
|
{ |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1
PASS
|
|
| 56 |
1
|
public void ... |
| 57 |
|
testIsSuitableMethod() |
| 58 |
|
{ |
| 59 |
1
|
int myNoArguments = 1; |
| 60 |
1
|
Method[] myPossibilities = AbstractDelegator.findMethod(IStringDisplay. |
| 61 |
|
class, "doDisplay", myNoArguments); |
| 62 |
|
|
| 63 |
1
|
Class myReturnClass = void.class; |
| 64 |
1
|
Class[] myArguments = new Class[] {String.class }; |
| 65 |
|
|
| 66 |
1
|
AbstractDelegator<IStringDisplay> myDelegate = new Delegator< |
| 67 |
|
IStringDisplay>(IStringDisplay.class); |
| 68 |
|
|
| 69 |
1
|
assertTrue("AbstractDelegator.searchCandidateMethods() did not " + |
| 70 |
|
"produce the expeted result.", myDelegate.isSuitableMethod( |
| 71 |
|
myPossibilities[0], myReturnClass, myArguments)); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
| 75 |
1
|
public void ... |
| 76 |
|
testSearchCandidateMethods() |
| 77 |
|
{ |
| 78 |
1
|
int myNumberOfArguments = 1; |
| 79 |
1
|
Method[] myPossibilities = |
| 80 |
|
AbstractDelegator.findMethod( |
| 81 |
|
IBadStringDisplay.class, "doDisplay", myNumberOfArguments); |
| 82 |
|
|
| 83 |
1
|
assertEquals("AbstractDelegator.searchCandidateMethods() did not " + |
| 84 |
|
"return the expected number of matches.", 1, |
| 85 |
|
myPossibilities.length); |
| 86 |
|
|
| 87 |
1
|
assertEquals("AbstractDelegator.searchCandidateMethods() did not " + |
| 88 |
|
"find the right match.", "doDisplay", |
| 89 |
|
myPossibilities[0].getName()); |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.5 |
1
PASS
|
|
| 93 |
1
|
public void ... |
| 94 |
|
testDelegateBuild() |
| 95 |
|
{ |
| 96 |
1
|
try |
| 97 |
|
{ |
| 98 |
|
|
| 99 |
1
|
new Delegator<IBadStringDisplay>(IBadStringDisplay.class); |
| 100 |
0
|
fail("Delegator multiple method interface was not rejected."); |
| 101 |
|
} |
| 102 |
|
catch (IllegalArgumentException anException) |
| 103 |
|
{ |
| 104 |
|
|
| 105 |
|
} |
| 106 |
|
|
| 107 |
1
|
try |
| 108 |
|
{ |
| 109 |
|
|
| 110 |
1
|
new Delegator<IBadStringDisplay2>(IBadStringDisplay2.class); |
| 111 |
0
|
fail("Delegator interface with no methods was not rejected."); |
| 112 |
|
} |
| 113 |
|
catch (IllegalArgumentException anException) |
| 114 |
|
{ |
| 115 |
|
|
| 116 |
|
} |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 2 |
Complexity Density: 0.17 |
1
PASS
|
|
| 123 |
1
|
public void ... |
| 124 |
|
testDelegator() |
| 125 |
|
throws Exception |
| 126 |
|
{ |
| 127 |
1
|
IDelegator<IStringDisplay> myDelegate = |
| 128 |
|
new Delegator<IStringDisplay>(IStringDisplay.class); |
| 129 |
|
|
| 130 |
1
|
Class1 myObject1 = new Class1(); |
| 131 |
1
|
Class2 myObject2 = new Class2(); |
| 132 |
|
|
| 133 |
1
|
IStringDisplay[] myItems = new IStringDisplay[3]; |
| 134 |
1
|
myItems[0] = myDelegate.build(myObject1, "show"); |
| 135 |
1
|
myItems[1] = myDelegate.build(myObject2, "display"); |
| 136 |
1
|
myItems[2] = myDelegate.build(Class3.class, |
| 137 |
|
"staticDisplay"); |
| 138 |
|
|
| 139 |
4
|
for (int i = 0; i < myItems.length; i++) |
| 140 |
|
{ |
| 141 |
3
|
IStringDisplay myItem = myItems[i]; |
| 142 |
3
|
myItem.doDisplay("test"); |
| 143 |
|
} |
| 144 |
|
|
| 145 |
1
|
final int myIterationsCount = 10000; |
| 146 |
1
|
timingTest(myItems, myObject1, myObject2, myIterationsCount); |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 2 |
Complexity Density: 0.14 |
1
PASS
|
|
| 153 |
1
|
public void ... |
| 154 |
|
testDynamicDelegator() |
| 155 |
|
throws Exception |
| 156 |
|
{ |
| 157 |
1
|
IDelegator<IDelegate> myDelegator = new DynamicDelegator(Void.TYPE, |
| 158 |
|
String.class); |
| 159 |
|
|
| 160 |
1
|
Class1 myObject1 = new Class1(); |
| 161 |
1
|
Class2 myObject2 = new Class2(); |
| 162 |
|
|
| 163 |
1
|
IDelegate[] myDelegates = new IDelegate[3]; |
| 164 |
1
|
myDelegates[0] = myDelegator.build(myObject1, "show"); |
| 165 |
1
|
myDelegates[1] = myDelegator.build(myObject2, "display"); |
| 166 |
|
|
| 167 |
1
|
Class3.reset(); |
| 168 |
1
|
myDelegates[2] = myDelegator.build(Class3.class, "staticDisplay"); |
| 169 |
|
|
| 170 |
4
|
for (int i = 0; i < myDelegates.length; i++) |
| 171 |
|
{ |
| 172 |
3
|
IDelegate myDelegate = myDelegates[i]; |
| 173 |
3
|
myDelegate.invoke("test"); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
1
|
assertEquals("Wrong calls count", 1, myObject1.getCount()); |
| 177 |
1
|
assertEquals("Wrong calls count", 1, myObject2.getCount()); |
| 178 |
1
|
assertEquals("Wrong calls count", 1, Class3.getCount()); |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (38) |
Complexity: 6 |
Complexity Density: 0.21 |
|
| 186 |
1
|
public void ... |
| 187 |
|
timingTest(IStringDisplay[] anItems, Class1 anObject1, Class2 anObject2, |
| 188 |
|
int anIterations) |
| 189 |
|
{ |
| 190 |
|
|
| 191 |
101
|
for (int i = 0; i < 100; i++) |
| 192 |
|
{ |
| 193 |
400
|
for (int j = 0; j < anItems.length; j++) |
| 194 |
|
{ |
| 195 |
300
|
IStringDisplay myItem = anItems[j]; |
| 196 |
300
|
myItem.doDisplay("test"); |
| 197 |
|
} |
| 198 |
|
|
| 199 |
100
|
anObject1.show("test"); |
| 200 |
100
|
anObject2.display("test"); |
| 201 |
100
|
Class3.staticDisplay("test"); |
| 202 |
|
} |
| 203 |
|
|
| 204 |
1
|
long myStart = System.currentTimeMillis(); |
| 205 |
|
|
| 206 |
10001
|
for (int i = 0; i < anIterations; i++) |
| 207 |
|
{ |
| 208 |
40000
|
for (int j = 0; j < anItems.length; j++) |
| 209 |
|
{ |
| 210 |
30000
|
IStringDisplay myItem = anItems[j]; |
| 211 |
30000
|
myItem.doDisplay("test"); |
| 212 |
|
} |
| 213 |
|
} |
| 214 |
|
|
| 215 |
1
|
long myEnd = System.currentTimeMillis(); |
| 216 |
1
|
double myDelegateTime = (myEnd - myStart) / 1000.; |
| 217 |
1
|
double myPerIteration = (1000. * 1000. * myDelegateTime) / anIterations; |
| 218 |
|
|
| 219 |
1
|
myStart = System.currentTimeMillis(); |
| 220 |
|
|
| 221 |
10001
|
for (int myJ = 0; myJ < anIterations; myJ++) |
| 222 |
|
{ |
| 223 |
10000
|
anObject1.show("test"); |
| 224 |
10000
|
anObject2.display("test"); |
| 225 |
10000
|
Class3.staticDisplay("test"); |
| 226 |
|
} |
| 227 |
|
|
| 228 |
1
|
myEnd = System.currentTimeMillis(); |
| 229 |
|
|
| 230 |
1
|
double myDirectTime = (myEnd - myStart) / 1000.; |
| 231 |
1
|
double myPerCallIteration = (1000. * 1000. * myDirectTime) / |
| 232 |
|
anIterations; |
| 233 |
|
|
| 234 |
1
|
theLogger.debug("Ran '" + anIterations + "' iterations "); |
| 235 |
1
|
theLogger.debug("Delegator Test took '" + myDelegateTime + "' secs"); |
| 236 |
1
|
theLogger.debug("per iteration '" + myPerIteration + " microsecs"); |
| 237 |
1
|
theLogger.debug("Direct Calls took '" + myDirectTime + " secs"); |
| 238 |
1
|
theLogger.debug("per iteration '" + myPerCallIteration + "' microsecs"); |
| 239 |
|
} |
| 240 |
|
|
| 241 |
|
|
| 242 |
|
|
| 243 |
|
|
| 244 |
|
private static |
| 245 |
|
interface |
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 246 |
|
IStringDisplay |
| 247 |
|
{ |
| 248 |
|
|
| 249 |
|
public void |
| 250 |
|
doDisplay(String aValue); |
| 251 |
|
} |
| 252 |
|
|
| 253 |
|
|
| 254 |
|
private static |
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 255 |
|
interface IBadStringDisplay |
| 256 |
|
{ |
| 257 |
|
|
| 258 |
|
public void |
| 259 |
|
doDisplay(String aValue); |
| 260 |
|
|
| 261 |
|
|
| 262 |
|
public void |
| 263 |
|
doDisplay2(String aValue); |
| 264 |
|
} |
| 265 |
|
|
| 266 |
|
|
| 267 |
|
private static |
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 268 |
|
interface IBadStringDisplay2 |
| 269 |
|
{ |
| 270 |
|
|
| 271 |
|
} |
| 272 |
|
|
| 273 |
|
|
| 274 |
|
private static |
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 275 |
|
class Class1 |
| 276 |
|
{ |
| 277 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 278 |
20202
|
public void ... |
| 279 |
|
show(String aValue) |
| 280 |
|
{ |
| 281 |
20202
|
theCount++; |
| 282 |
|
} |
| 283 |
|
|
| 284 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 285 |
1
|
public int ... |
| 286 |
|
getCount() |
| 287 |
|
{ |
| 288 |
1
|
return theCount; |
| 289 |
|
} |
| 290 |
|
|
| 291 |
|
|
| 292 |
|
|
| 293 |
|
|
| 294 |
|
private int theCount; |
| 295 |
|
} |
| 296 |
|
|
| 297 |
|
|
| 298 |
|
private static |
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 299 |
|
class Class2 |
| 300 |
|
{ |
| 301 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 302 |
20202
|
public void ... |
| 303 |
|
display(String aValue) |
| 304 |
|
{ |
| 305 |
20202
|
theCount++; |
| 306 |
|
} |
| 307 |
|
|
| 308 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 309 |
1
|
public int ... |
| 310 |
|
getCount() |
| 311 |
|
{ |
| 312 |
1
|
return theCount; |
| 313 |
|
} |
| 314 |
|
|
| 315 |
|
|
| 316 |
|
|
| 317 |
|
|
| 318 |
|
private int theCount; |
| 319 |
|
} |
| 320 |
|
|
| 321 |
|
|
| 322 |
|
|
| 323 |
|
private final static |
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
| 324 |
|
class Class3 |
| 325 |
|
|
| 326 |
|
{ |
| 327 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 328 |
20202
|
public static void ... |
| 329 |
|
staticDisplay(String aValue) |
| 330 |
|
{ |
| 331 |
20202
|
theCount++; |
| 332 |
|
} |
| 333 |
|
|
| 334 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 335 |
1
|
public static int ... |
| 336 |
|
getCount() |
| 337 |
|
{ |
| 338 |
1
|
return theCount; |
| 339 |
|
} |
| 340 |
|
|
| 341 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 342 |
1
|
public static void... |
| 343 |
|
reset() |
| 344 |
|
{ |
| 345 |
1
|
theCount = 0; |
| 346 |
|
} |
| 347 |
|
|
| 348 |
|
|
| 349 |
|
|
| 350 |
|
|
| 351 |
|
private static int theCount; |
| 352 |
|
} |
| 353 |
|
|
| 354 |
|
|
| 355 |
|
|
| 356 |
|
|
| 357 |
|
|
| 358 |
|
|
| 359 |
|
|
| 360 |
|
private final Logger theLogger = LoggerFactory.getLogger(this.getClass()); |
| 361 |
|
} |