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.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   * @author dolf
32   * 
33   */
34  public class GroovyFactoryTest {
35  
36      /**
37       * Test method for
38       * {@link com.fatwire.gst.foundation.controller.action.support.BaseFactory#getObject(java.lang.String, java.lang.Class)}
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 }