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 java.util.Arrays;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.tapestry.ioc.services.ExceptionTracker;
021: import org.apache.tapestry.ioc.test.IOCTestCase;
022: import org.testng.annotations.Test;
023:
024: public class ServiceLoggerTest extends IOCTestCase {
025: private void try_entry(String methodName, String expected,
026: Object... arguments) {
027: Log log = mockLog();
028: ExceptionTracker tracker = mockExceptionTracker();
029:
030: log.debug("[ENTER] " + expected);
031:
032: replay();
033:
034: new ServiceLogger(log, tracker).entry(methodName, arguments);
035:
036: verify();
037:
038: }
039:
040: protected final ExceptionTracker mockExceptionTracker() {
041: return newMock(ExceptionTracker.class);
042: }
043:
044: private void try_exit(String methodName, String expected,
045: Object result) {
046: Log log = mockLog();
047: ExceptionTracker tracker = mockExceptionTracker();
048:
049: log.debug("[ EXIT] " + expected);
050:
051: replay();
052:
053: new ServiceLogger(log, tracker).exit(methodName, result);
054:
055: verify();
056: }
057:
058: @Test
059: public void entry_tests() {
060: try_entry("fred", "fred()");
061: try_entry("barney", "barney(\"rubble\")", "rubble");
062: try_entry("yogi", "yogi(null, null)", null, null);
063: try_entry("wilma", "wilma(1, 2, 3)", 1, 2, 3);
064: try_entry("betty", "betty(\"rubble\", {1, 2, 3, \"four\"})",
065: "rubble", new Object[] { 1, 2, 3, "four" });
066: try_entry("betty",
067: "betty(\"rubble\", [1, 2, 3, \"four\", [5, 6]])",
068: "rubble", Arrays.asList(1, 2, 3, "four", Arrays.asList(
069: 5, 6)));
070: }
071:
072: @Test
073: public void exit_test() {
074: try_exit("fred", "fred [true]", true);
075: try_exit("barney", "barney [\"rubble\"]", "rubble");
076: }
077:
078: @Test
079: public void void_exit_test() {
080: Log log = mockLog();
081: ExceptionTracker tracker = mockExceptionTracker();
082:
083: log.debug("[ EXIT] wilma");
084:
085: replay();
086:
087: new ServiceLogger(log, tracker).voidExit("wilma");
088:
089: verify();
090: }
091:
092: @Test
093: public void fail_test_exception_not_already_logged() {
094: Log log = mockLog();
095: ExceptionTracker tracker = mockExceptionTracker();
096:
097: RuntimeException t = new RuntimeException("Ouch!");
098:
099: train_isDebugEnabled(log, true);
100:
101: train_exceptionLogged(tracker, t, false);
102:
103: log.debug("[ FAIL] wilma -- " + t.getClass().getName(), t);
104:
105: replay();
106:
107: new ServiceLogger(log, tracker).fail("wilma", t);
108:
109: verify();
110: }
111:
112: @Test
113: public void fail_test_exception_previously_logged() {
114: Log log = mockLog();
115: ExceptionTracker tracker = mockExceptionTracker();
116:
117: RuntimeException t = new RuntimeException("Ouch!");
118:
119: train_isDebugEnabled(log, true);
120:
121: train_exceptionLogged(tracker, t, true);
122:
123: log.debug("[ FAIL] wilma -- " + t.getClass().getName(), null);
124:
125: replay();
126:
127: new ServiceLogger(log, tracker).fail("wilma", t);
128:
129: verify();
130: }
131:
132: @Test
133: public void fail_debug_not_enabled() {
134: Log log = mockLog();
135: ExceptionTracker tracker = mockExceptionTracker();
136:
137: RuntimeException t = new RuntimeException("Ouch!");
138:
139: train_isDebugEnabled(log, false);
140:
141: replay();
142:
143: new ServiceLogger(log, tracker).fail("wilma", t);
144:
145: verify();
146: }
147:
148: private void train_exceptionLogged(ExceptionTracker tracker,
149: Throwable exception, boolean logged) {
150: expect(tracker.exceptionLogged(exception)).andReturn(logged);
151: }
152:
153: @Test
154: public void debug_enabled() {
155: Log log = mockLog();
156: ExceptionTracker tracker = mockExceptionTracker();
157:
158: train_isDebugEnabled(log, true);
159: train_isDebugEnabled(log, false);
160:
161: replay();
162:
163: ServiceLogger logger = new ServiceLogger(log, tracker);
164:
165: assertTrue(logger.isDebugEnabled());
166: assertFalse(logger.isDebugEnabled());
167:
168: verify();
169: }
170: }
|