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.prefs.Preferences;
25  
26  import javax.portlet.PortletRequest;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
32  import org.apache.jetspeed.mockobjects.request.MockRequestContext;
33  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
34  import org.apache.jetspeed.request.RequestContext;
35  import org.apache.jetspeed.security.SecurityException;
36  import org.apache.jetspeed.security.SecurityHelper;
37  import org.apache.jetspeed.security.User;
38  import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
39  import org.apache.jetspeed.util.descriptor.ExtendedPortletMetadata;
40  import org.apache.jetspeed.util.descriptor.PortletApplicationDescriptor;
41  
42  /***
43   * <p>
44   * Unit test for {@link UserInfoManager}
45   * </p>
46   * 
47   * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
48   */
49  public class TestUserInfoManager extends AbstractSecurityTestcase
50  {
51  
52      /*** The test MutablePortletApplication. */
53      private MutablePortletApplication portletApp;
54  
55      /*** The user info manager. */
56      private UserInfoManager single;
57  
58      private PortletRegistry portletRegistry;
59  
60      /***
61       * @see junit.framework.TestCase#setUp()
62       */
63      public void setUp() throws Exception
64      {
65          super.setUp();
66  
67          single = (UserInfoManager) ctx.getBean("org.apache.jetspeed.userinfo.UserInfoManager");
68          portletRegistry = (PortletRegistry) ctx.getBean("portletRegistry");
69      }
70  
71      /***
72       * @see junit.framework.TestCase#tearDown()
73       */
74      public void tearDown() throws Exception
75      {
76          cleanUp();
77          super.tearDown();
78      }
79  
80      public static Test suite()
81      {
82          // All methods starting with "test" will be executed in the test suite.
83          return new TestSuite(TestUserInfoManager.class);
84      }
85  
86      /*** Test set user info map. * */
87      public void testSingleSetUserInfoMap() throws Exception
88      {
89          innerTestSetUserInfoMap(single);
90      }
91  
92      // public void testMultiSetUserInfoMap() throws Exception
93      // {
94      // innerTestSetUserInfoMap(multi);
95      // }
96  
97      private void innerTestSetUserInfoMap(UserInfoManager uim) throws Exception
98      {
99          PortletApplicationDescriptor pad = new PortletApplicationDescriptor(new FileReader("test/testdata/deploy/portlet.xml"), "unit-test");
100         portletApp = pad.createPortletApplication();
101         assertNotNull("App is null", portletApp);
102 
103         // persist the app
104         try
105         {
106             portletRegistry.registerPortletApplication(portletApp);
107         }
108         catch (Exception e)
109         {
110             String msg = "Unable to register portlet application, " + portletApp.getName()
111                     + ", through the portlet portletRegistry: " + e.toString();
112 
113             throw new Exception(msg, e);
114         }
115 
116         RequestContext request = initRequestContext("anon");
117 
118         // Without linked attributes
119         // There are no preferences associated to the user profile.
120         Map userInfo = uim.getUserInfoMap(portletApp.getId(), request);
121         assertNull(PortletRequest.USER_INFO + " is null", userInfo);
122 
123         // The user has preferences associated to the user profile.
124         initUser();
125         request = initRequestContext("test");
126         userInfo = uim.getUserInfoMap(portletApp.getId(), request);
127         assertNotNull(PortletRequest.USER_INFO + " should not be null", userInfo);
128         assertEquals("should contain user.name.given", "Test Dude", (String) userInfo.get("user.name.given"));
129         assertEquals("should contain user.name.family", "Dudley", (String) userInfo.get("user.name.family"));
130         assertNull("should not contain user.home-info.online.email", userInfo.get("user.home-info.online.email"));
131 
132         // With linked attributes
133         ExtendedPortletMetadata extMetaData = new ExtendedPortletMetadata(new FileReader("test/testdata/deploy/jetspeed-portlet.xml"), portletApp);
134         extMetaData.load();
135 
136         // persist the app
137         try
138         {
139             portletRegistry.updatePortletApplication(portletApp);
140         }
141         catch (Exception e)
142         {
143             String msg = "Unable to update portlet application, " + portletApp.getName()
144                     + ", through the portlet portletRegistry: " + e.toString();
145 
146             throw new Exception(msg, e);
147         }
148 
149         userInfo = uim.getUserInfoMap(portletApp.getId(), request);
150         assertNotNull(PortletRequest.USER_INFO + " should not be null", userInfo);
151         assertEquals("should contain user-name-given", "Test Dude", (String) userInfo.get("user-name-given"));
152         assertEquals("should contain user-name-family", "Dudley", (String) userInfo.get("user-name-family"));
153     }
154 
155     /***
156      * <p>
157      * Initialize the mock request context.
158      * </p>
159      * 
160      * @param username
161      *            The username.
162      * @return The request context.
163      */
164     private RequestContext initRequestContext(String username)
165     {
166         RequestContext request = new MockRequestContext("default-other");
167 
168         request.setSubject(SecurityHelper.createSubject(username));
169         return request;
170     }
171 
172     /***
173      * <p>
174      * Init test user.
175      * </p>
176      */
177     private void initUser() throws Exception
178     {
179         User user = null;
180         try
181         {
182             ums.addUser("test", "password01");
183             user = ums.getUser("test");
184         }
185         catch (SecurityException sex)
186         {
187             assertTrue("user exists. should not have thrown an exception.", false);
188         }
189         Preferences userInfoPrefs = user.getPreferences().node("userinfo");
190         userInfoPrefs.put("user.name.given", "Test Dude");
191         userInfoPrefs.put("user.name.family", "Dudley");
192     }
193 
194     /***
195      * <p>
196      * Destroy user test object.
197      * </p>
198      */
199     protected void destroyUser()
200     {
201         try
202         {
203             if (ums.userExists("test"))
204             {
205                 ums.removeUser("test");
206             }
207         }
208         catch (SecurityException sex)
209         {
210             System.out.println("could not remove test users. exception caught: " + sex);
211         }
212     }
213 
214     /***
215      * <p>
216      * Clean up test.
217      * </p>
218      */
219     private void cleanUp() throws Exception
220     {
221         // remove the app
222         if (null != portletApp)
223         {
224             try
225             {
226                 portletRegistry.removeApplication(portletApp);
227             }
228             catch (Exception e)
229             {
230                 String msg = "Unable to remove portlet application, " + portletApp.getName()
231                         + ", through the portlet portletRegistry: " + e.toString();
232                 throw new Exception(msg, e);
233             }
234         }
235 
236         destroyUser();
237     }
238 
239     protected String[] getConfigurations()
240     {
241         String[] confs = super.getConfigurations();
242         List confList = new ArrayList(Arrays.asList(confs));
243         confList.add("registry.xml");
244         confList.add("rc3.xml");
245         confList.add("JETSPEED-INF/spring/user-info.xml");
246         confList.add("prefs.xml");
247         confList.add("cache.xml");
248         return (String[]) confList.toArray(new String[1]);
249     }
250 }