    1   /*
    2    * Copyright 2002-2006 The Apache Software Foundation.
    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 org.apache.commons.jexl;
    17
    18  import java.io.File;
    19  import java.net.URL;
    20
    21  import junit.framework.TestCase;
    22
    23  /***
    24   * Tests for ScriptFactory
    25   * @since 1.1
    26   */
    27  public class ScriptFactoryTest extends TestCase {
    28
    29      /***
    30       * Creates a new test case.
    31       * @param name the test case name.
    32       */
    33      public ScriptFactoryTest(String name) {
    34          super(name);
    35      }
    36
    37      /***
    38       * Ensure the factory can create a script from a String
    39       * @throws Exception on a parse error
    40       */
    41      public void testCreateFromString() throws Exception {
    42          String code = ";";
    43          assertNotNull("No script created", ScriptFactory.createScript(code));
    44      }
    45
    46      /***
    47       * Ensure the factory can create a script from a String
    48       * @throws Exception on a parse error
    49       */
    50      public void testCreateFromSimpleString() throws Exception {
    51          String code = "(1 + 2) * 4";
    52          assertNotNull("No script created", ScriptFactory.createScript(code));
    53      }
    54
    55      /***
    56       * Ensure the factory throws an NPE on an null String
    57       * @throws Exception on a parse error
    58       */
    59      public void testCreateFromNullString() throws Exception {
    60          String code = null;
    61          try {
    62              assertNotNull("No script created", ScriptFactory.createScript(code));
    63              fail("Null script created");
    64          } catch (NullPointerException e) {
    65              // expected
    66          }
    67      }
    68
    69      /***
    70       * Ensure the factory can create a script from a file.
    71       * @throws Exception on a parse error.
    72       */
    73      public void testCreateFromFile() throws Exception {
    74          File testScript = new File("src/test-scripts/test1.jexl");
    75          assertNotNull("No script created", ScriptFactory.createScript(testScript));
    76      }
    77
    78      /***
    79       * Ensure the factory throws npe on a null file.
    80       * @throws Exception on a parse error.
    81       */
    82      public void testCreateFromNullFile() throws Exception {
    83          File testScript = null;
    84          try
    85          {
    86              assertNotNull("No script created", ScriptFactory.createScript(testScript));
    87              fail("Null script created");
    88          }
    89          catch (NullPointerException e)
    90          {
    91              // expected
    92          }
    93      }
    94
    95      /***
    96       * Ensure the factory can create a script from a URL.
    97       * @throws Exception on a parse error.
    98       */
    99      public void testCreateFromURL() throws Exception {
    100         URL testUrl = new File("src/test-scripts/test1.jexl").toURL();
    101         assertNotNull("No script created", ScriptFactory.createScript(testUrl));
    102     }
    103
    104     /***
    105      * Ensure the factory throws an NPE on an null URL
    106      * @throws Exception on a parse error
    107      */
    108     public void testCreateFromNullURL() throws Exception {
    109         URL code = null;
    110         try {
    111             assertNotNull("No script created", ScriptFactory.createScript(code));
    112             fail("Null script created");
    113         } catch (NullPointerException e) {
    114             // expected
    115         }
    116     }
    117
    118 }