001 /*****************************************************************************
002 * Copyright (C) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file. *
007 * *
008 * Original code by *
009 *****************************************************************************/
010 package org.picocontainer.behaviors;
011
012 import static org.junit.Assert.assertEquals;
013 import static org.junit.Assert.assertNotNull;
014
015 import org.junit.Test;
016 import org.picocontainer.DefaultPicoContainer;
017
018 public class InterceptingTestCase {
019
020 public static interface Foo {
021 void one();
022 String two(String a, int b);
023 }
024
025 public static class FooImpl implements Foo {
026 private StringBuilder sb;
027
028 public FooImpl(StringBuilder sb) {
029 this.sb = sb;
030 }
031
032 public void one() {
033 sb.append("call-one(),");
034 }
035
036 public String two(String a, int b) {
037 sb.append("call-two('"+a+"',"+b+"),");
038 return "two";
039 }
040 }
041
042 @Test public void testPreAndPostObservation() {
043 final StringBuilder sb = new StringBuilder();
044 DefaultPicoContainer pico = new DefaultPicoContainer(new Intercepting());
045 pico.addComponent(StringBuilder.class, sb);
046 pico.addComponent(Foo.class, FooImpl.class);
047
048 Intercepted intercepted = pico.getComponentAdapter(Foo.class).findAdapterOfType(Intercepted.class);
049 final Intercepted.Controller interceptor = intercepted.getController();
050 intercepted.addPreInvocation(Foo.class, new Foo() {
051 public void one() {
052 sb.append("pre-one(),");
053 }
054 public String two(String a, int b) {
055 sb.append("pre-two('"+a+"',"+b+"),");
056 return null;
057 }
058 });
059 intercepted.addPostInvocation(Foo.class, new Foo() {
060 public void one() {
061 sb.append("post-one(),");
062 }
063 public String two(String a, int b) {
064 assertEquals("two", interceptor.getOriginalRetVal());
065 sb.append("post-two('"+a+"',"+b+"),");
066 return null;
067 }
068 });
069
070 Foo foo = pico.getComponent(Foo.class);
071 assertNotNull(foo);
072 foo.one();
073 assertEquals("two", foo.two("hello", 99));
074 assertEquals("pre-one(),call-one(),post-one(),pre-two('hello',99),call-two('hello',99),post-two('hello',99),", sb.toString());
075 assertEquals("Intercepted:ConstructorInjector-interface org.picocontainer.behaviors.InterceptingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
076 }
077
078 @Test public void testPreCanBlockInvocationWithAlternateReturnValue() {
079 final StringBuilder sb = new StringBuilder();
080 DefaultPicoContainer pico = new DefaultPicoContainer(new Intercepting());
081 pico.addComponent(Foo.class, FooImpl.class);
082 pico.addComponent(StringBuilder.class, sb);
083
084 Intercepted intercepted = pico.getComponentAdapter(Foo.class).findAdapterOfType(Intercepted.class);
085 final Intercepted.Controller interceptor = intercepted.getController();
086 intercepted.addPreInvocation(Foo.class, new Foo() {
087 public void one() {
088 interceptor.veto();
089 sb.append("veto-one(),");
090 }
091
092 public String two(String a, int b) {
093 interceptor.veto();
094 sb.append("veto-two('"+a+"',"+b+"),");
095 return "isVetoed";
096 }
097 });
098
099 Foo foo = pico.getComponent(Foo.class);
100 assertNotNull(foo);
101 foo.one();
102 assertEquals("isVetoed", foo.two("hello", 99));
103 assertEquals("veto-one(),veto-two('hello',99),", sb.toString());
104 assertEquals("Intercepted:ConstructorInjector-interface org.picocontainer.behaviors.InterceptingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
105 }
106
107 @Test public void testOverrideOfReturnValue() {
108 final StringBuilder sb = new StringBuilder();
109 DefaultPicoContainer pico = new DefaultPicoContainer(new Intercepting());
110 pico.addComponent(Foo.class, FooImpl.class);
111 pico.addComponent(StringBuilder.class, sb);
112 Intercepted intercepted = pico.getComponentAdapter(Foo.class).findAdapterOfType(Intercepted.class);
113 final Intercepted.Controller interceptor = intercepted.getController();
114 intercepted.addPreInvocation(Foo.class, new Foo() {
115 public void one() {
116 sb.append("pre-one(),");
117 }
118
119 public String two(String a, int b) {
120 sb.append("pre-two('"+a+"',"+b+"),");
121 return null;
122 }
123 });
124 intercepted.addPostInvocation(Foo.class, new Foo() {
125 public void one() {
126 interceptor.override();
127 sb.append("override-one(),");
128 }
129
130 public String two(String a, int b) {
131 interceptor.override();
132 sb.append("override-two('"+a+"',"+b+"),");
133 return "x";
134 }
135 });
136
137 Foo foo = pico.getComponent(Foo.class);
138 assertNotNull(foo);
139 foo.one();
140 assertEquals("x", foo.two("hello", 99));
141 assertEquals("pre-one(),call-one(),override-one(),pre-two('hello',99),call-two('hello',99),override-two('hello',99),", sb.toString());
142 assertEquals("Intercepted:ConstructorInjector-interface org.picocontainer.behaviors.InterceptingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
143 }
144
145 @Test public void testNothingHappensIfNoPreOrPost() {
146 final StringBuilder sb = new StringBuilder();
147 DefaultPicoContainer pico = new DefaultPicoContainer(new Intercepting());
148 pico.addComponent(Foo.class, FooImpl.class);
149 pico.addComponent(StringBuilder.class, sb);
150 Foo foo = pico.getComponent(Foo.class);
151 assertNotNull(foo);
152 foo.one();
153 assertEquals("two", foo.two("hello", 99));
154 assertEquals("call-one(),call-two('hello',99),", sb.toString());
155 assertEquals("Intercepted:ConstructorInjector-interface org.picocontainer.behaviors.InterceptingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
156 }
157
158
159
160 }