<commit_before>/*
 * Copyright 2015-2019 Netflix, Inc.
 *
 * 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.netflix.nebula.lint.rule.dependency

import com.netflix.nebula.lint.TestKitSpecification
import nebula.test.dependencies.Coordinate
import nebula.test.dependencies.maven.Pom

class UndeclaredDependencyRuleSpec extends TestKitSpecification {
    private static final def sample = new Coordinate('sample', 'alpha', '1.0')
    private static final def commonsLogging = new Coordinate('commons-logging', 'commons-logging', '1.2')
    private static final def commonsLang = new Coordinate('commons-lang', 'commons-lang', '2.6')
    private static final def lombok = new Coordinate('org.projectlombok', 'lombok', '1.18.10')
    private static final def junit = new Coordinate('junit', 'junit', '4.12')

    def main = '''
            import org.apache.commons.logging.Log;
            import org.apache.commons.logging.LogFactory;
            public class Main {
                public static void main(String[] args) {
                    Log log = LogFactory.getLog(Main.class);
                    log.info("foo");
                }
            }
        '''

    def setup() {
        definePluginOutsideOfPluginBlock = true
    }

    def 'simple - add undeclared dependencies as first-orders'() {
        given:
        def repo = new File(projectDir, 'repo')
        repo.mkdirs()

        def samplePom = new Pom(sample.getGroup(), sample.getArtifact(), sample.getVersion())
        samplePom.addDependency(commonsLogging.getGroup(), commonsLogging.getArtifact(), commonsLogging.getVersion())
        ArtifactHelpers.setupSamplePomWith(repo, sample, samplePom.generate())
        ArtifactHelpers.setupSampleJar(repo, sample)

        setupBuildGradleSimpleSetup(repo)
        buildFile << """\
            dependencies {
            ${deps.collect { "    compile '$it'" }.join('\n')}
            }
            """.stripIndent()

        when:

        createJavaSourceFile(main)

        then:
        def result = runTasksSuccessfully('fixGradleLint')

        result.output.contains('fixed')
        def dependencies = dependencies(buildFile)
        for (def expectedDependency : expected) {
            assert dependencies.contains(expectedDependency.toString())
        }

        where:
        deps     | expected
        [sample] | [sample, commonsLogging]
    }

    def 'adds dependencies alphabetically'() {
        given:
        def repo = new File(projectDir, 'repo')
        repo.mkdirs()

        def samplePom = new Pom(sample.getGroup(), sample.getArtifact(), sample.getVersion())
        samplePom.addDependency(lombok.getGroup(), lombok.getArtifact(), lombok.getVersion())
        samplePom.addDependency(junit.getGroup(), junit.getArtifact(), junit.getVersion())
        samplePom.addDependency(commonsLogging.getGroup(), commonsLogging.getArtifact(), commonsLogging.getVersion())
        samplePom.addDependency(commonsLang.getGroup(), commonsLang.getArtifact(), commonsLang.getVersion())
        ArtifactHelpers.setupSamplePomWith(repo, sample, samplePom.generate())
        ArtifactHelpers.setupSampleJar(repo, sample)

        setupBuildGradleSimpleSetup(repo)
        buildFile << """\
            
            buildscript {
              repositories {
                maven {
                  url "https://plugins.gradle.org/m2/"
                }
              }
              dependencies {
                classpath "io.franzbecker:gradle-lombok:1.14"
              }
            }
            
            apply plugin: "io.franzbecker.gradle-lombok"

            dependencies {
            ${deps.collect { "    compile '$it'" }.join('\n')}
            }
            """.stripIndent()
<commit_msg>fix lombok version for UndeclaredDependencyRuleSpec<commit_after>/*
 * Copyright 2015-2019 Netflix, Inc.
 *
 * 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.netflix.nebula.lint.rule.dependency

import com.netflix.nebula.lint.TestKitSpecification
import nebula.test.dependencies.Coordinate
import nebula.test.dependencies.maven.Pom

class UndeclaredDependencyRuleSpec extends TestKitSpecification {
    private static final def sample = new Coordinate('sample', 'alpha', '1.0')
    private static final def commonsLogging = new Coordinate('commons-logging', 'commons-logging', '1.2')
    private static final def commonsLang = new Coordinate('commons-lang', 'commons-lang', '2.6')
    private static final def lombok = new Coordinate('org.projectlombok', 'lombok', '1.18.10')
    private static final def junit = new Coordinate('junit', 'junit', '4.12')

    def main = '''
            import org.apache.commons.logging.Log;
            import org.apache.commons.logging.LogFactory;
            public class Main {
                public static void main(String[] args) {
                    Log log = LogFactory.getLog(Main.class);
                    log.info("foo");
                }
            }
        '''

    def setup() {
        definePluginOutsideOfPluginBlock = true
    }

    def 'simple - add undeclared dependencies as first-orders'() {
        given:
        def repo = new File(projectDir, 'repo')
        repo.mkdirs()

        def samplePom = new Pom(sample.getGroup(), sample.getArtifact(), sample.getVersion())
        samplePom.addDependency(commonsLogging.getGroup(), commonsLogging.getArtifact(), commonsLogging.getVersion())
        ArtifactHelpers.setupSamplePomWith(repo, sample, samplePom.generate())
        ArtifactHelpers.setupSampleJar(repo, sample)

        setupBuildGradleSimpleSetup(repo)
        buildFile << """\
            dependencies {
            ${deps.collect { "    compile '$it'" }.join('\n')}
            }
            """.stripIndent()

        when:

        createJavaSourceFile(main)

        then:
        def result = runTasksSuccessfully('fixGradleLint')

        result.output.contains('fixed')
        def dependencies = dependencies(buildFile)
        for (def expectedDependency : expected) {
            assert dependencies.contains(expectedDependency.toString())
        }

        where:
        deps     | expected
        [sample] | [sample, commonsLogging]
    }

    def 'adds dependencies alphabetically'() {
        given:
        def repo = new File(projectDir, 'repo')
        repo.mkdirs()

        def samplePom = new Pom(sample.getGroup(), sample.getArtifact(), sample.getVersion())
        samplePom.addDependency(lombok.getGroup(), lombok.getArtifact(), lombok.getVersion())
        samplePom.addDependency(junit.getGroup(), junit.getArtifact(), junit.getVersion())
        samplePom.addDependency(commonsLogging.getGroup(), commonsLogging.getArtifact(), commonsLogging.getVersion())
        samplePom.addDependency(commonsLang.getGroup(), commonsLang.getArtifact(), commonsLang.getVersion())
        ArtifactHelpers.setupSamplePomWith(repo, sample, samplePom.generate())
        ArtifactHelpers.setupSampleJar(repo, sample)

        setupBuildGradleSimpleSetup(repo)
        buildFile << """\
            
            buildscript {
              repositories {
                maven {
                  url "https://plugins.gradle.org/m2/"
                }
              }
              dependencies {
                classpath "io.franzbecker:gradle-lombok:3.2.0"
              }
            }
            
            apply plugin: "io.franzbecker.gradle-lombok"

            dependencies {
            ${deps.collect { "    compile '$it'" }.join('\n')}
            }
            """.stripIndent()

{'fraction_non_alphanumeric': 0.10753064798598949, 'fraction_numerical': 0.0070052539404553416, 'mean_word_length': 2.479285134037368, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1699536', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2279, 'n_words': 550}