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.tapestry.ioc.Location;
020: import org.apache.tapestry.ioc.internal.IOCInternalTestCase;
021: import org.apache.tapestry.ioc.internal.util.TapestryException;
022: import org.apache.tapestry.ioc.services.ExceptionAnalysis;
023: import org.apache.tapestry.ioc.services.ExceptionAnalyzer;
024: import org.apache.tapestry.ioc.services.ExceptionInfo;
025: import org.testng.annotations.AfterClass;
026: import org.testng.annotations.BeforeClass;
027: import org.testng.annotations.Test;
028:
029: public class ExceptionAnalyzerImplTest extends IOCInternalTestCase {
030: private ExceptionAnalyzer _analyzer;
031:
032: @BeforeClass
033: public void setup_analyzer() {
034: _analyzer = getService("ExceptionAnalyzer",
035: ExceptionAnalyzer.class);
036: }
037:
038: @AfterClass
039: public void cleanup_analyzer() {
040: _analyzer = null;
041: }
042:
043: @Test
044: public void basic_exception() {
045: String message = "Hey! We've Got Not Tomatoes!";
046:
047: Throwable t = new RuntimeException(message);
048:
049: ExceptionAnalysis ea = _analyzer.analyze(t);
050:
051: assertEquals(ea.getExceptionInfos().size(), 1);
052:
053: ExceptionInfo ei = ea.getExceptionInfos().get(0);
054:
055: assertEquals(ei.getClassName(), RuntimeException.class
056: .getName());
057: assertEquals(ei.getMessage(), message);
058:
059: assertTrue(ei.getPropertyNames().isEmpty());
060: assertFalse(ei.getStackTrace().isEmpty());
061: }
062:
063: @Test
064: public void exception_properties() {
065: Location l = mockLocation();
066:
067: replay();
068:
069: Throwable t = new TapestryException("Message", l, null);
070:
071: ExceptionAnalysis ea = _analyzer.analyze(t);
072:
073: assertEquals(ea.getExceptionInfos().size(), 1);
074:
075: ExceptionInfo ei = ea.getExceptionInfos().get(0);
076:
077: assertEquals(ei.getPropertyNames(), Arrays.asList("location"));
078:
079: assertEquals(ei.getProperty("location"), l);
080:
081: verify();
082: }
083:
084: @Test
085: public void nested_exceptions() {
086: Throwable inner = new RuntimeException("Inner");
087: Throwable outer = new RuntimeException("Outer", inner);
088:
089: ExceptionAnalysis ea = _analyzer.analyze(outer);
090:
091: assertEquals(ea.getExceptionInfos().size(), 2);
092:
093: ExceptionInfo ei = ea.getExceptionInfos().get(0);
094:
095: assertEquals(ei.getMessage(), "Outer");
096: assertTrue(ei.getStackTrace().isEmpty());
097:
098: ei = ea.getExceptionInfos().get(1);
099:
100: assertEquals(ei.getMessage(), "Inner");
101: assertFalse(ei.getStackTrace().isEmpty());
102: }
103:
104: @Test
105: public void middle_exception_removed_with_no_value() {
106: Throwable inner = new RuntimeException("Inner");
107: Throwable middle = new RuntimeException("Middle", inner);
108: Throwable outer = new RuntimeException("Outer: Middle", middle);
109:
110: ExceptionAnalysis ea = _analyzer.analyze(outer);
111:
112: assertEquals(ea.getExceptionInfos().size(), 2);
113:
114: ExceptionInfo ei = ea.getExceptionInfos().get(0);
115:
116: assertEquals(ei.getMessage(), "Outer: Middle");
117: assertTrue(ei.getStackTrace().isEmpty());
118:
119: ei = ea.getExceptionInfos().get(1);
120:
121: assertEquals(ei.getMessage(), "Inner");
122: assertFalse(ei.getStackTrace().isEmpty());
123: }
124:
125: @Test
126: public void middle_exception_retained_due_to_extra_property() {
127: Location l = mockLocation();
128:
129: replay();
130:
131: Throwable inner = new RuntimeException("Inner");
132: Throwable middle = new TapestryException("Middle", l, inner);
133: Throwable outer = new RuntimeException("Outer: Middle", middle);
134:
135: ExceptionAnalysis ea = _analyzer.analyze(outer);
136:
137: assertEquals(ea.getExceptionInfos().size(), 3);
138:
139: ExceptionInfo ei = ea.getExceptionInfos().get(0);
140:
141: assertEquals(ei.getMessage(), "Outer: Middle");
142: assertTrue(ei.getStackTrace().isEmpty());
143:
144: ei = ea.getExceptionInfos().get(1);
145:
146: assertEquals(ei.getMessage(), "Middle");
147: assertTrue(ei.getStackTrace().isEmpty());
148:
149: ei = ea.getExceptionInfos().get(2);
150:
151: assertEquals(ei.getMessage(), "Inner");
152: assertFalse(ei.getStackTrace().isEmpty());
153:
154: verify();
155: }
156:
157: }
|