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 java.rmi.RemoteException;
26  
27  import javax.ejb.CreateException;
28  import javax.ejb.EJBException;
29  import javax.ejb.EJBHome;
30  import javax.ejb.SessionContext;
31  
32  import junit.framework.TestCase;
33  
34  /**
35   * @TODO documentation
36   * @author rlogiacco@smartlab.net
37   */
38  public class EnterpriseDomainTest extends TestCase {
39  
40  	/**
41  	 * The mocked EJB
42  	 */
43  	public static class Mock extends EnterpriseDomain {
44  	
45  		private static final long serialVersionUID = 1L;
46  	
47  		private String surname = "";
48  	
49  		public void ejbActivate() throws EJBException, RemoteException {
50  			System.out.println("ejbActivate()");
51  		}
52  	
53  		public void ejbPassivate() throws EJBException, RemoteException {
54  			System.out.println("ejbPassivate()");
55  		}
56  	
57  		public void ejbRemove() throws EJBException, RemoteException {
58  			System.out.println("ejbRemove()");
59  		}
60  		
61  		public void ejbCreate() throws CreateException, RemoteException {
62  			System.out.println("ejbCreate()");
63  		}
64  		
65  		public void ejbCreate(String surname) throws CreateException, RemoteException {
66  			System.out.println("ejbCreate(" + surname + ")");
67  			this.surname = surname;
68  		}
69  	
70  		public void setSessionContext(SessionContext context) throws EJBException, RemoteException {
71  			System.out.println("setSessionContext(context)");
72  		}
73  	
74  		public String father() {
75  			return "father" + surname;
76  		}
77  	
78  		public String mother() {
79  			return "mother" + surname;
80  		}
81  	
82  		public String brother(String name, int age) {
83  			return "brother(" + name + surname + ") - " + age;
84  		}
85  	
86  		public String sister(String name, int age) throws BusinessException {
87  			throw new BusinessException(name + surname);
88  		}
89  		
90  		/**
91  		 * The mocked EJB interface
92  		 */
93  		public static interface Interface extends EnterpriseDomain.Remote {
94  	
95  			public String father();
96  	
97  			public String mother();
98  	
99  			public String brother(String name, int age);
100 	
101 			public String sister(String name, int age) throws BusinessException;
102 		}
103 		
104 		public static interface Home extends EJBHome {
105 			public Interface create();
106 			
107 			public Interface create(String surname);
108 		}
109 	}
110 
111 
112 	private EnterpriseDomain.RequestContext context = new EnterpriseDomain.RequestContext();
113 
114 
115 	/**
116 	 * Test method for
117 	 * {@link net.smartlab.web.EnterpriseDomain#execute(java.lang.String, java.lang.Object[], java.lang.String[], net.smartlab.web.EnterpriseDomain.RequestContext)}
118 	 * .
119 	 */
120 	public void testExecute() throws Exception {
121 		EnterpriseDomain mock = new Mock();
122 		super.assertEquals("father", mock.execute("father", null, null, context));
123 		super.assertEquals("mother", mock.execute("mother", null, null, context));
124 		super.assertEquals("brother(paul) - 35", mock.execute("brother", new Object[] {"paul", new Integer(35)}, new String[] {"java.lang.String", "int"},
125 				context));
126 		try {
127 			mock.execute("sister", new Object[] {"claire", new Integer(7)}, new String[] {"java.lang.String", "int"}, context);
128 			super.fail();
129 		} catch (Exception e) {
130 			// Expected
131 		}
132 	}
133 }