001/* 002 * jDTAUS Core RI Client Container 003 * Copyright (C) 2005 Christian Schulte 004 * <cs@schulte.it> 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License, or any later version. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 019 * 020 */ 021package org.jdtaus.core.container.ri.client.test; 022 023import junit.framework.Assert; 024import junit.framework.TestCase; 025import org.jdtaus.core.container.Container; 026import org.jdtaus.core.container.ContainerFactory; 027import org.jdtaus.core.container.DependencyCycleException; 028 029/** 030 * Tests the {@code DefaultContainer} implementation. 031 * 032 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 033 * @version $JDTAUS: DefaultContainerTest.java 8709 2012-10-02 21:07:40Z schulte $ 034 */ 035public class DefaultContainerTest extends TestCase 036{ 037 038 private static final String MODULE_NAME = 039 "jDTAUS Core ⁑ RI Client Container"; 040 041 public Container getContainer() 042 { 043 return ContainerFactory.getContainer(); 044 } 045 046 /** 047 * Tests the {@link Container#getObject(Class, String)} method to return the 048 * correctly initialized instance of {@link TestImplementation}. 049 */ 050 public void testInstantiation() throws Exception 051 { 052 assert this.getContainer() != null; 053 054 final TestSpecification spec = 055 (TestSpecification) this.getContainer(). 056 getObject( TestSpecification.class.getName(), MODULE_NAME ); 057 058 Assert.assertTrue( spec.isInitialized() ); 059 } 060 061 /** 062 * Tests the {@link Container#getObject(Class, String)} method to return the 063 * same instance of {@link TestImplementation} for successive calls. 064 */ 065 public void testSingleton() throws Exception 066 { 067 assert this.getContainer() != null; 068 069 final TestSpecification spec1 = 070 (TestSpecification) this.getContainer(). 071 getObject( TestSpecification.class.getName(), MODULE_NAME ); 072 073 final TestSpecification spec2 = 074 (TestSpecification) this.getContainer(). 075 getObject( TestSpecification.class.getName(), MODULE_NAME ); 076 077 final TestSpecification spec3 = 078 (TestSpecification) this.getContainer(). 079 getObject( TestSpecification.class.getName(), MODULE_NAME ); 080 081 final TestSpecification child = 082 (TestSpecification) this.getContainer(). 083 getObject( TestSpecification.class.getName(), 084 MODULE_NAME + " - Child" ); 085 086 Assert.assertTrue( spec1 == spec2 ); 087 Assert.assertTrue( spec2 == spec3 ); 088 Assert.assertTrue( spec3 == spec1 ); 089 Assert.assertFalse( child == spec1 ); 090 Assert.assertFalse( child == spec2 ); 091 Assert.assertFalse( child == spec3 ); 092 } 093 094 /** 095 * Tests that model properties are correctly provided to implementations. 096 */ 097 public void testProperties() 098 { 099 final TestSpecification spec1 = 100 (TestSpecification) this.getContainer(). 101 getObject( TestSpecification.class.getName(), MODULE_NAME ); 102 103 final MultitonSpecification child = 104 (MultitonSpecification) this.getContainer(). 105 getObject( MultitonSpecification.class.getName(), 106 MODULE_NAME + " - Child" ); 107 108 this.assertValidProperties( spec1 ); 109 this.assertValidProperties( child ); 110 this.assertValidProperties( spec1.getDependency() ); 111 this.assertValidProperties( child.getDependency() ); 112 } 113 114 /** 115 * Checks the given {@code TestSpecification} to provide the correct 116 * property values. 117 */ 118 private void assertValidProperties( final TestSpecification s ) 119 { 120 Assert.assertEquals( s.isBoolean(), true ); 121 Assert.assertEquals( s.getByte(), (byte) 1 ); 122 Assert.assertEquals( s.getChar(), 'X' ); 123 Assert.assertTrue( s.getDouble() == 0.1D ); 124 Assert.assertTrue( s.getFloat() == 0.1F ); 125 Assert.assertEquals( s.getInt(), 1 ); 126 Assert.assertEquals( s.getLong(), 1L ); 127 Assert.assertEquals( s.getShort(), (short) 1 ); 128 Assert.assertEquals( s.isBooleanObject(), Boolean.TRUE ); 129 Assert.assertEquals( s.getByteObject(), new Byte( (byte) 1 ) ); 130 Assert.assertEquals( s.getCharacterObject(), new Character( 'X' ) ); 131 Assert.assertEquals( s.getDoubleObject(), new Double( 0.1D ) ); 132 Assert.assertEquals( s.getFloatObject(), new Float( 0.1F ) ); 133 Assert.assertEquals( s.getIntegerObject(), new Integer( 1 ) ); 134 Assert.assertEquals( s.getLongObject(), new Long( 1L ) ); 135 Assert.assertEquals( s.getShortObject(), new Short( (short) 1 ) ); 136 Assert.assertEquals( s.getStringObject(), "TEST" ); 137 } 138 139 /** 140 * Checks that dependency cycles are detected correctly by throwing a 141 * corresponding {@link DependencyCycleException}. 142 */ 143 public void testDependencyCycleDetection() throws Exception 144 { 145 try 146 { 147 this.getContainer().getObject( 148 CycleTestSpecification.class.getName(), MODULE_NAME + 149 " - Cycle Test" ); 150 151 Assert.fail( "Dependency cycle not detected." ); 152 } 153 catch ( DependencyCycleException e ) 154 { 155 Assert.assertNotNull( e.getMessage() ); 156 System.out.println( e.toString() ); 157 } 158 } 159 160 /** 161 * Tests the {@link Container#getObject(Class, String)} method to corectly 162 * detect non-serializable implementation classes in context scope. 163 */ 164 public void testSerializable() throws Exception 165 { 166 assert this.getContainer() != null; 167 168 Assert.assertNotNull( this.getContainer().getObject( 169 ContextTestSpecification.class.getName(), MODULE_NAME ) ); 170 171 } 172 173 /** 174 * Tests the {@link Container#getObject(Class, String)} method to return an 175 * array of correctly initialized instances of {@link TestImplementation} 176 * when given a {@code null} value for the implementation name argument. 177 */ 178 public void testInstantiationNullImplementationName() throws Exception 179 { 180 assert this.getContainer() != null; 181 182 final TestSpecification[] spec = 183 (TestSpecification[]) this.getContainer(). 184 getObject( TestSpecification.class.getName(), null ); 185 186 for ( int i = spec.length - 1; i >= 0; i-- ) 187 { 188 Assert.assertTrue( spec[i].isInitialized() ); 189 } 190 } 191 192}