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(),
047 new NullLifecycleStrategy(), false, false);
048 DefaultPicoContainer pico = new DefaultPicoContainer();
049 pico.addComponent(MESSAGE);
050 try {
051 final Exception exception = (Exception) componentAdapter.getComponentInstance(pico, ComponentAdapter.NOTHING.class);
052 assertEquals(MESSAGE, exception.getMessage());
053 } catch (final AbstractInjector.UnsatisfiableDependenciesException ex) {
054 final Set<Object> set = new HashSet<Object>();
055 for (Object o : ex.getUnsatisfiableDependencies()) {
056 final List<Object> list = (List<Object>)o;
057 set.addAll(list);
058 }
059 assertTrue(set.contains(Throwable.class));
060 }
061 pico = new DefaultPicoContainer();
062 pico.addComponent(THROWABLE);
063 try {
064 final PicoException exception = (PicoException) componentAdapter.getComponentInstance(pico, ComponentAdapter.NOTHING.class);
065 assertSame(THROWABLE, exception.getCause());
066 } catch (final AbstractInjector.UnsatisfiableDependenciesException ex) {
067 final Set<Object> set = new HashSet<Object>();
068 for (Object o : ex.getUnsatisfiableDependencies()) {
069 final List<Object> list = (List<Object>)o;
070 set.addAll(list);
071 }
072 assertTrue(set.contains(String.class));
073 }
074 pico.addComponent(MESSAGE);
075 final PicoException exception = (PicoException) componentAdapter.getComponentInstance(pico, ComponentAdapter.NOTHING.class);
076 assertEquals(MESSAGE, exception.getMessage());
077 assertSame(THROWABLE, exception.getCause());
078 }
079
080 @Test public void testPicoInitializationException() {
081 executeTestOfStandardException(PicoCompositionException.class);
082 }
083
084 @Test public void testPicoInitializationExceptionWithDefaultConstructor() {
085 TestException e = new TestException(null);
086 assertNull(e.getMessage());
087 assertNull(e.getCause());
088 }
089
090 private static class TestException extends PicoCompositionException {
091 public TestException(final String message) {
092 super(message);
093 }
094 }
095
096 @Test public void testPrintStackTrace() throws IOException {
097 PicoException nestedException = new PicoException("Outer", new Exception("Inner")) {
098 };
099 PicoException simpleException = new PicoException("Outer") {
100 };
101 ByteArrayOutputStream out = new ByteArrayOutputStream();
102 PrintStream printStream = new PrintStream(out);
103 nestedException.printStackTrace(printStream);
104 simpleException.printStackTrace(printStream);
105 out.close();
106 assertTrue(out.toString().indexOf("Caused by:") > 0);
107 out = new ByteArrayOutputStream();
108 PrintWriter writer = new PrintWriter(out);
109 nestedException.printStackTrace(writer);
110 simpleException.printStackTrace(writer);
111 writer.flush();
112 out.close();
113 assertTrue(out.toString().indexOf("Caused by:") > 0);
114 //simpleException.printStackTrace();
115 }
116 }