001: package org.drools.base;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007:
008: import junit.framework.Assert;
009: import junit.framework.TestCase;
010:
011: import org.drools.Address;
012: import org.drools.Cheese;
013: import org.drools.CheeseEqual;
014: import org.drools.CheeseInterface;
015: import org.drools.Person;
016:
017: public class ShadowProxyFactoryTest extends TestCase {
018:
019: protected void setUp() throws Exception {
020: super .setUp();
021: }
022:
023: protected void tearDown() throws Exception {
024: super .tearDown();
025: }
026:
027: public void testProxyForClass() {
028: try {
029: // creating original object
030: final String originalType = "stilton";
031: final int originalPrice = 15;
032: final Cheese cheese = new Cheese(originalType,
033: originalPrice);
034:
035: // creating proxy
036: final Class proxy = ShadowProxyFactory
037: .getProxy(Cheese.class);
038: final Cheese cheeseProxy = (Cheese) proxy.getConstructor(
039: new Class[] { Cheese.class }).newInstance(
040: new Object[] { cheese });
041:
042: // proxy is proxying the values
043: Assert.assertEquals(originalType, cheeseProxy.getType());
044: Assert.assertEquals(originalPrice, cheeseProxy.getPrice());
045: Assert.assertSame(cheese, ((ShadowProxy) cheeseProxy)
046: .getShadowedObject());
047:
048: // proxy must recongnize the original object on equals()/hashcode() calls
049: //Assert.assertEquals( cheeseProxy.hashCode(), cheese.hashCode() );
050: Assert.assertEquals(cheeseProxy, cheese);
051:
052: // changing original values
053: final String actualType = "rotten stilton";
054: final int actualPrice = 1;
055: cheese.setType(actualType);
056: cheese.setPrice(actualPrice);
057:
058: // proxy does not see changes
059: Assert.assertEquals(actualType, cheese.getType());
060: Assert
061: .assertFalse(actualType.equals(cheeseProxy
062: .getType()));
063: Assert.assertEquals(originalType, cheeseProxy.getType());
064: Assert.assertEquals(actualPrice, cheese.getPrice());
065: Assert.assertFalse(actualPrice == cheeseProxy.getPrice());
066: Assert.assertEquals(originalPrice, cheeseProxy.getPrice());
067:
068: // reseting proxy
069: ((ShadowProxy) cheeseProxy).updateProxy();
070:
071: // now proxy see changes
072: Assert.assertEquals(actualType, cheese.getType());
073: Assert.assertEquals(actualType, cheeseProxy.getType());
074: Assert.assertFalse(originalType.equals(cheeseProxy
075: .getType()));
076: Assert.assertEquals(actualPrice, cheese.getPrice());
077: Assert.assertEquals(actualPrice, cheeseProxy.getPrice());
078: Assert.assertFalse(originalPrice == cheeseProxy.getPrice());
079:
080: } catch (final Exception e) {
081: fail("Error: " + e.getMessage());
082: }
083: }
084:
085: public void testProxyForInterface() {
086: try {
087: // creating original object
088: final String originalType = "stilton";
089: final int originalPrice = 15;
090: final Cheese cheese = new Cheese(originalType,
091: originalPrice);
092:
093: // creating proxy
094: final Class proxy = ShadowProxyFactory
095: .getProxy(CheeseInterface.class);
096: final CheeseInterface cheeseProxy = (CheeseInterface) proxy
097: .getConstructor(
098: new Class[] { CheeseInterface.class })
099: .newInstance(new Object[] { cheese });
100:
101: // proxy is proxying the values
102: Assert.assertEquals(originalType, cheeseProxy.getType());
103: Assert.assertEquals(originalPrice, cheeseProxy.getPrice());
104: Assert.assertSame(cheese, ((ShadowProxy) cheeseProxy)
105: .getShadowedObject());
106:
107: // changing original values
108: final String actualType = "rotten stilton";
109: final int actualPrice = 1;
110: cheese.setType(actualType);
111: cheese.setPrice(actualPrice);
112:
113: // proxy does not see changes
114: Assert.assertEquals(actualType, cheese.getType());
115: Assert
116: .assertFalse(actualType.equals(cheeseProxy
117: .getType()));
118: Assert.assertEquals(originalType, cheeseProxy.getType());
119: Assert.assertEquals(actualPrice, cheese.getPrice());
120: Assert.assertFalse(actualPrice == cheeseProxy.getPrice());
121: Assert.assertEquals(originalPrice, cheeseProxy.getPrice());
122:
123: // reseting proxy
124: ((ShadowProxy) cheeseProxy).updateProxy();
125:
126: // now proxy see changes
127: Assert.assertEquals(actualType, cheese.getType());
128: Assert.assertEquals(actualType, cheeseProxy.getType());
129: Assert.assertFalse(originalType.equals(cheeseProxy
130: .getType()));
131: Assert.assertEquals(actualPrice, cheese.getPrice());
132: Assert.assertEquals(actualPrice, cheeseProxy.getPrice());
133: Assert.assertFalse(originalPrice == cheeseProxy.getPrice());
134:
135: } catch (final Exception e) {
136: fail("Error: " + e.getMessage());
137: }
138: }
139:
140: public void testProxyForAPIClass() {
141: try {
142: // creating original object
143: final List list = new ArrayList();
144:
145: // creating proxy
146: final Class proxy = ShadowProxyFactory
147: .getProxy(ArrayList.class);
148: final List listProxy = (List) proxy.getConstructor(
149: new Class[] { ArrayList.class }).newInstance(
150: new Object[] { list });
151:
152: // proxy is proxying the values
153: Assert.assertEquals(list, listProxy);
154: Assert.assertSame(list, ((ShadowProxy) listProxy)
155: .getShadowedObject());
156:
157: } catch (final Exception e) {
158: fail("Error: " + e.getMessage());
159: }
160: }
161:
162: public void testEqualsHashCodeForClass() {
163: try {
164: // creating original object
165: final String originalType = "stilton";
166: final int originalPrice = 15;
167: final Cheese cheese = new Cheese(originalType,
168: originalPrice);
169:
170: // creating proxy
171: final Class proxy = ShadowProxyFactory
172: .getProxy(Cheese.class);
173: final Cheese cheeseProxy1 = (Cheese) proxy.getConstructor(
174: new Class[] { Cheese.class }).newInstance(
175: new Object[] { cheese });
176: final Cheese cheeseProxy2 = (Cheese) proxy.getConstructor(
177: new Class[] { Cheese.class }).newInstance(
178: new Object[] { cheese });
179:
180: int cheeseHash = cheese.hashCode();
181: Assert.assertEquals(cheeseProxy1, cheeseProxy2);
182: Assert.assertEquals(cheeseProxy2, cheeseProxy1);
183: Assert.assertEquals(cheeseHash, cheeseProxy1.hashCode());
184:
185: // changing original values
186: final String actualType = "rotten stilton";
187: final int actualPrice = 1;
188: cheese.setType(actualType);
189: cheese.setPrice(actualPrice);
190:
191: Assert.assertEquals(cheeseHash, cheeseProxy1.hashCode());
192:
193: // updating proxy1
194: ((ShadowProxy) cheeseProxy1).updateProxy();
195: cheeseHash = cheese.hashCode();
196:
197: Assert.assertEquals(cheeseHash, cheeseProxy1.hashCode());
198:
199: // they are still identity equals
200: Assert.assertTrue(cheeseProxy1.equals(cheeseProxy2));
201: Assert.assertTrue(cheeseProxy2.equals(cheeseProxy1));
202:
203: // updating proxy2
204: ((ShadowProxy) cheeseProxy2).updateProxy();
205:
206: // now they are equal again
207: Assert.assertEquals(cheeseProxy1, cheeseProxy2);
208: Assert.assertEquals(cheeseProxy2, cheeseProxy1);
209:
210: } catch (final Exception e) {
211: fail("Error: " + e.getMessage());
212: }
213: }
214:
215: // TODO: find a new way to test hashcode
216: // public void testEqualsHashCodeForClass2() {
217: // try {
218: // // creating original object
219: // final TestBean bean = new TestBean();
220: //
221: // // creating proxy
222: // final Class proxy = ShadowProxyFactory.getProxy( TestBean.class );
223: // final TestBean beanProxy1 = (TestBean) proxy.getConstructor( new Class[]{TestBean.class} ).newInstance( new Object[]{bean} );
224: // final TestBean beanProxy2 = (TestBean) proxy.getConstructor( new Class[]{TestBean.class} ).newInstance( new Object[]{bean} );
225: //
226: // Assert.assertEquals( beanProxy1, beanProxy2 );
227: // Assert.assertEquals( beanProxy2, beanProxy1 );
228: // Assert.assertEquals( -130900686 , beanProxy1.hashCode() );
229: //
230: // } catch ( final Exception e ) {
231: // fail( "Error: " + e.getMessage() );
232: // }
233: // }
234:
235: // private int cheeseHashCode(final Cheese cheese) {
236: // final int PRIME = 31;
237: // int result = 1;
238: // result = PRIME * result + ((cheese.getType() == null) ? 0 : cheese.getType().hashCode());
239: // result = PRIME * result + cheese.getPrice();
240: // return result;
241: // }
242:
243: public void testClassWithStaticMethod() {
244: try {
245: // creating original object
246: final String originalType = "stilton";
247: final int originalPrice = 15;
248: final Cheese cheese = new Cheese(originalType,
249: originalPrice);
250:
251: // creating proxy
252: final Class proxy = ShadowProxyFactory
253: .getProxy(Cheese.class);
254: final Cheese cheeseProxy1 = (Cheese) proxy.getConstructor(
255: new Class[] { Cheese.class }).newInstance(
256: new Object[] { cheese });
257: final Cheese cheeseProxy2 = (Cheese) proxy.getConstructor(
258: new Class[] { Cheese.class }).newInstance(
259: new Object[] { cheese });
260:
261: final int cheesehash = cheese.hashCode();
262: Assert.assertEquals(cheeseProxy1, cheeseProxy2);
263: Assert.assertEquals(cheeseProxy2, cheeseProxy1);
264: Assert.assertEquals(cheesehash, cheeseProxy1.hashCode());
265:
266: } catch (final Exception e) {
267: e.printStackTrace();
268: fail("Error: " + e.getMessage());
269: }
270: }
271:
272: public void testClassWithDelegateMethodWithLongParam() {
273: try {
274: // creating original object
275: final String originalType = "stilton";
276: final int originalPrice = 15;
277: final Cheese cheese = new Cheese(originalType,
278: originalPrice);
279:
280: // creating proxy
281: final Class proxy = ShadowProxyFactory
282: .getProxy(Cheese.class);
283: final Cheese cheeseProxy1 = (Cheese) proxy.getConstructor(
284: new Class[] { Cheese.class }).newInstance(
285: new Object[] { cheese });
286: final Cheese cheeseProxy2 = (Cheese) proxy.getConstructor(
287: new Class[] { Cheese.class }).newInstance(
288: new Object[] { cheese });
289:
290: final int cheesehash = cheese.hashCode();
291: Assert.assertEquals(cheeseProxy1, cheeseProxy2);
292: Assert.assertEquals(cheeseProxy2, cheeseProxy1);
293: Assert.assertEquals(cheesehash, cheeseProxy1.hashCode());
294:
295: } catch (final Exception e) {
296: e.printStackTrace();
297: fail("Error: " + e.getMessage());
298: }
299: }
300:
301: public void testProxyForCollections() {
302: try {
303: // creating original object
304: List originalList = new ArrayList();
305: originalList.add("a");
306: originalList.add("b");
307: originalList.add("c");
308: originalList.add("d");
309:
310: // creating proxy
311: final Class proxy = ShadowProxyFactory
312: .getProxy(originalList.getClass());
313: final List listProxy = (List) proxy.getConstructor(
314: new Class[] { originalList.getClass() })
315: .newInstance(new Object[] { originalList });
316: ((ShadowProxy) listProxy).setShadowedObject(originalList);
317:
318: // proxy is proxying the values
319: Assert.assertEquals("a", listProxy.get(0));
320: Assert.assertTrue(listProxy.contains("c"));
321: Assert.assertSame(originalList, ((ShadowProxy) listProxy)
322: .getShadowedObject());
323:
324: // proxy must recongnize the original object on equals() calls
325: Assert.assertEquals(listProxy, originalList);
326:
327: originalList.remove("c");
328: originalList.add("e");
329: Assert.assertTrue(listProxy.contains("c"));
330: Assert.assertFalse(listProxy.contains("e"));
331:
332: ((ShadowProxy) listProxy).updateProxy();
333: Assert.assertFalse(listProxy.contains("c"));
334: Assert.assertTrue(listProxy.contains("e"));
335:
336: // proxy must recongnize the original object on equals() calls
337: Assert.assertEquals(listProxy, originalList);
338: } catch (final Exception e) {
339: e.printStackTrace();
340: fail("Error: " + e.getMessage());
341: }
342: }
343:
344: public void testProxyForMaps() {
345: try {
346: // creating original object
347: Map originalMap = new HashMap();
348: originalMap.put("name", "Edson");
349: originalMap.put("surname", "Tirelli");
350: originalMap.put("age", "28");
351:
352: // creating proxy
353: final Class proxy = ShadowProxyFactory.getProxy(originalMap
354: .getClass());
355: final Map mapProxy = (Map) proxy.getConstructor(
356: new Class[] { originalMap.getClass() })
357: .newInstance(new Object[] { originalMap });
358: ((ShadowProxy) mapProxy).setShadowedObject(originalMap);
359:
360: // proxy is proxying the values
361: Assert.assertEquals("Edson", mapProxy.get("name"));
362: Assert.assertTrue(mapProxy.containsKey("age"));
363: Assert.assertSame(originalMap, ((ShadowProxy) mapProxy)
364: .getShadowedObject());
365:
366: // proxy must recongnize the original object on equals() calls
367: Assert.assertEquals(mapProxy, originalMap);
368:
369: originalMap.remove("age");
370: originalMap.put("hair", "brown");
371: Assert.assertTrue(mapProxy.containsKey("age"));
372: Assert.assertFalse(mapProxy.containsKey("hair"));
373:
374: ((ShadowProxy) mapProxy).updateProxy();
375: Assert.assertFalse(mapProxy.containsKey("age"));
376: Assert.assertTrue(mapProxy.containsKey("hair"));
377:
378: // proxy must recongnize the original object on equals() calls
379: Assert.assertEquals(mapProxy, originalMap);
380: } catch (final Exception e) {
381: e.printStackTrace();
382: fail("Error: " + e.getMessage());
383: }
384: }
385:
386: public void testProxyForMapsAttributes() {
387: try {
388: Person bob = new Person("bob", 30);
389: Address addr1 = new Address("street 1", "111",
390: "11-1111-1111");
391: Address addr2 = new Address("street 2", "222",
392: "22-2222-2222");
393: Address addr3 = new Address("street 3", "333",
394: "33-3333-3333");
395: Address addr4 = new Address("street 4", "444",
396: "44-4444-4444");
397: Map addresses = new HashMap();
398: addresses.put("home", addr1);
399: addresses.put("business", addr2);
400: bob.setAddresses(addresses);
401:
402: // creating proxy
403: final Class proxy = ShadowProxyFactory.getProxy(bob
404: .getClass());
405: final Person bobProxy = (Person) proxy.getConstructor(
406: new Class[] { bob.getClass() }).newInstance(
407: new Object[] { bob });
408: ((ShadowProxy) bobProxy).setShadowedObject(bob);
409:
410: // proxy is proxying the values
411: Assert.assertEquals(bob.getAddresses().get("business"),
412: bobProxy.getAddresses().get("business"));
413: Assert.assertSame(bob, ((ShadowProxy) bobProxy)
414: .getShadowedObject());
415:
416: // proxy must recongnize the original object on equals() calls
417: Assert.assertEquals(bobProxy, bob);
418:
419: bob.getAddresses().remove("business");
420: bob.getAddresses().put("parents", addr3);
421: bob.getAddresses().put("home", addr4);
422: Assert.assertTrue(bobProxy.getAddresses().containsKey(
423: "business"));
424: Assert.assertFalse(bobProxy.getAddresses().containsKey(
425: "parents"));
426: Assert.assertEquals(addr1, bobProxy.getAddresses().get(
427: "home"));
428:
429: ((ShadowProxy) bobProxy).updateProxy();
430: Assert.assertFalse(bobProxy.getAddresses().containsKey(
431: "business"));
432: Assert.assertTrue(bobProxy.getAddresses().containsKey(
433: "parents"));
434: Assert.assertEquals(addr4, bobProxy.getAddresses().get(
435: "home"));
436:
437: // proxy must recongnize the original object on equals() calls
438: Assert.assertEquals(bobProxy, bob);
439: } catch (final Exception e) {
440: e.printStackTrace();
441: fail("Error: " + e.getMessage());
442: }
443: }
444:
445: public void testProxyForCollectionAttributes() {
446: try {
447: Person bob = new Person("bob", 30);
448: Address addr1 = new Address("street 1", "111",
449: "11-1111-1111");
450: Address addr2 = new Address("street 2", "222",
451: "22-2222-2222");
452: Address addr3 = new Address("street 3", "333",
453: "33-3333-3333");
454: bob.getAddressList().add(addr1);
455: bob.getAddressList().add(addr2);
456:
457: // creating proxy
458: final Class proxy = ShadowProxyFactory.getProxy(bob
459: .getClass());
460: final Person bobProxy = (Person) proxy.getConstructor(
461: new Class[] { bob.getClass() }).newInstance(
462: new Object[] { bob });
463: ((ShadowProxy) bobProxy).setShadowedObject(bob);
464:
465: // proxy is proxying the values
466: Assert.assertEquals(2, bobProxy.getAddressList().size());
467: Assert.assertSame(bob, ((ShadowProxy) bobProxy)
468: .getShadowedObject());
469:
470: // proxy must recongnize the original object on equals() calls
471: Assert.assertEquals(bobProxy, bob);
472:
473: bob.getAddressList().remove(addr2);
474: bob.getAddressList().add(addr3);
475:
476: Assert
477: .assertTrue(bobProxy.getAddressList().contains(
478: addr2));
479: Assert.assertFalse(bobProxy.getAddressList()
480: .contains(addr3));
481:
482: ((ShadowProxy) bobProxy).updateProxy();
483: Assert.assertFalse(bobProxy.getAddressList()
484: .contains(addr2));
485: Assert
486: .assertTrue(bobProxy.getAddressList().contains(
487: addr3));
488:
489: // proxy must recongnize the original object on equals() calls
490: Assert.assertEquals(bobProxy, bob);
491: } catch (final Exception e) {
492: e.printStackTrace();
493: fail("Error: " + e.getMessage());
494: }
495: }
496:
497: public void testProxyForArrayAttributes() {
498: try {
499: Person bob = new Person("bob", 30);
500: Address addr1 = new Address("street 1", "111",
501: "11-1111-1111");
502: Address addr2 = new Address("street 2", "222",
503: "22-2222-2222");
504: Address addr3 = new Address("street 3", "333",
505: "33-3333-3333");
506: bob.getAddressArray()[0] = addr1;
507: bob.getAddressArray()[1] = addr2;
508:
509: // creating proxy
510: final Class proxy = ShadowProxyFactory.getProxy(bob
511: .getClass());
512: final Person bobProxy = (Person) proxy.getConstructor(
513: new Class[] { bob.getClass() }).newInstance(
514: new Object[] { bob });
515: ((ShadowProxy) bobProxy).setShadowedObject(bob);
516:
517: // proxy is proxying the values
518: Assert.assertEquals(addr1, bobProxy.getAddressArray()[0]);
519: Assert.assertEquals(addr2, bobProxy.getAddressArray()[1]);
520: Assert.assertSame(bob, ((ShadowProxy) bobProxy)
521: .getShadowedObject());
522:
523: // proxy must recongnize the original object on equals() calls
524: Assert.assertEquals(bobProxy, bob);
525:
526: bob.getAddressArray()[1] = addr3;
527:
528: Assert.assertEquals(addr1, bobProxy.getAddressArray()[0]);
529: Assert.assertEquals(addr2, bobProxy.getAddressArray()[1]);
530:
531: ((ShadowProxy) bobProxy).updateProxy();
532: Assert.assertEquals(addr1, bobProxy.getAddressArray()[0]);
533: Assert.assertEquals(addr3, bobProxy.getAddressArray()[1]);
534:
535: // proxy must recongnize the original object on equals() calls
536: Assert.assertEquals(bobProxy, bob);
537: } catch (final Exception e) {
538: e.printStackTrace();
539: fail("Error: " + e.getMessage());
540: }
541: }
542:
543: public void testProxyForInterface2() {
544: try {
545: // creating original object
546: final String original = "stilton";
547:
548: // creating proxy
549: final Class proxy = ShadowProxyFactory
550: .getProxy(Comparable.class);
551: final Comparable comparableProxy = (Comparable) proxy
552: .getConstructor(new Class[] { Comparable.class })
553: .newInstance(new Object[] { original });
554:
555: // proxy is proxying the values
556: Assert.assertEquals(comparableProxy, original);
557: Assert.assertSame(original, ((ShadowProxy) comparableProxy)
558: .getShadowedObject());
559: Assert.assertEquals(original.hashCode(), comparableProxy
560: .hashCode());
561:
562: } catch (final Exception e) {
563: fail("Error: " + e.getMessage());
564: }
565: }
566:
567: public void testProxyForClassWithEquals() {
568: try {
569: // creating original object
570: final String originalType = "stilton";
571: final int originalPrice = 15;
572: final CheeseEqual cheese = new CheeseEqual(originalType,
573: originalPrice);
574:
575: // creating proxy
576: final Class proxy = ShadowProxyFactory
577: .getProxy(CheeseEqual.class);
578: final CheeseEqual cheeseProxy = (CheeseEqual) proxy
579: .getConstructor(new Class[] { CheeseEqual.class })
580: .newInstance(new Object[] { cheese });
581:
582: // proxy is proxying the values
583: Assert.assertEquals(originalType, cheeseProxy.getType());
584: Assert.assertEquals(originalPrice, cheeseProxy.getPrice());
585: Assert.assertSame(cheese, ((ShadowProxy) cheeseProxy)
586: .getShadowedObject());
587:
588: // proxy must recongnize the original object on equals()/hashcode() calls
589: //Assert.assertEquals( cheeseProxy.hashCode(), cheese.hashCode() );
590: Assert.assertEquals(cheeseProxy, cheese);
591:
592: // changing original values
593: final String actualType = "rotten stilton";
594: final int actualPrice = 1;
595: cheese.setType(actualType);
596: cheese.setPrice(actualPrice);
597:
598: // proxy does not see changes
599: Assert.assertEquals(actualType, cheese.getType());
600: Assert
601: .assertFalse(actualType.equals(cheeseProxy
602: .getType()));
603: Assert.assertEquals(originalType, cheeseProxy.getType());
604: Assert.assertEquals(actualPrice, cheese.getPrice());
605: Assert.assertFalse(actualPrice == cheeseProxy.getPrice());
606: Assert.assertEquals(originalPrice, cheeseProxy.getPrice());
607:
608: // reseting proxy
609: ((ShadowProxy) cheeseProxy).updateProxy();
610:
611: // now proxy see changes
612: Assert.assertEquals(actualType, cheese.getType());
613: Assert.assertEquals(actualType, cheeseProxy.getType());
614: Assert.assertFalse(originalType.equals(cheeseProxy
615: .getType()));
616: Assert.assertEquals(actualPrice, cheese.getPrice());
617: Assert.assertEquals(actualPrice, cheeseProxy.getPrice());
618: Assert.assertFalse(originalPrice == cheeseProxy.getPrice());
619:
620: // Another cheese
621: final CheeseEqual cheese2 = new CheeseEqual("brie", 10);
622:
623: final CheeseEqual cheese2Proxy = (CheeseEqual) proxy
624: .getConstructor(new Class[] { CheeseEqual.class })
625: .newInstance(new Object[] { cheese2 });
626: assertFalse(cheeseProxy.equals(cheese2Proxy));
627: assertFalse(cheese2Proxy.equals(cheeseProxy));
628:
629: } catch (final Exception e) {
630: fail("Error: " + e.getMessage());
631: }
632: }
633:
634: }
|