001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022:
023: package org.jboss.ejb3.test.jacc.unit;
024:
025: import javax.ejb.EJBAccessException;
026: import javax.ejb.EJBException;
027: import javax.transaction.RollbackException;
028:
029: import org.jboss.ejb3.test.jacc.AllEntity;
030: import org.jboss.ejb3.test.jacc.SomeEntity;
031: import org.jboss.ejb3.test.jacc.StarEntity;
032: import org.jboss.ejb3.test.jacc.Stateful;
033: import org.jboss.ejb3.test.jacc.Stateless;
034: import org.jboss.security.SecurityAssociation;
035: import org.jboss.security.SimplePrincipal;
036: import org.jboss.test.JBossTestCase;
037: import junit.framework.Test;
038:
039: /**
040: *
041: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
042: * @version $Revision: 61026 $
043: */
044: public class JaccTestCase extends JBossTestCase {
045: org.apache.log4j.Logger log = getLog();
046:
047: static boolean deployed = false;
048: static int test = 0;
049:
050: public JaccTestCase(String name) {
051: super (name);
052: }
053:
054: public void testUnchecked() throws Exception {
055: Stateful stateful = (Stateful) getInitialContext().lookup(
056: "StatefulBean/remote");
057: Stateless stateless = (Stateless) getInitialContext().lookup(
058: "StatelessBean/remote");
059:
060: SecurityAssociation
061: .setPrincipal(new SimplePrincipal("somebody"));
062: SecurityAssociation.setCredential("password".toCharArray());
063:
064: int result = stateful.unchecked(1);
065: assertEquals(1, result);
066:
067: result = stateless.unchecked(10);
068: assertEquals(10, result);
069:
070: SecurityAssociation
071: .setPrincipal(new SimplePrincipal("rolefail"));
072: SecurityAssociation.setCredential("password".toCharArray());
073:
074: result = stateful.unchecked(100);
075: assertEquals(100, result);
076:
077: result = stateless.unchecked(99);
078: assertEquals(99, result);
079:
080: }
081:
082: public void testChecked() throws Exception {
083: Stateful stateful = (Stateful) getInitialContext().lookup(
084: "StatefulBean/remote");
085: Stateless stateless = (Stateless) getInitialContext().lookup(
086: "StatelessBean/remote");
087:
088: SecurityAssociation
089: .setPrincipal(new SimplePrincipal("somebody"));
090: SecurityAssociation.setCredential("password".toCharArray());
091:
092: int result = stateful.checked(5);
093: assertEquals(5, result);
094:
095: result = stateless.checked(50);
096: assertEquals(50, result);
097:
098: SecurityAssociation
099: .setPrincipal(new SimplePrincipal("rolefail"));
100: SecurityAssociation.setCredential("password".toCharArray());
101:
102: boolean securityException = false;
103: try {
104: stateful.checked(500);
105: } catch (EJBAccessException e) {
106: securityException = true;
107: }
108:
109: assertTrue(securityException);
110:
111: try {
112: stateless.checked(501);
113: } catch (EJBAccessException e) {
114: securityException = true;
115: }
116:
117: assertTrue(securityException);
118: }
119:
120: public void testAllEntity() throws Exception {
121: Stateless stateless = (Stateless) getInitialContext().lookup(
122: "StatelessBean/remote");
123: SecurityAssociation
124: .setPrincipal(new SimplePrincipal("somebody"));
125: SecurityAssociation.setCredential("password".toCharArray());
126:
127: System.out.println("Good role");
128: System.out.println("Inserting...");
129: AllEntity e = stateless.insertAllEntity();
130: System.out.println("Reading...");
131: e = stateless.readAllEntity(e.id);
132: e.val += "y";
133: System.out.println("Updating...");
134: stateless.updateAllEntity(e);
135: System.out.println("Deleting...");
136: stateless.deleteAllEntity(e);
137: System.out.println("Inserting...");
138: e = stateless.insertAllEntity();
139:
140: System.out.println("Bad role");
141: SecurityAssociation
142: .setPrincipal(new SimplePrincipal("rolefail"));
143: SecurityAssociation.setCredential("password".toCharArray());
144:
145: AllEntity ae2 = null;
146: try {
147: System.out.println("Inserting...");
148: ae2 = stateless.insertAllEntity();
149: throw new FailedException(
150: "Insert check not done for AllEntity");
151: } catch (FailedException ex) {
152: throw ex;
153: } catch (Exception ex) {
154: hasSecurityOrEJBException(ex);
155: }
156:
157: try {
158: System.out.println("Reading...");
159: ae2 = stateless.readAllEntity(e.id);
160: throw new FailedException(
161: "Read check not done for AllEntity");
162: } catch (FailedException ex) {
163: throw ex;
164: } catch (Exception ex) {
165: hasSecurityOrEJBException(ex);
166: }
167:
168: try {
169: e.val += "y";
170: stateless.updateAllEntity(e);
171: throw new FailedException(
172: "Update check not done for AllEntity");
173: } catch (FailedException ex) {
174: throw ex;
175: } catch (Exception ex) {
176: hasSecurityOrEJBException(ex);
177: }
178:
179: try {
180: stateless.deleteAllEntity(e);
181: throw new FailedException(
182: "Delete check not done for AllEntity");
183: } catch (FailedException ex) {
184: throw ex;
185: } catch (Exception ex) {
186: hasSecurityOrEJBException(ex);
187: }
188:
189: try {
190: e = stateless.insertAllEntity();
191: throw new FailedException(
192: "Insert check not done for AllEntity");
193: } catch (FailedException ex) {
194: throw ex;
195: } catch (Exception ex) {
196: hasSecurityOrEJBException(ex);
197: }
198: }
199:
200: public void testStarEntity() throws Exception {
201: Stateless stateless = (Stateless) getInitialContext().lookup(
202: "StatelessBean/remote");
203: SecurityAssociation
204: .setPrincipal(new SimplePrincipal("somebody"));
205: SecurityAssociation.setCredential("password".toCharArray());
206:
207: System.out.println("Good role");
208: System.out.println("Inserting...");
209: StarEntity e = stateless.insertStarEntity();
210: System.out.println("Reading...");
211: e = stateless.readStarEntity(e.id);
212: e.val += "y";
213: System.out.println("Updating...");
214: stateless.updateStarEntity(e);
215: System.out.println("Deleting...");
216: stateless.deleteStarEntity(e);
217: System.out.println("Inserting...");
218: e = stateless.insertStarEntity();
219:
220: System.out.println("Bad role");
221: SecurityAssociation
222: .setPrincipal(new SimplePrincipal("rolefail"));
223: SecurityAssociation.setCredential("password".toCharArray());
224:
225: StarEntity ae2 = null;
226: try {
227: System.out.println("Inserting...");
228: ae2 = stateless.insertStarEntity();
229: throw new FailedException(
230: "Insert check not done for StarEntity");
231: } catch (FailedException ex) {
232: throw ex;
233: } catch (Exception ex) {
234: hasSecurityOrEJBException(ex);
235: }
236:
237: try {
238: System.out.println("Reading...");
239: ae2 = stateless.readStarEntity(e.id);
240: throw new FailedException(
241: "Read check not done for StarEntity");
242: } catch (FailedException ex) {
243: throw ex;
244: } catch (Exception ex) {
245: hasSecurityOrEJBException(ex);
246: }
247:
248: try {
249: e.val += "y";
250: stateless.updateStarEntity(e);
251: throw new FailedException(
252: "Update check not done for StarEntity");
253: } catch (FailedException ex) {
254: throw ex;
255: } catch (Exception ex) {
256: hasSecurityOrEJBException(ex);
257: }
258:
259: try {
260: stateless.deleteStarEntity(e);
261: throw new FailedException(
262: "Delete check not done for StarEntity");
263: } catch (FailedException ex) {
264: throw ex;
265: } catch (Exception ex) {
266: hasSecurityOrEJBException(ex);
267: }
268:
269: try {
270: e = stateless.insertStarEntity();
271: throw new FailedException(
272: "Insert check not done for StarEntity");
273: } catch (FailedException ex) {
274: throw ex;
275: } catch (Exception ex) {
276: hasSecurityOrEJBException(ex);
277: }
278: }
279:
280: public void testSomeEntity() throws Exception {
281: Stateless stateless = (Stateless) getInitialContext().lookup(
282: "StatelessBean/remote");
283: SecurityAssociation
284: .setPrincipal(new SimplePrincipal("somebody"));
285: SecurityAssociation.setCredential("password".toCharArray());
286:
287: System.out.println("Good role");
288: System.out.println("Inserting...");
289: SomeEntity e = stateless.insertSomeEntity();
290:
291: try {
292: System.out.println("Reading...");
293: e = stateless.readSomeEntity(e.id);
294: throw new FailedException(
295: "Read check not done for SomeEntity");
296: } catch (FailedException ex) {
297: throw ex;
298: } catch (Exception ex) {
299: hasSecurityOrEJBException(ex);
300: }
301:
302: try {
303: e.val += "y";
304: System.out.println("Updating...");
305: stateless.updateSomeEntity(e);
306: throw new FailedException(
307: "Update check not done for SomeEntity");
308: } catch (FailedException ex) {
309: throw ex;
310: } catch (Exception ex) {
311: hasSecurityOrEJBException(ex);
312: }
313:
314: System.out.println("Deleting...");
315: stateless.deleteSomeEntity(e);
316: System.out.println("Inserting...");
317: e = stateless.insertSomeEntity();
318:
319: System.out.println("Bad role");
320: SecurityAssociation
321: .setPrincipal(new SimplePrincipal("rolefail"));
322: SecurityAssociation.setCredential("password".toCharArray());
323:
324: SomeEntity ae2 = null;
325: try {
326: System.out.println("Inserting...");
327: ae2 = stateless.insertSomeEntity();
328: throw new FailedException(
329: "Insert check not done for SomeEntity");
330: } catch (FailedException ex) {
331: throw ex;
332: } catch (Exception ex) {
333: hasSecurityOrEJBException(ex);
334: }
335:
336: try {
337: stateless.deleteSomeEntity(e);
338: throw new FailedException(
339: "Delete check not done for SomeEntity");
340: } catch (FailedException ex) {
341: throw ex;
342: } catch (Exception ex) {
343: if (ex.getCause() != null
344: && ex.getCause() instanceof RollbackException) {
345: System.err.println("EJBTHREE-894");
346: } else
347: hasSecurityOrEJBException(ex);
348: }
349:
350: try {
351: e = stateless.insertSomeEntity();
352: throw new FailedException(
353: "Insert check not done for SomeEntity");
354: } catch (FailedException ex) {
355: throw ex;
356: } catch (Exception ex) {
357: hasSecurityOrEJBException(ex);
358: }
359: }
360:
361: private void hasSecurityOrEJBException(Exception e)
362: throws FailedException {
363: Throwable t = e;
364:
365: while (t != null) {
366: //System.out.println(t);
367: String classname = t.getClass().getName();
368: if (classname.equals(EJBAccessException.class.getName())
369: || classname.equals(EJBException.class.getName())) {
370: return;
371: }
372: t = t.getCause();
373: }
374:
375: throw new FailedException("EJBAccessException not thrown", e);
376: }
377:
378: public static Test suite() throws Exception {
379: return getDeploySetup(JaccTestCase.class, "jacc-test.jar");
380: }
381:
382: private class FailedException extends Exception {
383: public FailedException(String msg) {
384: super (msg);
385: }
386:
387: public FailedException(String msg, Throwable t) {
388: super(msg, t);
389: }
390: }
391: }
|