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 *****************************************************************************/ 010package org.picocontainer.defaults; 011 012import org.junit.Test; 013import org.picocontainer.ComponentAdapter; 014import org.picocontainer.DefaultPicoContainer; 015import org.picocontainer.PicoCompositionException; 016import org.picocontainer.PicoException; 017import org.picocontainer.injectors.AbstractInjector; 018import org.picocontainer.injectors.ConstructorInjector; 019import org.picocontainer.monitors.AbstractComponentMonitor; 020 021import java.io.ByteArrayOutputStream; 022import java.io.IOException; 023import java.io.PrintStream; 024import java.io.PrintWriter; 025import java.util.HashSet; 026import java.util.List; 027import java.util.Set; 028 029import static org.junit.Assert.assertEquals; 030import static org.junit.Assert.assertNull; 031import static org.junit.Assert.assertSame; 032import static org.junit.Assert.assertTrue; 033 034/** 035 * Unit tests for the several PicoException classes. 036 */ 037@SuppressWarnings("serial") 038public 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}