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 *****************************************************************************/ 009package org.picocontainer.injectors; 010 011import org.junit.Test; 012import org.picocontainer.ComponentAdapter; 013import org.picocontainer.DefaultPicoContainer; 014import org.picocontainer.MutablePicoContainer; 015import org.picocontainer.Parameter; 016import org.picocontainer.injectors.ConstructorInjector; 017import org.picocontainer.monitors.NullComponentMonitor; 018 019import static junit.framework.Assert.assertNotNull; 020import static org.junit.Assert.fail; 021 022public final class NonPublicConstructorsTestCase { 023 024 @Test 025 public void doFirstSampleWithNotPublicConstructor() { 026 MutablePicoContainer container = new DefaultPicoContainer(); 027 028 ComponentAdapter<DummyNotPublicConstructor> dummyComponentAdapter = 029 new ConstructorInjector<DummyNotPublicConstructor>( 030 DummyNotPublicConstructor.class, 031 DummyNotPublicConstructor.class, null, new NullComponentMonitor(), false) 032 .withNonPublicConstructors(); 033 034 container.addAdapter(dummyComponentAdapter); 035 036 DummyNotPublicConstructor dummy = container.getComponent(DummyNotPublicConstructor.class); 037 assertNotNull(dummy); 038 } 039 040 @Test 041 public void doSecondSampleWithNotPublicClass() { 042 MutablePicoContainer container = new DefaultPicoContainer(); 043 044 ComponentAdapter<DummyNotPublicClass> dummyComponentAdapter = 045 new ConstructorInjector<DummyNotPublicClass>( 046 DummyNotPublicClass.class.getCanonicalName(), 047 DummyNotPublicClass.class, null, new NullComponentMonitor(), false) 048 .withNonPublicConstructors(); 049 050 container.addAdapter(dummyComponentAdapter); 051 052 Object item = container.getComponent(DummyNotPublicClass.class); 053 assertNotNull(item); 054 } 055 056 @Test 057 public void doThirdSampleWithProtectedConstructor() { 058 MutablePicoContainer container = new DefaultPicoContainer(); 059 060 ComponentAdapter<DummyProtectedConstructor> dummyComponentAdapter = 061 new ConstructorInjector<DummyProtectedConstructor>( 062 DummyProtectedConstructor.class, 063 DummyProtectedConstructor.class, null, new NullComponentMonitor(), false) 064 .withNonPublicConstructors(); 065 066 067 container.addAdapter(dummyComponentAdapter); 068 069 DummyProtectedConstructor dummy = container.getComponent(DummyProtectedConstructor.class); 070 assertNotNull(dummy); 071 072 } 073 074 public static class DummyProtectedConstructor { 075 protected DummyProtectedConstructor() { 076 } 077 } 078 079 public static class DummyNotPublicConstructor { 080 DummyNotPublicConstructor() { 081 } 082 } 083 084 static class DummyNotPublicClass { 085 } 086 087}