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