1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.userinfo;
18  
19  import java.io.FileReader;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Properties;
25  import java.util.prefs.Preferences;
26  
27  import javax.portlet.PortletRequest;
28  
29  import junit.framework.Test;
30  import junit.framework.TestSuite;
31  
32  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
33  import org.apache.jetspeed.mockobjects.request.MockRequestContext;
34  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
35  import org.apache.jetspeed.request.RequestContext;
36  import org.apache.jetspeed.security.SecurityException;
37  import org.apache.jetspeed.security.SecurityHelper;
38  import org.apache.jetspeed.security.User;
39  import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
40  import org.apache.jetspeed.util.descriptor.ExtendedPortletMetadata;
41  import org.apache.jetspeed.util.descriptor.PortletApplicationDescriptor;
42  
43  /***
44   * <p>
45   * Unit test for {@link UserInfoManager}
46   * </p>
47   * 
48   * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
49   */
50  public class TestUserInfoManager extends AbstractSecurityTestcase
51  {
52  
53      /*** The test MutablePortletApplication. */
54      private MutablePortletApplication portletApp;
55  
56      /*** The user info manager. */
57      private UserInfoManager single;
58  
59      private PortletRegistry portletRegistry;
60  
61      /***
62       * @see junit.framework.TestCase#setUp()
63       */
64      public void setUp() throws Exception
65      {
66          super.setUp();
67  
68          single = (UserInfoManager) ctx.getBean("org.apache.jetspeed.userinfo.UserInfoManager");
69          portletRegistry = (PortletRegistry) ctx.getBean("portletRegistry");
70      }
71  
72      /***
73       * @see junit.framework.TestCase#tearDown()
74       */
75      public void tearDown() throws Exception
76      {
77          cleanUp();
78          super.tearDown();
79      }
80  
81      public static Test suite()
82      {
83          // All methods starting with "test" will be executed in the test suite.
84          return new TestSuite(TestUserInfoManager.class);
85      }
86  
87      /*** Test set user info map. * */
88      public void testSingleSetUserInfoMap() throws Exception
89      {
90          innerTestSetUserInfoMap(single);
91      }
92  
93      // public void testMultiSetUserInfoMap() throws Exception
94      // {
95      // innerTestSetUserInfoMap(multi);
96      // }
97  
98      private void innerTestSetUserInfoMap(UserInfoManager uim) throws Exception
99      {
100         PortletApplicationDescriptor pad = new PortletApplicationDescriptor(new FileReader("test/testdata/deploy/portlet.xml"), "unit-test");
101         portletApp = pad.createPortletApplication();
102         assertNotNull("App is null", portletApp);
103 
104         // persist the app
105         try
106         {
107             portletRegistry.registerPortletApplication(portletApp);
108         }
109         catch (Exception e)
110         {
111             String msg = "Unable to register portlet application, " + portletApp.getName()
112                     + ", through the portlet portletRegistry: " + e.toString();
113 
114             throw new Exception(msg, e);
115         }
116 
117         RequestContext request = initRequestContext("anon");
118 
119         // Without linked attributes
120         // There are no preferences associated to the user profile.
121         Map userInfo = uim.getUserInfoMap(portletApp.getId(), request);
122         assertNull(PortletRequest.USER_INFO + " is null", userInfo);
123 
124         // The user has preferences associated to the user profile.
125         initUser();
126         request = initRequestContext("test");
127         userInfo = uim.getUserInfoMap(portletApp.getId(), request);
128         assertNotNull(PortletRequest.USER_INFO + " should not be null", userInfo);
129         assertEquals("should contain user.name.given", "Test Dude", (String) userInfo.get("user.name.given"));
130         assertEquals("should contain user.name.family", "Dudley", (String) userInfo.get("user.name.family"));
131         assertNull("should not contain user.home-info.online.email", userInfo.get("user.home-info.online.email"));
132 
133         // With linked attributes
134         ExtendedPortletMetadata extMetaData = new ExtendedPortletMetadata(new FileReader("test/testdata/deploy/jetspeed-portlet.xml"), portletApp);
135         extMetaData.load();
136 
137         // persist the app
138         try
139         {
140             portletRegistry.updatePortletApplication(portletApp);
141         }
142         catch (Exception e)
143         {
144             String msg = "Unable to update portlet application, " + portletApp.getName()
145                     + ", through the portlet portletRegistry: " + e.toString();
146 
147             throw new Exception(msg, e);
148         }
149 
150         userInfo = uim.getUserInfoMap(portletApp.getId(), request);
151         assertNotNull(PortletRequest.USER_INFO + " should not be null", userInfo);
152         assertEquals("should contain user-name-given", "Test Dude", (String) userInfo.get("user-name-given"));
153         assertEquals("should contain user-name-family", "Dudley", (String) userInfo.get("user-name-family"));
154     }
155 
156     /***
157      * <p>
158      * Initialize the mock request context.
159      * </p>
160      * 
161      * @param username
162      *            The username.
163      * @return The request context.
164      */
165     private RequestContext initRequestContext(String username)
166     {
167         RequestContext request = new MockRequestContext("default-other");
168 
169         request.setSubject(SecurityHelper.createSubject(username));
170         return request;
171     }
172 
173     /***
174      * <p>
175      * Init test user.
176      * </p>
177      */
178     private void initUser() throws Exception
179     {
180         User user = null;
181         try
182         {
183             ums.addUser("test", "password01");
184             user = ums.getUser("test");
185         }
186         catch (SecurityException sex)
187         {
188             assertTrue("user exists. should not have thrown an exception.", false);
189         }
190         Preferences userInfoPrefs = user.getPreferences().node("userinfo");
191         userInfoPrefs.put("user.name.given", "Test Dude");
192         userInfoPrefs.put("user.name.family", "Dudley");
193     }
194 
195     /***
196      * <p>
197      * Destroy user test object.
198      * </p>
199      */
200     protected void destroyUser()
201     {
202         try
203         {
204             if (ums.userExists("test"))
205             {
206                 ums.removeUser("test");
207             }
208         }
209         catch (SecurityException sex)
210         {
211             System.out.println("could not remove test users. exception caught: " + sex);
212         }
213     }
214 
215     /***
216      * <p>
217      * Clean up test.
218      * </p>
219      */
220     private void cleanUp() throws Exception
221     {
222         // remove the app
223         if (null != portletApp)
224         {
225             try
226             {
227                 portletRegistry.removeApplication(portletApp);
228             }
229             catch (Exception e)
230             {
231                 String msg = "Unable to remove portlet application, " + portletApp.getName()
232                         + ", through the portlet portletRegistry: " + e.toString();
233                 throw new Exception(msg, e);
234             }
235         }
236 
237         destroyUser();
238     }
239 
240     protected String[] getConfigurations()
241     {
242         String[] confs = super.getConfigurations();
243         List confList = new ArrayList(Arrays.asList(confs));
244         confList.add("jetspeed-base.xml");
245         confList.add("page-manager.xml");
246         confList.add("registry.xml");
247         confList.add("rc3.xml");
248         confList.add("JETSPEED-INF/spring/user-info.xml");
249         confList.add("prefs.xml");
250         confList.add("cache.xml");
251         return (String[]) confList.toArray(new String[1]);
252     }
253 
254     protected Properties getPostProcessProperties()
255     {
256         Properties p = super.getPostProcessProperties();
257         p.setProperty("supported.portletmode.autoswitch.config", "false");
258         p.setProperty("supported.portletmode.autoswitch.edit_defaults", "false");
259         p.setProperty("supported.portletmode.autoswitch.config.surrogate.portlet", "j2-admin::CustomConfigModePortlet");
260         return p;
261     }
262 }