aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process/src/test/java/org/sonar/process/PropsTest.java
blob: 29e6acd4159b8adf67857cad919dfaf31826132e (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
/*
 * 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.process;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

public class PropsTest {

  @Rule
  public TemporaryFolder temp = new TemporaryFolder();

  @Test
  public void value() {
    Properties p = new Properties();
    p.setProperty("foo", "bar");
    Props props = new Props(p);

    assertThat(props.value("foo")).isEqualTo("bar");
    assertThat(props.value("foo", "default value")).isEqualTo("bar");
    assertThat(props.value("unknown")).isNull();
    assertThat(props.value("unknown", "default value")).isEqualTo("default value");

    assertThat(props.nonNullValue("foo")).isEqualTo("bar");
    try {
      props.nonNullValue("other");
      fail();
    } catch (IllegalArgumentException e) {
      assertThat(e).hasMessage("Missing property: other");
    }
  }

  @Test
  public void valueAsInt() {
    Properties p = new Properties();
    p.setProperty("foo", "33");
    p.setProperty("blank", "");
    Props props = new Props(p);

    assertThat(props.valueAsInt("foo")).isEqualTo(33);
    assertThat(props.valueAsInt("foo", 44)).isEqualTo(33);
    assertThat(props.valueAsInt("blank")).isNull();
    assertThat(props.valueAsInt("blank", 55)).isEqualTo(55);
    assertThat(props.valueAsInt("unknown")).isNull();
    assertThat(props.valueAsInt("unknown", 44)).isEqualTo(44);
  }

  @Test
  public void valueAsInt_not_integer() {
    Properties p = new Properties();
    p.setProperty("foo", "bar");
    Props props = new Props(p);

    try {
      props.valueAsInt("foo");
      fail();
    } catch (IllegalStateException e) {
      assertThat(e).hasMessage("Value of property foo is not an integer: bar");
    }
  }

  @Test
  public void booleanOf() {
    Properties p = new Properties();
    p.setProperty("foo", "True");
    p.setProperty("bar", "false");
    Props props = new Props(p);

    assertThat(props.valueAsBoolean("foo")).isTrue();
    assertThat(props.valueAsBoolean("bar")).isFalse();
    assertThat(props.valueAsBoolean("unknown")).isFalse();
  }

  @Test
  public void booleanOf_default_value() {
    Properties p = new Properties();
    p.setProperty("foo", "true");
    p.setProperty("bar", "false");
    Props props = new Props(p);

    assertThat(props.valueAsBoolean("unset", false)).isFalse();
    assertThat(props.valueAsBoolean("unset", true)).isTrue();
    assertThat(props.valueAsBoolean("foo", false)).isTrue();
    assertThat(props.valueAsBoolean("bar", true)).isFalse();
  }

  @Test
  public void setDefault() {
    Properties p = new Properties();
    p.setProperty("foo", "foo_value");
    Props props = new Props(p);
    props.setDefault("foo", "foo_def");
    props.setDefault("bar", "bar_def");

    assertThat(props.value("foo")).isEqualTo("foo_value");
    assertThat(props.value("bar")).isEqualTo("bar_def");
    assertThat(props.value("other")).isNull();
  }

  @Test
  public void set() {
    Properties p = new Properties();
    p.setProperty("foo", "old_foo");
    Props props = new Props(p);
    props.set("foo", "new_foo");
    props.set("bar", "new_bar");

    assertThat(props.value("foo")).isEqualTo("new_foo");
    assertThat(props.value("bar")).isEqualTo("new_bar");
  }

  @Test
  public void raw_properties() {
    Properties p = new Properties();
    p.setProperty("encrypted_prop", "{aes}abcde");
    p.setProperty("clear_prop", "foo");
    Props props = new Props(p);

    assertThat(props.rawProperties()).hasSize(2);
    // do not decrypt
    assertThat(props.rawProperties().get("encrypted_prop")).isEqualTo("{aes}abcde");
    assertThat(props.rawProperties().get("clear_prop")).isEqualTo("foo");
  }

  @Test
  public void nonNullValueAsFile() throws IOException {
    File file = temp.newFile();
    Props props = new Props(new Properties());
    props.set("path", file.getAbsolutePath());

    assertThat(props.nonNullValueAsFile("path")).isEqualTo(file);

    try {
      props.nonNullValueAsFile("other_path");
      fail();
    } catch (IllegalArgumentException e) {
      assertThat(e).hasMessage("Property other_path is not set");
    }
  }
}