1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.web;
24  
25  import javax.naming.Context;
26  import javax.naming.InitialContext;
27  
28  import junit.framework.TestCase;
29  
30  import org.mockejb.MockContainer;
31  import org.mockejb.SessionBeanDescriptor;
32  import org.mockejb.jndi.MockContextFactory;
33  
34  /**
35   * @author rlogiacco@smartlab.net
36   */
37  public class EnterpriseDomainBuilderTest extends TestCase {
38  
39  	private Context context;
40  
41  
42  	/**
43  	 * @see junit.framework.TestCase#setUp()
44  	 */
45  	protected void setUp() throws Exception {
46  		MockContextFactory.setAsInitial();
47  		this.context = new InitialContext();
48  		MockContainer mockContainer = new MockContainer(context);
49  		SessionBeanDescriptor mock = new SessionBeanDescriptor("mock", EnterpriseDomainTest.Mock.Home.class, EnterpriseDomainTest.Mock.Interface.class, new EnterpriseDomainTest.Mock());
50  		// creates Home and binds it to JNDI
51  		mockContainer.deploy(mock);
52  	}
53  
54  	/**
55  	 * Test method for
56  	 * {@link net.smartlab.web.EnterpriseDomainBuilder#getInstance(java.lang.String, java.lang.Class)}
57  	 * .
58  	 */
59  	public void testGetInstanceStringClass() {
60  		Object object = EnterpriseDomainBuilder.getInstance("mock", EnterpriseDomainTest.Mock.Interface.class);
61  		super.assertNotNull(object);
62  		super.assertTrue(object instanceof EnterpriseDomainTest.Mock.Interface);
63  		super.assertEquals("father", ((EnterpriseDomainTest.Mock.Interface)object).father());
64  		super.assertEquals("mother", ((EnterpriseDomainTest.Mock.Interface)object).mother());
65  		super.assertEquals("brother(paul) - 35", ((EnterpriseDomainTest.Mock.Interface)object).brother("paul", 35));
66  		try {
67  			((EnterpriseDomainTest.Mock.Interface)object).sister("claire", 7);
68  			super.fail("exception expected");
69  		} catch (Exception e) {
70  			super.assertEquals(BusinessException.class, e.getClass());
71  			super.assertEquals("claire", e.getMessage());
72  		}
73  	}
74  
75  	/**
76  	 * Test method for
77  	 * {@link net.smartlab.web.EnterpriseDomainBuilder#getInstance(java.lang.String, java.lang.Class, javax.naming.Context)}
78  	 * .
79  	 */
80  	public void testGetInstanceStringClassContext() {
81  		Object object = EnterpriseDomainBuilder.getInstance("mock", EnterpriseDomainTest.Mock.Interface.class, context);
82  		super.assertNotNull(object);
83  		super.assertTrue(object instanceof EnterpriseDomainTest.Mock.Interface);
84  		super.assertEquals("father", ((EnterpriseDomainTest.Mock.Interface)object).father());
85  		super.assertEquals("mother", ((EnterpriseDomainTest.Mock.Interface)object).mother());
86  		super.assertEquals("brother(paul) - 35", ((EnterpriseDomainTest.Mock.Interface)object).brother("paul", 35));
87  		try {
88  			((EnterpriseDomainTest.Mock.Interface)object).sister("claire", 7);
89  			super.fail("exception expected");
90  		} catch (Exception e) {
91  			super.assertEquals(BusinessException.class, e.getClass());
92  			super.assertEquals("claire", e.getMessage());
93  		}
94  	}
95  
96  	/**
97  	 * Test method for
98  	 * {@link net.smartlab.web.EnterpriseDomainBuilder#getInstance(java.lang.String, java.lang.Class, javax.naming.Context, java.lang.Object[])}
99  	 * .
100 	 */
101 	public void testGetInstanceStringClassContextObjectArray() {
102 		Object object = EnterpriseDomainBuilder.getInstance("mock", EnterpriseDomainTest.Mock.Interface.class, context, new Object[] {"family"});
103 		super.assertNotNull(object);
104 		super.assertTrue(object instanceof EnterpriseDomainTest.Mock.Interface);
105 		super.assertEquals("fatherfamily", ((EnterpriseDomainTest.Mock.Interface)object).father());
106 		super.assertEquals("motherfamily", ((EnterpriseDomainTest.Mock.Interface)object).mother());
107 		super.assertEquals("brother(paulfamily) - 35", ((EnterpriseDomainTest.Mock.Interface)object).brother("paul", 35));
108 		try {
109 			((EnterpriseDomainTest.Mock.Interface)object).sister("claire", 7);
110 			super.fail("exception expected");
111 		} catch (Exception e) {
112 			super.assertEquals(BusinessException.class, e.getClass());
113 			super.assertEquals("clairefamily", e.getMessage());
114 		}
115 	}
116 }