aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-api/src/test/java/org/sonar/runner/impl/SimulatedLauncherTest.java
blob: aeb4e887c1eaaafd63c7ecc695a54f894c1b33ba (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
/*
 * 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.impl;

import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.junit.Before;
import org.junit.Test;
import org.sonar.home.cache.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class SimulatedLauncherTest {
  private static final String VERSION = "5.2";
  private SimulatedLauncher launcher;
  private Logger logger;
  private String filename;

  @Rule
  public TemporaryFolder temp = new TemporaryFolder();

  @Before
  public void setUp() {
    logger = mock(Logger.class);
    launcher = new SimulatedLauncher(VERSION, logger);
    filename = new File(temp.getRoot(), "props").getAbsolutePath();
  }

  @Test
  public void testDump() throws IOException {
    Properties global = new Properties();
    global.putAll(createProperties(true));
    Properties analysis = new Properties();
    analysis.putAll(createProperties(false));

    launcher.start(global, null, false);
    launcher.execute(analysis);
    assertDump(global, analysis);
  }

  @Test(expected = IllegalStateException.class)
  public void error_if_no_dump_file() {
    launcher.execute(new Properties());
  }

  @Test
  public void no_ops() {
    launcher.syncProject(null);
  }

  @Test
  public void testOldExecute() {
    Properties global = new Properties();
    global.putAll(createProperties(true));
    Properties analysis = new Properties();
    analysis.putAll(createProperties(false));

    launcher.start(global, null, false);
    launcher.executeOldVersion(analysis, null);

  }

  private Properties createProperties(boolean global) {
    Properties prop = new Properties();
    prop.put("key1_" + global, "value1");
    prop.put("key2_" + global, "value2");
    prop.put(InternalProperties.RUNNER_DUMP_TO_FILE, filename);
    return prop;
  }

  @Test
  public void version() {
    assertThat(launcher.getVersion()).isEqualTo(VERSION);
  }

  private void assertDump(Properties global, Properties analysis) throws IOException {
    if (analysis != null) {
      Properties p = new Properties();
      p.load(new FileInputStream(new File(filename)));
      assertThat(p).isEqualTo(analysis);
    } else {
      assertThat(new File(filename)).doesNotExist();
    }

    if (global != null) {
      Properties p = new Properties();
      p.load(new FileInputStream(new File(filename + ".global")));
      assertThat(p).isEqualTo(global);
    } else {
      assertThat(new File(filename + ".global")).doesNotExist();
    }
  }

}