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 org.picocontainer.ComponentAdapter;
013    import org.picocontainer.PicoException;
014    import org.picocontainer.PicoCompositionException;
015    import org.picocontainer.DefaultPicoContainer;
016    import org.picocontainer.lifecycle.NullLifecycleStrategy;
017    import org.picocontainer.injectors.ConstructorInjector;
018    import org.picocontainer.injectors.AbstractInjector;
019    import org.picocontainer.monitors.AbstractComponentMonitor;
020    
021    import java.io.ByteArrayOutputStream;
022    import java.io.IOException;
023    import java.io.PrintStream;
024    import java.io.PrintWriter;
025    import java.util.HashSet;
026    import java.util.List;
027    import java.util.Set;
028    
029    import junit.framework.TestCase;
030    
031    /**
032     * Unit tests for the several PicoException classes.
033     */
034    public class PicoExceptionsTestCase
035            extends TestCase {
036    
037        final static public String MESSAGE = "Message of the exception";
038        final static public Throwable THROWABLE = new Throwable();
039    
040        @SuppressWarnings({ "unchecked" })
041        final void executeTestOfStandardException(final Class clazz) {
042            final ComponentAdapter componentAdapter = new ConstructorInjector(clazz, clazz, null, new AbstractComponentMonitor(),
043                                                                              new NullLifecycleStrategy());
044            DefaultPicoContainer pico = new DefaultPicoContainer();
045            pico.addComponent(MESSAGE);
046            try {
047                final Exception exception = (Exception) componentAdapter.getComponentInstance(pico);
048                assertEquals(MESSAGE, exception.getMessage());
049            } catch (final AbstractInjector.UnsatisfiableDependenciesException ex) {
050                final Set<Object> set = new HashSet<Object>();
051                for (Object o : ex.getUnsatisfiableDependencies()) {
052                    final List<Object> list = (List<Object>)o;
053                    set.addAll(list);
054                }
055                assertTrue(set.contains(Throwable.class));
056            }
057            pico = new DefaultPicoContainer();
058            pico.addComponent(THROWABLE);
059            try {
060                final PicoException exception = (PicoException) componentAdapter.getComponentInstance(pico);
061                assertSame(THROWABLE, exception.getCause());
062            } catch (final AbstractInjector.UnsatisfiableDependenciesException ex) {
063                final Set<Object> set = new HashSet<Object>();
064                for (Object o : ex.getUnsatisfiableDependencies()) {
065                    final List<Object> list = (List<Object>)o;
066                    set.addAll(list);
067                }
068                assertTrue(set.contains(String.class));
069            }
070            pico.addComponent(MESSAGE);
071            final PicoException exception = (PicoException) componentAdapter.getComponentInstance(pico);
072            assertEquals(MESSAGE, exception.getMessage());
073            assertSame(THROWABLE, exception.getCause());
074        }
075    
076        public void testPicoInitializationException() {
077            executeTestOfStandardException(PicoCompositionException.class);
078        }
079    
080        public void testPicoInitializationExceptionWithDefaultConstructor() {
081            TestException e = new TestException(null);
082            assertNull(e.getMessage());
083            assertNull(e.getCause());
084        }
085    
086        private static class TestException extends PicoCompositionException {
087            public TestException(final String message) {
088                super(message);
089            }
090        }
091    
092        public void testPrintStackTrace() throws IOException {
093            PicoException nestedException = new PicoException("Outer", new Exception("Inner")) {
094            };
095            PicoException simpleException = new PicoException("Outer") {
096            };
097            ByteArrayOutputStream out = new ByteArrayOutputStream();
098            PrintStream printStream = new PrintStream(out);
099            nestedException.printStackTrace(printStream);
100            simpleException.printStackTrace(printStream);
101            out.close();
102            assertTrue(out.toString().indexOf("Caused by:") > 0);
103            out = new ByteArrayOutputStream();
104            PrintWriter writer = new PrintWriter(out);
105            nestedException.printStackTrace(writer);
106            simpleException.printStackTrace(writer);
107            writer.flush();
108            out.close();
109            assertTrue(out.toString().indexOf("Caused by:") > 0);
110            //simpleException.printStackTrace();
111        }
112    }