View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jci.compilers;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.apache.commons.jci.readers.ResourceReader;
24  import org.apache.commons.jci.stores.MemoryResourceStore;
25  
26  /**
27   * 
28   * @author tcurdt
29   */
30  public final class RhinoJavaCompilerTestCase extends AbstractCompilerTestCase {
31  
32      @Override
33      public JavaCompiler createJavaCompiler() {
34          return new RhinoJavaCompiler();
35      }
36  
37      @Override
38      public String getCompilerName() {
39          return "rhino";
40      }
41  
42      @Override
43      public void testSimpleCompile() throws Exception {
44          final JavaCompiler compiler = createJavaCompiler(); 
45  
46          final ResourceReader reader = new ResourceReader() {
47              final private Map<String, byte[]> sources = new HashMap<String, byte[]>() {
48                  private static final long serialVersionUID = 1L;
49                  {
50                      put("jci/Simple.js", (
51                              " var i = 0;\n" +
52                              "\n"
53                      ).getBytes());
54                  }};
55  
56              public byte[] getBytes( final String pResourceName ) {
57                  return sources.get(pResourceName);
58              }
59  
60              public boolean isAvailable( final String pResourceName ) {
61                  return sources.containsKey(pResourceName);
62              }
63  
64          };
65  
66          final MemoryResourceStore store = new MemoryResourceStore();
67          final CompilationResult result = compiler.compile(
68                  new String[] {
69                          "jci/Simple.js"
70                  }, reader, store);
71  
72          assertEquals(toString(result.getErrors()), 0, result.getErrors().length);
73          assertEquals(toString(result.getWarnings()), 0, result.getWarnings().length);
74  
75          final byte[] clazzBytes = store.read("jci/Simple.class");
76          assertNotNull(clazzBytes);
77          assertTrue(clazzBytes.length > 0);
78      }
79  
80      @Override
81      public void testExtendedCompile() throws Exception {
82      }
83  
84      @Override
85      public void testInternalClassCompile() throws Exception {
86      }
87  
88      @Override
89      public void testUppercasePackageNameCompile() throws Exception {
90          final JavaCompiler compiler = createJavaCompiler(); 
91  
92          final ResourceReader reader = new ResourceReader() {
93              final private Map<String, byte[]> sources = new HashMap<String, byte[]>() {
94                  private static final long serialVersionUID = 1L;
95                  {
96                      put("Jci/Simple.js", (
97                              " var i = 0;\n" +
98                              "\n"
99                      ).getBytes());
100                 }};
101 
102             public byte[] getBytes( final String pResourceName ) {
103                 return sources.get(pResourceName);
104             }
105 
106             public boolean isAvailable( final String pResourceName ) {
107                 return sources.containsKey(pResourceName);
108             }
109 
110         };
111 
112         final MemoryResourceStore store = new MemoryResourceStore();
113         final CompilationResult result = compiler.compile(
114                 new String[] {
115                         "Jci/Simple.js"
116                 }, reader, store);
117 
118         assertEquals(toString(result.getErrors()), 0, result.getErrors().length);
119         assertEquals(toString(result.getWarnings()), 0, result.getWarnings().length);
120 
121         final byte[] clazzBytes = store.read("Jci/Simple.class");
122         assertNotNull(clazzBytes);
123         assertTrue(clazzBytes.length > 0);
124     }
125 
126     @Override
127     public void testCrossReferenceCompilation() throws Exception {
128         // NA
129     }
130 
131     @Override
132     public void testAdditionalTopLevelClassCompile() throws Exception {
133         // NA
134     }
135 
136 }