View Javadoc

1   package test.crispy;
2   
3   import java.util.Hashtable;
4   import java.util.Map;
5   
6   import junit.framework.TestCase;
7   import net.sf.crispy.ExceptionWrapper;
8   import net.sf.crispy.InvocationException;
9   import net.sf.crispy.StackTraceElementWrapper;
10  import net.sf.crispy.util.Converter;
11  
12  public class ExceptionWrapperTest extends TestCase {
13  
14  	public void testExceptionWrapper() throws Exception {
15  		String lvMessage = "JUnit-Test-Message";
16  		JUnitTestException lvException = new JUnitTestException(lvMessage);
17  		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper(lvException);
18  		assertEquals(lvMessage, lvExceptionWrapper.getMessage());
19  		assertEquals(JUnitTestException.class.getName(), lvExceptionWrapper.getExceptionClassName());
20  		assertNotNull(lvExceptionWrapper.getStackTraceByteArray());
21  		assertTrue(lvExceptionWrapper.getStackTraceByteArray().length > 0);
22  		assertNotNull(lvExceptionWrapper.getStackTraceElementWrapperList());
23  		assertTrue(lvExceptionWrapper.getStackTraceElementWrapperList().size() > 1);
24  	}
25  	
26  	public void testNewInstance() throws Exception {
27  		String lvMessage = "JUnit-Test-Message";
28  		JUnitTestException lvException = new JUnitTestException(lvMessage);
29  		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper(lvException);
30  		
31  		Exception lvException2 = lvExceptionWrapper.newExceptionInstance();
32  		assertNotNull(lvException2);
33  		assertTrue(lvException2 instanceof JUnitTestException);
34  		assertEquals(lvMessage, lvException2.getMessage());
35  	}
36  	
37  	public void testNewInstanceWithExceptionSerializer_DefaultException() throws Exception {
38  		String lvMessage = "JUnit-Test-Message-2";
39  		JUnitTestException3 lvException = new JUnitTestException3(lvMessage, null);
40  		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper(lvException, true);
41  		Object lvResult = lvExceptionWrapper.newExceptionInstance();
42  		assertTrue(lvResult instanceof InvocationException);
43  		Throwable lvCause = ((Exception) lvResult).getCause();
44  		assertNull(lvCause);
45  		assertEquals(lvMessage, ((Exception) lvResult).getMessage());
46  	}
47  	
48  	public void testNewInstanceWithOutExceptionSerializer_DefaultException() throws Exception {
49  		String lvMessage = "JUnit-Test-Message-3";
50  		JUnitTestException3 lvException = new JUnitTestException3(lvMessage, null);
51  		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper(lvException);
52  		Object lvResult = lvExceptionWrapper.newExceptionInstance();
53  		assertTrue(lvResult instanceof InvocationException);
54  		Throwable lvCause = ((Exception) lvResult).getCause();
55  		assertNull(lvCause);
56  		assertEquals(lvMessage, ((Exception) lvResult).getMessage());
57  	}
58  	
59  	public void testNewInstanceWithExceptionSerializer() throws Exception {
60  		String lvMessage = "JUnit-Test-Message-4";
61  		JUnitTestException2 lvException2 = new JUnitTestException2(lvMessage);
62  		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper(lvException2, true);
63  		Object lvResult = lvExceptionWrapper.newExceptionInstance();
64  		assertNotNull(lvResult);
65  		assertTrue(lvResult instanceof JUnitTestException2);
66  		assertEquals(lvMessage, ((Exception) lvResult).getCause().getMessage());
67  	}
68  
69  	public void testNewInstanceWithOutExceptionSerializer() throws Exception {
70  		String lvMessage = "JUnit-Test-Message-5";
71  		JUnitTestException2 lvException2 = new JUnitTestException2(lvMessage);
72  		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper(lvException2);
73  		Object lvResult = lvExceptionWrapper.newExceptionInstance();
74  		assertNotNull(lvResult);
75  		assertTrue(lvResult instanceof JUnitTestException2);
76  		assertEquals(lvMessage, ((Exception) lvResult).getMessage());
77  	}
78  
79  	public void testNewInstanceWithExceptionWithNoArgsInConstructor() throws Exception {
80  		JUnitTestException4 lvException = new JUnitTestException4();
81  		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper(lvException);
82  		Object lvResult = lvExceptionWrapper.newExceptionInstance();
83  		assertNotNull(lvResult);
84  		assertTrue(lvResult instanceof JUnitTestException4);
85  		assertNull(((Exception) lvResult).getMessage());
86  	}
87  
88  	
89  	public void testConvertExceptionWrapper() throws Exception {
90  		String lvMessage = "JUnit-Test-Message";
91  		JUnitTestException lvException = new JUnitTestException(lvMessage);
92  		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper(lvException);
93  
94  		Converter lvConverter = new Converter();
95  		Object lvSimple = lvConverter.makeSimple(lvExceptionWrapper);
96  		Object lvComplex = lvConverter.makeComplex(lvSimple);
97  		
98  		assertNotNull(lvSimple);
99  		assertNotNull(lvComplex);
100 		assertTrue(lvComplex instanceof ExceptionWrapper);
101 		ExceptionWrapper lvExceptionWrapper2 = (ExceptionWrapper) lvComplex;
102 		assertEquals(lvExceptionWrapper.getExceptionClassName(), lvExceptionWrapper2.getExceptionClassName());
103 		assertEquals(lvExceptionWrapper.getMessage(), lvExceptionWrapper2.getMessage());
104 		assertEquals(lvExceptionWrapper.getStackTraceByteArray(), lvExceptionWrapper2.getStackTraceByteArray());
105 		assertEquals(lvExceptionWrapper.getStackTraceElementWrapperList().size(), lvExceptionWrapper2.getStackTraceElementWrapperList().size());
106 	}
107 	
108 	public void testStackTraceElementWrapper() throws Exception {
109 		String lvMessage = "JUnit-Test-Message";
110 		JUnitTestException lvException = new JUnitTestException(lvMessage);
111 		StackTraceElement lvElement = lvException.getStackTrace()[0];
112 		StackTraceElementWrapper lvElementWrapper = new StackTraceElementWrapper(lvElement);
113 		assertEquals(lvElement.getClassName(), lvElementWrapper.getClassName());
114 		assertEquals(lvElement.getFileName(), lvElementWrapper.getFileName());
115 		assertEquals(lvElement.getMethodName(), lvElementWrapper.getMethodName());
116 		assertEquals(lvElement.getLineNumber(), lvElementWrapper.getLineNumber());
117 		assertNotNull(lvElementWrapper.toString());
118 	}
119 	
120 	public void testStackTraceElementConverter() throws Exception {
121 		String lvMessage = "JUnit-Test-Message";
122 		JUnitTestException lvException = new JUnitTestException(lvMessage);
123 		StackTraceElement lvElement = lvException.getStackTrace()[0];
124 		StackTraceElementWrapper lvElementWrapper = new StackTraceElementWrapper(lvElement);
125 
126 		Converter lvConverter = new Converter();
127 		Object lvSimple = lvConverter.makeSimple(lvElementWrapper);
128 		Object lvComplex = lvConverter.makeComplex(lvSimple);
129 		
130 		assertNotNull(lvSimple);
131 		assertNotNull(lvComplex);
132 		assertTrue(lvComplex instanceof StackTraceElementWrapper);
133 		assertTrue(lvComplex instanceof StackTraceElementWrapper);
134 		StackTraceElementWrapper lvElementWrapper2 = (StackTraceElementWrapper) lvComplex;
135 		assertEquals(lvElementWrapper.getClassName(), lvElementWrapper2.getClassName());
136 		assertEquals(lvElementWrapper.getFileName(), lvElementWrapper2.getFileName());
137 		assertEquals(lvElementWrapper.getMethodName(), lvElementWrapper2.getMethodName());
138 		assertEquals(lvElementWrapper.getLineNumber(), lvElementWrapper2.getLineNumber());
139 		assertEquals(lvElementWrapper.isNativeMethod(), lvElementWrapper2.isNativeMethod());
140 	}
141 	
142 	public void testIfThrowableThanHandleThrowable() throws Exception {
143 		assertNull(ExceptionWrapper.isThrowableThanHandleThrowable(null));
144 		
145 		assertNotNull(ExceptionWrapper.isThrowableThanHandleThrowable("Test-String"));
146 		assertEquals("Test-String", ExceptionWrapper.isThrowableThanHandleThrowable("Test-String"));
147 		
148 		Object o = ExceptionWrapper.isThrowableThanHandleThrowable(new JUnitTestException("Test-Exception"));
149 		assertNotNull(o);
150 		assertTrue(o instanceof ExceptionWrapper);
151 		ExceptionWrapper ew = (ExceptionWrapper) o;
152 		assertEquals(JUnitTestException.class.getName(), ew.getExceptionClassName());
153 		assertEquals("Test-Exception", ew.getMessage());
154 	}
155 	
156 	public void testIsResultExceptionThanThrowIt() throws Exception {
157 		assertNull(ExceptionWrapper.isResultExceptionThanThrowIt(null));
158 		
159 		assertNotNull(ExceptionWrapper.isResultExceptionThanThrowIt("Test-String"));
160 		assertEquals("Test-String", ExceptionWrapper.isResultExceptionThanThrowIt("Test-String"));
161 		
162 		try {
163 			Map map = new Hashtable();
164 			map.put("class", ExceptionWrapper.class.getName());
165 			map.put("exceptionClassName", JUnitTestException.class.getName());
166 			map.put("message", "Test-Exception-Massage");
167 			ExceptionWrapper.isResultExceptionThanThrowIt(map);
168 			fail("Exception must thrown a JUnitTestException.");
169 		} catch (JUnitTestException e) {
170 			assertTrue(true);
171 		}
172 	}
173 	
174 	public void testIsResultExceptionThanThrowIt2() throws Exception {
175 		assertNull(ExceptionWrapper.isResultExceptionThanThrowIt(null));
176 		
177 		assertNotNull(ExceptionWrapper.isResultExceptionThanThrowIt("Test-String"));
178 		assertEquals("Test-String", ExceptionWrapper.isResultExceptionThanThrowIt("Test-String"));
179 		
180 		try {
181 			ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper(new JUnitTestException("Test-Exception-Massage"));
182 			ExceptionWrapper.isResultExceptionThanThrowIt(lvExceptionWrapper);
183 			fail("Exception must thrown a JUnitTestException.");
184 		} catch (JUnitTestException e) {
185 			assertTrue(true);
186 		}
187 	}
188 
189 	public void testIsResultExceptionThanThrowIt3() throws Exception {
190 		Object o = ExceptionWrapper.isResultExceptionThanThrowIt(new Hashtable());
191 		assertNotNull(o);
192 		assertTrue(o instanceof Map);
193 		assertEquals(new Hashtable(), o);
194 	}
195 
196 	public void testNewExceptionInstanceErrors() throws Exception {
197 		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper();
198 		
199 		Exception lvException = lvExceptionWrapper.newExceptionInstance();
200 		assertNotNull(lvException);
201 		assertTrue(lvException instanceof InvocationException);
202 		assertEquals("no message available", lvException.getMessage());
203 		
204 		lvExceptionWrapper.setExceptionClassName("not.valid.class.name");
205 		assertNotNull(lvException);
206 		assertTrue(lvException instanceof InvocationException);
207 		assertEquals("no message available", lvException.getMessage());
208 	}
209 	
210 	public void testNewExceptionInstance() throws Exception {
211 		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper();
212 		lvExceptionWrapper.setExceptionClassName(JUnitTestException.class.getName());
213 		String lvMessage = "Test-Message";
214 		lvExceptionWrapper.setMessage(lvMessage);
215 		
216 		Exception lvException = lvExceptionWrapper.newExceptionInstance();
217 		assertNotNull(lvException);
218 		assertTrue(lvException instanceof JUnitTestException);
219 		assertEquals(lvMessage, lvException.getMessage());
220 	}
221 	
222 	public void testNewExceptionInstance2() throws Exception {
223 		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper();
224 		lvExceptionWrapper.setExceptionClassName(JUnitTestException2.class.getName());
225 		String lvMessage = "Test-Message";
226 		lvExceptionWrapper.setMessage(lvMessage);
227 		
228 		Exception lvException = lvExceptionWrapper.newExceptionInstance();
229 		assertNotNull(lvException);
230 		assertTrue(lvException instanceof JUnitTestException2);
231 		assertNotNull(lvException.getMessage());
232 		assertEquals(lvMessage, lvException.getMessage());
233 	}
234 
235 	public void testNewExceptionInstance3() throws Exception {
236 		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper();
237 		lvExceptionWrapper.setExceptionClassName(JUnitTestException3.class.getName());
238 		String lvMessage = "Test-Message";
239 		lvExceptionWrapper.setMessage(lvMessage);
240 		
241 		Exception lvException = lvExceptionWrapper.newExceptionInstance();
242 		assertNotNull(lvException);
243 		assertTrue(lvException instanceof InvocationException);
244 		assertEquals(lvMessage, lvException.getMessage());
245 	}
246 
247 
248 	public void testExceptionPlaceHolderIsNull() throws Exception {
249 		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper();
250 		assertNull(lvExceptionWrapper.getExceptionPlaceHolder());
251 		
252 		lvExceptionWrapper = new ExceptionWrapper(new JUnitTestException("Test-Message"));
253 		assertNull(lvExceptionWrapper.getExceptionPlaceHolder());
254 		
255 		lvExceptionWrapper = new ExceptionWrapper(new JUnitTestException("Test-Message"), false);
256 		assertNull(lvExceptionWrapper.getExceptionPlaceHolder());
257 	}
258 	
259 	public void testExceptionPlaceHolderIsNotNull() throws Exception {
260 		ExceptionWrapper lvExceptionWrapper = new ExceptionWrapper(new JUnitTestException("Test-Message"), true);
261 		assertNotNull(lvExceptionWrapper.getExceptionPlaceHolder());
262 	}
263 
264 }