KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > security > unit > TimerRunAsTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.security.unit;
23
24 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
25 import javax.security.auth.login.Configuration JavaDoc;
26 import javax.security.auth.login.LoginContext JavaDoc;
27
28 import org.jboss.ejb3.test.security.TimerTester;
29 import org.jboss.logging.Logger;
30 import org.jboss.security.SecurityAssociation;
31 import org.jboss.security.SimplePrincipal;
32 import org.jboss.security.auth.callback.UsernamePasswordHandler;
33 import org.jboss.security.auth.login.XMLLoginConfigImpl;
34 import org.jboss.test.JBossTestCase;
35 import junit.framework.Test;
36
37 /**
38  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
39  * @version $Revision: 39361 $
40  */

41 public class TimerRunAsTestCase extends JBossTestCase
42 {
43    private static final Logger log = Logger
44    .getLogger(TimerRunAsTestCase.class);
45
46    static boolean deployed = false;
47    static int test = 0;
48
49    public TimerRunAsTestCase(String JavaDoc name)
50    {
51       super(name);
52    }
53    
54    public void testSecurityAssociation()
55    {
56       SecurityAssociation.clear();
57       SecurityAssociation.pushSubjectContext(null, new SimplePrincipal("bill"), "password".toCharArray());
58       assertEquals("bill", SecurityAssociation.getPrincipal().getName());
59       SecurityAssociation.popSubjectContext();
60       assertNull(SecurityAssociation.getPrincipal());
61    }
62    
63    public void testNoSecurityAssociationPrincipal() throws Exception JavaDoc
64    {
65       SecurityAssociation.clear();
66       
67       AppConfigurationEntry JavaDoc[] entries;
68       XMLLoginConfigImpl config = new XMLLoginConfigImpl();
69       config.setConfigResource("jaas-test-config.xml");
70       config.loadConfig();
71       Configuration.setConfiguration(config);
72       
73       entries = Configuration.getConfiguration().getAppConfigurationEntry("timer-runas-test");
74       assertEquals(1, entries.length);
75       
76       UsernamePasswordHandler handler = new UsernamePasswordHandler(null, null);
77       LoginContext JavaDoc lc = new LoginContext JavaDoc("timer-runas-test", handler);
78       
79       lc.login();
80       
81       TimerTester test = (TimerTester) getInitialContext().lookup("TimerTester");
82       assertNotNull(test);
83       
84       test.startTimer(5000);
85       Thread.sleep(6000);
86       assertTrue(test.isTimerCalled());
87       
88       lc.logout();
89    }
90
91    public static Test suite() throws Exception JavaDoc
92    {
93       return getDeploySetup(TimerRunAsTestCase.class, "timer-runas-security.jar");
94    }
95 }
Popular Tags