View Javadoc

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.core.statement.naturallanguage;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import junit.framework.Assert;
24  
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.kuali.student.core.exceptions.OperationFailedException;
28  
29  public class ContextRegistryTest {
30  
31  	private ContextRegistry<DeveloperContext> emptyRegistry;
32  	private ContextRegistry<DeveloperContext> fullRegistry;
33  	
34  	private DeveloperContext context1 = new DeveloperContext();
35  	private DeveloperContext context2 = new DeveloperContext();
36  	private DeveloperContext context3 = new DeveloperContext();
37  
38  	private static class DeveloperContext implements Context<String> {
39  
40  	    public Map<String, Object> createContextMap(String id) throws OperationFailedException {
41  	        Map<String, Object> contextMap = new HashMap<String, Object>();
42  	        contextMap.put(id, id);
43  	        
44  	        return contextMap;
45  	    }
46  	}
47  	
48      @Before
49      public void setUp() throws Exception {
50      	emptyRegistry = new ContextRegistry<DeveloperContext>();
51  		
52  		Map<String, List<DeveloperContext>> map = new HashMap<String, List<DeveloperContext>>();
53  		List<DeveloperContext> list1 = new ArrayList<DeveloperContext>();
54  		list1.add(context1);
55  		list1.add(context2);
56  		map.put("c1", list1);
57  		List<DeveloperContext> list2 = new ArrayList<DeveloperContext>();
58  		list2.add(context3);
59  		map.put("c2", list2);
60  		
61      	fullRegistry = new ContextRegistry<DeveloperContext>(map);
62      }
63  
64      @Test
65  	public void testAdd() throws Exception {
66      	List<DeveloperContext> list = new ArrayList<DeveloperContext>();
67      	DeveloperContext context = new DeveloperContext();
68      	list.add(context);
69  		emptyRegistry.add("123", context);
70  		
71  		Assert.assertNotNull(emptyRegistry.get("123"));
72  		Assert.assertEquals(1, emptyRegistry.get("123").size());
73  		Assert.assertEquals(context, emptyRegistry.get("123").get(0));
74  	}
75  
76  	@Test
77  	public void testGet() throws Exception {
78      	List<DeveloperContext> list = new ArrayList<DeveloperContext>();
79  		DeveloperContext context = new DeveloperContext();
80      	list.add(context);
81  		emptyRegistry.add("123", context);
82  		
83  		Assert.assertNotNull(emptyRegistry.get("123"));
84  		Assert.assertEquals(1, emptyRegistry.get("123").size());
85  		Assert.assertEquals(context, emptyRegistry.get("123").get(0));
86  	}
87  
88  	@Test
89  	public void testGet_MultipleContext() throws Exception {
90  		Assert.assertEquals(2, fullRegistry.size());
91  		Assert.assertNotNull(fullRegistry.get("c1"));
92  		Assert.assertEquals(2, fullRegistry.get("c1").size());
93  		Assert.assertEquals(context1, fullRegistry.get("c1").get(0));
94  		Assert.assertEquals(context2, fullRegistry.get("c1").get(1));
95  		Assert.assertNotNull(fullRegistry.get("c2"));
96  		Assert.assertEquals(1, fullRegistry.get("c2").size());
97  		Assert.assertEquals(context3, fullRegistry.get("c2").get(0));
98  	}
99  
100 	@Test
101 	public void testContainsKey() throws Exception {
102 		DeveloperContext context = new DeveloperContext();
103 		emptyRegistry.add("123", context);
104 		
105 		Assert.assertTrue(emptyRegistry.containsKey("123"));
106 	}
107 
108 	@Test
109 	public void testContainsKey_MultipleContext() throws Exception {
110 		Assert.assertTrue(fullRegistry.containsKey("c1"));
111 		Assert.assertTrue(fullRegistry.containsKey("c2"));
112 	}
113 
114 	@Test
115 	public void testRemove() throws Exception {
116 		DeveloperContext context = new DeveloperContext();
117 		emptyRegistry.add("123", context);
118 		
119 		Assert.assertNotNull(emptyRegistry.get("123"));
120 		emptyRegistry.remove("123");
121 		Assert.assertFalse(emptyRegistry.containsKey("123"));
122 		Assert.assertNull(emptyRegistry.get("123"));
123 	}
124 
125 	@Test
126 	public void testRemove_MultipleContext() throws Exception {
127 		fullRegistry.remove("c1");
128 		Assert.assertFalse(fullRegistry.containsKey("c1"));
129 		Assert.assertNull(fullRegistry.get("c1"));
130 	}
131 }