Package com.antonytrupe.server

Source Code of com.antonytrupe.server.ScriptEngineTest

package com.antonytrupe.server;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.junit.Ignore;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

public class ScriptEngineTest extends GAETestCase {

  @Test
  public void testComment() {
    // //var one="1";
    // "success";
    String script = "//var one=\"1\";\n\"success\";";
    final Context cx = Context.enter();
    final Scriptable scope = cx.initStandardObjects();

    Object result = cx.evaluateString(scope, script, "test", 1, null);

    assertEquals("success", result);
  }

  @Test
  @Ignore
  public void testCommentFromFile() {
    String pathName = "war/test/com/antonytrupe/hex/testSource.js";
    final Context cx = Context.enter();
    final Scriptable scope = cx.initStandardObjects();

    Object result = null;
    try {

      Scanner s = new Scanner(new File(pathName));
      StringBuilder builder = new StringBuilder();
      while (s.hasNextLine()) {
        builder.append("\n").append(s.nextLine());
      }

      result = cx.evaluateString(scope, builder.toString(), "test", 1,
          null);
      s.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    

    assertEquals("success", result);
  }

  @Test
  public void btestB() throws ScriptException {

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    String script = "var one=new Object(); one.two=new Object(); one.two.three=function(){};";
    String objectName = "one";
    // String methodName = "two.three";

    engine.eval(script);

    Object obj = engine.get(objectName);
    assertNotNull(obj);
    // Invocable inv = (Invocable) engine;

    // inv.invokeMethod(obj, methodName);

  }

  @Test
  public void testA() throws ScriptException, NoSuchMethodException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    String script = "function one(map){\n"
        + "var entries=map.entrySet().toArray();\n"
        + "for(entry in entries){\n"
        + "print( entries[entry].getKey());\n"
        + "print( entries[entry].getValue());\n" + "}}"; //$NON-NLS-2$
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("keyOne", "valueOne"); //$NON-NLS-2$
    String functionName = "one";
    engine.eval(script);
    Invocable inv = (Invocable) engine;
    inv.invokeFunction(functionName, map);
  }

  @Test
  public void testC() throws ScriptException, NoSuchMethodException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    String script = "function one(map){\n"
        + "var entries=map.entrySet().toArray();\n"
        + "for(entry in entries){\n"
        + "print( entries[entry].getKey());\n"
        + "print( entries[entry].getValue()[0]);\n" + "}}";
    HashMap<String, String[]> map = new HashMap<String, String[]>();
    map.put("keyOne", new String[] { "valueOne" });
    String functionName = "one";
    engine.eval(script);
    Invocable inv = (Invocable) engine;
    inv.invokeFunction(functionName, map);
  }

  @SuppressWarnings("unchecked")
  @Test
  public void testMemCacheHashMap() throws CacheException {
    Cache cache = CacheManager.getInstance().getCacheFactory()
        .createCache(Collections.emptyMap());
    String name = "name";
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("", "");
    cache.put(name, map);
  }
}
TOP

Related Classes of com.antonytrupe.server.ScriptEngineTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.