<commit_before>/*
 * 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.internal.installation

import org.gradle.test.fixtures.file.TestFile
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.junit.Rule
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.ClassNode
import spock.lang.Specification

class GradleFatJarTest extends Specification {

    private static final String CLASS_NAME = 'org/gradle/test/Registry'

    @Rule
    TestNameTestDirectoryProvider tmpDir = new TestNameTestDirectoryProvider()

    def jarFile = tmpDir.file('lib.jar')

    def "throws exception if provided class is null"() {
        when:
        GradleFatJar.containsMarkerFile(null)

        then:
        def t = thrown(IllegalArgumentException)
        t.message == 'Need to provide valid class reference'
    }

    def "does not find marker file for class loaded from outside of JAR"() {
        expect:
        !GradleFatJar.containsMarkerFile(String.class)
    }

    def "can find marker file contained in fat JAR"() {
        given:
        createJarWithMarkerFile(jarFile)
        def clazz = loadClassForJar()

        expect:
        GradleFatJar.containsMarkerFile(clazz)
    }

    def "cannot find marker file in standard JAR file"() {
        given:
        createJarWithoutMarkerFile(jarFile)
        def clazz = loadClassForJar()

        expect:
        !GradleFatJar.containsMarkerFile(clazz)
    }

    private Class<?> loadClassForJar() {
        def classLoader = new URLClassLoader(jarFile.toURI().toURL())
        classLoader.loadClass(CLASS_NAME.replace('/', '.'))
    }

    private void createJarWithMarkerFile(TestFile jar) {
        handleAsJarFile(jar) { TestFile contents ->
            writeClass(contents)
            contents.createFile(GradleFatJar.MARKER_FILENAME)
        }
    }<commit_msg>For now mark these tests with @LeaksFileHandle<commit_after>/*
 * 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.internal.installation

import org.gradle.test.fixtures.file.LeaksFileHandles
import org.gradle.test.fixtures.file.TestFile
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.junit.Rule
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.ClassNode
import spock.lang.Specification

class GradleFatJarTest extends Specification {

    private static final String CLASS_NAME = 'org/gradle/test/Registry'

    @Rule
    TestNameTestDirectoryProvider tmpDir = new TestNameTestDirectoryProvider()

    def jarFile = tmpDir.file('lib.jar')

    def "throws exception if provided class is null"() {
        when:
        GradleFatJar.containsMarkerFile(null)

        then:
        def t = thrown(IllegalArgumentException)
        t.message == 'Need to provide valid class reference'
    }

    def "does not find marker file for class loaded from outside of JAR"() {
        expect:
        !GradleFatJar.containsMarkerFile(String.class)
    }

    @LeaksFileHandles
    def "can find marker file contained in fat JAR"() {
        given:
        createJarWithMarkerFile(jarFile)
        def clazz = loadClassForJar()

        expect:
        GradleFatJar.containsMarkerFile(clazz)
    }

    @LeaksFileHandles
    def "cannot find marker file in standard JAR file"() {
        given:
        createJarWithoutMarkerFile(jarFile)
        def clazz = loadClassForJar()

        expect:
        !GradleFatJar.containsMarkerFile(clazz)
    }

    private Class<?> loadClassForJar() {
        def classLoader = new URLClassLoader(jarFile.toURI().toURL())
        classLoader.loadClass(CLASS_NAME.replace('/', '.'))
    }

    private void createJarWithMarkerFile(TestFile jar) {
        handleAsJarFile(jar) { TestFile contents ->
            writeClass(contents)
            contents.createFile(GradleFatJar.MARKER_FILENAME)
        }
    }
{'fraction_non_alphanumeric': 0.08015794669299112, 'fraction_numerical': 0.0031589338598223098, 'mean_word_length': 3.172981878088962, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6652138', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1393, 'n_words': 422}