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
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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.application;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.process.MessageException;
import org.sonar.process.ProcessConstants;
import org.sonar.process.Props;
import java.io.File;
import java.util.Properties;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
public class JdbcSettingsTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
JdbcSettings settings = new JdbcSettings();
@Test
public void driver_provider() throws Exception {
assertThat(settings.driverProvider("jdbc:oracle:thin:@localhost/XE")).isEqualTo(JdbcSettings.Provider.ORACLE);
assertThat(settings.driverProvider("jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance"))
.isEqualTo(JdbcSettings.Provider.MYSQL);
try {
settings.driverProvider("jdbc:sqlserver://localhost");
fail();
} catch (MessageException e) {
assertThat(e).hasMessage("Unsupported JDBC driver provider: sqlserver");
}
try {
settings.driverProvider("oracle:thin:@localhost/XE");
fail();
} catch (MessageException e) {
assertThat(e).hasMessage("Bad format of JDBC URL: oracle:thin:@localhost/XE");
}
}
@Test
public void check_mysql_parameters() throws Exception {
Props props = new Props(new Properties());
// minimal -> ok
settings.checkUrlParameters(JdbcSettings.Provider.MYSQL,
"jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8");
// full -> ok
settings.checkUrlParameters(JdbcSettings.Provider.MYSQL,
"jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance");
// missing required -> ko
try {
settings.checkUrlParameters(JdbcSettings.Provider.MYSQL,
"jdbc:mysql://localhost:3306/sonar?characterEncoding=utf8");
fail();
} catch (MessageException e) {
assertThat(e).hasMessage("JDBC URL must have the property 'useUnicode=true'");
}
}
@Test
public void check_oracle() throws Exception {
File home = temp.newFolder();
File driverFile = new File(home, "extensions/jdbc-driver/oracle/ojdbc6.jar");
FileUtils.touch(driverFile);
Props props = new Props(new Properties());
props.set("sonar.jdbc.url", "jdbc:oracle:thin:@localhost/XE");
settings.checkAndComplete(home, props);
assertThat(props.nonNullValueAsFile(ProcessConstants.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
}
@Test
public void check_h2() throws Exception {
File home = temp.newFolder();
Props props = new Props(new Properties());
props.set("sonar.jdbc.url", "jdbc:h2:tcp://localhost:9092/sonar");
settings.checkAndComplete(home, props);
assertThat(props.value(ProcessConstants.JDBC_DRIVER_PATH)).isNull();
}
@Test
public void check_postgresql() throws Exception {
File home = temp.newFolder();
File driverFile = new File(home, "lib/jdbc/postgresql/pg.jar");
FileUtils.touch(driverFile);
Props props = new Props(new Properties());
props.set("sonar.jdbc.url", "jdbc:postgresql://localhost/sonar");
settings.checkAndComplete(home, props);
assertThat(props.nonNullValueAsFile(ProcessConstants.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
}
@Test
public void check_mssql() throws Exception {
File home = temp.newFolder();
File driverFile = new File(home, "lib/jdbc/jtds/jtds.jar");
FileUtils.touch(driverFile);
Props props = new Props(new Properties());
props.set("sonar.jdbc.url", "jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor");
settings.checkAndComplete(home, props);
assertThat(props.nonNullValueAsFile(ProcessConstants.JDBC_DRIVER_PATH)).isEqualTo(driverFile);
}
@Test
public void driver_file() throws Exception {
File home = temp.newFolder();
File driverFile = new File(home, "extensions/jdbc-driver/oracle/ojdbc6.jar");
FileUtils.touch(driverFile);
String path = settings.driverPath(home, JdbcSettings.Provider.ORACLE);
assertThat(path).isEqualTo(driverFile.getAbsolutePath());
}
@Test
public void driver_dir_does_not_exist() throws Exception {
File home = temp.newFolder();
try {
settings.driverPath(home, JdbcSettings.Provider.ORACLE);
fail();
} catch (MessageException e) {
assertThat(e).hasMessage("Directory does not exist: extensions/jdbc-driver/oracle");
}
}
@Test
public void no_files_in_driver_dir() throws Exception {
File home = temp.newFolder();
FileUtils.forceMkdir(new File(home, "extensions/jdbc-driver/oracle"));
try {
settings.driverPath(home, JdbcSettings.Provider.ORACLE);
fail();
} catch (MessageException e) {
assertThat(e).hasMessage("Directory does not contain JDBC driver: extensions/jdbc-driver/oracle");
}
}
@Test
public void too_many_files_in_driver_dir() throws Exception {
File home = temp.newFolder();
FileUtils.touch(new File(home, "extensions/jdbc-driver/oracle/ojdbc5.jar"));
FileUtils.touch(new File(home, "extensions/jdbc-driver/oracle/ojdbc6.jar"));
try {
settings.driverPath(home, JdbcSettings.Provider.ORACLE);
fail();
} catch (MessageException e) {
assertThat(e).hasMessage("Directory must contain only one JAR file: extensions/jdbc-driver/oracle");
}
}
}
|