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.Arrays;
21 import java.util.Collection;
22 import java.util.List;
23
24 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
25 import org.junit.Test;
26
27 import com.fatwire.gst.foundation.controller.action.Factory;
28 import com.fatwire.gst.foundation.test.MockICS;
29
30
31
32
33
34 public class GroovyFactoryTest {
35
36
37
38
39
40
41 @Test
42 public void testGetObject() {
43
44 MockICS ics = new MockICS();
45 GroovyClassLoader gcl = new GroovyClassLoader();
46
47 gcl.addClasspath("./src/test/groovy");
48
49 GroovyFactory factory = new GroovyFactory(ics, gcl);
50 List<?> list = factory.getObject("foo", List.class);
51 org.junit.Assert.assertNotNull(list);
52
53 }
54
55 @Test
56 public void testGetObject_bad_source() {
57
58 MockICS ics = new MockICS();
59 GroovyClassLoader gcl = new GroovyClassLoader();
60
61 gcl.addClasspath("./src/test/bad-groovy");
62 try {
63
64 GroovyFactory factory = new GroovyFactory(ics, gcl);
65 factory.getObject("foo", List.class);
66 } catch (MultipleCompilationErrorsException e) {
67
68 return;
69 }
70 org.junit.Assert.fail("should not have reached beyond the exception");
71
72 }
73
74 @Test
75 public void testGetObject_no_method() {
76
77 MockICS ics = new MockICS();
78 GroovyClassLoader gcl = new GroovyClassLoader();
79
80 gcl.addClasspath("./src/test/groovy");
81
82 GroovyFactory factory = new GroovyFactory(ics, gcl);
83 Collection<?> list = factory.getObject("foo", Collection.class);
84 org.junit.Assert.assertNull(list);
85
86 }
87
88 @Test
89 public void testGetObject_from_root() {
90
91 MockICS ics = new MockICS();
92 Factory root = new Factory() {
93
94 @SuppressWarnings("unchecked")
95 @Override
96 public <T> T getObject(String name, Class<T> type) {
97 return (T) ((Collection.class.isAssignableFrom(type)) ? Arrays.asList("tomato", "salad") : null);
98
99 }
100
101 };
102
103 GroovyClassLoader gcl = new GroovyClassLoader();
104
105 gcl.addClasspath("./src/test/groovy");
106
107 GroovyFactory factory = new GroovyFactory(ics, gcl, root);
108 Collection<?> list = factory.getObject("foobar", Collection.class);
109 org.junit.Assert.assertNotNull(list);
110 org.junit.Assert.assertEquals(2, list.size());
111
112 }
113 }