KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
23 import java.net.InetAddress JavaDoc;
24 import java.util.Calendar JavaDoc;
25
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import servletunit.struts.MockStrutsTestCase;
29
30 import com.sslexplorer.boot.Context;
31 import com.sslexplorer.core.UserDatabaseManager;
32 import com.sslexplorer.properties.DefaultPropertyProfile;
33 import com.sslexplorer.properties.Property;
34 import com.sslexplorer.properties.PropertyProfile;
35 import com.sslexplorer.properties.impl.realms.RealmKey;
36 import com.sslexplorer.security.Constants;
37 import com.sslexplorer.security.LogonController;
38 import com.sslexplorer.security.LogonControllerFactory;
39 import com.sslexplorer.security.Role;
40 import com.sslexplorer.security.SessionInfo;
41 import com.sslexplorer.security.User;
42
43 /**
44  * Starting point for any Struts based unit testing
45  */

46 public abstract class AbstractMockStrutsTestCase extends MockStrutsTestCase
47 {
48         private static Context context_;
49         private static boolean finished_;
50         private final String JavaDoc contextDirectory_;
51         private final String JavaDoc configurationFile_;
52         private final String JavaDoc userName = "test";
53         private User user;
54
55         /**
56          * Default constructor
57          * @param contextDirectory - path to the WEB-INF directory
58          * @param configurationFile - the Struts configuration file
59          */

60         public AbstractMockStrutsTestCase ( String JavaDoc contextDirectory, String JavaDoc configurationFile )
61         {
62                 contextDirectory_ = contextDirectory;
63                 configurationFile_ = configurationFile;
64         }
65         
66         private static void oneTimeSetUp () throws Exception JavaDoc
67         {
68                 if ( context_ == null )
69                         context_ = TestContext.getTestContext ();
70         }
71
72         private static void oneTimeTearDown ()
73         {
74                 if ( context_ != null && finished_ )
75                         context_.shutdown ( false );
76         }
77         
78         protected void setUp () throws Exception JavaDoc
79         {
80                 super.setUp ();
81                 oneTimeSetUp ();
82                 setContextDirectory ( new File JavaDoc ( contextDirectory_ ) );
83                 setConfigFile ( configurationFile_ );
84                 getMockRequest ().setRemoteAddr ( "127.0.0.1" );
85                 getMockRequest ().setServletPath ( "" );
86                 getMockRequest ().setContextPath ( "" );
87                 authenticate ();
88         }
89         
90         private void authenticate () throws Exception JavaDoc
91         {
92                 LogonController logonController = LogonControllerFactory.getInstance();
93                 user = UserDatabaseManager.getInstance().getDefaultUserDatabase().createAccount(userName, "password", "", "Full Name", new Role[]{});
94                 SessionInfo nextSession = SessionInfo.nextSession ( getSession (), "logonTicket", user, InetAddress.getLocalHost (), SessionInfo.ALL_CONTEXTS, "" );
95                 Property.setProperty(new RealmKey("security.administrators", user.getRealm().getResourceId()), user.getPrincipalName(), nextSession);
96                 logonController.addSession ( "logonTicket", nextSession, request, response);
97                 HttpSession JavaDoc session = getRequest ().getSession ();
98                 session.setAttribute ( Constants.LOGON_TICKET, "logonTicket" );
99                 session.setAttribute ( Constants.SELECTED_PROFILE, getPropertyProfile () );
100         }
101         
102         private static PropertyProfile getPropertyProfile ()
103         {
104                 Calendar JavaDoc instance = Calendar.getInstance ();
105                 return new DefaultPropertyProfile (-1, -1, "", "", "", instance, instance );
106         }
107
108         protected void tearDown () throws Exception JavaDoc
109         {
110                 super.tearDown ();
111                 oneTimeTearDown ();
112                 LogonControllerFactory.getInstance().logoff ( "logonTicket" );
113                 clearRequestParameters ();
114                 UserDatabaseManager.getInstance().getDefaultUserDatabase().deleteAccount(user);
115                 for (File JavaDoc file : TestContext.DB_DIR.listFiles()) {
116                     String JavaDoc name = file.getName();
117                     if (name.indexOf("explorer_configuration") != -1 || name.indexOf("upgrade") != -1 || name.indexOf("versions") != -1) {
118                         file.delete();
119                     }
120                 }
121         }
122         
123         /**
124          * Verifies if the supplied tile matches the actual forward.
125          * @param name
126          */

127         public void verifyTilesForward ( String JavaDoc name )
128         {
129                 assertEquals ( name, getActualForward () );
130         }
131         
132         /**
133          * A hack, should always remain the last test to do correct teardown
134          */

135         public void testZFinished ()
136         {
137                 finished_ = true;
138         }
139 }
Popular Tags