001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.services;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.tapestry.ioc.services.LoggingDecorator;
019: import org.apache.tapestry.ioc.test.IOCTestCase;
020: import org.testng.Assert;
021: import org.testng.annotations.Test;
022: import org.xml.sax.SAXParseException;
023:
024: /**
025: * Use the LoggingDecorator in a number of ways to verify its behavior. In some ways we are testing
026: * the code dynamically generated by the LoggingDecorator as much as we are testing the decorator
027: * itself -- one proves the other.
028: */
029: public class LoggingDecoratorImplTest extends IOCTestCase {
030: public interface UpcaseService {
031: String upcase(String input);
032: }
033:
034: public interface AdderService {
035: long add(long operand1, long operand2);
036: }
037:
038: public interface ToStringService {
039: String toString();
040: }
041:
042: public interface ExceptionService {
043: void parse() throws SAXParseException;
044: }
045:
046: @Test
047: public void void_method() {
048: Log log = mockLog();
049: Runnable delegate = mockRunnable();
050:
051: train_isDebugEnabled(log, true);
052: log.debug("[ENTER] run()");
053:
054: delegate.run();
055:
056: log.debug("[ EXIT] run");
057:
058: replay();
059:
060: LoggingDecorator ld = newLoggingDecorator();
061: Runnable interceptor = ld.build(Runnable.class, delegate,
062: "foo.Bar", log);
063:
064: interceptor.run();
065:
066: assertEquals(interceptor.toString(),
067: "<Logging interceptor for foo.Bar(java.lang.Runnable)>");
068:
069: verify();
070: }
071:
072: private LoggingDecoratorImpl newLoggingDecorator() {
073: return new LoggingDecoratorImpl(new ClassFactoryImpl(),
074: new ExceptionTrackerImpl());
075: }
076:
077: @Test
078: public void method_throws_runtime_exception() {
079: Throwable t = new RuntimeException("From delegate.");
080: Log log = mockLog();
081: Runnable delegate = mockRunnable();
082:
083: train_isDebugEnabled(log, true);
084: log.debug("[ENTER] run()");
085:
086: delegate.run();
087: setThrowable(t);
088:
089: train_isDebugEnabled(log, true);
090:
091: log.debug("[ FAIL] run -- " + t.getClass().getName(), t);
092:
093: replay();
094:
095: LoggingDecorator ld = newLoggingDecorator();
096: Runnable interceptor = ld.build(Runnable.class, delegate,
097: "foo.Bar", log);
098:
099: try {
100: interceptor.run();
101: unreachable();
102: } catch (RuntimeException ex) {
103: Assert.assertSame(ex, t);
104: }
105:
106: verify();
107: }
108:
109: @Test
110: public void method_throws_checked_exception() throws Exception {
111: Throwable t = new SAXParseException("From delegate.", null);
112: Log log = mockLog();
113: ExceptionService delegate = newMock(ExceptionService.class);
114:
115: train_isDebugEnabled(log, true);
116: log.debug("[ENTER] parse()");
117:
118: delegate.parse();
119: setThrowable(t);
120:
121: train_isDebugEnabled(log, true);
122:
123: log.debug("[ FAIL] parse -- " + t.getClass().getName(), t);
124:
125: replay();
126:
127: LoggingDecorator ld = newLoggingDecorator();
128: ExceptionService interceptor = ld.build(ExceptionService.class,
129: delegate, "foo.Bar", log);
130:
131: try {
132: interceptor.parse();
133: unreachable();
134: } catch (SAXParseException ex) {
135: Assert.assertSame(ex, t);
136: }
137:
138: verify();
139: }
140:
141: @Test
142: public void object_parameter_and_return_type() {
143: Log log = mockLog();
144: UpcaseService delegate = new UpcaseService() {
145: public String upcase(String input) {
146: return input.toUpperCase();
147: }
148: };
149:
150: train_isDebugEnabled(log, true);
151: log.debug("[ENTER] upcase(\"barney\")");
152:
153: log.debug("[ EXIT] upcase [\"BARNEY\"]");
154:
155: replay();
156:
157: LoggingDecorator ld = newLoggingDecorator();
158: UpcaseService interceptor = ld.build(UpcaseService.class,
159: delegate, "foo.Bar", log);
160:
161: assertEquals(interceptor.upcase("barney"), "BARNEY");
162:
163: verify();
164: }
165:
166: @Test
167: public void primitive_parameter_and_return_type() {
168: Log log = mockLog();
169: AdderService delegate = new AdderService() {
170: public long add(long operand1, long operand2) {
171: return operand1 + operand2;
172: }
173: };
174:
175: train_isDebugEnabled(log, true);
176: log.debug("[ENTER] add(6, 13)");
177:
178: log.debug("[ EXIT] add [19]");
179:
180: replay();
181:
182: LoggingDecorator ld = newLoggingDecorator();
183: AdderService interceptor = ld.build(AdderService.class,
184: delegate, "foo.Bar", log);
185:
186: assertEquals(interceptor.add(6, 13), 19);
187:
188: verify();
189: }
190:
191: @Test
192: public void to_string_method_in_service_interface_is_delegated() {
193: Log log = mockLog();
194: ToStringService delegate = new ToStringService() {
195: @Override
196: public String toString() {
197: return "FROM DELEGATE";
198: }
199: };
200:
201: train_isDebugEnabled(log, true);
202: log.debug("[ENTER] toString()");
203:
204: log.debug("[ EXIT] toString [\"FROM DELEGATE\"]");
205:
206: replay();
207:
208: LoggingDecorator ld = newLoggingDecorator();
209: ToStringService interceptor = ld.build(ToStringService.class,
210: delegate, "foo.Bar", log);
211:
212: assertEquals(interceptor.toString(), "FROM DELEGATE");
213:
214: verify();
215: }
216: }
|