KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > PicoExceptionsTestCase


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Joerg Schaible *
9  *****************************************************************************/

10 package org.picocontainer.defaults;
11
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.PrintStream JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.picocontainer.ComponentAdapter;
24 import org.picocontainer.PicoException;
25 import org.picocontainer.PicoInitializationException;
26 import org.picocontainer.PicoInstantiationException;
27 import org.picocontainer.PicoIntrospectionException;
28 import org.picocontainer.PicoRegistrationException;
29
30 /**
31  * Unit tests for the several PicoException classes.
32  */

33 public class PicoExceptionsTestCase
34         extends TestCase {
35
36     final static public String JavaDoc MESSAGE = "Message of the exception";
37     final static public Throwable JavaDoc THROWABLE = new Throwable JavaDoc();
38
39     final void executeTestOfStandardException(final Class JavaDoc clazz) {
40         final ComponentAdapter componentAdapter = new ConstructorInjectionComponentAdapter(clazz, clazz, null, true, new DelegatingComponentMonitor());
41         DefaultPicoContainer pico = new DefaultPicoContainer();
42         pico.registerComponentInstance(MESSAGE);
43         try {
44             final Exception JavaDoc exception = (Exception JavaDoc) componentAdapter.getComponentInstance(pico);
45             assertEquals(MESSAGE, exception.getMessage());
46         } catch (final UnsatisfiableDependenciesException ex) {
47             final Set JavaDoc set = new HashSet JavaDoc();
48             for (final Iterator JavaDoc iter = ex.getUnsatisfiableDependencies().iterator(); iter.hasNext();) {
49                 final List JavaDoc list = (List JavaDoc) iter.next();
50                 set.addAll(list);
51             }
52             assertTrue(set.contains(Throwable JavaDoc.class));
53         }
54         pico = new DefaultPicoContainer();
55         pico.registerComponentInstance(THROWABLE);
56         try {
57             final PicoException exception = (PicoException) componentAdapter.getComponentInstance(pico);
58             assertSame(THROWABLE, exception.getCause());
59         } catch (final UnsatisfiableDependenciesException ex) {
60             final Set JavaDoc set = new HashSet JavaDoc();
61             for (final Iterator JavaDoc iter = ex.getUnsatisfiableDependencies().iterator(); iter.hasNext();) {
62                 final List JavaDoc list = (List JavaDoc) iter.next();
63                 set.addAll(list);
64             }
65             assertTrue(set.contains(String JavaDoc.class));
66         }
67         pico.registerComponentInstance(MESSAGE);
68         final PicoException exception = (PicoException) componentAdapter.getComponentInstance(pico);
69         assertEquals(MESSAGE, exception.getMessage());
70         assertSame(THROWABLE, exception.getCause());
71     }
72
73     public void testPicoInitializationException() {
74         executeTestOfStandardException(PicoInitializationException.class);
75     }
76
77     public void testPicoInitializationExceptionWithDefaultConstructor() {
78         TestException e = new TestException();
79         assertNull(e.getMessage());
80         assertNull(e.getCause());
81     }
82     
83     private static class TestException extends PicoInitializationException {
84         
85     }
86
87     public void testPicoInstantiationException() {
88         executeTestOfStandardException(PicoInstantiationException.class);
89     }
90
91     public void testPicoIntrospectionException() {
92         executeTestOfStandardException(PicoIntrospectionException.class);
93     }
94     
95     public void testPicoRegistrationException() {
96         executeTestOfStandardException(PicoRegistrationException.class);
97     }
98
99     public void testCyclicDependencyException() {
100         final CyclicDependencyException cdEx = new CyclicDependencyException(getClass());
101         cdEx.push(String JavaDoc.class);
102         final Class JavaDoc[] classes = cdEx.getDependencies();
103         assertEquals(2, classes.length);
104         assertSame(getClass(), classes[0]);
105         assertSame(String JavaDoc.class, classes[1]);
106         assertTrue(cdEx.getMessage().indexOf(getClass().getName()) >= 0);
107     }
108
109     public void testPrintStackTrace() throws IOException JavaDoc {
110         PicoException nestedException = new PicoException("Outer", new Exception JavaDoc("Inner")) {
111         };
112         PicoException simpleException = new PicoException("Outer") {
113         };
114         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
115         PrintStream JavaDoc printStream = new PrintStream JavaDoc(out);
116         nestedException.printStackTrace(printStream);
117         simpleException.printStackTrace(printStream);
118         out.close();
119         assertTrue(out.toString().indexOf("Caused by:") > 0);
120         out = new ByteArrayOutputStream JavaDoc();
121         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(out);
122         nestedException.printStackTrace(writer);
123         simpleException.printStackTrace(writer);
124         writer.flush();
125         out.close();
126         assertTrue(out.toString().indexOf("Caused by:") > 0);
127         //simpleException.printStackTrace();
128
}
129 }
130
Popular Tags