/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */
package org.apache.tinkerpop.gremlin.groovy.jsr223;

import org.apache.tinkerpop.gremlin.groovy.TypeCheckedCustomizerProvider;
import org.codehaus.groovy.control.MultipleCompilationErrorsException;
import org.junit.Test;

import javax.script.ScriptException;

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

/**
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
public class GremlinGroovyScriptEngineTypeCheckedTest {
    @Test
    public void shouldTypeCheck() throws Exception {
        // with no type checking this should pass
        try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine()) {
            assertEquals(255, scriptEngine.eval("((Object) new java.awt.Color(255, 255, 255)).red"));
        }

        final TypeCheckedCustomizerProvider provider = new TypeCheckedCustomizerProvider();
        try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(provider)) {
            scriptEngine.eval("((Object) new java.awt.Color(255, 255, 255)).red");
            fail("Should have failed type checking");
        } catch (ScriptException se) {
            assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
        }
    }

    @Test
    public void shouldTypeCheckWithExtension() throws Exception {
        // with no type checking extension this should pass
        final TypeCheckedCustomizerProvider providerNoExtension = new TypeCheckedCustomizerProvider();
        try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) {
            assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"));
        }

        final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider("PreventColorUsageExtension.groovy");
        try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
            scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
            fail("Should have failed type checking");
        } catch (ScriptException se) {
            assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
        }
    }
}

@@ -3051,11 +3051,1590 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    public void shouldTypeCheckWithMultipleExtension() throws Exception %7B%0A        // with no type checking extension this should pass%0A        final TypeCheckedCustomizerProvider providerNoExtension = new TypeCheckedCustomizerProvider();%0A        try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) %7B%0A            assertEquals(255, scriptEngine.eval(%22def c = new java.awt.Color(255, 255, 255); c.red%22));%0A            assertEquals(1l, scriptEngine.eval(%22def c = new java.util.concurrent.CountDownLatch(1); c.count%22));%0A        %7D%0A%0A        final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider(%0A                %22PreventColorUsageExtension.groovy,PreventCountDownLatchUsageExtension.groovy%22);%0A        try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) %7B%0A            scriptEngine.eval(%22def c = new java.awt.Color(255, 255, 255); c.red%22);%0A            fail(%22Should have failed type checking%22);%0A        %7D catch (ScriptException se) %7B%0A            assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());%0A        %7D%0A%0A        try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) %7B%0A            scriptEngine.eval(%22def c = new java.util.concurrent.CountDownLatch(1); c.count%22);%0A            fail(%22Should have failed type checking%22);%0A        %7D catch (ScriptException se) %7B%0A            assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());%0A        %7D%0A    %7D
 %0A%7D%0A

Add test for multiple extensions on TypeChecking