View Javadoc
1   /*
2    * Copyright 2011 FatWire 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  
17  package com.fatwire.gst.foundation.groovy;
18  
19  import java.lang.reflect.Field;
20  import java.lang.reflect.Method;
21  
22  import junit.framework.TestCase;
23  
24  import com.fatwire.gst.foundation.controller.action.Action;
25  import com.fatwire.gst.foundation.controller.annotation.InjectForRequest;
26  
27  public class GroovyLoaderTest extends TestCase {
28  
29      public void testLoad() {
30          DiskGroovyLoader loader = new DiskGroovyLoader();
31          loader.bootEngine("./src/test/groovy");
32          loader.precompile();
33          Object a;
34          try {
35              a = loader.load(null,"test/MyAction");
36              if (a instanceof Action) {
37                  Action action = (Action) a;
38                  action.handleRequest(null);
39                  for (Field field : action.getClass().getFields()) {
40                      InjectForRequest anno = field.getAnnotation(InjectForRequest.class);
41                      if ("foo".equals(field.getName()))
42                          assertNotNull(anno);
43                  }
44                  for (Method method : action.getClass().getMethods()) {
45                      if ("setSomething".equalsIgnoreCase(method.getName())) {
46                          InjectForRequest anno = method.getAnnotation(InjectForRequest.class);
47                          assertNotNull(anno);
48                      }
49                  }
50              } else {
51                  fail("not an Action");
52              }
53          } catch (Exception e) {
54              e.printStackTrace();
55              fail(e.getMessage());
56          }
57      }
58  
59      public void testJavaClass() {
60          //
61          DiskGroovyLoader loader = new DiskGroovyLoader();
62          loader.bootEngine("./src/test/groovy");
63          Object a;
64          try {
65              a = loader.load(null,"com.fatwire.gst.foundation.groovy.action.GroovyActionLocator");
66              assertNotNull(a);
67  
68          } catch (Exception e) {
69              e.printStackTrace();
70              fail(e.getMessage());
71          }
72  
73      }
74  }