001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.userinfo;
018:
019: import java.io.FileReader;
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Properties;
025: import java.util.prefs.Preferences;
026:
027: import javax.portlet.PortletRequest;
028:
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031:
032: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
033: import org.apache.jetspeed.mockobjects.request.MockRequestContext;
034: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
035: import org.apache.jetspeed.request.RequestContext;
036: import org.apache.jetspeed.security.SecurityException;
037: import org.apache.jetspeed.security.SecurityHelper;
038: import org.apache.jetspeed.security.User;
039: import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
040: import org.apache.jetspeed.util.descriptor.ExtendedPortletMetadata;
041: import org.apache.jetspeed.util.descriptor.PortletApplicationDescriptor;
042:
043: /**
044: * <p>
045: * Unit test for {@link UserInfoManager}
046: * </p>
047: *
048: * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
049: */
050: public class TestUserInfoManager extends AbstractSecurityTestcase {
051:
052: /** The test MutablePortletApplication. */
053: private MutablePortletApplication portletApp;
054:
055: /** The user info manager. */
056: private UserInfoManager single;
057:
058: private PortletRegistry portletRegistry;
059:
060: /**
061: * @see junit.framework.TestCase#setUp()
062: */
063: public void setUp() throws Exception {
064: super .setUp();
065:
066: single = (UserInfoManager) ctx
067: .getBean("org.apache.jetspeed.userinfo.UserInfoManager");
068: portletRegistry = (PortletRegistry) ctx
069: .getBean("portletRegistry");
070: }
071:
072: /**
073: * @see junit.framework.TestCase#tearDown()
074: */
075: public void tearDown() throws Exception {
076: cleanUp();
077: super .tearDown();
078: }
079:
080: public static Test suite() {
081: // All methods starting with "test" will be executed in the test suite.
082: return new TestSuite(TestUserInfoManager.class);
083: }
084:
085: /** Test set user info map. * */
086: public void testSingleSetUserInfoMap() throws Exception {
087: innerTestSetUserInfoMap(single);
088: }
089:
090: // public void testMultiSetUserInfoMap() throws Exception
091: // {
092: // innerTestSetUserInfoMap(multi);
093: // }
094:
095: private void innerTestSetUserInfoMap(UserInfoManager uim)
096: throws Exception {
097: PortletApplicationDescriptor pad = new PortletApplicationDescriptor(
098: new FileReader("test/testdata/deploy/portlet.xml"),
099: "unit-test");
100: portletApp = pad.createPortletApplication();
101: assertNotNull("App is null", portletApp);
102:
103: // persist the app
104: try {
105: portletRegistry.registerPortletApplication(portletApp);
106: } catch (Exception e) {
107: String msg = "Unable to register portlet application, "
108: + portletApp.getName()
109: + ", through the portlet portletRegistry: "
110: + e.toString();
111:
112: throw new Exception(msg, e);
113: }
114:
115: RequestContext request = initRequestContext("anon");
116:
117: // Without linked attributes
118: // There are no preferences associated to the user profile.
119: Map userInfo = uim.getUserInfoMap(portletApp.getId(), request);
120: assertNull(PortletRequest.USER_INFO + " is null", userInfo);
121:
122: // The user has preferences associated to the user profile.
123: initUser();
124: request = initRequestContext("test");
125: userInfo = uim.getUserInfoMap(portletApp.getId(), request);
126: assertNotNull(PortletRequest.USER_INFO + " should not be null",
127: userInfo);
128: assertEquals("should contain user.name.given", "Test Dude",
129: (String) userInfo.get("user.name.given"));
130: assertEquals("should contain user.name.family", "Dudley",
131: (String) userInfo.get("user.name.family"));
132: assertNull("should not contain user.home-info.online.email",
133: userInfo.get("user.home-info.online.email"));
134:
135: // With linked attributes
136: ExtendedPortletMetadata extMetaData = new ExtendedPortletMetadata(
137: new FileReader(
138: "test/testdata/deploy/jetspeed-portlet.xml"),
139: portletApp);
140: extMetaData.load();
141:
142: // persist the app
143: try {
144: portletRegistry.updatePortletApplication(portletApp);
145: } catch (Exception e) {
146: String msg = "Unable to update portlet application, "
147: + portletApp.getName()
148: + ", through the portlet portletRegistry: "
149: + e.toString();
150:
151: throw new Exception(msg, e);
152: }
153:
154: userInfo = uim.getUserInfoMap(portletApp.getId(), request);
155: assertNotNull(PortletRequest.USER_INFO + " should not be null",
156: userInfo);
157: assertEquals("should contain user-name-given", "Test Dude",
158: (String) userInfo.get("user-name-given"));
159: assertEquals("should contain user-name-family", "Dudley",
160: (String) userInfo.get("user-name-family"));
161: }
162:
163: /**
164: * <p>
165: * Initialize the mock request context.
166: * </p>
167: *
168: * @param username
169: * The username.
170: * @return The request context.
171: */
172: private RequestContext initRequestContext(String username) {
173: RequestContext request = new MockRequestContext("default-other");
174:
175: request.setSubject(SecurityHelper.createSubject(username));
176: return request;
177: }
178:
179: /**
180: * <p>
181: * Init test user.
182: * </p>
183: */
184: private void initUser() throws Exception {
185: User user = null;
186: try {
187: ums.addUser("test", "password01");
188: user = ums.getUser("test");
189: } catch (SecurityException sex) {
190: assertTrue(
191: "user exists. should not have thrown an exception.",
192: false);
193: }
194: Preferences userInfoPrefs = user.getPreferences().node(
195: "userinfo");
196: userInfoPrefs.put("user.name.given", "Test Dude");
197: userInfoPrefs.put("user.name.family", "Dudley");
198: }
199:
200: /**
201: * <p>
202: * Destroy user test object.
203: * </p>
204: */
205: protected void destroyUser() {
206: try {
207: if (ums.userExists("test")) {
208: ums.removeUser("test");
209: }
210: } catch (SecurityException sex) {
211: System.out
212: .println("could not remove test users. exception caught: "
213: + sex);
214: }
215: }
216:
217: /**
218: * <p>
219: * Clean up test.
220: * </p>
221: */
222: private void cleanUp() throws Exception {
223: // remove the app
224: if (null != portletApp) {
225: try {
226: portletRegistry.removeApplication(portletApp);
227: } catch (Exception e) {
228: String msg = "Unable to remove portlet application, "
229: + portletApp.getName()
230: + ", through the portlet portletRegistry: "
231: + e.toString();
232: throw new Exception(msg, e);
233: }
234: }
235:
236: destroyUser();
237: }
238:
239: protected String[] getConfigurations() {
240: String[] confs = super .getConfigurations();
241: List confList = new ArrayList(Arrays.asList(confs));
242: confList.add("jetspeed-base.xml");
243: confList.add("page-manager.xml");
244: confList.add("registry.xml");
245: confList.add("rc3.xml");
246: confList.add("JETSPEED-INF/spring/user-info.xml");
247: confList.add("prefs.xml");
248: confList.add("cache.xml");
249: return (String[]) confList.toArray(new String[1]);
250: }
251:
252: protected Properties getPostProcessProperties() {
253: Properties p = super .getPostProcessProperties();
254: p.setProperty("supported.portletmode.autoswitch.config",
255: "false");
256: p.setProperty("supported.portletmode.autoswitch.edit_defaults",
257: "false");
258: p
259: .setProperty(
260: "supported.portletmode.autoswitch.config.surrogate.portlet",
261: "j2-admin::CustomConfigModePortlet");
262: return p;
263: }
264: }
|