001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/search/tags/sakai_2-4-1/search-integration-test/src/test/org/sakaiproject/search/component/test/LoaderComponentIntegrationTest.java $
003: * $Id: LoaderComponentIntegrationTest.java 9189 2006-05-09 19:49:20Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.search.component.test;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileReader;
025: import java.lang.reflect.Constructor;
026: import java.lang.reflect.InvocationHandler;
027: import java.lang.reflect.Method;
028: import java.lang.reflect.Proxy;
029: import java.util.Date;
030: import java.util.PropertyResourceBundle;
031:
032: import junit.extensions.TestSetup;
033: import junit.framework.Test;
034: import junit.framework.TestCase;
035: import junit.framework.TestSuite;
036:
037: import org.apache.catalina.loader.WebappClassLoader;
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040: import org.sakaiproject.authz.api.AuthzGroupService;
041: import org.sakaiproject.authz.api.SecurityService;
042: import org.sakaiproject.component.api.ComponentManager;
043: import org.sakaiproject.component.impl.SpringCompMgr;
044: import org.sakaiproject.search.api.SearchIndexBuilder;
045: import org.sakaiproject.search.api.SearchService;
046: import org.sakaiproject.site.api.Group;
047: import org.sakaiproject.site.api.Site;
048: import org.sakaiproject.site.api.SiteService;
049: import org.sakaiproject.tool.api.Session;
050: import org.sakaiproject.tool.cover.SessionManager;
051: import org.sakaiproject.user.api.UserDirectoryService;
052: import org.sakaiproject.user.api.UserEdit;
053:
054: import uk.ac.cam.caret.sakai.rwiki.service.api.RWikiObjectService;
055: import uk.ac.cam.caret.sakai.rwiki.service.api.RWikiSecurityService;
056: import uk.ac.cam.caret.sakai.rwiki.service.api.RenderService;
057: import uk.ac.cam.caret.sakai.rwiki.service.message.api.PreferenceService;
058:
059: /**
060: * @author ieb
061: */
062: public class LoaderComponentIntegrationTest extends TestCase {
063: private static Log logger = LogFactory
064: .getLog(LoaderComponentIntegrationTest.class);
065:
066: private SiteService siteService;
067:
068: private Site site;
069:
070: private Group group1;
071:
072: private Site targetSite;
073:
074: private Group group2;
075:
076: private UserDirectoryService userDirService;
077:
078: private RWikiObjectService rwikiObjectservice = null;
079:
080: private SecurityService securityService = null;
081:
082: private AuthzGroupService authzGroupService = null;
083:
084: private RWikiSecurityService rwikiSecurityService = null;
085:
086: private RenderService renderService = null;
087:
088: private PreferenceService preferenceService = null;
089:
090: private SearchService searchService = null;
091:
092: private SearchIndexBuilder searchIndexBuilder = null;
093:
094: // Constants
095: private static final String GROUP1_TITLE = "group1";
096:
097: private static final String GROUP2_TITLE = "group2";
098:
099: private static final String loaderDirectory = "/Users/ieb/Caret/testdataset";
100:
101: public static Test suite() {
102: TestSetup setup = new TestSetup(new TestSuite(
103: LoaderComponentIntegrationTest.class)) {
104: protected void setUp() throws Exception {
105: oneTimeSetup();
106: }
107: };
108: return setup;
109: }
110:
111: /**
112: * Setup test fixture (runs once for each test method called)
113: */
114: public void setUp() throws Exception {
115:
116: // Get the services we need for the tests
117: siteService = (SiteService) getService(SiteService.class
118: .getName());
119: userDirService = (UserDirectoryService) getService(UserDirectoryService.class
120: .getName());
121: rwikiObjectservice = (RWikiObjectService) getService(RWikiObjectService.class
122: .getName());
123: renderService = (RenderService) getService(RenderService.class
124: .getName());
125:
126: securityService = (SecurityService) getService(SecurityService.class
127: .getName());
128:
129: rwikiSecurityService = (RWikiSecurityService) getService(RWikiSecurityService.class
130: .getName());
131:
132: authzGroupService = (AuthzGroupService) getService(AuthzGroupService.class
133: .getName());
134:
135: preferenceService = (PreferenceService) getService(PreferenceService.class
136: .getName());
137:
138: searchService = (SearchService) getService(SearchService.class
139: .getName());
140: searchIndexBuilder = (SearchIndexBuilder) getService(SearchIndexBuilder.class
141: .getName());
142:
143: assertNotNull(
144: "Cant find site service as org.sakaiproject.service.legacy.authzGroup.AuthzGroupService ",
145: authzGroupService);
146:
147: assertNotNull(
148: "Cant find site service as org.sakaiproject.service.legacy.security.SecurityService ",
149: securityService);
150: assertNotNull("Cant find site service as securityService ",
151: rwikiSecurityService);
152:
153: assertNotNull(
154: "Cant find site service as org.sakaiproject.service.legacy.site.SiteService ",
155: siteService);
156: assertNotNull(
157: "Cant find User Directory service as org.sakaiproject.service.legacy.user.UserDirectoryService ",
158: userDirService);
159: assertNotNull(
160: "Cant find User Preference Service as uk.ac.cam.caret.sakai.rwiki.service.message.api.PreferenceService ",
161: preferenceService);
162: assertNotNull("Cant find RWiki Object service as "
163: + RWikiObjectService.class.getName(),
164: rwikiObjectservice);
165: assertNotNull("Cant find Render Service service as "
166: + RenderService.class.getName(), renderService);
167: assertNotNull(
168: "Cant find Search Service as org.sakaiproject.search.api.SearchService ",
169: searchService);
170: assertNotNull(
171: "Cant find Search Index Builder as org.sakaiproject.search.api.SearchIndexBuilder ",
172: searchIndexBuilder);
173: // Set username as admin
174: setUser("admin");
175:
176: tearDown();
177:
178: userDirService.addUser("test.user.1", "test.user.1", "Jane",
179: "Doe", "jd1@foo.com", "123", null, null);
180: userDirService.addUser("test.user.2", "test.user.2", "Joe",
181: "Schmoe", "js2@foo.com", "123", null, null);
182: userDirService.addUser("test.ta.1", "test.ta.1", "TA", "Doe",
183: "tajd1@foo.com", "123", null, null);
184:
185: // Create a site
186: site = siteService.addSite(generateSiteId(), "course");
187: targetSite = siteService.addSite(generateSiteId(), "course");
188: // Create a group for SectionAwareness to, er, become aware of
189: group1 = site.addGroup();
190: group1.setTitle(GROUP1_TITLE);
191:
192: // Save the group
193: siteService.save(site);
194:
195: site.addMember("test.user.1", "Student", true, false);
196: // Save the site and its new member
197: siteService.save(site);
198:
199: site.addMember("test.ta.1", "TA", true, false);
200: siteService.save(site);
201:
202: // Add a user to a group
203: group1.addMember("test.user.1", "Student", true, false);
204: group1.addMember("test.ta.1", "TA", true, false);
205:
206: group2 = targetSite.addGroup();
207: group2.setTitle(GROUP2_TITLE);
208:
209: // Save the group
210: siteService.save(targetSite);
211:
212: targetSite.addMember("test.user.1", "Student", true, false);
213:
214: // Save the site and its new member
215: siteService.save(targetSite);
216:
217: // Add a user to a group
218: group2.addMember("test.user.1", "Student", true, false);
219:
220: logger.info("Site Ref " + site.getReference());
221: logger.info("Target Site ref " + targetSite.getReference());
222: logger.info("Group 1 ref " + group1.getReference());
223: logger.info("Group 2 ref " + group2.getReference());
224:
225: }
226:
227: /**
228: * Remove the newly created objects, so we can run more tests with a clean
229: * slate.
230: */
231: public void tearDown() throws Exception {
232: setUser("admin");
233: try {
234: // Remove the site (along with its groups)
235: siteService.removeSite(site);
236:
237: } catch (Throwable t) {
238: }
239: try {
240: // Remove the site (along with its groups)
241: siteService.removeSite(targetSite);
242:
243: } catch (Throwable t) {
244: }
245: try {
246: // Remove the users
247: UserEdit user1 = userDirService.editUser("test.user.1");
248: userDirService.removeUser(user1);
249: } catch (Throwable t) {
250: // logger.info("Failed to remove user ",t);
251: logger.info("Failed to remove user " + t.getMessage());
252:
253: }
254:
255: try {
256: UserEdit user2 = userDirService.editUser("test.user.2");
257: userDirService.removeUser(user2);
258: } catch (Throwable t) {
259: // logger.info("Failed to remove user ",t);
260: logger.info("Failed to remove user " + t.getMessage());
261: }
262: try {
263: // Remove the users
264: UserEdit user1 = userDirService.editUser("test.ta.1");
265: userDirService.removeUser(user1);
266: } catch (Throwable t) {
267: // logger.info("Failed to remove user ",t);
268: logger.info("Failed to remove user " + t.getMessage());
269: }
270: }
271:
272: public void testLoadFromDisk() throws Exception {
273: File d = new File(loaderDirectory);
274: if (!d.exists()) {
275: d.mkdirs();
276: logger
277: .error("Directory for loader information does not exist so it has been created at "
278: + d.getAbsolutePath());
279: }
280: File[] files = d.listFiles();
281: for (int i = 0; i < files.length; i++) {
282: File f = files[i];
283: String pageName = f.getName();
284: int li = pageName.lastIndexOf(".");
285: if (li > -1) {
286: pageName = pageName.substring(0, li);
287: }
288: FileReader fr = new FileReader(f);
289: char[] c = new char[4096];
290: StringBuffer contents = new StringBuffer();
291: int ci = -1;
292: while ((ci = fr.read(c)) != -1) {
293: contents.append(c, 0, ci);
294: }
295: try {
296: logger.info("Adding " + pageName);
297: rwikiObjectservice.update(pageName,
298: site.getReference(), new Date(), contents
299: .toString());
300: logger.info("Added " + pageName);
301: } catch (Exception ex) {
302: logger.error("Problem ", ex);
303: }
304: }
305: while (!searchIndexBuilder.isBuildQueueEmpty()) {
306: Thread.sleep(5000);
307: }
308: searchIndexBuilder.destroy();
309: Thread.sleep(15000);
310: }
311:
312: public void testRefreshIndex() throws Exception {
313: searchIndexBuilder.refreshIndex();
314: while (!searchIndexBuilder.isBuildQueueEmpty()) {
315: Thread.sleep(5000);
316: }
317: searchIndexBuilder.destroy();
318: Thread.sleep(15000);
319: }
320:
321: public void testRebuildIndex() throws Exception {
322: searchIndexBuilder.rebuildIndex();
323: while (!searchIndexBuilder.isBuildQueueEmpty()) {
324: Thread.sleep(5000);
325: }
326: searchIndexBuilder.destroy();
327: Thread.sleep(15000);
328: }
329:
330: public void testRebuildIndexAndWait() throws Exception {
331: searchIndexBuilder.refreshIndex();
332: searchIndexBuilder.rebuildIndex();
333: searchIndexBuilder.refreshIndex();
334: Thread.sleep(15000);
335: while (!searchIndexBuilder.isBuildQueueEmpty()) {
336: Thread.sleep(5000);
337: }
338: searchIndexBuilder.destroy();
339: Thread.sleep(15000);
340: }
341:
342: // Stolen shamelessly from test-harness, since I want to run these test inside exlipse
343:
344: protected static ComponentManager compMgr;
345:
346: /**
347: * Initialize the component manager once for all tests, and log in as admin.
348: */
349: protected static void oneTimeSetup() throws Exception {
350: if (compMgr == null) {
351: // Find the sakai home dir
352: String tomcatHome = getTomcatHome();
353: String sakaiHome = tomcatHome + File.separatorChar
354: + "sakai" + File.separatorChar;
355: String componentsDir = tomcatHome + "components/";
356:
357: // Set the system properties needed by the sakai component manager
358: System.setProperty("sakai.home", sakaiHome);
359: System.setProperty(
360: ComponentManager.SAKAI_COMPONENTS_ROOT_SYS_PROP,
361: componentsDir);
362:
363: // Get a tomcat classloader
364: logger
365: .debug("Creating a tomcat classloader for component loading");
366: WebappClassLoader wcloader = new WebappClassLoader(Thread
367: .currentThread().getContextClassLoader());
368: wcloader.start();
369:
370: // Initialize spring component manager
371: logger
372: .debug("Loading component manager via tomcat's classloader");
373: Class clazz = wcloader.loadClass(SpringCompMgr.class
374: .getName());
375: Constructor constructor = clazz
376: .getConstructor(new Class[] { ComponentManager.class });
377: compMgr = (ComponentManager) constructor
378: .newInstance(new Object[] { null });
379: Method initMethod = clazz.getMethod("init", new Class[0]);
380: initMethod.invoke(compMgr, new Object[0]);
381: }
382:
383: // Sign in as admin
384: if (SessionManager.getCurrentSession() == null) {
385: SessionManager.startSession();
386: Session session = SessionManager.getCurrentSession();
387: session.setUserId("admin");
388: }
389: }
390:
391: /**
392: * Close the component manager when the tests finish.
393: */
394: public static void oneTimeTearDown() {
395: if (compMgr != null) {
396: compMgr.close();
397: }
398: }
399:
400: /**
401: * Fetches the "maven.tomcat.home" property from the maven build.properties
402: * file located in the user's $HOME directory.
403: *
404: * @return
405: * @throws Exception
406: */
407: private static String getTomcatHome() throws Exception {
408: String testTomcatHome = System.getProperty("test.tomcat.home");
409: if (testTomcatHome != null && testTomcatHome.length() > 0) {
410: return testTomcatHome;
411: } else {
412: String homeDir = System.getProperty("user.home");
413: File file = new File(homeDir + File.separatorChar
414: + "build.properties");
415: FileInputStream fis = new FileInputStream(file);
416: PropertyResourceBundle rb = new PropertyResourceBundle(fis);
417: return rb.getString("maven.tomcat.home");
418: }
419: }
420:
421: /**
422: * Convenience method to get a service bean from the Sakai component manager.
423: *
424: * @param beanId The id of the service
425: *
426: * @return The service, or null if the ID is not registered
427: */
428: public static final Object getService(String beanId) {
429: return org.sakaiproject.component.cover.ComponentManager
430: .get(beanId);
431: }
432:
433: /**
434: * Convenience method to set the current user in sakai. By default, the user
435: * is admin.
436: *
437: * @param userUid The user to become
438: */
439: public static final void setUser(String userUid) {
440: Session session = SessionManager.getCurrentSession();
441: session.setUserId(userUid);
442: }
443:
444: /**
445: * Convenience method to create a somewhat unique site id for testing. Useful
446: * in tests that need to create a site to run tests upon.
447: *
448: * @return A string suitable for using as a site id.
449: */
450: protected String generateSiteId() {
451: return "site-" + getClass().getName() + "-"
452: + Math.floor(Math.random() * 100000);
453: }
454:
455: /**
456: * Returns a dynamic proxy for a service interface. Useful for testing with
457: * customized service implementations without needing to write custom stubs.
458: *
459: * @param clazz The service interface class
460: * @param handler The invocation handler that defines how the dynamic proxy should behave
461: *
462: * @return The dynamic proxy to use as a collaborator
463: */
464: public static final Object getServiceProxy(Class clazz,
465: InvocationHandler handler) {
466: return Proxy.newProxyInstance(Thread.currentThread()
467: .getContextClassLoader(), new Class[] { clazz },
468: handler);
469: }
470:
471: }
|