Package com.antonytrupe.server

Source Code of com.antonytrupe.server.GameEngineTest

package com.antonytrupe.server;

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

import java.util.HashMap;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mozilla.javascript.ScriptableObject;

import com.antonytrupe.server.games.GameEngine;
import com.antonytrupe.server.games.GameEngineException;
import com.antonytrupe.server.tend.API;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

public class GameEngineTest extends GAETestCase {

  private static GameEngine ge;

  @Before
  public void before() {
    ge = new GameEngine(new String[] { "/com/antonytrupe/tend/Resource.js",
        "/com/antonytrupe/tend/ResourceGroup.js",
        "/com/antonytrupe/tend/Point.js",
        "/com/antonytrupe/tend/Board.js",
        "/com/antonytrupe/tend/Player.js",
        "/com/antonytrupe/tend/Trade.js",
        "/com/antonytrupe/tend/Settlement.js" });
  }

  @Test
  @Ignore
  public void createSaveBoard() throws GameEngineException {
    API api = new API();
    ScriptableObject board = api.createBoard();

    String json = (String) ge.invoke(board, "stringify");

    Long id = api.getId(board);
    Entity result;
    if (id != null) {
      result = ge.persistence.save("Board", id, "json", json);
    } else {
      result = ge.persistence.save("Board");
      id = result.getKey().getId();
    }
    api.setId(board, result.getKey().getId());

    json = (String) ge.invoke(board, "stringify");

    result = ge.persistence.save("Board", id, "json", json);

    api.update(board, json);

    json = (String) ge.invoke(board, "stringify");

    assertEquals(1, board.get("id"));
  }

  @Test
  public void createBoard() throws GameEngineException {

    ScriptableObject board = ge.execute("new Board();");

    ge.invoke(board, "grow", new Integer[] { 1, 0, 3, 1 });
    // board.grow(1, 0, 3, 1);

    ge.invoke(board, "grow", new Integer[] { 0, 1, 5, 3 });
    // board.grow(0, 1, 5, 3);

    ge.invoke(board, "grow", new Integer[] { 2, 4, 1, 1 });
    // board.grow(2, 4, 1, 1); // return the board data

    assertEquals(32, board.get("size"));

  }

  @Test
  public void testEdgeSave() throws EntityNotFoundException {
    String clazz = "testScriptClazz";
    String name = "testScriptName";
    HashMap<String, Object> values = new HashMap<String, Object>();
    String code = "testScriptCode";
    values.put("code", code);
    ge.persistence.save(clazz, name, values);

    DatastoreService datastore = DatastoreServiceFactory
        .getDatastoreService();

    Entity e = datastore.get(KeyFactory.createKey(clazz, name));

    assertEquals(name, e.getKey().getName());
    assertEquals(clazz, e.getKey().getKind());
    assertEquals(code, ((Text) e.getProperty("code")).getValue());
  }

  @Test
  public void testInternalSave() throws EntityNotFoundException {
    String clazz = "testScriptClazz";
    String name = "testScriptName";
    HashMap<String, Object> values = new HashMap<String, Object>();
    String code = "testScriptCode";
    values.put("code", code);
    ge.persistence.save(clazz, name, values);

    DatastoreService datastore = DatastoreServiceFactory
        .getDatastoreService();

    Entity e = datastore.get(KeyFactory.createKey(clazz, name));

    assertEquals(name, e.getKey().getName());
    assertEquals(clazz, e.getKey().getKind());
    assertEquals(code, ((Text) e.getProperty("code")).getValue());
  }

  @Test
  public void testInternalGet() {
    String clazz = "testScriptClazz";
    String name = "testScriptName";
    HashMap<String, Object> values = new HashMap<String, Object>();
    String code = "testScriptCode";
    values.put("code", code);
    ge.persistence.save(clazz, name, values);

    HashMap<String, Object> o = ge.persistence.get(clazz, name);

    // assertEquals(name, o.get("name"));
    // assertEquals(clazz, o.get("clazz"));
    assertEquals(code, o.get("code"));
  }

  @Test
  @Ignore
  public void testInternalGetAll() {
    String clazz = "testScriptClazz";
    String name = "testScriptName";
    HashMap<String, Object> values = new HashMap<String, Object>();
    String code = "testScriptCode";
    values.put("code", code);
    ge.persistence.save(clazz, name, values);

    HashMap<Object, HashMap<String, Object>> o = ge.persistence
        .getAll(clazz);

    assertEquals(1, o.size());
    assertEquals(code, o.get(0).get("code"));
  }

  @Test
  public void testInternalGetFail() {
    String clazz = "DoesNotExistClazz";
    String name = "DoesNotExistName";
    HashMap<String, Object> r = ge.persistence.get(clazz, name);

    assertEquals(HashMap.class, r.getClass());
    assertNotSame(name, r.get("name"));
  }

  @Test
  public void testInternalGetAllFail() {
    String clazz = "DoesNotExist";
    HashMap<Object, HashMap<String, Object>> r = ge.persistence
        .getAll(clazz);

    assertEquals(HashMap.class, r.getClass());
    assertEquals(0, r.size());
  }

}
TOP

Related Classes of com.antonytrupe.server.GameEngineTest

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.