14b57f91a7d5110df7b97d9691170eeff4faa9e0
[sqoop.git] / src / test / org / apache / sqoop / manager / oracle / util / OracleUtils.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.oracle.util;
20
21 import java.sql.Connection;
22 import java.sql.SQLException;
23 import java.sql.Statement;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import org.apache.sqoop.SqoopOptions;
29 import org.apache.sqoop.manager.ConnManager;
30 import org.apache.sqoop.testutil.SqlUtil;
31
32 /**
33 * Helper methods for Oracle testing.
34 */
35 public final class OracleUtils {
36
37 public static final Log LOG = LogFactory.getLog(OracleUtils.class.getName());
38
39 // Express edition hardcoded name.
40 public static final String ORACLE_DATABASE_NAME = "xe";
41
42 public static final String CONNECT_STRING = System.getProperty("sqoop.test.oracle.connectstring", "jdbc:oracle:thin:@//localhost/" + ORACLE_DATABASE_NAME);
43 public static final String ORACLE_USER_NAME = System.getProperty("sqoop.test.oracle.username", "SQOOPTEST");
44 public static final String ORACLE_USER_PASS = System.getProperty("sqoop.test.oracle.password", "12345");
45
46 public static final String ORACLE_SECONDARY_USER_NAME = "SQOOPTEST2";
47 public static final String ORACLE_SECONDARY_USER_PASS = "ABCDEF";
48
49 public static final String ORACLE_INVALID_USER_NAME = "invalidusr";
50 public static final String SYSTEMTEST_TABLE_NAME = "ORAOOP_TEST";
51 public static final int SYSTEMTEST_NUM_ROWS = 100;
52 public static final int INTEGRATIONTEST_NUM_ROWS = 10000;
53 // Number of mappers if wanting to override default setting
54 public static final int NUM_MAPPERS = 0;
55 // Oracle degree of parallelism to use when creating table.
56 // If 0 we will calculate a recommended value
57 public static final int ORACLE_PARALLEL_DEGREE = 0;
58
59 private OracleUtils() { }
60
61 public static void setOracleAuth(SqoopOptions options) {
62 options.setUsername(ORACLE_USER_NAME);
63 options.setPassword(ORACLE_USER_PASS);
64 }
65
66 public static void setOracleSecondaryUserAuth(SqoopOptions options) {
67 options.setUsername(ORACLE_SECONDARY_USER_NAME);
68 options.setPassword(ORACLE_SECONDARY_USER_PASS);
69 }
70
71 /**
72 * Drop a table if it exists.
73 * Use the executeStatement method in {@link SqlUtil} instead.
74 */
75 @Deprecated
76 public static void dropTable(String tableName, ConnManager manager)
77 throws SQLException {
78 Connection connection = null;
79 Statement st = null;
80
81 try {
82 connection = manager.getConnection();
83 connection.setAutoCommit(false);
84 st = connection.createStatement();
85
86 // create the database table and populate it with data.
87 st.executeUpdate(getDropTableStatement(tableName));
88
89 connection.commit();
90 } finally {
91 try {
92 if (null != st) {
93 st.close();
94 }
95 } catch (SQLException sqlE) {
96 LOG.warn("Got SQLException when closing connection: " + sqlE);
97 }
98 }
99 }
100
101 public static String getDropTableStatement(String tableName) {
102 return "BEGIN EXECUTE IMMEDIATE 'DROP TABLE " + tableName + "'; "
103 + "exception when others then null; end;";
104 }
105
106 }