View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.renderkit.html.scarborough.standard;
21  
22  import org.junit.After;
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.mozilla.javascript.Context;
26  import org.mozilla.javascript.JavaScriptException;
27  import org.mozilla.javascript.Scriptable;
28  
29  import java.io.File;
30  import java.io.FileReader;
31  import java.io.IOException;
32  
33  public abstract class AbstractJavaScriptTestBase {
34  
35    private Context cx;
36    private Scriptable scope;
37  
38    @Before
39    protected void setUp() throws Exception {
40      cx = Context.enter();
41      scope = cx.initStandardObjects(null);
42    }
43  
44    @After
45    protected void tearDown() throws Exception {
46      Context.exit();
47    }
48  
49    @Test
50    public void testDummy() {
51  
52    }
53  
54    protected Object eval(final String script) {
55      return cx.evaluateString(scope, script, "test", 1, null);
56    }
57  
58    protected int evalInt(final String script) {
59      final Object o = eval(script);
60      if (o instanceof Number) {
61        return ((Number) o).intValue();
62      }
63      throw new JavaScriptException(null, "invalid return type "
64          + o.getClass().getName() + " with value " + o, 0);
65    }
66  
67    protected long evalLong(final String script) {
68      final Object o = eval(script);
69      if (o instanceof Number) {
70        return ((Number) o).longValue();
71      }
72      throw new JavaScriptException(null, "invalid return type "
73          + o.getClass().getName() + " with value " + o, 0);
74    }
75  
76    protected boolean evalBoolean(final String script) {
77      final Object o = eval(script);
78      if (o instanceof Boolean) {
79        return ((Boolean) o).booleanValue();
80      }
81      throw new JavaScriptException(null, "invalid return type "
82          + o.getClass().getName() + " with value " + o, 0);
83    }
84  
85    // XXX directory handling +  Maven reactor current dir problem
86    protected void loadScriptFile(final String jsFile)
87        throws IOException {
88      String fileName
89          = "src/main/resources/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/script/" + jsFile;
90      File file = new File(fileName);
91      if (!file.exists()) {
92        fileName = System.getProperty("basedir") + "/" + fileName;
93        file = new File(fileName);
94      }
95      final FileReader fileReader = new FileReader(file);
96      cx.evaluateReader(scope, fileReader, jsFile, 0, null);
97    }
98  }