aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/utils/System2Test.java
blob: f1e1c7b24fd0de4919265ed3b2d63c338bc556b8 (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
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT 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  02110-1301, USA.
 */
package org.sonar.api.utils;

import java.io.Closeable;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import org.apache.commons.lang.SystemUtils;
import org.junit.Test;

import static java.util.UUID.randomUUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

public class System2Test {
  @Test
  public void testNow() {
    long start = System.currentTimeMillis();
    long now = System2.INSTANCE.now();
    long end = System.currentTimeMillis();
    assertThat(now).isGreaterThanOrEqualTo(start).isLessThanOrEqualTo(end);
  }

  @Test
  public void testProperties() {
    Properties expected = System.getProperties();
    assertThat(System2.INSTANCE.properties()).isNotNull().isEqualTo(expected);
  }

  @Test
  public void testProperty() {
    String expected = System.getProperty("java.version");
    assertThat(System2.INSTANCE.property("java.version")).isNotNull().isEqualTo(expected);
  }

  @Test
  public void testSetProperty() {
    String key = "System2Test.testSetProperty";
    String value = randomUUID().toString();
    System2.INSTANCE.setProperty(key, value);
    assertThat(System2.INSTANCE.property(key)).isEqualTo(value);
  }

  @Test
  public void testEnvVariables() {
    Map<String, String> expected = System.getenv();
    assertThat(System2.INSTANCE.envVariables()).isNotNull().isEqualTo(expected);
  }

  @Test
  public void testEnvVariable() {
    // assume that there's at least one env variable
    if (System.getenv().isEmpty()) {
      fail("Test can't succeed because there are no env variables. How is it possible ?");
    }
    String key = System.getenv().keySet().iterator().next();
    String expected = System.getenv(key);
    assertThat(System2.INSTANCE.envVariable(key)).isNotNull().isEqualTo(expected);
    assertThat(System2.INSTANCE.envVariable("UNKNOWN_VAR")).isNull();
  }

  @Test
  public void testIsOsWindows() {
    assertThat(System2.INSTANCE.isOsWindows()).isEqualTo(SystemUtils.IS_OS_WINDOWS);
  }

  @Test
  public void testIsJavaAtLeast17() {
    if (SystemUtils.IS_JAVA_1_6) {
      assertThat(System2.INSTANCE.isJavaAtLeast17()).isFalse();
    } else {
      assertThat(System2.INSTANCE.isJavaAtLeast17()).isTrue();
    }
  }

  @Test
  public void testPrintln() {
    // well, how to assert that ? Adding a System3 dependency to System2 ? :-)
    System2.INSTANCE.println("foo");
  }

  @Test
  public void testGetResource() {
    String name = "META-INF/MANIFEST.MF";
    assertThat(System2.INSTANCE.getResource(name)).isEqualTo(getClass().getResource(name));
  }

  @Test
  public void close() {
    class MyCloseable implements Closeable {
      boolean isClosed = false;

      @Override
      public void close() throws IOException {
        isClosed = true;
      }
    }

    MyCloseable closeable = new MyCloseable();
    System2.INSTANCE.close(closeable);
    assertThat(closeable.isClosed).isTrue();
  }

  @Test
  public void close_throws_exception_on_error() {
    Closeable closeable = new Closeable() {
      @Override
      public void close() throws IOException {
        throw new IOException("expected");
      }
    };
    try {
      System2.INSTANCE.close(closeable);
      fail();
    } catch (IllegalStateException e) {
      assertThat(e.getCause().getMessage()).isEqualTo("expected");
    }
  }

  @Test
  public void getDefaultTimezone() {
    assertThat(System2.INSTANCE.getDefaultTimeZone().getID()).isEqualTo(TimeZone.getDefault().getID());

  }
}