io.starter.TestReact.java Source code

Java tutorial

Introduction

Here is the source code for io.starter.TestReact.java

Source

/**
 * this file licensed under Apache-style FOSS license
 * 
 * Copyright 2015, Starter Inc.
 */
package io.starter;

import static org.junit.Assert.assertEquals;
import io.starter.reactjs.JSXTransformer;
import io.starter.reactjs.ignite.Generator;

import java.net.URISyntaxException;
import java.util.Arrays;

import org.codehaus.jettison.json.JSONArray;
import org.json.JSONObject;
import org.junit.*;

/**
 * @author john
 *
 */
public class TestReact extends TestBase {

    @Test
    /**
     * Test the basic transformation of a simple JSX content
     * 
     * @throws URISyntaxException
     */
    public void TestBasicTransform() throws URISyntaxException {
        JSXTransformer jsxTransformer = new JSXTransformer();
        // Using the CDN does not work
        // jsxTransformer.setModulePaths(Arrays.asList("http://fb.me/"));
        jsxTransformer.setModulePaths(Arrays.asList("public"));
        jsxTransformer.setJsxTransformerJS(
                System.getProperty("user.dir") + "/src/main/react/js/JSXTransformer-0.13.3.js");
        jsxTransformer.init();
        String js = jsxTransformer
                .transform(componentInitCodeWeb + "<h1>Hello, world!</h1>,document.getElementById('example'));");
        assertEquals(
                "/** @jsx React.DOM */ React.renderComponent(React.createElement(\"h1\", null, \"Hello, world!\"),document.getElementById('example'));",
                js);
    }

    public static String indent = "    ";

    public static String componentInitCodeWeb = "/** @jsx React.DOM */ React.renderComponent(" + indent + indent
            + "${component.content}" + ");";

    public JSONObject getJSON() {
        JSONObject dtx = new JSONObject();

        dtx.put("name", "Object 2");
        dtx.put("description", "Test Object Number Two");

        // set a url
        String urx = "/table_view.jsp?tablename=User&query=id:";
        dtx.put("url", urx);

        // add children
        JSONArray jarr = new JSONArray();
        for (int t = 0; t < 5; t++) {
            JSONObject jab = new JSONObject();
            jab.put("name", "Location " + t);
            jab.put("latitude", (t * (430 * Math.random())) + "." + Math.random());
            jab.put("longitude", (t * (1300 * Math.random())) + "." + Math.random());

            jarr.put(jab);
        }

        dtx.put("locations", jarr);

        return dtx;
    }

    /**
     * generate jsx from a JSON object
     */
    @Test
    @Ignore(value = "until we implement")
    public void generateReactJSXFromJSON() {

        JSONObject dtx = getJSON();
        String jsx = Generator.getJSXFromJSON(dtx);
        System.err.println(jsx);
    }

    /**
     * generate jsx from a JSON object
     */
    @Test
    public void generateReactNativeJSXFromJSON() {

        JSONObject dtx = getJSON();

        String jsx = Generator.getJSXFromJSON(dtx);
        System.err.println(jsx);

    }

}