org.geoserver.script.function.ScriptFunctionTest.java Source code

Java tutorial

Introduction

Here is the source code for org.geoserver.script.function.ScriptFunctionTest.java

Source

/* Copyright (c) 2001 - 2013 OpenPlans - www.openplans.org. All rights reserved.
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */
package org.geoserver.script.function;

import java.io.File;
import java.util.Collections;

import org.apache.commons.io.FileUtils;
import org.geoserver.script.ScriptTestSupport;
import org.opengis.filter.expression.Function;

public class ScriptFunctionTest extends ScriptTestSupport {

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        File function = scriptMgr.getFunctionRoot();
        File script = new File(function, "factorial." + getExtension());

        FileUtils.copyURLToFile(getClass().getResource(script.getName()), script);
    }

    protected String getExtension() {
        return "js";
    }

    public void testName() throws Exception {
        ScriptFunctionFactory sff = new ScriptFunctionFactory(scriptMgr);
        assertEquals(1, sff.getFunctionNames().size());
        assertEquals("factorial", sff.getFunctionNames().get(0).getName());
    }

    public void testRun() throws Exception {
        ScriptFunctionFactory sff = new ScriptFunctionFactory(scriptMgr);

        Function f = sff.function("factorial", Collections.EMPTY_LIST, null);
        assertNotNull(f);
        assertEquals(120, ((Number) f.evaluate(5)).intValue());
    }
}