001: /*******************************************************************************
002: * Copyright (c) 2006 The Regents of the University of California, The MIT Corporation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: ******************************************************************************/package org.sakaiproject.tool.gradebook.test;
016:
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Date;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import junit.framework.Assert;
024:
025: import org.sakaiproject.service.gradebook.shared.StaleObjectModificationException;
026: import org.sakaiproject.tool.gradebook.Assignment;
027: import org.sakaiproject.tool.gradebook.Comment;
028: import org.sakaiproject.tool.gradebook.Gradebook;
029:
030: /**
031: * Author:Louis Majanja <louis@media.berkeley.edu>
032: * Date: Oct 24, 2006
033: * Time: 2:20:55 PM
034: */
035: public class GradeCommentTest extends GradebookTestBase {
036:
037: protected Gradebook gradebook;
038:
039: protected void onSetUpInTransaction() throws Exception {
040: super .onSetUpInTransaction();
041: // Create a gradebook to work with
042: String className = this .getClass().getName();
043: String gradebookName = className + (new Date()).getTime();
044: gradebookFrameworkService.addGradebook(gradebookName,
045: gradebookName);
046:
047: // Set up a holder for enrollments, teaching assignments, and sections.
048: integrationSupport.createCourse(gradebookName, gradebookName,
049: false, false, false);
050:
051: // Grab the gradebook for use in the tests
052: gradebook = gradebookManager.getGradebook(gradebookName);
053: }
054:
055: public void testAssignmentGradeComments() throws Exception {
056: // Create enrollment records.
057: List studentUids = Arrays.asList(new String[] {
058: "testStudentUserUid1", "testStudentUserUid2",
059: "testStudentUserUid3", });
060: addUsersEnrollments(gradebook, studentUids);
061:
062: // Create an asssignment.
063: Long asnId = gradebookManager.createAssignment(gradebook
064: .getId(), "Scores Entered Test", new Double(10),
065: new Date(), Boolean.FALSE, Boolean.FALSE);
066: Assignment asn = gradebookManager.getAssignmentWithStats(asnId);
067:
068: // Make sure comments start off as null.
069: List persistentComments = gradebookManager.getComments(asn,
070: studentUids);
071: Assert.assertTrue(persistentComments.isEmpty());
072:
073: // Add a comment.
074: List comments = new ArrayList();
075: comments.add(new Comment((String) studentUids.get(0),
076: "First Comment", asn));
077: gradebookManager.updateComments(comments);
078:
079: // Make sure we stored just the one comment.
080: persistentComments = gradebookManager.getComments(asn,
081: studentUids);
082: Assert.assertTrue(persistentComments.size() == 1);
083: Comment comment = (Comment) persistentComments.get(0);
084: Assert.assertTrue(comment.getCommentText().equals(
085: "First Comment"));
086:
087: // Leave the first comment as is, add a comment.
088: comments = new ArrayList();
089: comments.add(new Comment((String) studentUids.get(1),
090: "Next comment", asn));
091: comment.setCommentText("");
092: comments.add(comment);
093: gradebookManager.updateComments(comments);
094:
095: persistentComments = gradebookManager.getComments(asn,
096: studentUids);
097: Assert.assertTrue(persistentComments.size() == 2);
098: for (Iterator iter = persistentComments.iterator(); iter
099: .hasNext();) {
100: comment = (Comment) iter.next();
101: if (comment.getStudentId().equals(studentUids.get(0))) {
102: Assert
103: .assertTrue(comment.getCommentText().length() == 0);
104: }
105: }
106:
107: // Currently the Student View reads comments from the database
108: // into an ArrayList
109: List studentComments = gradebookManager
110: .getStudentAssignmentComments((String) studentUids
111: .get(1), gradebook.getId());
112: Iterator iter = studentComments.iterator();
113: while (iter.hasNext()) {
114: Comment asnComment = (Comment) iter.next();
115: if (asnComment.getStudentId().equals(studentUids.get(1)))
116: Assert.assertTrue(asnComment.getCommentText().equals(
117: "Next comment"));
118: if (asnComment.getStudentId().equals(studentUids.get(2)))
119: Assert.assertTrue(asnComment == null);
120: }
121:
122: // Make sure we emulate an optimistic locking failure when we try
123: // to create a new comment record that's already in the database.
124: // (This test has to go last, since it will cause transaction
125: // rollback.)
126: comments = new ArrayList();
127: comments.add(new Comment((String) studentUids.get(0), "Oops",
128: asn));
129: try {
130: gradebookManager.updateComments(comments);
131: fail();
132: } catch (StaleObjectModificationException e) {
133: }
134:
135: }
136:
137: }
|