/*
 * Copyright 2016 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.integtests.tooling.r32

import com.google.common.base.Throwables
import org.gradle.integtests.tooling.fixture.TargetGradleVersion
import org.gradle.integtests.tooling.fixture.ToolingApiSpecification
import org.gradle.integtests.tooling.fixture.ToolingApiVersion
import org.gradle.integtests.tooling.r16.CustomModel
import org.gradle.tooling.GradleConnectionException
import spock.lang.Issue

class NonSerializableExceptionCrossVersionSpec extends ToolingApiSpecification {
    def createBuildFile(int exceptionNestingLevel) {
        file('build.gradle') << """
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.gradle.tooling.provider.model.ToolingModelBuilder
import javax.inject.Inject

allprojects {
    apply plugin: CustomPlugin
}

class CustomException extends Exception {
    Thread thread = Thread.currentThread() // non-serializable field
    CustomException(String msg) {
        super(msg)
    }
}

class CustomBuilder implements ToolingModelBuilder {
    boolean canBuild(String modelName) {
        return modelName == '${CustomModel.name}'
    }
    Object buildAll(String modelName, Project project) {
        throw ${'new RuntimeException(' * exceptionNestingLevel}new CustomException("Something went wrong in building the model")${')' * exceptionNestingLevel}
    }
}

class CustomPlugin implements Plugin<Project> {
    @Inject
    CustomPlugin(ToolingModelBuilderRegistry registry) {
        registry.register(new CustomBuilder())
    }

    public void apply(Project project) {
    }
}
"""
    }

    @Issue("GRADLE-3307")
    @ToolingApiVersion(">=3.2")
    @TargetGradleVersion(">=2.4")
    def "returns proper error message when non-serializable exception is thrown"() {
        when:
        createBuildFile(exceptionNestingLevel)
        withConnection { connection ->
            connection.model(CustomModel).get()
        }

        then:
        def e = thrown(GradleConnectionException)
        def exceptionString = Throwables.getStackTraceAsString(e)
        !exceptionString.contains("java.io.NotSerializableException")
        exceptionString.contains('Caused by: CustomException: Something went wrong in building the model')

        where:
        exceptionNestingLevel << (0..2).toList()
    }
}

Test non serializable exceptions in TAPI model builders
Test non serializable exceptions in TAPI model builders

- GRADLE-3307

+review REVIEW-6309
