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