Clover Coverage Report - Kuali Student 1.2-SNAPSHOT (Aggregated)
Coverage timestamp: Thu Mar 3 2011 04:02:59 EST
137   365   19   10.54
2   207   0.14   13
13     1.46  
1    
 
  ApplicationStateDaoImplTest       Line # 35 137 0% 19 7 95.4% 0.95394737
 
  (10)
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
14    */
15   
16    package org.kuali.student.common.ui.server.applicationstate.dao.impl;
17   
18    import java.util.ArrayList;
19    import java.util.HashMap;
20    import java.util.List;
21    import java.util.Map;
22   
23    import org.junit.Assert;
24    import org.junit.Test;
25    import org.kuali.student.common.exceptions.AlreadyExistsException;
26    import org.kuali.student.common.exceptions.DoesNotExistException;
27    import org.kuali.student.common.test.spring.AbstractTransactionalDaoTest;
28    import org.kuali.student.common.test.spring.Dao;
29    import org.kuali.student.common.test.spring.PersistenceFileLocation;
30    import org.kuali.student.common.ui.server.applicationstate.dao.ApplicationStateDao;
31    import org.kuali.student.common.ui.server.applicationstate.entity.ApplicationState;
32    import org.kuali.student.common.ui.server.applicationstate.entity.KeyValuePair;
33   
34    @PersistenceFileLocation("classpath:META-INF/application-state-persistence.xml")
 
35    public class ApplicationStateDaoImplTest extends AbstractTransactionalDaoTest {
36   
37    @Dao(value = "org.kuali.student.common.ui.server.applicationstate.dao.impl.ApplicationStateDaoImpl")
38    public ApplicationStateDao dao;
39   
 
40  5 toggle private Map<String, String> getMap(List<KeyValuePair> list) {
41  5 Map<String, String> map = new HashMap<String, String>();
42  5 for(KeyValuePair pair : list) {
43  11 map.put(pair.getKey(), pair.getValue());
44    }
45  5 return map;
46    }
47   
 
48  8 toggle private List<KeyValuePair> getKeyValuePairList(int startIndex, int n) {
49  8 List<KeyValuePair> list = new ArrayList<KeyValuePair>();
50  28 for(int i=startIndex; i<(startIndex+n); i++) {
51  20 list.add(new KeyValuePair("key-"+i, "value-"+i));
52    }
53  8 return list;
54    }
55   
 
56  10 toggle private ApplicationState getApplicationState(String appId, String refKey, String refType, String userId, List<KeyValuePair> list) {
57  10 ApplicationState appState = new ApplicationState();
58  10 appState.setApplicationId(appId);
59  10 appState.setKeyValueList(list);
60  10 appState.setReferenceKey(refKey);
61  10 appState.setReferenceType(refType);
62  10 appState.setUserId(userId);
63  10 return appState;
64    }
65   
 
66  1 toggle @Test
67    public void testUpdateApplicationState1() throws Exception {
68  1 List<KeyValuePair> list = getKeyValuePairList(1,3);
69  1 ApplicationState appState = getApplicationState("1", "2", "course", null, list);
70  1 ApplicationState newAppState = dao.createApplicationState(appState);
71   
72  1 ApplicationState getAppState = dao.getApplicationState("1", "2", "course");
73   
74  1 list = new ArrayList<KeyValuePair>();
75  1 list.add(new KeyValuePair("k1", "v1"));
76  1 list.add(new KeyValuePair("k2", "v2"));
77  1 getAppState.setKeyValueList(list);
78   
79  1 dao.update(getAppState);
80   
81  1 getAppState = dao.getApplicationState("1", "2", "course");
82  1 Assert.assertNotNull(getAppState);
83  1 Assert.assertEquals(newAppState.toString(), getAppState.toString());
84  1 Assert.assertEquals(newAppState.getKeyValueList().toString(), getAppState.getKeyValueList().toString());
85   
86  1 ApplicationState getAppState2 = dao.fetch(ApplicationState.class, newAppState.getId());
87  1 Assert.assertEquals(getAppState.toString(), getAppState2.toString());
88  1 Assert.assertEquals(getAppState.getKeyValueList().toString(), getAppState2.getKeyValueList().toString());
89   
90  1 dao.delete(ApplicationState.class, newAppState.getId());
91    }
92   
 
93  1 toggle @Test
94    public void testUpdateApplicationState2() throws Exception {
95  1 List<KeyValuePair> list = getKeyValuePairList(1,3);
96  1 ApplicationState appState = getApplicationState("1", "2", "course", null, list);
97  1 ApplicationState newAppState = dao.createApplicationState(appState);
98   
99  1 ApplicationState getAppState = dao.getApplicationState("1", "2", "course");
100   
101  1 getAppState.setApplicationId("A");
102  1 getAppState.setReferenceKey("B");
103  1 getAppState.setReferenceType("program");
104  1 list = new ArrayList<KeyValuePair>();
105  1 list.add(new KeyValuePair("k1", "v1"));
106  1 list.add(new KeyValuePair("k2", "v2"));
107  1 getAppState.setKeyValueList(list);
108   
109  1 dao.update(getAppState);
110   
111  1 getAppState = dao.getApplicationState("A", "B", "program");
112   
113  1 Assert.assertNotNull(getAppState);
114  1 Assert.assertEquals(newAppState.toString(), getAppState.toString());
115  1 Assert.assertEquals(newAppState.getKeyValueList().toString(), getAppState.getKeyValueList().toString());
116   
117  1 ApplicationState getAppState2 = dao.fetch(ApplicationState.class, newAppState.getId());
118  1 Assert.assertEquals(getAppState.toString(), getAppState2.toString());
119  1 Assert.assertEquals(getAppState.getKeyValueList().toString(), getAppState2.getKeyValueList().toString());
120   
121  1 dao.delete(ApplicationState.class, newAppState.getId());
122    }
123   
 
124  1 toggle @Test
125    public void testCreateGetApplicationState1() throws Exception {
126  1 List<KeyValuePair> list = getKeyValuePairList(1,3);
127  1 ApplicationState appState = getApplicationState("1", "2", "course", null, list);
128  1 ApplicationState newAppState = dao.createApplicationState(appState);
129   
130  1 ApplicationState getAppState = dao.getApplicationState("1", "2", "course");
131   
132  1 Assert.assertNotNull(getAppState);
133  1 Map<String, String> map2 = getMap(getAppState.getKeyValueList());
134   
135  1 Assert.assertEquals(3, map2.size());
136  1 Assert.assertEquals("value-1", map2.get("key-1"));
137  1 Assert.assertEquals("value-2", map2.get("key-2"));
138  1 Assert.assertEquals("value-3", map2.get("key-3"));
139   
140  1 dao.delete(ApplicationState.class, newAppState.getId());
141    }
142   
 
143  1 toggle @Test
144    public void testGetApplicationState_NoApplicationState() throws Exception {
145  1 try {
146  1 dao.getApplicationState("1", "2", "course");
147  0 Assert.fail("Should have thrown an exception for no application state");
148    } catch(DoesNotExistException e) {
149  1 Assert.assertNotNull(e);
150    }
151    }
152   
 
153  1 toggle @Test
154    public void testGetApplicationState_NoApplicationStateforUserId() throws Exception {
155  1 try {
156  1 dao.getApplicationState("1", "2", "course", "wil");
157  0 Assert.fail("Should have thrown an exception for no application state");
158    } catch(DoesNotExistException e) {
159  1 Assert.assertNotNull(e);
160    }
161    }
162   
 
163  1 toggle @Test
164    public void testCreateApplicationState_NullParameters() throws Exception {
165  1 try {
166  1 ApplicationState appState = getApplicationState(null, null, null, null, null);
167  1 dao.createApplicationState(appState);
168  0 Assert.fail("Should have thrown a persistence exception for null parameters");
169    } catch(javax.persistence.PersistenceException e) {
170  1 Assert.assertNotNull(e);
171    }
172    }
173   
 
174  1 toggle @Test
175    public void testCreateApplicationState1_Error_DuplicateKey() throws Exception {
176  1 List<KeyValuePair> list = new ArrayList<KeyValuePair>();
177  1 list.add(new KeyValuePair("key-1", "value-1"));
178  1 ApplicationState appState = getApplicationState("1", "2", "course", null, list);
179   
180  1 ApplicationState newAppState1 = null;
181   
182  1 try {
183  1 newAppState1 = dao.createApplicationState(appState);
184  1 dao.createApplicationState(appState);
185  0 dao.getApplicationState("1", "2", "course");
186  0 Assert.fail("Should have thrown a duplicate key persistence exception");
187    } catch(AlreadyExistsException e ) {
188  1 Assert.assertTrue(true);
189  1 Assert.assertTrue(e.getMessage() != null);
190    } finally {
191  1 dao.delete(ApplicationState.class, newAppState1.getId());
192    }
193    }
194   
 
195  1 toggle @Test
196    public void testCreateApplicationState1_Error_DuplicateKeyUserId() throws Exception {
197  1 ApplicationState newAppState = null;
198  1 List<KeyValuePair> list = getKeyValuePairList(1,3);
199  1 ApplicationState appState = getApplicationState("1", "2", "course", "tom", list);
200   
201  1 try {
202  1 newAppState = dao.createApplicationState(appState);
203  1 dao.createApplicationState(appState);
204  0 dao.getApplicationState("1", "2", "course");
205  0 Assert.fail("Should have thrown a duplicate key persistence exception");
206    } catch(AlreadyExistsException e ) {
207  1 Assert.assertTrue(true);
208  1 Assert.assertTrue(e.getMessage() != null);
209    } finally {
210  1 dao.delete(newAppState);
211    }
212    }
213   
 
214  1 toggle @Test
215    public void testCreateGetApplicationState3() throws Exception {
216  1 List<KeyValuePair> list1 = getKeyValuePairList(1,2);
217  1 ApplicationState appState1 = getApplicationState("1", "2", "course1", null, list1);
218  1 ApplicationState newAppState1 = dao.createApplicationState(appState1);
219   
220  1 List<KeyValuePair> list2 = getKeyValuePairList(3,2);
221  1 ApplicationState appState2 = getApplicationState("11", "22", "course2", null, list2);
222  1 ApplicationState newAppState2 = dao.createApplicationState(appState2);
223   
224  1 ApplicationState getAppState1 = dao.getApplicationState("1", "2", "course1");
225  1 Assert.assertNotNull(getAppState1);
226  1 Map<String, String> returnedMap1 = getMap(getAppState1.getKeyValueList());
227  1 Assert.assertEquals(2, returnedMap1.size());
228  1 Assert.assertEquals("value-1", returnedMap1.get("key-1"));
229  1 Assert.assertEquals("value-2", returnedMap1.get("key-2"));
230   
231  1 ApplicationState getAppState2 = dao.getApplicationState("11", "22", "course2");
232  1 Assert.assertNotNull(getAppState2);
233  1 Map<String, String> returnedMap2 = getMap(getAppState2.getKeyValueList());
234  1 Assert.assertEquals(2, returnedMap2.size());
235  1 Assert.assertEquals("value-3", returnedMap2.get("key-3"));
236  1 Assert.assertEquals("value-4", returnedMap2.get("key-4"));
237   
238  1 dao.delete(newAppState1);
239  1 dao.delete(newAppState2);
240    }
241   
 
242  1 toggle @Test
243    public void testGetApplicationStateByAppIdRefIdRefTypeUserId() throws Exception {
244  1 List<KeyValuePair> list1 = getKeyValuePairList(1,2);
245  1 ApplicationState appState1 = getApplicationState("1", "2", "course1", "tom", list1);
246  1 ApplicationState newAppState1 = dao.createApplicationState(appState1);
247   
248  1 List<KeyValuePair> list2 = getKeyValuePairList(3,2);
249  1 ApplicationState appState2 = getApplicationState("11", "22", "course2", "jim", list2);
250  1 ApplicationState newAppState2 = dao.createApplicationState(appState2);
251   
252  1 ApplicationState as1 = dao.getApplicationState("1", "2", "course1", "tom");
253  1 Map<String, String> returnedMap1 = getMap(as1.getKeyValueList());
254  1 Assert.assertEquals(2, returnedMap1.size());
255  1 Assert.assertEquals("value-1", returnedMap1.get("key-1"));
256  1 Assert.assertEquals("value-2", returnedMap1.get("key-2"));
257   
258  1 ApplicationState as2 = dao.getApplicationState("11", "22", "course2", "jim");
259  1 Map<String, String> returnedMap2 = getMap(as2.getKeyValueList());
260  1 Assert.assertEquals(2, returnedMap2.size());
261  1 Assert.assertEquals("value-3", returnedMap2.get("key-3"));
262  1 Assert.assertEquals("value-4", returnedMap2.get("key-4"));
263   
264  1 dao.delete(newAppState1);
265  1 dao.delete(newAppState2);
266    }
267   
268    /*
269    @Test
270    public void testGetApplicationState_User_Perf1() throws Exception {
271    int n = 100;
272    long t1 = System.nanoTime();
273    List<KeyValuePair> list1 = getKeyValuePairList(0,n);
274    ApplicationState appState1 = getApplicationState("1", "2", "course1", "lisa", list1);
275    dao.createApplicationState(appState1);
276    long t2 = System.nanoTime();
277    long total = (t2-t1)/1000000;
278    System.out.println("\ntestGetApplicationState_User_Perf1:createApplicationState(" + n + ") = " + total + " millis");
279   
280    t1 = System.nanoTime();
281    ApplicationState appState = dao.getApplicationState("1", "2", "course1", "lisa");
282    t2 = System.nanoTime();
283    total = (t2-t1)/1000000;
284    Assert.assertEquals(n, appState.getKeyValueList().size());
285    System.out.println("testGetApplicationState_User_Perf1:getApplicationState(" + n + ") = " + total + " millis");
286    }
287   
288    @Test
289    public void testGetApplicationState_User_Perf2() throws Exception {
290    int n = 100;
291    long t1 = System.nanoTime();
292    for(int i=0; i<n; i++) {
293    List<KeyValuePair> list = getKeyValuePairList(1,1);
294    ApplicationState appState = getApplicationState("appId"+i, "refId"+i, "refType"+i, "tom"+i, list);
295    dao.createApplicationState(appState);
296    }
297    long t2 = System.nanoTime();
298    long total = (t2-t1)/1000000;
299    System.out.println("\ntestGetApplicationState_User_Perf2:createApplicationState(" + n + ") = " + total + " millis");
300   
301    t1 = System.nanoTime();
302    for(int i=0; i<n; i++) {
303    ApplicationState appState = dao.getApplicationState("appId"+i, "refId"+i, "refType"+i, "tom"+i);
304    }
305    t2 = System.nanoTime();
306    total = (t2-t1)/1000000;
307    System.out.println("testGetApplicationState_User_Perf2:getApplicationState(" + n + ") = " + total + " millis");
308    }
309   
310    @Test
311    public void testGetApplicationState_User_Perf3() throws Exception {
312    int n = 100;
313    long t1 = System.nanoTime();
314    for(int i=0; i<n; i++) {
315    List<KeyValuePair> list = getKeyValuePairList(0,n/5);
316    ApplicationState appState = getApplicationState("appId"+i, "refId"+i, "refType"+i, "tom"+i, list);
317    dao.createApplicationState(appState);
318    }
319    long t2 = System.nanoTime();
320    long total = (t2-t1)/1000000;
321    System.out.println("\ntestGetApplicationState_User_Perf3:createApplicationState(" + n + ") = " + total + " millis");
322   
323    t1 = System.nanoTime();
324    for(int i=0; i<n; i++) {
325    ApplicationState appState = dao.getApplicationState("appId"+i, "refId"+i, "refType"+i, "tom"+i);
326    }
327    t2 = System.nanoTime();
328    total = (t2-t1)/1000000;
329    System.out.println("testGetApplicationState_User_Perf3:getApplicationState(" + n + ") = " + total + " millis");
330    }
331   
332    @Test
333    public void testCreateApplicationStateCollection_Perf4() throws Exception {
334    int n = 100;
335    List<ApplicationState> newAppStateList = new ArrayList<ApplicationState>();
336    for(int i=0; i<n; i++) {
337    List<KeyValuePair> list = getKeyValuePairList(1,n/5);
338    ApplicationState appState = getApplicationState("appId-1", "refKey"+i, "course"+i, null, list);
339    newAppStateList.add(appState);
340    }
341    long t1 = System.nanoTime();
342    List<String> idList = dao.createApplicationState(newAppStateList);
343    long t2 = System.nanoTime();
344    long total = (t2-t1)/1000000;
345    System.out.println("\ntestCreateApplicationStateCollection_Perf4:createApplicationState(" + n + ") = " + total + " millis");
346   
347    t1 = System.nanoTime();
348    List<ApplicationState> appStateList = dao.getApplicationState("appId-1");
349    t2 = System.nanoTime();
350    total = (t2-t1)/1000000;
351    System.out.println("testCreateApplicationStateCollection_Perf4:getApplicationState(" + n + ") = " + total + " millis");
352   
353    Assert.assertEquals(n, appStateList.size());
354    Map<String, String> map2 = getMap(appStateList.get(0).getKeyValueList());
355   
356    t1 = System.nanoTime();
357    for(String id : idList) {
358    dao.delete(ApplicationState.class, id);
359    }
360    t2 = System.nanoTime();
361    total = (t2-t1)/1000000;
362    System.out.println("testCreateApplicationStateCollection_Perf4:delete(" + n + ") = " + total + " millis");
363    }
364    */
365    }