summaryrefslogtreecommitdiff
path: root/spec/integration/java/maven_spec.rb
blob: 49ee82cf1846f7cf0ff99154457ac80b2d2ed028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'spec_helper'

RSpec.describe "maven" do
  include_examples "each report version", "java", "maven"
  include_examples "each report version", "java", "maven-multimodules"

  describe "When the maven dependencies come from a custom public maven repository" do
    it 'is able to detect some of the licenses' do
      runner.add_file('pom.xml', fixture_file_content('java/pom-public-gitlab-repository.xml'))

      report = runner.scan(env: {
        'CI_PROJECT_ID' => '6130122'
      })

      expect(report).to match_schema
      expect(report.dependency_names).to match_array(%w[example jaxb-api])
      expect(report.licenses_for('example')).to match_array(['MIT'])
      expect(report.licenses_for('jaxb-api')).to match_array(['GPL-2.0-only', 'cddl 1.1'])
    end

    it 'downloads packages from by using a custom `settings.xml`' do
      runner.add_file('pom.xml', fixture_file_content('java/pom-public-gitlab-repository.xml'))
      runner.add_file('my_settings.xml', fixture_file_content('java/custom-maven-settings.xml'))

      report = runner.scan(env: {
        'CI_PROJECT_ID' => 'invalid',
        'MAVEN_CLI_OPTS' => "--settings my_settings.xml"
      })

      expect(report).to match_schema
      expect(report[:dependencies]).to match_array([{ name: 'example', url: '', description: '', paths: ['.'], licenses: ['MIT'] },
                                                    { description: '', licenses: ['GPL-2.0-only', 'cddl 1.1'], name: 'jaxb-api', paths: ['.'], url: '' }])
    end
  end

  describe "When using the `SETUP_CMD`" do
    it 'executes the custom script' do
      runner.add_file('custom.sh') do
        <<~SCRIPT
        #!/bin/bash -l

        echo 'hello'
        SCRIPT
      end

      report = runner.scan(env: {
        'SETUP_CMD' => 'bash custom.sh'
      })

      expect(report).to match_schema
    end
  end

  describe "When scanning a project with multiple modules" do
    before do
      runner.mount(dir: fixture_file('java/maven-multimodule'))
    end

    it 'detects dependences from each module' do
      report = runner.scan

      expect(report).to match_schema
      expect(report[:dependencies]).not_to be_empty

      [
        { name: "asm", licenses: ["BSD-4-Clause"] },
        { name: "asm-commons", licenses: ["BSD-4-Clause"] },
        { name: "jackson-annotations", licenses: ["Apache-2.0"] },
        { name: "jackson-core", licenses: ["Apache-2.0"] },
        { name: "jackson-databind", licenses: ["Apache-2.0"] },
        { name: "jackson-dataformat-xml", licenses: ["Apache-2.0"] },
        { name: "jackson-module-jaxb-annotations", licenses: ["Apache-2.0"] },
        { name: "log4j-api", licenses: ["Apache-2.0"] },
        { name: "log4j-core", licenses: ["Apache-2.0"] },
        { name: "netty-all", licenses: ["Apache-2.0"] },
        { name: "stax2-api", licenses: ["BSD-4-Clause"] }
      ].each do |dependency|
        expect(report.licenses_for(dependency[:name])).to match_array(dependency[:licenses])
      end

      expect(report.dependency_names).not_to include('junit')
    end
  end

  context "when connecting to a custom package registry with a self signed certificate", environment: 'offline' do
    let(:bundle) { fixture_file_content('java/maven.crt') }
    let(:report) { runner.scan(env: { 'ADDITIONAL_CA_CERT_BUNDLE' => bundle, 'LOG_LEVEL' => 'debug' }) }

    before do
      runner.add_file('pom.xml') do
        fixture_file_content('java/pom-single.xml.erb', {
          group_id: 'com.fasterxml.jackson.core',
          artifact_id: 'jackson-core',
          version: '2.10.0',
          repository_id: 'custom',
          repository_url: "https://#{private_maven_host}/artifactory/mvn-cache"
        })
      end
    end

    specify { expect(report).to match_schema }
    specify { expect(report.dependency_names).to match_array(['jackson-core']) }
    specify { expect(report.licenses_for('jackson-core')).to match_array(['Apache-2.0']) }
  end

  context "when specifying the version of java using environment variables" do
    let(:output_file) { Pathname.new(runner.project_path.join('output.txt')) }

    before do
      runner.add_file('custom.sh') do
        <<~SCRIPT
        #!/bin/bash -l

        java -version &> '#{output_file}'
        SCRIPT
      end
    end

    it 'prioritizes `LM_JAVA_VERSION` over the `ASDF_JAVA_VERSION`' do
      runner.scan(env: {
        'ASDF_JAVA_VERSION' => 'adopt-openjdk-11.0.7+10',
        'LM_JAVA_VERSION' => '8',
        'SETUP_CMD' => 'bash custom.sh'
      })

      expect(output_file).to exist
      expect(output_file.read).to include('openjdk version "1.8.0_252"')
    end

    it 'reads the ASDF_JAVA_VERSION' do
      runner.scan(env: {
        'ASDF_JAVA_VERSION' => 'adopt-openjdk-11.0.7+10.1',
        'SETUP_CMD' => 'bash custom.sh'
      })

      expect(output_file).to exist
      expect(output_file.read).to include('openjdk version "11.0.7"')
    end

    it 'defaults to java 8' do
      runner.scan(env: { 'SETUP_CMD' => 'bash custom.sh' })

      expect(output_file).to exist
      expect(output_file.read).to include('openjdk version "1.8.0_252"')
    end
  end
end