/*
 *
 *  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.flex.compiler.internal.codegen.externals;

import java.io.File;
import java.io.IOException;

import org.apache.flex.compiler.clients.EXTERNC;
import org.apache.flex.compiler.clients.ExternCConfiguration;
import org.apache.flex.compiler.internal.codegen.externals.reference.MethodReference;
import org.apache.flex.compiler.internal.codegen.externals.reference.ReferenceModel;
import org.apache.flex.utils.FilenameNormalization;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

import com.google.javascript.jscomp.Result;
import com.google.javascript.rhino.jstype.JSType;

public abstract class ExternalsTestBase
{
    private static File unitTestBaseDir = new File(
            FilenameNormalization.normalize("test-files/externals_unit_tests"));

    // Only used for testing, all configuration must happen in configure()
    protected ExternCConfiguration config;
    protected EXTERNC client;
    protected ReferenceModel model;

    @Before
    public void setUp() throws IOException
    {
        config = new ExternCConfiguration();
        configure(config);
        client = new EXTERNC(config);
        model = client.getModel();
    }

    protected abstract void configure(ExternCConfiguration config)
            throws IOException;

    @After
    public void tearDown()
    {
        model = null;
    }

    protected Result compile(String fileName) throws IOException
    {
        return compile(new File(unitTestBaseDir, fileName));
    }

    protected Result compile(File file) throws IOException
    {
        config.addExternal(file);
        return compile();
    }

    protected Result compile() throws IOException
    {
        Result result = client.compile();
        Assert.assertTrue(result.success);
        return result;
    }

    protected JSType evaluateParam(MethodReference method, String paramName)
    {
        JSType jsType = method.getComment().getParameterType(paramName).evaluate(
                null, client.getCompiler().getJSCompiler().getTypeRegistry());
        return jsType;
    }

}

@@ -876,16 +876,60 @@
 rnals;%0A%0A
+import static org.junit.Assert.assertTrue;%0A%0A
 import j
@@ -1567,29 +1567,16 @@
 ew File(
-%0A            
 Filename
@@ -2101,28 +2101,16 @@
  config)
-%0A           
  throws 
@@ -2802,16 +2802,21 @@
 valuate(
+null,
 %0A       
@@ -2827,14 +2827,8 @@
     
- null,
  cli
@@ -2911,10 +2911,642 @@
 %0A    %7D%0A%0A
+    /**%0A     * Clean, compile a js file based on the test method name.%0A     * %0A     * @param relativeTestDir unitTestBaseDir relative base test directory.%0A     * @throws IOException%0A     */%0A    protected void assertCompileTestFileSuccess(String relativeTestDir) throws IOException%0A    %7B%0A        if (config.getAsRoot() != null)%0A        %7B%0A            client.cleanOutput();%0A        %7D%0A        final StackTraceElement%5B%5D ste = Thread.currentThread().getStackTrace();%0A        final String methodName = ste%5B2%5D.getMethodName();%0A        Result result = compile(relativeTestDir + methodName + %22.js%22);%0A        assertTrue(result.success);%0A    %7D%0A
 %7D%0A

Build JS test files based on the Test Method Name