/*
 *  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.codehaus.groovy.classgen;

import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyCodeSource;
import groovy.lang.GroovyObject;
import groovy.lang.Script;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.CompileUnit;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.FieldExpression;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * Base class for test cases.
 */
public abstract class TestSupport {

    /**
     * Asserts the script runs without any exceptions.
     */
    protected final void assertScript(String scriptText) throws Exception {
        String id = java.util.UUID.randomUUID().toString();
        String scriptName = new StringBuilder("TestScript")
            .append(id,  0,  8)
            .append(id,  9, 13)
            .append(id, 14, 18)
            .append(id, 19, 23)
            .append(id, 24, 36)
            .append(".groovy")
            .toString();
        Class<?> scriptClass = loader.parseClass(
            new GroovyCodeSource(scriptText, scriptName, "/groovy/testSupport")
        );
        Script script = InvokerHelper.createScript(scriptClass, new Binding());
        script.run();
    }

    protected final void assertScriptFile(String fileName) throws Exception {
        Class<?> scriptClass = loader.parseClass(
            new GroovyCodeSource(new File(fileName))
        );
        Script script = InvokerHelper.createScript(scriptClass, new Binding());
        script.run();
    }

    protected final GroovyObject compile (String fileName) throws Exception {
        Class<?> groovyClass = loader.parseClass(
            new GroovyCodeSource(new File(fileName))
        );
        GroovyObject groovyObject = (GroovyObject) groovyClass.getDeclaredConstructor().newInstance();
        assertNotNull(groovyObject);
        return groovyObject;
    }

    protected final void assertGetProperty(Object bean, String property, Object expected) throws Exception {
        PropertyDescriptor descriptor = getDescriptor(bean, property);
        Method method = descriptor.getReadMethod();
        assertNotNull(method, "has getter method");

        Object[] args = {};
        Object value = invokeMethod(bean, method, args);
        assertEquals(expected, value, "property value");
    }

    protected final void assertSetProperty(Object bean, String property, Object newValue) throws Exception {
        PropertyDescriptor descriptor = getDescriptor(bean, property);
        Method method = descriptor.getWriteMethod();
        assertNotNull(method, "has setter method");
        Object[] args = {newValue};
        Object value = invokeMethod(bean, method, args);
        assertEquals(null, value, "should return null");
        assertGetProperty(bean, property, newValue);
    }

    protected final void assertField(Class<?> clazz, String name, int modifiers, ClassNode type) throws Exception {
        Field field = clazz.getDeclaredField(name);
        assertNotNull(field, "Found field called: " + name);
        assertEquals(name, field.getName());
        assertEquals(type.getName(), field.getType().getName());
        assertEquals(modifiers, field.getModifiers());
    }

    protected final ExpressionStatement createPrintlnStatement(Expression expression) throws NoSuchFieldException {
        FieldExpression systemDotOut = new FieldExpression(FieldNode.newStatic(System.class, "out"));
        return new ExpressionStatement(new MethodCallExpression(systemDotOut, "println", expression));
    }

    //--------------------------------------------------------------------------

    private GroovyClassLoader loader;

    @BeforeEach
    void setUpTestCase() throws Exception {
        ClassLoader parentLoader = getClass().getClassLoader();
        loader = new GroovyClassLoader(parentLoader);
    }

    @AfterEach
    void tearDownTestCase() throws Exception {
        loader.close();
        loader = null;
    }

    protected final Class<?> loadClass(ClassNode classNode) {
        var unit = new CompileUnit(loader, new CompilerConfiguration());
        var module = new ModuleNode(unit);
        classNode.setModule(module);

        return loader.defineClass(classNode, classNode.getName() + ".groovy", "groovy.testSupport");
    }

    private PropertyDescriptor getDescriptor(Object bean, String property) throws Exception {
        BeanInfo info = Introspector.getBeanInfo(bean.getClass());
        PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
        for (PropertyDescriptor descriptor : descriptors) {
            if (descriptor.getName().equals(property)) {
                return descriptor;
            }
        }
        fail("Could not find property: " + property + " on bean: " + bean);
        return null;
    }

    private Object invokeMethod(Object bean, Method method, Object[] args) throws Exception {
        try {
            return method.invoke(bean, args);
        }
        catch (InvocationTargetException e) {
            fail("InvocationTargetException: " + e.getTargetException());
            return null;
        }
    }
}
