/*
 * Copyright 2011 the original author or authors.
 *
 * 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 org.gradle.api.internal.tasks.compile

import org.gradle.api.internal.tasks.compile.daemon.CompilerDaemonFactory
import org.gradle.api.tasks.compile.CompileOptions
import spock.lang.Specification

class DefaultJavaCompilerFactoryTest extends Specification {
    def factory = new DefaultJavaCompilerFactory(new File("daemon-work-dir"), Mock(CompilerDaemonFactory))
    def options = new CompileOptions()
    
    def "creates in-process compiler when not forking"() {
        options.fork = false

        expect:
        def compiler = factory.create(options)
        compiler instanceof NormalizingJavaCompiler
        compiler.delegate instanceof JdkJavaCompiler
    }

    def "creates in-process compiler when not forking and joint compilation"() {
        options.fork = false

        expect:
        def compiler = factory.createForJointCompilation(options)
        compiler instanceof JdkJavaCompiler
    }

    def "creates command line compiler when forking and forkOptions.executable is set"() {
        options.fork = true
        options.forkOptions.executable = "/path/to/javac"

        expect:
        def compiler = factory.create(options)
        compiler instanceof NormalizingJavaCompiler
        compiler.delegate instanceof CommandLineJavaCompiler
    }

    def "creates command line compiler when forking and forkOptions.executable is set and joint compilation"() {
        options.fork = true
        options.forkOptions.executable = "/path/to/javac"

        expect:
        def compiler = factory.createForJointCompilation(options)
        compiler instanceof CommandLineJavaCompiler
    }

    def "creates daemon compiler when forking"() {
        options.fork = true

        expect:
        def compiler = factory.create(options)
        compiler instanceof NormalizingJavaCompiler
        compiler.delegate instanceof DaemonJavaCompiler
        compiler.delegate.delegate instanceof JdkJavaCompiler
    }

    def "creates in-process compiler when forking and joint compilation"() {
        options.fork = true

        expect:
        def compiler = factory.createForJointCompilation(options)
        compiler instanceof JdkJavaCompiler
    }
}
