1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
//Tags: JDK1.4
//Copyright (C) 2005 Robert Schuster <thebohemian@gmx.net>
//This file is part of Mauve.
//Mauve is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2, or (at your option)
//any later version.
//Mauve is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Mauve; see the file COPYING. If not, write to
//the Free Software Foundation, 59 Temple Place - Suite 330,
//Boston, MA 02111-1307, USA.
package gnu.testlet.java.beans.EventHandler;
import java.beans.EventHandler;
import java.math.BigInteger;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
/**
* <p>This tests more complex behavior of the <code>EventHandler</code></p>
*/
public class check14b implements Testlet {
public interface Listener {
void listen();
void listen(Object o);
void listen(String m);
}
public class Event {
public boolean isBooleanValue() {
return true;
}
public BigInteger getBigValue() {
return new BigInteger("903281390123809123");
}
public int getIntValue() {
return 0xDEADBEEF;
}
public Integer getIntegerValue() {
return new Integer(0xDEADBEEF);
}
}
public class EventSub extends Event {
boolean method = false;
boolean property = false;
public EventSub getSelf() {
method = true;
return this;
}
public EventSub getGetSelf() {
property = true;
return this;
}
}
public interface Listener2 {
// The check. prefix is a workaround for a Jikes bug.
public void listen(check14b.Event e);
}
private boolean noArgMethodCalled;
private boolean objectArgMethodCalled;
private boolean stringArgMethodCalled;
private boolean numberArgMethodCalled;
private boolean calledSetter;
public void test(TestHarness harness) {
Listener l = (Listener)
EventHandler.create(Listener.class, this, "targetMethod");
l.listen();
harness.check(noArgMethodCalled, true, "prefer no arg method");
harness.check(objectArgMethodCalled, false);
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = false;
l.listen(new Object());
harness.check(noArgMethodCalled, true, "prefer no arg method (Object given)");
harness.check(objectArgMethodCalled, false);
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = false;
l.listen("GNU!");
harness.check(noArgMethodCalled, true, "prefer no arg method (String given)");
harness.check(objectArgMethodCalled, false);
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = false;
// We try to apply the value from the "eventValue" property of the Event
// object to the target object (= this) by using its "value" property.
// This tests whether the implementation properly retrieves the 'setValue'
// method when it is given as a property name.
Listener2 l2 = (Listener2)
EventHandler.create(Listener2.class, this, "value", "booleanValue", null);
l2.listen(new Event());
harness.check(calledSetter, true, "setter invoked as property");
calledSetter = false;
// Now we do the same test but use a subclass of Event. Though the
// implementation should be able to find the "eventValue" property.
l2.listen(new EventSub());
harness.check(calledSetter, true);
calledSetter = false;
// This is basically the same test as above but now the target method
// has to be found via a method name instead of a property name.
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setValue", "booleanValue", null);
l2.listen(new Event());
harness.check(calledSetter, true, "setter invoked as method");
// The situation is that we have a BigInteger value but no "targetValue" property
// of that type (ie. no setTargetValue(BigInteger) method). However the implementation
// should find the property with Object-type of the same name instead and use the
// corresponding method. It is important that we do not use a primitive for this test.
harness.checkPoint("replacement for BigInteger property");
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "targetValue", "bigValue", null);
l2.listen(new Event());
harness.check(noArgMethodCalled, false);
harness.check(objectArgMethodCalled, true); // -> this one should be called
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = false;
// Now the same test with a boolean property. The reason for a special test
// is that internally booleans are represented by Boolean.TYPE.
// The funny thing is that Boolean.TYPE.getSuperclass() is null but the
// EventHandler mechanism should nevertheless find an Object property and
// its method.
harness.checkPoint("replacement for boolean property");
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "targetValue", "booleanValue", null);
l2.listen(new Event());
harness.check(noArgMethodCalled, false);
harness.check(objectArgMethodCalled, true); // -> this one should be called
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = false;
// Again the same kind of test but now the property is an int and the expected
// property method has a 'java.lang.Number' argument. Again the internally used
// Integer.TYPE superclass is null and not Number.
harness.checkPoint("replacement for integer property");
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "numberValue", "intValue", null);
l2.listen(new Event());
harness.check(noArgMethodCalled, false);
harness.check(objectArgMethodCalled, false);
harness.check(numberArgMethodCalled, true); // -> this one should be called
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = numberArgMethodCalled = false;
// Repeats the BigInteger test but uses an explicit method name instead of a property.
harness.checkPoint("replacement for BigInteger method");
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setTargetValue", "bigValue", null);
l2.listen(new Event());
harness.check(noArgMethodCalled, false);
harness.check(objectArgMethodCalled, true); // -> this one should be called
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = false;
// Same as boolean test but uses method name instead of property name.
harness.checkPoint("replacement for boolean method");
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setTargetValue", "booleanValue", null);
l2.listen(new Event());
harness.check(noArgMethodCalled, false);
harness.check(objectArgMethodCalled, true); // -> this one should be called
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = false;
// Same as integer test but uses method name instead of property name.
harness.checkPoint("replacement for integer method");
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setNumberValue", "intValue", null);
l2.listen(new Event());
harness.check(noArgMethodCalled, false);
harness.check(objectArgMethodCalled, false);
harness.check(numberArgMethodCalled, true); // -> this one should be called
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = numberArgMethodCalled = false;
// The EventHandler supports method names in the property string as well.
// This tests whether the implementation evaluates this properly.
harness.checkPoint("property as method names");
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setNumberValue", "getSelf.getIntValue", null);
boolean exceptionThrown = false;
try {
l2.listen(new EventSub());
} catch(Exception e) {
exceptionThrown = true;
}
harness.check(exceptionThrown, false);
harness.check(noArgMethodCalled, false);
harness.check(objectArgMethodCalled, false);
harness.check(numberArgMethodCalled, true); // -> this one should be called
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = numberArgMethodCalled = false;
// The EventHandler can even use a mix of method names and property names. This tests
// the implementation's support for this.
harness.checkPoint("property and method names");
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setNumberValue", "self.getSelf.self.getSelf.intValue", null);
exceptionThrown = false;
try {
l2.listen(new EventSub());
} catch(Exception e) {
exceptionThrown = true;
}
harness.check(exceptionThrown, false);
harness.check(noArgMethodCalled, false);
harness.check(objectArgMethodCalled, false);
harness.check(numberArgMethodCalled, true); // -> this one should be called
harness.check(stringArgMethodCalled, false);
noArgMethodCalled = objectArgMethodCalled = stringArgMethodCalled = numberArgMethodCalled = false;
// This simply checks that in case of ambiguity ("getSelf" as a method and as a property available)
// the property variant is preferred.
harness.checkPoint("name ambiguity preference");
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setNumberValue", "getSelf.intValue", null);
EventSub event = new EventSub();
l2.listen(event);
harness.check(event.property, true);
harness.check(event.method, false);
harness.checkPoint("primitive to wrapper");
calledSetter = false;
// This tests automatic primitive to wrapper conversion. That means the value retrieved from
// the event object is a primitive one and the target accepts it as an instance of a wrapper
// class.
// The 4 subtests check that the target and the source can use the property name as well
// as the method name.
// source: property name
// target: property name
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "integerValue", "intValue");
l2.listen(new EventSub());
harness.check(calledSetter, true);
calledSetter = false;
// source: property name
// target: method name
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setIntegerValue", "intValue");
l2.listen(new EventSub());
harness.check(calledSetter, true);
calledSetter = false;
// source: method name
// target: method name
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setIntegerValue", "getIntValue");
l2.listen(new EventSub());
harness.check(calledSetter, true);
calledSetter = false;
// source: method name
// target: property name
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "integerValue", "getIntValue");
l2.listen(new EventSub());
harness.check(calledSetter, true);
calledSetter = false;
harness.checkPoint("wrapper to primitive");
// The following 4 tests check whether the property conversion works the other
// way to. Now the value retrieved from the event object is an instance of a
// wrapper class and the target accepts a primitive.
// source: property name
// target: property name
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "intValue", "integerValue");
l2.listen(new EventSub());
harness.check(calledSetter, true);
calledSetter = false;
// source: property name
// target: method name
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setIntValue", "integerValue");
l2.listen(new EventSub());
harness.check(calledSetter, true);
calledSetter = false;
// source: method name
// target: method name
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "setIntValue", "getIntegerValue");
l2.listen(new EventSub());
harness.check(calledSetter, true);
calledSetter = false;
// source: method name
// target: property name
l2 = (Listener2)
EventHandler.create(Listener2.class, this, "intValue", "getIntegerValue");
l2.listen(new EventSub());
harness.check(calledSetter, true);
calledSetter = false;
}
public void targetMethod() {
noArgMethodCalled = true;
}
public void targetMethod(Object o) {
objectArgMethodCalled = true;
}
public void targetMethod(String m) {
stringArgMethodCalled = true;
}
public void setTargetValue() {
noArgMethodCalled = true;
}
public void setTargetValue(Object o) {
objectArgMethodCalled = true;
}
public void setTargetValue(String m) {
stringArgMethodCalled = true;
}
public void setNumberValue() {
noArgMethodCalled = true;
}
public void setNumberValue(Object o) {
objectArgMethodCalled = true;
}
public void setNumberValue(Number n) {
numberArgMethodCalled = true;
}
public void setNumberValue(String m) {
stringArgMethodCalled = true;
}
public void setValue(boolean arg) {
calledSetter = arg;
}
public void setIntegerValue(Integer value) {
calledSetter = true;
}
public void setIntValue(int value) {
calledSetter = true;
}
}
|