KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > j2eeca > F_securityTest


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_securityTest.java,v 1.2 2004/03/19 11:57:19 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.j2eeca;
27
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.rmi.PortableRemoteObject JavaDoc;
31 import javax.transaction.UserTransaction JavaDoc;
32
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35
36 import org.objectweb.jonas.jtests.beans.j2eeca.securedCA;
37 import org.objectweb.jonas.jtests.beans.j2eeca.securedCAHome;
38 import org.objectweb.jonas.jtests.util.JTestCase;
39
40 /**
41  * Tests Jonas Connector Architecture container-managed sign-on.
42  */

43
44 public class F_securityTest extends JTestCase {
45
46     private static UserTransaction JavaDoc utx = null;
47
48     static Context JavaDoc ctx = null;
49
50     // Lookup bean home
51

52     protected static String JavaDoc BEAN_HOME = "securedCAHome";
53     protected static securedCAHome home = null;
54     private static final String JavaDoc RAR_JNDI_NAME = "FictionalSecurity";
55     static securedCA bean = null;
56     final public int CLOSE_HANDLE = 0;
57     final public int CLOSE_PHYSICAL = 1;
58     public int CloseType = 0;
59     public String JavaDoc UseBeans="j2eeca";
60
61     public F_securityTest(String JavaDoc name) {
62         super(name);
63     }
64
65     protected void setUp() {
66         super.setUp();
67         try {
68             // get JNDI initial context
69

70             if (ctx == null) {
71                 ctx = new InitialContext JavaDoc();
72             }
73
74             // We want to start transactions from client: get UserTransaction
75

76             if (utx == null) {
77                 utx = (UserTransaction JavaDoc) ctx.lookup("javax.transaction.UserTransaction");
78             }
79
80             if (home == null) {
81                 useBeans(UseBeans, false);
82             }
83             getBean();
84             assertTrue (5 == 5);
85
86         } catch (Exception JavaDoc e) {
87             fail("Cannot lookup UserTransaction in setUp: " +e);
88         }
89     }
90     private void getBean() throws Exception JavaDoc {
91         // Connecting to securedCAHome thru JNDI
92

93         if (home == null) {
94             home = (securedCAHome)PortableRemoteObject.narrow(
95                                 ctx.lookup(BEAN_HOME),
96                                 securedCAHome.class);
97             bean = home.create();
98         }
99     }
100     protected void tearDown() throws Exception JavaDoc {
101         bean.closeUp(CloseType);
102     }
103     protected void startUp(String JavaDoc testName) {
104         try {
105             bean.method1(RAR_JNDI_NAME, testName);
106         } catch (Exception JavaDoc ee) {
107             ee.printStackTrace();
108             System.exit(2);
109         }
110     }
111
112 // test list ****************************************************
113
/**
114      * Container managed sign-on.
115      *
116      * The application server provides the required security information for
117      * the resource principal through its configured security policies and mechanisms.
118      *
119      * When this class is deployed, the <code>secured.xml</code> file contains:
120      * <p>
121      * <code>&lt;res-auth&gt;Container&lt;/res-auth&gt;</code>
122      *
123      */

124     public void testSecurityContextContainer() throws Exception JavaDoc {
125         CloseType=CLOSE_PHYSICAL;
126         bean.setResAuth("Container");
127         startUp("testSecurityContextContainer");
128         //bean.setMatchNull(true);
129
String JavaDoc pw = bean.getSecurityPassword();
130         String JavaDoc u = bean.getSecurityUserName();
131         String JavaDoc realResAuth = bean.getResAuth();
132         if (realResAuth.equals("Container")) {
133             assertEquals("defaultPassword", pw);
134             assertEquals("defaultUserName", u);
135         } else {
136             assertFalse(pw.equals("defaultPassword"));
137         }
138     }
139 // end test list ****************************************************
140

141     public static Test suite() {
142         return new TestSuite(F_securityTest.class);
143     }
144
145     public static void main (String JavaDoc args[]) {
146
147         String JavaDoc testtorun = null;
148
149         // Get args
150

151         for (int argn = 0; argn < args.length; argn++) {
152
153             String JavaDoc s_arg = args[argn];
154             Integer JavaDoc i_arg;
155
156             if (s_arg.equals("-n")) {
157                 testtorun = args[++argn];
158             }
159         }
160
161         if (testtorun == null) {
162             junit.textui.TestRunner.run(suite());
163         } else {
164             junit.textui.TestRunner.run(new F_securityTest(testtorun));
165         }
166     }
167 }
168
Popular Tags