1 10 package org.picocontainer.defaults; 11 12 import java.io.ByteArrayOutputStream ; 13 import java.io.IOException ; 14 import java.io.PrintStream ; 15 import java.io.PrintWriter ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Set ; 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 33 public class PicoExceptionsTestCase 34 extends TestCase { 35 36 final static public String MESSAGE = "Message of the exception"; 37 final static public Throwable THROWABLE = new Throwable (); 38 39 final void executeTestOfStandardException(final Class 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 exception = (Exception ) componentAdapter.getComponentInstance(pico); 45 assertEquals(MESSAGE, exception.getMessage()); 46 } catch (final UnsatisfiableDependenciesException ex) { 47 final Set set = new HashSet (); 48 for (final Iterator iter = ex.getUnsatisfiableDependencies().iterator(); iter.hasNext();) { 49 final List list = (List ) iter.next(); 50 set.addAll(list); 51 } 52 assertTrue(set.contains(Throwable .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 set = new HashSet (); 61 for (final Iterator iter = ex.getUnsatisfiableDependencies().iterator(); iter.hasNext();) { 62 final List list = (List ) iter.next(); 63 set.addAll(list); 64 } 65 assertTrue(set.contains(String .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 .class); 102 final Class [] classes = cdEx.getDependencies(); 103 assertEquals(2, classes.length); 104 assertSame(getClass(), classes[0]); 105 assertSame(String .class, classes[1]); 106 assertTrue(cdEx.getMessage().indexOf(getClass().getName()) >= 0); 107 } 108 109 public void testPrintStackTrace() throws IOException { 110 PicoException nestedException = new PicoException("Outer", new Exception ("Inner")) { 111 }; 112 PicoException simpleException = new PicoException("Outer") { 113 }; 114 ByteArrayOutputStream out = new ByteArrayOutputStream (); 115 PrintStream printStream = new PrintStream (out); 116 nestedException.printStackTrace(printStream); 117 simpleException.printStackTrace(printStream); 118 out.close(); 119 assertTrue(out.toString().indexOf("Caused by:") > 0); 120 out = new ByteArrayOutputStream (); 121 PrintWriter writer = new PrintWriter (out); 122 nestedException.printStackTrace(writer); 123 simpleException.printStackTrace(writer); 124 writer.flush(); 125 out.close(); 126 assertTrue(out.toString().indexOf("Caused by:") > 0); 127 } 129 } 130 | Popular Tags |