be205c8778d87c0d09008b8de69bec8aee4cad29
[sqoop.git] / src / test / org / apache / sqoop / manager / mysql / MySQLTestUtils.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.sqoop.manager.mysql;
20
21 import org.apache.commons.lang3.StringUtils;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.sqoop.SqoopOptions;
25 import org.apache.sqoop.manager.ConnManager;
26
27 import java.sql.Connection;
28 import java.sql.PreparedStatement;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.util.ArrayList;
32
33 /**
34 * Utilities for mysql-based tests.
35 */
36 public final class MySQLTestUtils {
37
38 public static final Log LOG = LogFactory.getLog(
39 MySQLTestUtils.class.getName());
40
41 private String hostUrl;
42
43 private String userName;
44 private String userPass;
45
46 private String mysqlDbNAme;
47 private String mySqlConnectString;
48
49 public MySQLTestUtils() {
50 hostUrl = System.getProperty(
51 "sqoop.test.mysql.connectstring.host_url",
52 "jdbc:mysql://localhost/");
53 userName = System.getProperty("sqoop.test.mysql.username", getCurrentUser());
54 userPass = System.getProperty("sqoop.test.mysql.password");
55
56 mysqlDbNAme = System.getProperty("sqoop.test.mysql.databasename", "sqooptestdb");
57 mySqlConnectString = getHostUrl() + getMysqlDbNAme();
58 }
59
60 public String getHostUrl() {
61 return hostUrl;
62 }
63
64 public String getUserName() {
65 return userName;
66 }
67
68 public String getUserPass() {
69 return userPass;
70 }
71
72 public String getMysqlDbNAme() {
73 return mysqlDbNAme;
74 }
75
76
77 public String getMySqlConnectString() {
78 return mySqlConnectString;
79 }
80
81 public String[] addUserNameAndPasswordToArgs(String[] extraArgs) {
82 int extraLength = isSet(getUserPass()) ? 4 : 2;
83 String[] moreArgs = new String[extraArgs.length + extraLength];
84 int i = 0;
85 for (i = 0; i < extraArgs.length; i++) {
86 moreArgs[i] = extraArgs[i];
87 }
88
89 // Add username argument for mysql.
90 moreArgs[i++] = "--username";
91 moreArgs[i++] = getUserName();
92 if (isSet(userPass)) {
93 moreArgs[i++] = "--password";
94 moreArgs[i++] = getUserPass();
95 }
96 return moreArgs;
97 }
98
99 private static String getCurrentUser() {
100 // First, check the $USER environment variable.
101 String envUser = System.getenv("USER");
102 if (null != envUser) {
103 return envUser;
104 }
105 // Fall back to user.name system property
106 envUser = System.getProperty("user.name");
107 if (null != envUser) {
108 return envUser;
109 }
110 throw new RuntimeException("MySQL username not set and unable to get system user. Please set it"
111 + " with '-Dsqoop.test.mysql.username=...' or USER environment variable!");
112 }
113
114 public void addPasswordIfIsSet(ArrayList<String> args) {
115 if (isSet(userPass)) {
116 args.add("--password");
117 args.add(getUserPass());
118 }
119 }
120
121 private boolean isSet(String userPass) {
122 return !StringUtils.isBlank(userPass);
123 }
124
125 public void addPasswordIfIsSet(SqoopOptions opts) {
126 if (isSet(userPass)) {
127 opts.setPassword(getUserPass());
128 }
129 }
130
131 public void dropTableIfExists(String table, ConnManager manager) throws SQLException {
132 Connection conn = manager.getConnection();
133 PreparedStatement statement = conn.prepareStatement(
134 "DROP TABLE IF EXISTS " + table,
135 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
136 try {
137 statement.executeUpdate();
138 conn.commit();
139 } finally {
140 statement.close();
141 }
142 }
143 }