1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.fatwire.gst.foundation.groovy.context;
17
18 import groovy.lang.GroovyClassLoader;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
27 import org.junit.Test;
28
29 import com.fatwire.gst.foundation.controller.action.Factory;
30 import com.fatwire.gst.foundation.test.MockICS;
31
32
33
34
35
36 public class GroovyFactoryTest {
37
38
39
40
41
42
43 @Test
44 public void testGetObject() {
45
46 MockICS ics = new MockICS();
47 GroovyClassLoader gcl = new GroovyClassLoader();
48
49 gcl.addClasspath("./src/test/groovy");
50
51 GroovyFactory factory = new GroovyFactory(ics, gcl);
52 List<?> list = factory.getObject("foo", List.class);
53 org.junit.Assert.assertNotNull(list);
54
55 }
56
57 @Test
58 public void testGetObject_bad_source() {
59
60 MockICS ics = new MockICS();
61 GroovyClassLoader gcl = new GroovyClassLoader();
62
63 gcl.addClasspath("./src/test/bad-groovy");
64 try {
65
66 GroovyFactory factory = new GroovyFactory(ics, gcl);
67 factory.getObject("foo", List.class);
68 } catch (MultipleCompilationErrorsException e) {
69
70 return;
71 }
72 org.junit.Assert.fail("should not have reached beyond the exception");
73
74 }
75
76 @Test
77 public void testGetObject_no_method() {
78
79 MockICS ics = new MockICS();
80 GroovyClassLoader gcl = new GroovyClassLoader();
81
82 gcl.addClasspath("./src/test/groovy");
83
84 GroovyFactory factory = new GroovyFactory(ics, gcl);
85 Map<?,?> map = factory.getObject("foo", Map.class);
86 org.junit.Assert.assertNull(map);
87
88 }
89
90 @Test
91 public void testGetObject_from_root() {
92
93 MockICS ics = new MockICS();
94 Factory root = new Factory() {
95
96 @SuppressWarnings("unchecked")
97 @Override
98 public <T> T getObject(String name, Class<T> c) {
99 ArrayList<String> al = new ArrayList<String>();
100 al.add("tomato");
101 al.add("salad");
102 return (T) ((al.getClass().isAssignableFrom(c)) ? al: null);
103
104 }
105
106 };
107
108 GroovyClassLoader gcl = new GroovyClassLoader();
109
110 gcl.addClasspath("./src/test/groovy");
111
112 GroovyFactory factory = new GroovyFactory(ics, gcl, root);
113 Collection<?> list;
114
115
116 list = factory.getObject("foobar1", List.class);
117 org.junit.Assert.assertNotNull("Getting object from the ObjectFactory", list);
118 org.junit.Assert.assertEquals("Getting object from the ObjectFactory", 0, list.size());
119
120
121 list = factory.getObject("foobar2", Set.class);
122 org.junit.Assert.assertNull("Request a Set, which neither factory can return", list);
123
124
125
126 list = factory.getObject("foobar3", Collection.class);
127 org.junit.Assert.assertNotNull("Request a collection, which both factories can return - we expect the ObjectFactory one to take precedence", list);
128 org.junit.Assert.assertEquals(0, list.size());
129
130
131 list = factory.getObject("foobar4", ArrayList.class);
132 org.junit.Assert.assertNotNull("Request an ArrayList which only the root can return.", list);
133 org.junit.Assert.assertEquals(2, list.size());
134 }
135 }