aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java
blob: a56030df99a6dad878966a8c9ad76f715ef0ccc4 (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
/*
 * SonarQube Runner - API
 * Copyright (C) 2011 SonarSource
 * dev@sonar.codehaus.org
 *
 * 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.Test;

import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

import static org.mockito.Mockito.verify;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.doThrow;
import static org.fest.assertions.Assertions.assertThat;

public class UtilsTest {
  @Test
  public void should_join_strings() {
    assertThat(Utils.join(new String[] {}, ",")).isEqualTo("");
    assertThat(Utils.join(new String[] {"foo"}, ",")).isEqualTo("foo");
    assertThat(Utils.join(new String[] {"foo", "bar"}, ",")).isEqualTo("foo,bar");
  }

  @Test
  public void task_should_require_project() {
    Properties props = new Properties();
    assertThat(Utils.taskRequiresProject(props)).isTrue();

    props.setProperty("sonar.task", "scan");
    assertThat(Utils.taskRequiresProject(props)).isTrue();
  }

  @Test
  public void task_should_not_require_project() {
    Properties props = new Properties();
    props.setProperty("sonar.task", "views");
    assertThat(Utils.taskRequiresProject(props)).isFalse();
  }

  @Test
  public void close_quietly() throws IOException {
    Closeable c = mock(Closeable.class);
    doThrow(IOException.class).when(c).close();
    Utils.closeQuietly(c);
    verify(c).close();
  }
  
  @Test
  public void close_quietly_null() throws IOException {
    Utils.closeQuietly(null);
  }

  @Test
  public void delete_non_empty_directory() throws IOException {
    /*-
     * Create test structure:
     * tmp 
     *   |-folder1
     *        |- file1
     *        |- folder2
     *             |- file2
     */
    Path tmpDir = Files.createTempDirectory("junit");
    Path folder1 = tmpDir.resolve("folder1");
    Files.createDirectories(folder1);
    Path file1 = folder1.resolve("file1");
    Files.write(file1, "test1".getBytes());

    Path folder2 = folder1.resolve("folder2");
    Files.createDirectories(folder2);
    Path file2 = folder1.resolve("file2");
    Files.write(file2, "test2".getBytes());

    // delete it
    assertThat(tmpDir.toFile()).exists();
    Utils.deleteQuietly(tmpDir.toFile());
    assertThat(tmpDir.toFile()).doesNotExist();
  }
}