package com.github.rnewson.couchdb.lucene;

/**
 * Copyright 2009 Robert Newson
 * 
 * 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 static org.junit.Assert.assertThat;

import org.apache.lucene.document.Document;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Test;
import org.mozilla.javascript.EcmaError;

public class RhinoTest {

    private Rhino rhino;

    @After
    public void cleanup() {
        if (rhino != null) {
            rhino.close();
        }
    }

    @Test
    public void testRhino() throws Exception {
        rhino = new Rhino("db,", "function(doc) { var ret = new Document(); "
                + "ret.field(\"foo\", doc.size); return ret }");
        final String doc = "{\"deleteme\":\"true\", \"size\":13}";
        Document[] ret = rhino.map("doc", doc);
        assertThat(ret.length, CoreMatchers.equalTo(1));
        assertThat(ret[0].getField("foo"), CoreMatchers.notNullValue());
    }

    @Test
    public void testNoReturn() throws Exception {
        rhino = new Rhino("db", "function(doc) {}");
        Document[] ret = rhino.map("doc", "{}");
        assertThat(ret.length, CoreMatchers.equalTo(0));
    }

    @Test(expected = EcmaError.class)
    public void testBadCode() throws Exception {
        rhino = new Rhino("db", "function(doc) {no_such_function(); }");
        rhino.map("doc", "{}");
    }

    @Test
    public void testBadCodeRecovers() throws Exception {
        try {
            testBadCode();
        } catch (EcmaError e) {
            // Ignored.
        }
        testRhino();
    }

    @Test(expected = RuntimeException.class)
    public void testBadReturn() throws Exception {
        rhino = new Rhino("db", "function(doc) {return 1;}");
        rhino.map("doc", "{}");
    }

    @Test
    public void testCtor() throws Exception {
        rhino = new Rhino("db", "function(doc) { return new Document(\"foo\", 1); }");
        Document[] ret = rhino.map("doc", "{}");
        assertThat(ret.length, CoreMatchers.equalTo(1));
        assertThat(ret[0].getField("foo"), CoreMatchers.notNullValue());
    }

    @Test
    public void testMultipleReturn() throws Exception {
        rhino = new Rhino("db", "function(doc) { " + "var ret = []; "
                + "for(var v in doc) {var d = new Document(); d.field(v, doc[v]); ret.push(d)} " + "return ret; " + "}");
        Document[] ret = rhino.map("doc", "{\"foo\": 1, \"bar\": 2}");
        assertThat(ret.length, CoreMatchers.equalTo(2));
    }

    @Test
    public void testDate() throws Exception {
        rhino = new Rhino("db", "function(doc) { var ret = new Document(); "
                + "ret.date(\"bar\", \"2009-01-0T00:00:00Z\"); return ret;}");
        Document[] ret = rhino.map("doc", "{\"foo\": 1, \"bar\": 2}");
        assertThat(ret.length, CoreMatchers.equalTo(1));
        assertThat(ret[0].getField("bar"), CoreMatchers.notNullValue());
    }

}

@@ -1173,31 +1173,41 @@
 ret.
+add(doc.size, %7B%5C%22
 field
-(
+%5C%22:
 %5C%22foo%5C%22
-, doc.size
+%7D
 ); r
@@ -1215,16 +1215,17 @@
 turn ret
+;
  %7D%22);%0A  
@@ -2427,18 +2427,30 @@
 ent(
+1, %7B%5C%22field%5C%22:
 %5C%22foo%5C%22
-, 1
+%7D
 ); %7D
@@ -2843,23 +2843,34 @@
 ; d.
-field(v, doc%5Bv%5D
+add(doc%5Bv%5D, %7B%5C%22field%5C%22: v%7D
 ); r
@@ -3203,45 +3203,43 @@
 ret.
-date(%5C%22bar%5C%22, %5C%222009-01-0T00:00:00Z
+add(new Date(), %7B%5C%22field%5C%22:%5C%22bar
 %5C%22
+%7D
 ); r

fix RhinoTest [#13 state:resolved]