001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app/src/test/org/sakaiproject/test/section/CourseManagerTest.java $
003: * $Id: CourseManagerTest.java 18134 2006-11-14 18:59:25Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
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.test.section;
021:
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import junit.framework.Assert;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.section.api.CourseManager;
031: import org.sakaiproject.section.api.SectionManager;
032: import org.sakaiproject.section.api.coursemanagement.Course;
033: import org.sakaiproject.section.api.coursemanagement.CourseSection;
034: import org.sakaiproject.section.api.coursemanagement.ParticipationRecord;
035: import org.sakaiproject.section.api.coursemanagement.User;
036: import org.sakaiproject.section.api.facade.Role;
037: import org.sakaiproject.component.section.support.UserManager;
038:
039: public class CourseManagerTest extends SectionsTestBase {
040: private static final Log log = LogFactory
041: .getLog(CourseManagerTest.class);
042:
043: private CourseManager courseManager;
044: private SectionManager sectionManager;
045: private UserManager userManager;
046:
047: protected void onSetUpInTransaction() throws Exception {
048: courseManager = (CourseManager) applicationContext
049: .getBean("org.sakaiproject.section.api.CourseManager");
050: sectionManager = (SectionManager) applicationContext
051: .getBean("org.sakaiproject.section.api.SectionManager");
052: userManager = (UserManager) applicationContext
053: .getBean("org.sakaiproject.component.section.support.UserManager");
054: }
055:
056: public void testRemoveStudentFromCourse() throws Exception {
057: Course course = courseManager.createCourse("site",
058: "course title", false, false, false);
059: User student1 = userManager.createUser("userUid", "foo", "bar",
060: "baz");
061: CourseSection section1 = sectionManager.addSection(course
062: .getUuid(), "a section", "a category", null, null,
063: null, null, false, false, false, false, false, false,
064: false);
065: CourseSection section2 = sectionManager.addSection(course
066: .getUuid(), "another section", "another category",
067: null, null, null, null, false, false, false, false,
068: false, false, false);
069:
070: // Enroll the user in the course
071: courseManager.addEnrollment(student1, course);
072:
073: // Enroll the user as a student in both sections
074: sectionManager.addSectionMembership(student1.getUserUid(),
075: Role.STUDENT, section1.getUuid());
076: sectionManager.addSectionMembership(student1.getUserUid(),
077: Role.STUDENT, section2.getUuid());
078:
079: // Make sure the user is enrolled in the two sections
080: Collection enrollments = sectionManager.getSectionEnrollments(
081: student1.getUserUid(), course.getUuid());
082: Assert.assertTrue(enrollments.size() == 2);
083: }
084:
085: public void testRemoveTaFromCourse() throws Exception {
086: Course course = courseManager.createCourse("site",
087: "course title", false, false, false);
088: User ta1 = userManager.createUser("userUid", "foo", "bar",
089: "baz");
090: CourseSection section1 = sectionManager.addSection(course
091: .getUuid(), "a section", "a category", null, null,
092: null, null, false, false, false, false, false, false,
093: false);
094: CourseSection section2 = sectionManager.addSection(course
095: .getUuid(), "another section", "another category",
096: null, null, null, null, false, false, false, false,
097: false, false, false);
098:
099: // Enroll the user in the course
100: courseManager.addTA(ta1, course);
101:
102: // Enroll the user as a student in both sections
103: sectionManager.addSectionMembership(ta1.getUserUid(), Role.TA,
104: section1.getUuid());
105: sectionManager.addSectionMembership(ta1.getUserUid(), Role.TA,
106: section2.getUuid());
107:
108: // Make sure the user is a member of two sections
109: Collection memberships1 = sectionManager
110: .getSectionTeachingAssistants(section1.getUuid());
111: Collection memberships2 = sectionManager
112: .getSectionTeachingAssistants(section2.getUuid());
113: Assert.assertTrue(memberships1.size() == 1);
114: Assert.assertTrue(memberships2.size() == 1);
115: }
116:
117: public void testRemoveOrphanedSectionMemberships() throws Exception {
118: Course course = courseManager.createCourse("site",
119: "course title", false, false, false);
120: User ta1 = userManager.createUser("userUid1", "foo1", "bar1",
121: "baz1");
122: User ta2 = userManager.createUser("userUid2", "foo2", "bar2",
123: "baz2");
124: User student1 = userManager.createUser("userUid3", "foo3",
125: "bar3", "baz3");
126: User student2 = userManager.createUser("userUid4", "foo4",
127: "bar4", "baz4");
128: CourseSection section1 = sectionManager.addSection(course
129: .getUuid(), "a section", "a category", null, null,
130: null, null, false, false, false, false, false, false,
131: false);
132: CourseSection section2 = sectionManager.addSection(course
133: .getUuid(), "another section", "another category",
134: null, null, null, null, false, false, false, false,
135: false, false, false);
136:
137: // Enroll the users in the course
138: courseManager.addTA(ta1, course);
139: courseManager.addTA(ta2, course);
140: courseManager.addEnrollment(student1, course);
141: courseManager.addEnrollment(student2, course);
142:
143: // Enroll the users in both sections
144: sectionManager.addSectionMembership(ta1.getUserUid(), Role.TA,
145: section1.getUuid());
146: sectionManager.addSectionMembership(ta1.getUserUid(), Role.TA,
147: section2.getUuid());
148: sectionManager.addSectionMembership(ta2.getUserUid(), Role.TA,
149: section1.getUuid());
150: sectionManager.addSectionMembership(ta2.getUserUid(), Role.TA,
151: section2.getUuid());
152: sectionManager.addSectionMembership(student1.getUserUid(),
153: Role.STUDENT, section1.getUuid());
154: sectionManager.addSectionMembership(student1.getUserUid(),
155: Role.STUDENT, section2.getUuid());
156: sectionManager.addSectionMembership(student2.getUserUid(),
157: Role.STUDENT, section1.getUuid());
158: sectionManager.addSectionMembership(student2.getUserUid(),
159: Role.STUDENT, section2.getUuid());
160:
161: // Remove the #2 users from the course
162: courseManager.removeCourseMembership(ta2.getUserUid(), course);
163: courseManager.removeCourseMembership(student2.getUserUid(),
164: course);
165:
166: // Ensure that the second student and ta are no longer associated with any sections
167: List section1Tas = sectionManager
168: .getSectionTeachingAssistants(section1.getUuid());
169: for (Iterator iter = section1Tas.iterator(); iter.hasNext();) {
170: ParticipationRecord record = (ParticipationRecord) iter
171: .next();
172: if (record.getUser().getUserUid().equals(ta2.getUserUid())) {
173: fail();
174: }
175: }
176: List section2Tas = sectionManager
177: .getSectionTeachingAssistants(section2.getUuid());
178: for (Iterator iter = section2Tas.iterator(); iter.hasNext();) {
179: ParticipationRecord record = (ParticipationRecord) iter
180: .next();
181: if (record.getUser().getUserUid().equals(ta2.getUserUid())) {
182: fail();
183: }
184: }
185:
186: List section1Students = sectionManager
187: .getSectionTeachingAssistants(section1.getUuid());
188: for (Iterator iter = section1Students.iterator(); iter
189: .hasNext();) {
190: ParticipationRecord record = (ParticipationRecord) iter
191: .next();
192: if (record.getUser().getUserUid().equals(
193: student2.getUserUid())) {
194: fail();
195: }
196: }
197: List section2Students = sectionManager
198: .getSectionTeachingAssistants(section2.getUuid());
199: for (Iterator iter = section2Students.iterator(); iter
200: .hasNext();) {
201: ParticipationRecord record = (ParticipationRecord) iter
202: .next();
203: if (record.getUser().getUserUid().equals(
204: student2.getUserUid())) {
205: fail();
206: }
207: }
208:
209: Assert.assertEquals(1, sectionManager
210: .getTotalEnrollments(section1.getUuid()));
211: Assert.assertEquals(1, sectionManager
212: .getTotalEnrollments(section2.getUuid()));
213:
214: }
215:
216: }
|