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.DebugHelper;
25  import com.fatwire.gst.foundation.controller.action.Action;
26  import com.fatwire.gst.foundation.controller.annotation.InjectForRequest;
27  
28  public class GroovyLoaderTest extends TestCase {
29  
30      public void testLoad() {
31          DiskGroovyLoader loader = new DiskGroovyLoader();
32          loader.bootEngine("./src/test/groovy");
33          loader.precompile();
34          Object a;
35          try {
36              long t = System.nanoTime();
37              a = loader.load("test/MyAction");
38              System.out.println(DebugHelper.microToHuman((System.nanoTime() - t) / 1000L));
39              // System.out.println(a.getClass());
40              if (a instanceof Action) {
41                  Action action = (Action) a;
42                  action.handleRequest(null);
43                  for (Field field : action.getClass().getFields()) {
44                      // System.out.println(field.getName());
45                      InjectForRequest anno = field.getAnnotation(InjectForRequest.class);
46                      if ("foo".equals(field.getName()))
47                          assertNotNull(anno);
48                  }
49                  for (Method method : action.getClass().getMethods()) {
50                      if ("setSomething".equalsIgnoreCase(method.getName())) {
51                          InjectForRequest anno = method.getAnnotation(InjectForRequest.class);
52                          assertNotNull(anno);
53                      }
54                  }
55              } else {
56                  fail("not an Action");
57              }
58          } catch (Exception e) {
59              e.printStackTrace();
60              fail(e.getMessage());
61          }
62      }
63  
64      public void testJavaClass() {
65          //
66          DiskGroovyLoader loader = new DiskGroovyLoader();
67          loader.bootEngine("./src/test/groovy");
68          Object a;
69          try {
70              a = loader.load("com.fatwire.gst.foundation.groovy.action.GroovyActionLocator");
71              assertNotNull(a);
72  
73          } catch (Exception e) {
74              e.printStackTrace();
75              fail(e.getMessage());
76          }
77  
78      }
79  }