001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     * Original code by Joerg Schaible                                           *
009     *****************************************************************************/
010    package org.picocontainer.defaults;
011    
012    import java.io.ByteArrayOutputStream;
013    import java.io.IOException;
014    import java.io.PrintStream;
015    import java.io.PrintWriter;
016    import java.util.HashSet;
017    import java.util.Iterator;
018    import java.util.List;
019    import java.util.Set;
020    
021    import junit.framework.TestCase;
022    
023    import org.picocontainer.ComponentAdapter;
024    import org.picocontainer.PicoException;
025    import org.picocontainer.PicoInitializationException;
026    import org.picocontainer.PicoInstantiationException;
027    import org.picocontainer.PicoIntrospectionException;
028    import org.picocontainer.PicoRegistrationException;
029    
030    /**
031     * Unit tests for the several PicoException classes.
032     */
033    public class PicoExceptionsTestCase
034            extends TestCase {
035    
036        final static public String MESSAGE = "Message of the exception";
037        final static public Throwable THROWABLE = new Throwable();
038    
039        final void executeTestOfStandardException(final Class clazz) {
040            final ComponentAdapter componentAdapter = new ConstructorInjectionComponentAdapter(clazz, clazz, null, true, new DelegatingComponentMonitor());
041            DefaultPicoContainer pico = new DefaultPicoContainer();
042            pico.registerComponentInstance(MESSAGE);
043            try {
044                final Exception exception = (Exception) componentAdapter.getComponentInstance(pico);
045                assertEquals(MESSAGE, exception.getMessage());
046            } catch (final UnsatisfiableDependenciesException ex) {
047                final Set set = new HashSet();
048                for (final Iterator iter = ex.getUnsatisfiableDependencies().iterator(); iter.hasNext();) {
049                    final List list = (List) iter.next();
050                    set.addAll(list);
051                }
052                assertTrue(set.contains(Throwable.class));
053            }
054            pico = new DefaultPicoContainer();
055            pico.registerComponentInstance(THROWABLE);
056            try {
057                final PicoException exception = (PicoException) componentAdapter.getComponentInstance(pico);
058                assertSame(THROWABLE, exception.getCause());
059            } catch (final UnsatisfiableDependenciesException ex) {
060                final Set set = new HashSet();
061                for (final Iterator iter = ex.getUnsatisfiableDependencies().iterator(); iter.hasNext();) {
062                    final List list = (List) iter.next();
063                    set.addAll(list);
064                }
065                assertTrue(set.contains(String.class));
066            }
067            pico.registerComponentInstance(MESSAGE);
068            final PicoException exception = (PicoException) componentAdapter.getComponentInstance(pico);
069            assertEquals(MESSAGE, exception.getMessage());
070            assertSame(THROWABLE, exception.getCause());
071        }
072    
073        public void testPicoInitializationException() {
074            executeTestOfStandardException(PicoInitializationException.class);
075        }
076    
077        public void testPicoInitializationExceptionWithDefaultConstructor() {
078            TestException e = new TestException();
079            assertNull(e.getMessage());
080            assertNull(e.getCause());
081        }
082        
083        private static class TestException extends PicoInitializationException {
084            
085        }
086    
087        public void testPicoInstantiationException() {
088            executeTestOfStandardException(PicoInstantiationException.class);
089        }
090    
091        public void testPicoIntrospectionException() {
092            executeTestOfStandardException(PicoIntrospectionException.class);
093        }
094        
095        public void testPicoRegistrationException() {
096            executeTestOfStandardException(PicoRegistrationException.class);
097        }
098    
099        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            //simpleException.printStackTrace();
128        }
129    }