Java Code Examples for org.mozilla.javascript.NativeJavaObject#unwrap()

The following examples show how to use org.mozilla.javascript.NativeJavaObject#unwrap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CelosWorkflowConfigurationParserTest.java    From celos with Apache License 2.0 6 votes vote down vote up
@Test
public void testTakesChangesUsername() throws Exception {

    URL resource = Thread.currentThread().getContextClassLoader().getResource("com/collective/celos/defaults-oozie-props");
    File defaults = new File(resource.toURI());

    WorkflowConfigurationParser parser = new WorkflowConfigurationParser(defaults, ImmutableMap.of("CELOS_USER_JS_VAR", "nameIsChanged"));

    String func = "function (slotId) {" +
            "        return {" +
            "            \"oozie.wf.application.path\": \"/workflow.xml\"," +
            "            \"inputDir\": \"/input\"," +
            "            \"outputDir\": \"/output\"" +
            "        }" +
            "    }";

    String str = "importDefaults(\"test\"); celos.makePropertiesGen(" + func + "); ";
    NativeJavaObject jsResult = (NativeJavaObject) parser.evaluateReader(new StringReader(str), "string",  new MemoryStateDatabase().openConnection());
    PropertiesGenerator generator = (PropertiesGenerator) jsResult.unwrap();
    Assert.assertEquals(generator.getProperties(null).get("user.name").asText(), "nameIsChanged");
}
 
Example 2
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 6 votes vote down vote up
@Test
public void testHiveInputWithData() throws Exception {

    String tableCreationScript = "table creation script";
    String js =
            "var table = ci.fixTable([\"col1\", \"col2\"], [[\"row1\", \"row2\"],[\"row11\", \"row22\"]]);" +
            "ci.hiveInput(\"dbname\", \"tablename\", ci.fixFile(\"" + tableCreationScript + "\"), table)";

    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader(js), "string");
    HiveTableDeployer hiveTableDeployer = (HiveTableDeployer) creatorObj.unwrap();

    Assert.assertEquals(hiveTableDeployer.getDatabaseName(), new DatabaseName("dbname"));
    Assert.assertEquals(hiveTableDeployer.getTableName(), "tablename");

    FixFile tableCreationFile = hiveTableDeployer.getTableCreationScriptFile().create(null);
    Assert.assertEquals(IOUtils.toString(tableCreationFile.getContent()), tableCreationScript);
    Assert.assertEquals(hiveTableDeployer.getDataFileCreator().getClass(), StringArrayFixTableCreator.class);
}
 
Example 3
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 6 votes vote down vote up
@Test
public void testFixDir() throws Exception {
    String js = "" +
            "ci.fixDir({" +
            "   file1: ci.fixFile('123')," +
            "   file2: ci.fixFile('234')" +
            "})";

    TestConfigurationParser parser = new TestConfigurationParser();
    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader(js), "string");
    FixDirHierarchyCreator creator = (FixDirHierarchyCreator) creatorObj.unwrap();
    FixDir fixDir = creator.create(null);

    Assert.assertEquals(fixDir.getChildren().size(), 2);
    Assert.assertTrue(IOUtils.contentEquals(fixDir.getChildren().get("file1").asFile().getContent(), new ByteArrayInputStream("123".getBytes())));
    Assert.assertTrue(IOUtils.contentEquals(fixDir.getChildren().get("file2").asFile().getContent(), new ByteArrayInputStream("234".getBytes())));
}
 
Example 4
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 6 votes vote down vote up
@Test
public void testHiveInput() throws Exception {

    String tableCreationScript = "table creation script";
    String js = "ci.hiveInput(\"dbname\", \"tablename\", ci.fixFile(\"" + tableCreationScript + "\"))";

    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader(js), "string");
    HiveTableDeployer hiveTableDeployer = (HiveTableDeployer) creatorObj.unwrap();

    FixFile tableCreationFile = hiveTableDeployer.getTableCreationScriptFile().create(null);

    Assert.assertEquals(hiveTableDeployer.getDatabaseName(), new DatabaseName("dbname"));
    Assert.assertEquals(hiveTableDeployer.getTableName(), "tablename");
    Assert.assertEquals(IOUtils.toString(tableCreationFile.getContent()), tableCreationScript);
    Assert.assertNull(hiveTableDeployer.getDataFileCreator());
}
 
Example 5
Source File: RhinoExpression.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Evaluates the defined expression. If an exception or an evaluation error occures, the evaluation returns null and
 * the error is logged. The current datarow and a copy of the expressions properties are set to script-internal
 * variables. Changes to the properties will not alter the expressions original properties and will be lost when the
 * evaluation is finished.
 * <p/>
 * Expressions do not maintain a state and no assumptions about the order of evaluation can be made.
 *
 * @return the evaluated value or null.
 */
public Object getValue() {
  if ( expression == null ) {
    return null;
  }

  LegacyDataRowWrapper wrapper = null;
  try {
    final ContextFactory contextFactory = new ContextFactory();
    final Context context = contextFactory.enterContext();
    final Scriptable scope = context.initStandardObjects();
    wrapper = initializeScope( scope );

    final Object o = context.evaluateString( scope, expression, getName(), 1, null );
    if ( o instanceof NativeJavaObject ) {
      final NativeJavaObject object = (NativeJavaObject) o;
      return object.unwrap();
    }
    return o;
  } finally {
    if ( wrapper != null ) {
      wrapper.setParent( null );
    }
    Context.exit();
  }
}
 
Example 6
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test(expected = JavaScriptException.class)
public void testRecursiveFsObjectComparer3() throws IOException {
    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader("ci.plainCompare(ci.fixFileFromResource(\"stuff\"))"), "string");
    RecursiveFsObjectComparer creator = (RecursiveFsObjectComparer) creatorObj.unwrap();
}
 
Example 7
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpandJson() throws IOException {
    String js = "ci.expandJson(ci.tableToJson(ci.hiveTable(\"dbname\", \"tablename\")), [\"field1\", \"field2\"])";

    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader(js), "string");
    ConversionCreator creator = (ConversionCreator) creatorObj.unwrap();
    Assert.assertEquals(JsonExpandConverter.class, creator.getFixObjectConverter().getClass());
}
 
Example 8
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testFixTableComparerOrdered() throws IOException {
    String js =
            "var table1 = ci.fixTable([\"col1\", \"col2\"], [[\"row1\", \"row2\"],[\"row11\", \"row22\"]]);" +
                    "var table2 = ci.fixTable([\"col1\", \"col2\"], [[\"row1\", \"row2\"],[\"row11\", \"row22\"]]);" +
                    "ci.fixTableCompare(table1, table2, true, true);";

    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader(js), "string");
    FixTableComparer comparer = (FixTableComparer) creatorObj.unwrap();
    Assert.assertEquals(true, comparer.isColumnNamesOrdered());
    Assert.assertEquals(true, comparer.isRespectRowOrder());

}
 
Example 9
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test(expected = JavaScriptException.class)
public void testJsonCompareFails() throws IOException {
    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader("ci.jsonCompare(ci.fixDirFromResource(\"stuff\"))"), "string");

    JsonContentsComparer comparer = (JsonContentsComparer) creatorObj.unwrap();
    Assert.assertEquals(comparer.getIgnorePaths(), new HashSet(Lists.newArrayList("path1", "path2")));
}
 
Example 10
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testJsonCompare2() throws IOException {
    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader("ci.jsonCompare(ci.fixDirFromResource(\"stuff\"), ci.fixDirFromResource(\"stuff\"), [\"path1\", \"path2\"])"), "string");

    JsonContentsComparer comparer = (JsonContentsComparer) creatorObj.unwrap();
    Assert.assertEquals(comparer.getIgnorePaths(), new HashSet(Lists.newArrayList("path1", "path2")));
}
 
Example 11
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testJsonCompare() throws IOException {
    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader("ci.jsonCompare(ci.fixDirFromResource(\"stuff\"), ci.fixDirFromResource(\"stuff\"))"), "string");

    JsonContentsComparer comparer = (JsonContentsComparer) creatorObj.unwrap();
    Assert.assertEquals(comparer.getIgnorePaths(), new HashSet<String>());
}
 
Example 12
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testAvroToJson() throws IOException {
    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader("ci.avroToJson(\"1\")"), "string");

    ConversionCreator converter = (ConversionCreator) creatorObj.unwrap();

    Assert.assertTrue(converter.getFixObjectConverter() instanceof FixDirRecursiveConverter);
    FixDirRecursiveConverter fixDirRecursiveConverter = (FixDirRecursiveConverter) converter.getFixObjectConverter();
    Assert.assertTrue(fixDirRecursiveConverter.getFixFileConverter() instanceof AvroToJsonConverter);
    Assert.assertTrue(converter.getCreator() instanceof OutputFixDirFromHdfsCreator);
}
 
Example 13
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testHiveInputOnlyDbName() throws Exception {

    String js = "ci.hiveInput(\"dbname\")";

    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader(js), "string");
    HiveTableDeployer hiveTableDeployer = (HiveTableDeployer) creatorObj.unwrap();

    Assert.assertEquals(hiveTableDeployer.getDatabaseName(), new DatabaseName("dbname"));
    Assert.assertNull(hiveTableDeployer.getTableName());
    Assert.assertNull(hiveTableDeployer.getTableCreationScriptFile());
    Assert.assertNull(hiveTableDeployer.getDataFileCreator());
}
 
Example 14
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test(expected = JavaScriptException.class)
public void testRecursiveFsObjectComparer1() throws IOException {
    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader("ci.plainCompare()"), "string");
    RecursiveFsObjectComparer creator = (RecursiveFsObjectComparer) creatorObj.unwrap();
}
 
Example 15
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testHdfsInputDeployerCall2() throws IOException {
    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader("ci.hdfsInput(ci.fixFileFromResource(\"stuff\"), \"here\")"), "string");
    HdfsInputDeployer creator = (HdfsInputDeployer) creatorObj.unwrap();
    Assert.assertEquals(new Path("here"), creator.getPath());
}
 
Example 16
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testHiveTable() throws IOException {
    String js = "ci.hiveTable(\"dbname\", \"tablename\")";

    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader(js), "string");
    OutputFixTableFromHiveCreator creator = (OutputFixTableFromHiveCreator) creatorObj.unwrap();
    TestRun testRun = mock(TestRun.class);
    doReturn(UUID.nameUUIDFromBytes("fake".getBytes())).when(testRun).getTestUUID();
    Assert.assertEquals("Hive table celosci_dbname_144c9def_ac04_369c_bbfa_d8efaa8ea194.tablename", creator.getDescription(testRun));
}
 
Example 17
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test(expected = JavaScriptException.class)
public void fixDirFromResourceFails() throws IOException {
    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader("ci.fixDirFromResource()"), "string");
    FixDirFromResourceCreator creator = (FixDirFromResourceCreator) creatorObj.unwrap();
    Assert.assertEquals(new File("/stuff"), creator.getPath(testRun));
}
 
Example 18
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void fixDirFromResource() throws IOException {
    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader("ci.fixDirFromResource(\"stuff\")"), "string");
    FixDirFromResourceCreator creator = (FixDirFromResourceCreator) creatorObj.unwrap();
    Assert.assertEquals(new File("/stuff"), creator.getPath(testRun));
}
 
Example 19
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 3 votes vote down vote up
@Test
public void testTableToJson() throws IOException {
    String js = "ci.tableToJson(ci.hiveTable(\"dbname\", \"tablename\"))";

    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader(js), "string");
    ConversionCreator creator = (ConversionCreator) creatorObj.unwrap();

    Assert.assertEquals(FixTableToJsonFileConverter.class, creator.getFixObjectConverter().getClass());

}
 
Example 20
Source File: TestConfigurationParserTest.java    From celos with Apache License 2.0 3 votes vote down vote up
@Test
public void testTableToTSV() throws IOException {
    String js = "ci.tableToTSV(ci.hiveTable(\"dbname\", \"tablename\"))";

    TestConfigurationParser parser = new TestConfigurationParser();

    NativeJavaObject creatorObj = (NativeJavaObject) parser.evaluateTestConfig(new StringReader(js), "string");
    ConversionCreator creator = (ConversionCreator) creatorObj.unwrap();

    Assert.assertEquals(FixTableToTSVFileConverter.class, creator.getFixObjectConverter().getClass());

}