aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java
blob: a1ffdcedd84f30c7c148a3882f32481849d91c86 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * SonarQube Runner - API
 * Copyright (C) 2011 SonarSource
 * sonarqube@googlegroups.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.runner.api;

import org.junit.rules.ExpectedException;

import org.sonar.runner.api.EmbeddedRunner.IssueListenerAdapter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentMatcher;
import org.sonar.home.cache.Logger;
import org.sonar.runner.batch.IsolatedLauncher;
import org.sonar.runner.impl.IsolatedLauncherFactory;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class EmbeddedRunnerTest {

  @Rule
  public TemporaryFolder temp = new TemporaryFolder();

  @Rule
  public ExpectedException expectedException = ExpectedException.none();

  @Test
  public void should_create() {
    assertThat(EmbeddedRunner.create(mock(LogOutput.class))).isNotNull().isInstanceOf(EmbeddedRunner.class);
  }

  private IsolatedLauncherFactory batchLauncher;
  private IsolatedLauncher launcher;
  private EmbeddedRunner runner;

  @Before
  public void setUp() {
    batchLauncher = mock(IsolatedLauncherFactory.class);
    launcher = mock(IsolatedLauncher.class);
    when(launcher.getVersion()).thenReturn("5.2");
    when(batchLauncher.createLauncher(any(Properties.class))).thenReturn(launcher);
    runner = new EmbeddedRunner(batchLauncher, mock(Logger.class), mock(LogOutput.class));
  }

  @Test
  public void test_syncProject() {
    String projectKey = "proj";
    runner.start();
    runner.syncProject(projectKey);
    verify(launcher).syncProject(projectKey);
  }

  @Test
  public void test_fail_projectSync_old_sq() {
    when(launcher.getVersion()).thenReturn("5.0");

    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("version");

    runner.setGlobalProperty("sonar.projectKey", "foo");
    runner.start();
    runner.syncProject("foo");
  }

  @Test
  public void test_app() {
    EmbeddedRunner runner = EmbeddedRunner.create(mock(LogOutput.class)).setApp("Eclipse", "3.1");
    assertThat(runner.app()).isEqualTo("Eclipse");
    assertThat(runner.appVersion()).isEqualTo("3.1");
  }

  @Test
  public void test_back_compatibility() {
    when(launcher.getVersion()).thenReturn("4.5");

    Properties analysisProps = new Properties();
    analysisProps.put("sonar.dummy", "summy");

    runner.setGlobalProperty("sonar.projectKey", "foo");
    runner.start();
    runner.runAnalysis(analysisProps);
    runner.stop();

    verify(batchLauncher).createLauncher(argThat(new ArgumentMatcher<Properties>() {
      @Override
      public boolean matches(Object o) {
        return "foo".equals(((Properties) o).getProperty("sonar.projectKey"));
      }
    }));

    // it should have added a few properties to analysisProperties, and have merged global props
    final String[] mustHaveKeys = {"sonar.working.directory", "sonar.sourceEncoding", "sonar.projectBaseDir",
      "sonar.projectKey", "sonar.dummy"};

    verify(launcher).executeOldVersion(argThat(new ArgumentMatcher<Properties>() {
      @Override
      public boolean matches(Object o) {
        Properties m = (Properties) o;
        for (String s : mustHaveKeys) {
          if (!m.containsKey(s)) {
            return false;
          }
        }
        return true;
      }
    }));
  }

  @Test
  public void should_set_properties() {
    EmbeddedRunner runner = EmbeddedRunner.create(mock(LogOutput.class));
    runner.setGlobalProperty("sonar.projectKey", "foo");
    runner.addGlobalProperties(new Properties() {
      {
        setProperty("sonar.login", "admin");
        setProperty("sonar.password", "gniark");
      }
    });

    assertThat(runner.globalProperty("sonar.projectKey", null)).isEqualTo("foo");
    assertThat(runner.globalProperty("sonar.login", null)).isEqualTo("admin");
    assertThat(runner.globalProperty("sonar.password", null)).isEqualTo("gniark");
    assertThat(runner.globalProperty("not.set", "this_is_default")).isEqualTo("this_is_default");
  }

  @Test
  public void should_launch_batch() {
    runner.setGlobalProperty("sonar.projectKey", "foo");
    runner.start();
    runner.runAnalysis(new Properties());
    runner.stop();

    verify(batchLauncher).createLauncher(argThat(new ArgumentMatcher<Properties>() {
      @Override
      public boolean matches(Object o) {
        return "foo".equals(((Properties) o).getProperty("sonar.projectKey"));
      }
    }));

    // it should have added a few properties to analysisProperties
    final String[] mustHaveKeys = {"sonar.working.directory", "sonar.sourceEncoding", "sonar.projectBaseDir"};

    verify(launcher).execute(argThat(new ArgumentMatcher<Properties>() {
      @Override
      public boolean matches(Object o) {
        Properties m = (Properties) o;
        for (String s : mustHaveKeys) {
          if (!m.containsKey(s)) {
            return false;
          }
        }
        return true;
      }
    }));
  }

  @Test
  public void test_issue_adapter() {
    final List<Issue> issuesRecorded = new LinkedList<>();
    IssueListener apiIssueListener = new IssueListener() {
      @Override
      public void handle(Issue issue) {
        issuesRecorded.add(issue);
      }
    };
    IssueListenerAdapter adapter = new IssueListenerAdapter(apiIssueListener);

    org.sonar.runner.batch.IssueListener.Issue batchIssue = new org.sonar.runner.batch.IssueListener.Issue();
    batchIssue.setAssigneeName("assignee");
    adapter.handle(batchIssue);

    assertThat(issuesRecorded).hasSize(1);
    assertThat(issuesRecorded.get(0).getAssigneeName()).isEqualTo("assignee");
  }

  @Test(expected = IllegalArgumentException.class)
  public void reject_issue_listener_old_version() {
    when(launcher.getVersion()).thenReturn("4.5");
    launch_with_issue_listener();
  }

  @Test
  public void launch_with_issue_listener() {
    runner.start();
    runner.runAnalysis(mock(Properties.class), mock(IssueListener.class));
    runner.stop();

    verify(launcher).execute(any(Properties.class), any(org.sonar.runner.batch.IssueListener.class));
  }

  @Test
  public void should_launch_batch_analysisProperties() {
    runner.setGlobalProperty("sonar.projectKey", "foo");
    runner.start();

    Properties analysisProperties = new Properties();
    analysisProperties.put("sonar.projectKey", "value1");
    runner.runAnalysis(analysisProperties);
    runner.stop();

    verify(batchLauncher).createLauncher(argThat(new ArgumentMatcher<Properties>() {
      @Override
      public boolean matches(Object o) {
        return "foo".equals(((Properties) o).getProperty("sonar.projectKey"));
      }
    }));

    verify(launcher).execute(argThat(new ArgumentMatcher<Properties>() {
      @Override
      public boolean matches(Object o) {
        return "value1".equals(((Properties) o).getProperty("sonar.projectKey"));
      }
    }));
  }

  @Test
  public void should_launch_in_simulation_mode() throws IOException {
    File dump = temp.newFile();
    Properties p = new Properties();

    p.setProperty("sonar.projectKey", "foo");
    p.setProperty("sonarRunner.dumpToFile", dump.getAbsolutePath());
    runner.start();
    runner.runAnalysis(p);
    runner.stop();

    Properties props = new Properties();
    props.load(new FileInputStream(dump));

    assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
  }

  @Test
  public void should_set_default_platform_encoding() throws Exception {
    Properties p = new Properties();
    p.setProperty("sonar.task", "scan");
    runner.initSourceEncoding(p);
    assertThat(p.getProperty("sonar.sourceEncoding", null)).isEqualTo(Charset.defaultCharset().name());
  }

  @Test
  public void should_use_parameterized_encoding() throws Exception {
    Properties p = new Properties();
    p.setProperty("sonar.task", "scan");
    p.setProperty("sonar.sourceEncoding", "THE_ISO_1234");
    runner.initSourceEncoding(p);
    assertThat(p.getProperty("sonar.sourceEncoding", null)).isEqualTo("THE_ISO_1234");
  }

  @Test
  public void should_not_init_encoding_if_not_project_task() throws Exception {
    Properties p = new Properties();
    p.setProperty("sonar.task", "views");
    runner.initSourceEncoding(p);
    assertThat(p.getProperty("sonar.sourceEncoding", null)).isNull();
  }

}