1 package org.codehaus.mojo.dbunit;
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 import java.sql.Connection;
28 import java.sql.Driver;
29 import java.sql.SQLException;
30 import java.util.Properties;
31
32 import org.apache.maven.plugin.AbstractMojo;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.MojoFailureException;
35 import org.apache.maven.settings.Server;
36 import org.apache.maven.settings.Settings;
37 import org.dbunit.database.DatabaseConfig;
38 import org.dbunit.database.DatabaseConnection;
39 import org.dbunit.database.ForwardOnlyResultSetTableFactory;
40 import org.dbunit.database.IDatabaseConnection;
41 import org.dbunit.database.IMetadataHandler;
42 import org.dbunit.dataset.datatype.IDataTypeFactory;
43
44
45
46
47
48
49
50
51 public abstract class AbstractDbUnitMojo
52 extends AbstractMojo
53 {
54
55
56
57
58
59
60
61 protected String driver;
62
63
64
65
66
67
68 protected String username;
69
70
71
72
73
74
75 protected String password;
76
77
78
79
80
81
82
83 protected String url;
84
85
86
87
88
89
90 protected String schema;
91
92
93
94
95
96
97 protected String dataTypeFactoryName = "org.dbunit.dataset.datatype.DefaultDataTypeFactory";
98
99
100
101
102
103 protected boolean supportBatchStatement;
104
105
106
107
108
109
110 protected boolean useQualifiedTableNames;
111
112
113
114
115
116 protected boolean datatypeWarning;
117
118
119
120
121
122
123 protected String escapePattern;
124
125
126
127
128
129
130
131 protected boolean skipOracleRecycleBinTables;
132
133
134
135
136
137
138 protected boolean skip;
139
140
141
142
143
144
145 private Settings settings;
146
147
148
149
150
151
152 private String settingsKey;
153
154
155
156
157
158
159 protected String metadataHandlerName;
160
161
162
163
164
165
166
167 private boolean caseSensitiveTableNames;
168
169
170
171
172
173 public void execute()
174 throws MojoExecutionException, MojoFailureException
175 {
176 loadUserInfoFromSettings();
177 }
178
179 IDatabaseConnection createConnection()
180 throws Exception
181 {
182
183
184 Class dc = Class.forName( driver );
185 Driver driverInstance = (Driver) dc.newInstance();
186 Properties info = new Properties();
187 info.put( "user", username );
188
189 if ( password != null )
190 {
191 info.put( "password", password );
192 }
193
194 Connection conn = driverInstance.connect( url, info );
195
196 if ( conn == null )
197 {
198
199 throw new SQLException( "No suitable Driver for " + url );
200 }
201 conn.setAutoCommit( true );
202
203 IDatabaseConnection connection = new DatabaseConnection( conn, schema );
204 DatabaseConfig config = connection.getConfig();
205 config.setFeature( DatabaseConfig.FEATURE_BATCHED_STATEMENTS, supportBatchStatement );
206 config.setFeature( DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, useQualifiedTableNames );
207 config.setFeature( DatabaseConfig.FEATURE_DATATYPE_WARNING, datatypeWarning );
208 config.setFeature( DatabaseConfig.FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES, this.skipOracleRecycleBinTables );
209 config.setFeature( DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, caseSensitiveTableNames );
210
211 config.setProperty( DatabaseConfig.PROPERTY_ESCAPE_PATTERN, escapePattern );
212 config.setProperty( DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, new ForwardOnlyResultSetTableFactory() );
213
214
215 IDataTypeFactory dataTypeFactory = (IDataTypeFactory) Class.forName( dataTypeFactoryName ).newInstance();
216 config.setProperty( DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory );
217
218
219 IMetadataHandler metadataHandler = (IMetadataHandler) Class.forName( metadataHandlerName ).newInstance();
220 config.setProperty( DatabaseConfig.PROPERTY_METADATA_HANDLER, metadataHandler );
221
222 return connection;
223 }
224
225
226
227
228 private void loadUserInfoFromSettings()
229 throws MojoExecutionException
230 {
231 if ( this.settingsKey == null )
232 {
233 this.settingsKey = url;
234 }
235
236 if ( ( username == null || password == null ) && ( settings != null ) )
237 {
238 Server server = this.settings.getServer( this.settingsKey );
239
240 if ( server != null )
241 {
242 if ( username == null )
243 {
244 username = server.getUsername();
245 }
246
247 if ( password == null )
248 {
249 password = server.getPassword();
250 }
251 }
252 }
253
254 if ( username == null )
255 {
256
257 username = "" ;
258 }
259
260 if ( password == null )
261 {
262
263 password = "" ;
264 }
265 }
266
267
268 }