Package dovetaildb.util

Source Code of dovetaildb.util.UtilTest

package dovetaildb.util;
/*
* Copyright 2008 Phillip Schanely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.Arrays;
import java.util.HashSet;
import junit.framework.TestCase;

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

import dovetaildb.ResultMap;

public class UtilTest extends TestCase {

  @Test
  public void testSencode() {
    assertEquals("sfoobar",Util.sencode("foobar"));
    assertEquals('n',Util.sencode(new Float(1.0f)).charAt(0));
    assertEquals(Util.sencode(new Long(1)),Util.sencode(new Integer(1)));
    assertEquals(Util.sencode(new Double(1.0f)),Util.sencode(new Integer(1)));
    assertEquals(Util.sencode(new Double(1.0f)),Util.sencode(new Float(1.0f)));
    assertTrue(Util.sencode(new Double( 1.0)).compareTo(Util.sencode(new Double( 1.0))) == 0);
    assertTrue(Util.sencode(new Double( 1.1)).compareTo(Util.sencode(new Double( 1.0))) 0);
    assertTrue(Util.sencode(new Double( 1.0)).compareTo(Util.sencode(new Double( 0.0))) 0);
    assertTrue(Util.sencode(new Double(-1.0)).compareTo(Util.sencode(new Double( 0.0))) 0);
    assertTrue(Util.sencode(new Double(-1.0)).compareTo(Util.sencode(new Double( 1.0))) 0);
    assertTrue(Util.sencode(new Double(-1.1)).compareTo(Util.sencode(new Double( 1.0))) 0);
  }

  @Test
  public void testRoundtrip() {
    assertEquals("foobar", Util.sdecode(Util.sencode("foobar")));
    assertEquals(100.0, Util.sdecode(Util.sencode(Long.valueOf(100L))));
    assertEquals(0.0, Util.sdecode(Util.sencode(0.0)));
    assertEquals(-1.0, Util.sdecode(Util.sencode(-1.0)));
    assertEquals(-4.7, Util.sdecode(Util.sencode(-4.7)));
  }
 
  @Test
  public void testGenUuid() {
    HashSet<String> allIds = new HashSet<String>();
    for(int i=0; i<1000; i++) {
      String ret = Util.genUUID();
      boolean isin = allIds.contains(ret);
      assertFalse(isin);
      allIds.add(ret);
    }
  }
 
  @Test
  public void testWrapScriptable() {
    Context ctx = Context.enter();
    ctx.setOptimizationLevel(-1);
    try {
      Scriptable scope = ctx.initStandardObjects();
      //Scriptable jsObj = (Scriptable)ctx.evaluateString(scope, "{\"name\":\"phil\",\"nums\":[3,4,5]}", "inline", 0, null);
      /*
      Object o = ctx.evaluateString(scope, "[{six:'seven'}]", "inline", 0, null);
      System.out.println(o.getClass().toString());
      Object i1 = ((org.mozilla.javascript.NativeArray)o).get(0, scope);
      System.out.println(i1.getClass().toString());
      System.out.println(i1);
      //Scriptable jsObj = (Scriptable)o;
      */
      ResultMap entry;
      Object o;

      o = ctx.evaluateString(scope, "function foo(){return {six:7}};foo()", "inline", 0, null);
      entry = Util.wrapScriptableMap((Scriptable)o);
      assertEquals(7.0,entry.get("six"));
      assertEquals(Util.literalList().a("six"), Arrays.asList(entry.getIds()));

      o = ctx.evaluateString(scope, "function foo(){return {six:['seven','eight']}};foo()", "inline", 0, null);
      entry = Util.wrapScriptableMap((Scriptable)o);
      assertEquals(Util.literalList().a("seven").a("eight"),entry.get("six"));

      o = ctx.evaluateString(scope, "function foo(){return {six:{a:7,b:8}}};foo()", "inline", 0, null);
      entry = Util.wrapScriptableMap((Scriptable)o);
      o = entry.get("six");
      assertEquals(Util.literalMap().p("a",7.0).p("b",8.0),entry.get("six"));

      o = ctx.evaluateString(scope, "function foo(){return {a:7,b:8}};foo()", "inline", 0, null);
      entry = Util.wrapScriptableMap((Scriptable)o);
      assertEquals(Util.literalSet().a(7.0).a(8.0), new HashSet(entry.values()));
     
      //assertEquals(Util.literalList().a(3).a(4).a(5), entry.get("nums"));
    } finally { Context.exit(); }
  }
 
  public void testBytesToInt() {
    byte[] bytes = new byte[]{0,0,0,-128};
    assertEquals(0x80000000L,       Util.leBytesToUInt(bytes, 0));
    assertEquals(Integer.MIN_VALUE, Util.leBytesToInt(bytes, 0));
  }
}
TOP

Related Classes of dovetaildb.util.UtilTest

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.