View Javadoc
1   /*
2    * Copyright 2012 Oracle Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author dolf
34   * 
35   */
36  public class GroovyFactoryTest {
37  
38      /**
39       * Test method for
40       * {@link com.fatwire.gst.foundation.controller.action.support.BaseFactory#getObject(java.lang.String, java.lang.Class)}
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         // first check to see if we get the item back from the ObjectFactory classloader.
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         // now request a Set, which won't be returned from ObjectFactory, and it can't be returned from the local factory
121         list = factory.getObject("foobar2", Set.class);
122         org.junit.Assert.assertNull("Request a Set, which neither factory can return", list);
123 
124         // requesting the collection will return the entry in the ObjectFactory because the returned List is a Collection
125         // so instead request a Set, which won't be returned by ObjectFactory.
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         // request an array list, which ObjectFactory can't return but the root factory can return
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 }