KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > testcontainer > AbstractTest


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.testcontainer;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.junit.AfterClass;
31 import org.junit.Test;
32
33 import com.sslexplorer.boot.Context;
34 import com.sslexplorer.core.UserDatabaseManager;
35 import com.sslexplorer.policyframework.Policy;
36 import com.sslexplorer.policyframework.PolicyDatabase;
37 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
38 import com.sslexplorer.policyframework.PolicyUtil;
39 import com.sslexplorer.properties.Property;
40 import com.sslexplorer.properties.impl.realms.RealmKey;
41 import com.sslexplorer.realms.Realm;
42 import com.sslexplorer.security.Role;
43 import com.sslexplorer.security.SessionInfo;
44 import com.sslexplorer.security.User;
45 import com.sslexplorer.security.UserDatabase;
46
47 /**
48  */

49 public abstract class AbstractTest{
50     private static final String JavaDoc TEST_PROPERTY = "sslexplorer.testing";
51     private static final String JavaDoc DEV_CONFIG_PROPERTY = "sslexplorer.useDevConfig";
52     private static final String JavaDoc DEV_EXTENSIONS_PROPERTY = "sslexplorer.devExtensions";
53     private static final String JavaDoc DATABASE_TYPE = "builtIn";
54     protected static String JavaDoc USERNAME = "testAdministrator";
55     protected static String JavaDoc PASSWORD = "newPassword";
56     private static Context context_;
57
58     /**
59      * @param pluginName
60      * @throws Exception
61      */

62     public static void setUp(String JavaDoc pluginName) throws Exception JavaDoc{
63         deleteDatabase();
64         System.setProperty(TEST_PROPERTY, "true");
65         System.setProperty(DEV_CONFIG_PROPERTY, "true");
66         System.setProperty(DEV_EXTENSIONS_PROPERTY, pluginName);
67         context_ = TestContext.getTestContext();
68         createSuperUser();
69         assertPolicies();
70         // is there a super user and can login?
71
PolicyUtil.checkLogin(getAccount(USERNAME));
72     }
73
74     private static void createSuperUser() throws Exception JavaDoc {
75         UserDatabase userDatabase = getUserService().createUserDatabase(DATABASE_TYPE, UserDatabaseManager.DEFAULT_REALM_NAME, UserDatabaseManager.DEFAULT_REALM_DESCRIPTION, true);
76         User user = userDatabase.createAccount(USERNAME, "", "", "", new Role[] {});
77         userDatabase.changePassword(user.getPrincipalName(), "", PASSWORD, false);
78
79         Property.setProperty(new RealmKey("security.userDatabase", userDatabase.getRealm()), DATABASE_TYPE, getSessionInfo());
80         Property.setProperty(new RealmKey("security.administrators", userDatabase.getRealm()), USERNAME, getSessionInfo());
81     }
82
83     private static void assertPolicies() throws Exception JavaDoc {
84         List JavaDoc<Policy> policies = PolicyDatabaseFactory.getInstance().getPolicies();
85         assertEquals("There should be only one policy", policies.size(), 1);
86         Policy policy = policies.get(0);
87         assertEquals("The policy should be called 'Everyone'", policy.getResourceName(), "Everyone");
88         assertEquals("The policy id should be '0'", policy.getResourceId(), 0);
89     }
90     
91     /**
92      *
93      */

94     @AfterClass
95     public static void oneTimeTearDown() {
96         if (context_ != null){
97             context_.shutdown(false);
98         }
99         deleteDatabase();
100     }
101
102     /**
103      * This can be called from anywhere as long as the TestContext has been initialised.
104      */

105     private static void deleteDatabase() {
106         for (File JavaDoc file : TestContext.DB_DIR.listFiles()) {
107             file.delete();
108         }
109     }
110
111     /**
112      * @return SessionInfo
113      * @throws Exception
114      */

115     protected static SessionInfo getSessionInfo() throws Exception JavaDoc {
116         User account = getAccount(USERNAME);
117         // the super user should exist, the name used is the name use
118
return SessionInfo.nextSession(null, "testPolicyAdmin", account, InetAddress.getLocalHost(), SessionInfo.ALL_CONTEXTS, "");
119     }
120     
121     /**
122      * @return UserDatabase
123      * @throws Exception
124      */

125     public static UserDatabase getDefaultUserDatabase() throws Exception JavaDoc {
126         return getUserService().getDefaultUserDatabase();
127     }
128     
129     /**
130      * @return Realm
131      * @throws Exception
132      */

133     public static Realm getDefaultRealm() throws Exception JavaDoc {
134         return getUserService().getDefaultRealm();
135     }
136     
137     /**
138      * @return UserDatabaseManager
139      * @throws Exception
140      */

141     public static UserDatabaseManager getUserService() throws Exception JavaDoc {
142         return UserDatabaseManager.getInstance();
143     }
144     
145     /**
146      * @param username
147      * @return User
148      * @throws Exception
149      */

150     public static User getAccount(String JavaDoc username) throws Exception JavaDoc {
151         return getDefaultUserDatabase().getAccount(username);
152     }
153     
154     /**
155      * @return User
156      * @throws Exception
157      */

158     public static User createAccount() throws Exception JavaDoc {
159         return createAccount("username", "password", "username@company.com", "Full Name");
160     }
161     
162     /**
163      * @param username
164      * @param password
165      * @param email
166      * @param fullname
167      * @return User
168      * @throws Exception
169      */

170     public static User createAccount(String JavaDoc username, String JavaDoc password, String JavaDoc email, String JavaDoc fullname) throws Exception JavaDoc {
171         return getDefaultUserDatabase().createAccount(username, password, email, fullname, new Role[] {});
172     }
173
174     /**
175      * @param user
176      * @param roles
177      * @return User
178      * @throws Exception
179      */

180     public static User updateAccountRoles (User user, Collection JavaDoc<Role> roles) throws Exception JavaDoc {
181         return updateAccount(user, user.getEmail(), user.getFullname(), roles.toArray(new Role[roles.size()]));
182     }
183     
184     /**
185      * @param user
186      * @param email
187      * @param fullname
188      * @param roles
189      * @return User
190      * @throws Exception
191      */

192     public static User updateAccount(User user, String JavaDoc email, String JavaDoc fullname, Role[] roles) throws Exception JavaDoc {
193         getDefaultUserDatabase().updateAccount(user, email, fullname, roles);
194         return getAccount(user.getPrincipalName());
195     }
196
197     /**
198      * @param users
199      * @throws Exception
200      */

201     public static void deleteAccount(User... users) throws Exception JavaDoc {
202         for (User user : users) {
203             getDefaultUserDatabase().deleteAccount(user);
204         }
205     }
206
207     /**
208      * @return Role
209      * @throws Exception
210      */

211     public static Role createRole() throws Exception JavaDoc {
212         return createRole("testRole");
213     }
214     
215     /**
216      * @param rolename
217      * @return Role
218      * @throws Exception
219      */

220     public static Role createRole(String JavaDoc rolename) throws Exception JavaDoc {
221         return getDefaultUserDatabase().createRole(rolename);
222     }
223     
224     /**
225      * @param roles
226      * @throws Exception
227      */

228     public static void deleteRole(Role...roles) throws Exception JavaDoc {
229         for (Role role : roles) {
230             getDefaultUserDatabase().deleteRole(role.getPrincipalName());
231         }
232     }
233     
234     /**
235      * @return PolicyDatabase
236      * @throws Exception
237      */

238     public static PolicyDatabase getPolicyService() throws Exception JavaDoc {
239         return PolicyDatabaseFactory.getInstance();
240     }
241
242     /**
243      */

244     @Test
245     public void finalTestWhichFailsDueToBadTearDown() {
246         assertTrue(true);
247     }
248 }
Popular Tags