com.asakusafw.testdriver.FlowPartTester.java Source code

Java tutorial

Introduction

Here is the source code for com.asakusafw.testdriver.FlowPartTester.java

Source

/**
 * Copyright 2011-2015 Asakusa Framework Team.
 *
 * 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.
 */
package com.asakusafw.testdriver;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.asakusafw.compiler.flow.FlowDescriptionDriver;
import com.asakusafw.compiler.testing.DirectFlowCompiler;
import com.asakusafw.compiler.testing.JobflowInfo;
import com.asakusafw.testdriver.core.DataModelSourceFactory;
import com.asakusafw.testdriver.core.TestModerator;
import com.asakusafw.testdriver.core.VerifierFactory;
import com.asakusafw.testdriver.core.VerifyContext;
import com.asakusafw.vocabulary.external.ImporterDescription;
import com.asakusafw.vocabulary.flow.FlowDescription;
import com.asakusafw.vocabulary.flow.graph.FlowGraph;

/**
 * ???
 * @since 0.2.0
 * @version 0.5.2
 */
public class FlowPartTester extends TesterBase {

    static final Logger LOG = LoggerFactory.getLogger(FlowPartTester.class);

    private final List<FlowPartDriverInput<?>> inputs = new LinkedList<FlowPartDriverInput<?>>();
    private final List<FlowPartDriverOutput<?>> outputs = new LinkedList<FlowPartDriverOutput<?>>();

    private final FlowDescriptionDriver descDriver = new FlowDescriptionDriver();

    /**
     * 
     *
     * @param callerClass 
     */
    public FlowPartTester(Class<?> callerClass) {
        super(callerClass);
    }

    /**
     * ?
     *
     * @param <T> ModelType
     * @param name ????????????
     * @param modelType ModelType
     * @return 
     */
    public <T> FlowPartDriverInput<T> input(String name, Class<T> modelType) {
        FlowPartDriverInput<T> input = new FlowPartDriverInput<T>(driverContext, descDriver, name, modelType);
        inputs.add(input);
        return input;
    }

    /**
     * ???
     *
     * @param <T> ModelType
     * @param name ????????????
     * @param modelType ModelType
     * @return 
     */
    public <T> FlowPartDriverOutput<T> output(String name, Class<T> modelType) {
        FlowPartDriverOutput<T> output = new FlowPartDriverOutput<T>(driverContext, descDriver, name, modelType);
        outputs.add(output);
        return output;
    }

    /**
     * ??????
     * @param flowDescription ??
     * @throws IllegalStateException ??????
     */
    public void runTest(FlowDescription flowDescription) {
        try {
            try {
                runTestInternal(flowDescription);
            } finally {
                driverContext.cleanUpTemporaryResources();
            }
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    private void runTestInternal(FlowDescription flowDescription) throws IOException {
        LOG.info("?????: {}", driverContext.getCallerClass().getName());

        if (driverContext.isSkipValidateCondition() == false) {
            LOG.info("??????: {}", driverContext.getCallerClass().getName());
            validateTestCondition();
        }

        // ?
        LOG.info("??????: {}", flowDescription.getClass().getName());
        FlowGraph flowGraph = descDriver.createFlowGraph(flowDescription);

        // ?
        driverContext.validateCompileEnvironment();

        File compileWorkDir = driverContext.getCompilerWorkingDirectory();
        if (compileWorkDir.exists()) {
            FileUtils.forceDelete(compileWorkDir);
        }

        String batchId = "testing"; //$NON-NLS-1$
        String flowId = "flowpart"; //$NON-NLS-1$
        JobflowInfo jobflowInfo = DirectFlowCompiler.compile(flowGraph, batchId, flowId, "test.flowpart", //$NON-NLS-1$
                FlowPartDriverUtils.createWorkingLocation(driverContext), compileWorkDir,
                Arrays.asList(new File[] { DirectFlowCompiler.toLibraryPath(flowDescription.getClass()) }),
                flowDescription.getClass().getClassLoader(), driverContext.getOptions());

        // ?
        driverContext.validateExecutionEnvironment();

        JobflowExecutor executor = new JobflowExecutor(driverContext);
        driverContext.prepareCurrentJobflow(jobflowInfo);

        // ?
        LOG.info("??????: {}", driverContext.getCallerClass().getName());
        executor.cleanWorkingDirectory();
        executor.cleanInputOutput(jobflowInfo);
        executor.cleanExtraResources(getExternalResources());

        LOG.info("??????: {}", driverContext.getCallerClass().getName());
        executor.prepareExternalResources(getExternalResources());
        executor.prepareInput(jobflowInfo, inputs);
        executor.prepareOutput(jobflowInfo, outputs);

        LOG.info("??????: {}", flowDescription.getClass().getName());
        VerifyContext verifyContext = new VerifyContext(driverContext);
        executor.runJobflow(jobflowInfo);
        verifyContext.testFinished();

        // ??
        LOG.info("??????: {}", driverContext.getCallerClass().getName());
        executor.verify(jobflowInfo, verifyContext, outputs);
    }

    private void validateTestCondition() throws IOException {
        TestModerator moderator = new TestModerator(driverContext.getRepository(), driverContext);
        for (Map.Entry<? extends ImporterDescription, ? extends DataModelSourceFactory> entry : getExternalResources()
                .entrySet()) {
            ImporterDescription description = entry.getKey();
            String label = String.format("Resource(%s)", description); //$NON-NLS-1$
            DataModelSourceFactory source = entry.getValue();
            moderator.validate(entry.getKey().getModelType(), label, source);
        }
        for (DriverInputBase<?> port : inputs) {
            String label = String.format("Input(%s)", port.getName()); //$NON-NLS-1$
            Class<?> type = port.getModelType();
            DataModelSourceFactory source = port.getSource();
            if (source != null) {
                moderator.validate(type, label, source);
            }
        }
        for (DriverOutputBase<?> port : outputs) {
            String label = String.format("Output(%s)", port.getName()); //$NON-NLS-1$
            Class<?> type = port.getModelType();
            DataModelSourceFactory source = port.getSource();
            if (source != null) {
                moderator.validate(type, label, source);
            }
            VerifierFactory verifier = port.getVerifier();
            if (verifier != null) {
                moderator.validate(type, label, verifier);
            }
        }
    }
}