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 Exception exception = (Exception) componentAdapter.getComponentInstance(pico, ComponentAdapter.NOTHING.class);
049 assertEquals(MESSAGE, exception.getMessage());
050 pico = new DefaultPicoContainer();
051 pico.addComponent(THROWABLE);
052 exception = (PicoException) componentAdapter.getComponentInstance(pico, ComponentAdapter.NOTHING.class);
053 assertSame(THROWABLE, exception.getCause());
054 pico.addComponent(MESSAGE);
055 exception = (PicoException) componentAdapter.getComponentInstance(pico, ComponentAdapter.NOTHING.class);
056 assertEquals(MESSAGE, exception.getMessage());
057 assertSame(THROWABLE, exception.getCause());
058 }
059
060 @Test public void testPicoInitializationException() {
061 executeTestOfStandardException(PicoCompositionException.class);
062 }
063
064 @Test public void testPicoInitializationExceptionWithDefaultConstructor() {
065 TestException e = new TestException(null);
066 assertNull(e.getMessage());
067 assertNull(e.getCause());
068 }
069
070 private static class TestException extends PicoCompositionException {
071 public TestException(final String message) {
072 super(message);
073 }
074 }
075
076 @Test public void testPrintStackTrace() throws IOException {
077 PicoException nestedException = new PicoException("Outer", new Exception("Inner")) {
078 };
079 PicoException simpleException = new PicoException("Outer") {
080 };
081 ByteArrayOutputStream out = new ByteArrayOutputStream();
082 PrintStream printStream = new PrintStream(out);
083 nestedException.printStackTrace(printStream);
084 simpleException.printStackTrace(printStream);
085 out.close();
086 assertTrue(out.toString().indexOf("Caused by:") > 0);
087 out = new ByteArrayOutputStream();
088 PrintWriter writer = new PrintWriter(out);
089 nestedException.printStackTrace(writer);
090 simpleException.printStackTrace(writer);
091 writer.flush();
092 out.close();
093 assertTrue(out.toString().indexOf("Caused by:") > 0);
094 //simpleException.printStackTrace();
095 }
096 }