001: /**********************************************************************************
002: *
003: * $Id: GradebookTestBase.java 21942 2007-02-27 01:31:47Z louis@media.berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2005 The Regents of the University of California, The MIT Corporation
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.tool.gradebook.test;
022:
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.sakaiproject.component.section.support.IntegrationSupport;
029: import org.sakaiproject.component.section.support.UserManager;
030: import org.sakaiproject.section.api.SectionAwareness;
031: import org.sakaiproject.section.api.coursemanagement.EnrollmentRecord;
032: import org.sakaiproject.section.api.facade.Role;
033: import org.sakaiproject.service.gradebook.shared.GradebookExternalAssessmentService;
034: import org.sakaiproject.service.gradebook.shared.GradebookFrameworkService;
035: import org.sakaiproject.service.gradebook.shared.GradebookService;
036: import org.sakaiproject.tool.gradebook.Assignment;
037: import org.sakaiproject.tool.gradebook.CourseGrade;
038: import org.sakaiproject.tool.gradebook.GradableObject;
039: import org.sakaiproject.tool.gradebook.Gradebook;
040: import org.sakaiproject.tool.gradebook.business.GradebookManager;
041: import org.sakaiproject.tool.gradebook.facades.Authn;
042: import org.sakaiproject.tool.gradebook.facades.Authz;
043: import org.sakaiproject.tool.gradebook.facades.UserDirectoryService;
044: import org.sakaiproject.tool.gradebook.facades.EventTrackingService;
045: import org.sakaiproject.tool.gradebook.facades.test.AuthnTestImpl;
046: import org.springframework.test.AbstractTransactionalSpringContextTests;
047:
048: /**
049: * Base class for gradebook test classes that provides the spring application
050: * context. The database used is an in-memory hsqldb by default, but this can
051: * be overridden to test specific database configurations by setting the "mem"
052: * system property to "false". In the "mem=false" case, the database configuration
053: * is set in the hibernate.properties file in the "hibernate.properties.dir" directory.
054: *
055: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
056: */
057: public abstract class GradebookTestBase extends
058: AbstractTransactionalSpringContextTests {
059: protected Authz authz;
060: protected Authn authn;
061: protected GradebookManager gradebookManager;
062: protected GradebookService gradebookService;
063: protected GradebookFrameworkService gradebookFrameworkService;
064: protected GradebookExternalAssessmentService gradebookExternalAssessmentService;
065: protected SectionAwareness sectionAwareness;
066: protected UserDirectoryService userDirectoryService;
067: protected IntegrationSupport integrationSupport;
068: protected UserManager userManager;
069: protected EventTrackingService eventTrackingService;
070:
071: protected void onSetUpInTransaction() throws Exception {
072: authn = (Authn) applicationContext
073: .getBean("org_sakaiproject_tool_gradebook_facades_Authn");
074: authz = (Authz) applicationContext
075: .getBean("org_sakaiproject_tool_gradebook_facades_Authz");
076: gradebookService = (GradebookService) applicationContext
077: .getBean("org_sakaiproject_service_gradebook_GradebookService");
078: gradebookFrameworkService = (GradebookFrameworkService) applicationContext
079: .getBean("org_sakaiproject_service_gradebook_GradebookFrameworkService");
080: gradebookExternalAssessmentService = (GradebookExternalAssessmentService) applicationContext
081: .getBean("org_sakaiproject_service_gradebook_GradebookExternalAssessmentService");
082: gradebookManager = (GradebookManager) applicationContext
083: .getBean("org_sakaiproject_tool_gradebook_business_GradebookManager");
084: sectionAwareness = (SectionAwareness) applicationContext
085: .getBean("org.sakaiproject.section.api.SectionAwareness");
086: userDirectoryService = (UserDirectoryService) applicationContext
087: .getBean("org_sakaiproject_tool_gradebook_facades_UserDirectoryService");
088: integrationSupport = (IntegrationSupport) applicationContext
089: .getBean("org.sakaiproject.component.section.support.IntegrationSupport");
090: userManager = (UserManager) applicationContext
091: .getBean("org.sakaiproject.component.section.support.UserManager");
092: eventTrackingService = (EventTrackingService) applicationContext
093: .getBean("org_sakaiproject_tool_gradebook_facades_EventTrackingService");
094: }
095:
096: /**
097: * @see org.springframework.test.AbstractDependencyInjectionSpringContextTests#getConfigLocations()
098: */
099: protected String[] getConfigLocations() {
100: String[] configLocations = {
101: "spring-db.xml",
102: "spring-beans.xml",
103: "spring-facades.xml",
104:
105: // To avoid warning messages every time the Gradebook code
106: // finds out that the fresh DB doesn't have any grading scales
107: // defined, use the configuration which explicitly defines
108: // them.
109: "spring-service-with-grade-mappings-test.xml",
110:
111: "spring-hib.xml",
112: "spring-beans-test.xml",
113: "spring-hib-test.xml",
114:
115: // SectionAwareness integration support.
116: "classpath*:org/sakaiproject/component/section/support/spring-integrationSupport.xml",
117: "classpath*:org/sakaiproject/component/section/spring-beans.xml",
118: "classpath*:org/sakaiproject/component/section/spring-services.xml", };
119: return configLocations;
120: }
121:
122: protected List addUsersEnrollments(Gradebook gradebook,
123: Collection studentUids) {
124: List enrollments = new ArrayList();
125: for (Iterator iter = studentUids.iterator(); iter.hasNext();) {
126: String studentUid = (String) iter.next();
127: userManager.createUser(studentUid, null, null, null);
128: EnrollmentRecord sectionEnrollment = (EnrollmentRecord) integrationSupport
129: .addSiteMembership(studentUid, gradebook.getUid(),
130: Role.STUDENT);
131: enrollments.add(sectionEnrollment);
132: }
133: return enrollments;
134: }
135:
136: public IntegrationSupport getIntegrationSupport() {
137: return integrationSupport;
138: }
139:
140: public void setIntegrationSupport(
141: IntegrationSupport integrationSupport) {
142: this .integrationSupport = integrationSupport;
143: }
144:
145: public UserManager getUserManager() {
146: return userManager;
147: }
148:
149: public void setUserManager(UserManager userManager) {
150: this .userManager = userManager;
151: }
152:
153: protected void setAuthnId(String newUserUid) {
154: if (authn instanceof AuthnTestImpl) {
155: ((AuthnTestImpl) authn).setAuthnContext(newUserUid);
156: } else {
157: throw new UnsupportedOperationException();
158: }
159: }
160:
161: /**
162: * Utility method for legacy tests after the "getCourseGradeWithStats" method stopped being used
163: * by the application.
164: */
165: protected CourseGrade getCourseGradeWithStats(Long gradebookId) {
166: List gradableObjects = gradebookManager
167: .getAssignmentsAndCourseGradeWithStats(gradebookId,
168: Assignment.DEFAULT_SORT, true);
169: for (Iterator iter = gradableObjects.iterator(); iter.hasNext();) {
170: GradableObject gradableObject = (GradableObject) iter
171: .next();
172: if (gradableObject instanceof CourseGrade) {
173: return (CourseGrade) gradableObject;
174: }
175: }
176: return null;
177: }
178:
179: }
|