1   package com.germinus.merlin.dao;
2   
3   import com.germinus.merlin.model.Course;
4   import com.germinus.merlin.model.assignment.Assignment;
5   import com.germinus.merlin.model.assignment.AssignmentOnline;
6   
7   import java.util.List;
8   /**
9    * @author Jes�s Rodr�guez, G�rminus XXI
10   * @version 
11   * @since 1.0
12   */
13  public class AssigmentDaoTest extends BaseDaoTestCase {
14      private AssignmentDao dao;
15      /** Constants for the test*/
16      private final static int NUM_ASSIGNMENTS = 18;
17      private final static Long[] IDS_ASSIGNMENTS = {1L,2L};
18      @SuppressWarnings("unused")
19  	private final static String [] NAMES_ASSIGNMENTS = {"assignment1","assignment2"};
20      @SuppressWarnings("unused")
21  	private final static Long ID_COURSE = 10264L;
22      /**
23       * 
24       * @param dao
25       * @author Jes�s Rodr�guez, G�rminus XXI
26       * @since 1.0
27       */
28      public void setAssignmentDao(AssignmentDao dao) {
29          this.dao = dao;
30      }
31      /**
32       * Test if the remove method works.  
33       * @throws Exception
34       * @author Jes�s Rodr�guez, G�rminus XXI
35       * @since 1.0
36       */
37      public void testRemoveAssignment() throws Exception {
38          dao.remove(IDS_ASSIGNMENTS[0]);
39          List<Assignment> assignments = dao.getAll();
40          assertEquals(NUM_ASSIGNMENTS-1, assignments.size());
41      }
42      /**
43       * 
44       * @throws Exception
45       * @author Jes�s Rodr�guez, G�rminus XXI
46       * @since 1.0
47       */
48      public void testGetAllAssignmet() throws Exception {
49      	List<Assignment> assignmentsList = dao.getAll();
50      	String assertMsg= new String ("\n\t<Expected> "+NUM_ASSIGNMENTS+" <Actual> "+assignmentsList.size());
51      	assertEquals(assertMsg,NUM_ASSIGNMENTS,assignmentsList.size());
52      }
53      
54      /**
55       * 
56       * @throws Exception
57       * @author Jes�s Rodr�guez, G�rminus XXI
58       * @since 1.0
59       */
60      public void testGetAssignmentInvalid() throws Exception {
61          
62          try {
63          	@SuppressWarnings("unused")
64  			Assignment assignment = dao.get(1L);
65          } catch (Exception e) {
66              assertNotNull(e);
67          }
68          assertTrue(true);
69      }
70      /**
71       * 
72       * @throws Exception
73       * @author Jes�s Rodr�guez, G�rminus XXI
74       * @since 1.0
75       */
76      public void testExistsAssignmentId() throws Exception {
77      	String assertMsg;
78      	for (int i=0; i<IDS_ASSIGNMENTS.length; i++){
79      		assertMsg= new String ("\n\t<Expected> "+"Exist Assignment "+IDS_ASSIGNMENTS[i]+" <Actual> "+"Not exist");
80          	assertTrue(assertMsg,dao.exists(IDS_ASSIGNMENTS[i]));
81          }
82          
83      }
84      /**
85       * 
86       * @throws Exception
87       * @author Jes�s Rodr�guez, G�rminus XXI
88       * @since 1.0
89       */    
90      public void testGetAssignmentsInitials() throws Exception {
91      	String assertMsg = null;
92      	Assignment assignment = null;
93          
94          for (int i=0; i<IDS_ASSIGNMENTS.length; i++)
95          {
96  	        try {
97  	        	assertMsg= new String ("\n\t<Expected> "+"Exist Assignment "+IDS_ASSIGNMENTS[i]+" <Actual> "+"Not exist");
98  	        	assignment = dao.get(IDS_ASSIGNMENTS[i]);
99  	        	assertNotNull(assertMsg,assignment);
100 	            assignment = null;
101 	        } catch (Exception e) {
102 	            assertTrue(assertMsg,false);
103 	        }
104         }
105         
106     }
107     /**
108      * 
109      * @throws Exception
110      * @author Jes�s Rodr�guez, G�rminus XXI
111      * @since 1.0
112      */
113     public void testSaveAssignment() throws Exception {
114     	//We need one course that exist in the system to save a assignment
115     	Assignment assignmentAux = (Assignment) dao.getAll().toArray()[0];
116     	Course courseAux = assignmentAux.getCourse();
117     	//Create the new assignment
118     	Assignment newAssignment = new AssignmentOnline();
119     	newAssignment.setName("Nuevo assignment a a�adir");
120     	newAssignment.setDescription("Descripcion del nuevo assignment");
121     	newAssignment.setCourse(courseAux);
122     	//Save the assignment
123         dao.save(newAssignment);
124         flush();
125 
126         List<Assignment> assignmentsList = dao.getAll();
127         String assertMsg = new String ("\n\t<Expected> "+(NUM_ASSIGNMENTS+1)+" <Actual> "+assignmentsList.size());
128         assertEquals(assertMsg,NUM_ASSIGNMENTS+1, assignmentsList.size());
129         
130     } 
131 }