mirror of
https://github.com/apache/sqoop.git
synced 2025-05-03 05:50:31 +08:00
SQOOP-3308: Mock ConnManager field in TestTableDefWriter
(Szabolcs Vasas via Boglarka Egyed)
This commit is contained in:
parent
7186b9d654
commit
d2c40366a7
@ -20,7 +20,6 @@
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
@ -79,17 +78,6 @@ public TableDefWriter(final SqoopOptions opts, final ConnManager connMgr,
|
|||||||
this.commentsEnabled = withComments;
|
this.commentsEnabled = withComments;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Integer> externalColTypes;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the column type map to be used.
|
|
||||||
* (dependency injection for testing; not used in production.)
|
|
||||||
*/
|
|
||||||
public void setColumnTypes(Map<String, Integer> colTypes) {
|
|
||||||
this.externalColTypes = colTypes;
|
|
||||||
LOG.debug("Using test-controlled type map");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the column names to import.
|
* Get the column names to import.
|
||||||
*/
|
*/
|
||||||
@ -97,14 +85,6 @@ public void setColumnTypes(Map<String, Integer> colTypes) {
|
|||||||
String [] colNames = options.getColumns();
|
String [] colNames = options.getColumns();
|
||||||
if (null != colNames) {
|
if (null != colNames) {
|
||||||
return colNames; // user-specified column names.
|
return colNames; // user-specified column names.
|
||||||
} else if (null != externalColTypes) {
|
|
||||||
// Test-injection column mapping. Extract the col names from this.
|
|
||||||
ArrayList<String> keyList = new ArrayList<String>();
|
|
||||||
for (String key : externalColTypes.keySet()) {
|
|
||||||
keyList.add(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
return keyList.toArray(new String[keyList.size()]);
|
|
||||||
} else if (null != inputTableName) {
|
} else if (null != inputTableName) {
|
||||||
return connManager.getColumnNames(inputTableName);
|
return connManager.getColumnNames(inputTableName);
|
||||||
} else {
|
} else {
|
||||||
@ -119,16 +99,11 @@ public String getCreateTableStmt() throws IOException {
|
|||||||
Map<String, Integer> columnTypes;
|
Map<String, Integer> columnTypes;
|
||||||
Properties userMapping = options.getMapColumnHive();
|
Properties userMapping = options.getMapColumnHive();
|
||||||
Boolean isHiveExternalTableSet = !StringUtils.isBlank(options.getHiveExternalTableDir());
|
Boolean isHiveExternalTableSet = !StringUtils.isBlank(options.getHiveExternalTableDir());
|
||||||
if (externalColTypes != null) {
|
// Get these from the database.
|
||||||
// Use pre-defined column types.
|
if (null != inputTableName) {
|
||||||
columnTypes = externalColTypes;
|
columnTypes = connManager.getColumnTypes(inputTableName);
|
||||||
} else {
|
} else {
|
||||||
// Get these from the database.
|
columnTypes = connManager.getColumnTypesForQuery(options.getSqlQuery());
|
||||||
if (null != inputTableName) {
|
|
||||||
columnTypes = connManager.getColumnTypes(inputTableName);
|
|
||||||
} else {
|
|
||||||
columnTypes = connManager.getColumnTypesForQuery(options.getSqlQuery());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String [] colNames = getColumnNames();
|
String [] colNames = getColumnNames();
|
||||||
|
@ -23,12 +23,13 @@
|
|||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.apache.hadoop.conf.Configuration;
|
import org.apache.hadoop.conf.Configuration;
|
||||||
|
import org.apache.sqoop.manager.ConnManager;
|
||||||
import org.apache.sqoop.util.SqlTypeMap;
|
import org.apache.sqoop.util.SqlTypeMap;
|
||||||
|
|
||||||
import org.apache.sqoop.SqoopOptions;
|
import org.apache.sqoop.SqoopOptions;
|
||||||
import org.apache.sqoop.tool.ImportTool;
|
|
||||||
import org.apache.sqoop.testutil.HsqldbTestServer;
|
import org.apache.sqoop.testutil.HsqldbTestServer;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
@ -39,6 +40,9 @@
|
|||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.Matchers.anyString;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,9 +53,34 @@ public class TestTableDefWriter {
|
|||||||
public static final Log LOG = LogFactory.getLog(
|
public static final Log LOG = LogFactory.getLog(
|
||||||
TestTableDefWriter.class.getName());
|
TestTableDefWriter.class.getName());
|
||||||
|
|
||||||
|
private ConnManager connManager;
|
||||||
|
|
||||||
|
private Configuration conf;
|
||||||
|
|
||||||
|
private SqoopOptions options;
|
||||||
|
|
||||||
|
private TableDefWriter writer;
|
||||||
|
|
||||||
|
private String inputTable;
|
||||||
|
|
||||||
|
private String outputTable;
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public ExpectedException thrown = ExpectedException.none();
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() {
|
||||||
|
inputTable = HsqldbTestServer.getTableName();
|
||||||
|
outputTable = "outputTable";
|
||||||
|
connManager = mock(ConnManager.class);
|
||||||
|
conf = new Configuration();
|
||||||
|
options = new SqoopOptions();
|
||||||
|
when(connManager.getColumnTypes(anyString())).thenReturn(new SqlTypeMap<String, Integer>());
|
||||||
|
when(connManager.getColumnNames(anyString())).thenReturn(new String[]{});
|
||||||
|
|
||||||
|
writer = new TableDefWriter(options, connManager, inputTable, outputTable, conf, false);
|
||||||
|
}
|
||||||
|
|
||||||
// Test getHiveOctalCharCode and expect an IllegalArgumentException.
|
// Test getHiveOctalCharCode and expect an IllegalArgumentException.
|
||||||
private void expectExceptionInCharCode(int charCode) {
|
private void expectExceptionInCharCode(int charCode) {
|
||||||
thrown.expect(IllegalArgumentException.class);
|
thrown.expect(IllegalArgumentException.class);
|
||||||
@ -73,14 +102,6 @@ public void testHiveOctalCharCode() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDifferentTableNames() throws Exception {
|
public void testDifferentTableNames() throws Exception {
|
||||||
Configuration conf = new Configuration();
|
|
||||||
SqoopOptions options = new SqoopOptions();
|
|
||||||
TableDefWriter writer = new TableDefWriter(options, null,
|
|
||||||
"inputTable", "outputTable", conf, false);
|
|
||||||
|
|
||||||
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
|
||||||
writer.setColumnTypes(colTypes);
|
|
||||||
|
|
||||||
String createTable = writer.getCreateTableStmt();
|
String createTable = writer.getCreateTableStmt();
|
||||||
String loadData = writer.getLoadDataStmt();
|
String loadData = writer.getLoadDataStmt();
|
||||||
|
|
||||||
@ -91,24 +112,15 @@ public void testDifferentTableNames() throws Exception {
|
|||||||
assertTrue(createTable.indexOf(
|
assertTrue(createTable.indexOf(
|
||||||
"CREATE TABLE IF NOT EXISTS `outputTable`") != -1);
|
"CREATE TABLE IF NOT EXISTS `outputTable`") != -1);
|
||||||
assertTrue(loadData.indexOf("INTO TABLE `outputTable`") != -1);
|
assertTrue(loadData.indexOf("INTO TABLE `outputTable`") != -1);
|
||||||
assertTrue(loadData.indexOf("/inputTable'") != -1);
|
assertTrue(loadData.indexOf("/" + inputTable + "'") != -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDifferentTargetDirs() throws Exception {
|
public void testDifferentTargetDirs() throws Exception {
|
||||||
String targetDir = "targetDir";
|
String targetDir = "targetDir";
|
||||||
String inputTable = "inputTable";
|
|
||||||
String outputTable = "outputTable";
|
|
||||||
|
|
||||||
Configuration conf = new Configuration();
|
|
||||||
SqoopOptions options = new SqoopOptions();
|
|
||||||
// Specify a different target dir from input table name
|
// Specify a different target dir from input table name
|
||||||
options.setTargetDir(targetDir);
|
options.setTargetDir(targetDir);
|
||||||
TableDefWriter writer = new TableDefWriter(options, null,
|
|
||||||
inputTable, outputTable, conf, false);
|
|
||||||
|
|
||||||
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
|
||||||
writer.setColumnTypes(colTypes);
|
|
||||||
|
|
||||||
String createTable = writer.getCreateTableStmt();
|
String createTable = writer.getCreateTableStmt();
|
||||||
String loadData = writer.getLoadDataStmt();
|
String loadData = writer.getLoadDataStmt();
|
||||||
@ -125,18 +137,8 @@ public void testDifferentTargetDirs() throws Exception {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPartitions() throws Exception {
|
public void testPartitions() throws Exception {
|
||||||
String[] args = {
|
options.setHivePartitionKey("ds");
|
||||||
"--hive-partition-key", "ds",
|
options.setHivePartitionValue("20110413");
|
||||||
"--hive-partition-value", "20110413",
|
|
||||||
};
|
|
||||||
Configuration conf = new Configuration();
|
|
||||||
SqoopOptions options =
|
|
||||||
new ImportTool().parseArguments(args, null, null, false);
|
|
||||||
TableDefWriter writer = new TableDefWriter(options,
|
|
||||||
null, "inputTable", "outputTable", conf, false);
|
|
||||||
|
|
||||||
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
|
||||||
writer.setColumnTypes(colTypes);
|
|
||||||
|
|
||||||
String createTable = writer.getCreateTableStmt();
|
String createTable = writer.getCreateTableStmt();
|
||||||
String loadData = writer.getLoadDataStmt();
|
String loadData = writer.getLoadDataStmt();
|
||||||
@ -152,18 +154,8 @@ public void testPartitions() throws Exception {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLzoSplitting() throws Exception {
|
public void testLzoSplitting() throws Exception {
|
||||||
String[] args = {
|
options.setUseCompression(true);
|
||||||
"--compress",
|
options.setCompressionCodec("lzop");
|
||||||
"--compression-codec", "lzop",
|
|
||||||
};
|
|
||||||
Configuration conf = new Configuration();
|
|
||||||
SqoopOptions options =
|
|
||||||
new ImportTool().parseArguments(args, null, null, false);
|
|
||||||
TableDefWriter writer = new TableDefWriter(options,
|
|
||||||
null, "inputTable", "outputTable", conf, false);
|
|
||||||
|
|
||||||
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
|
||||||
writer.setColumnTypes(colTypes);
|
|
||||||
|
|
||||||
String createTable = writer.getCreateTableStmt();
|
String createTable = writer.getCreateTableStmt();
|
||||||
String loadData = writer.getLoadDataStmt();
|
String loadData = writer.getLoadDataStmt();
|
||||||
@ -181,19 +173,13 @@ public void testLzoSplitting() throws Exception {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUserMappingNoDecimal() throws Exception {
|
public void testUserMappingNoDecimal() throws Exception {
|
||||||
String[] args = {
|
options.setMapColumnHive("id=STRING,value=INTEGER");
|
||||||
"--map-column-hive", "id=STRING,value=INTEGER",
|
|
||||||
};
|
|
||||||
Configuration conf = new Configuration();
|
|
||||||
SqoopOptions options =
|
|
||||||
new ImportTool().parseArguments(args, null, null, false);
|
|
||||||
TableDefWriter writer = new TableDefWriter(options,
|
|
||||||
null, HsqldbTestServer.getTableName(), "outputTable", conf, false);
|
|
||||||
|
|
||||||
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
||||||
colTypes.put("id", Types.INTEGER);
|
colTypes.put("id", Types.INTEGER);
|
||||||
colTypes.put("value", Types.VARCHAR);
|
colTypes.put("value", Types.VARCHAR);
|
||||||
writer.setColumnTypes(colTypes);
|
|
||||||
|
setUpMockConnManager(HsqldbTestServer.getTableName(), colTypes);
|
||||||
|
|
||||||
String createTable = writer.getCreateTableStmt();
|
String createTable = writer.getCreateTableStmt();
|
||||||
|
|
||||||
@ -208,15 +194,7 @@ public void testUserMappingNoDecimal() throws Exception {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUserMappingWithDecimal() throws Exception {
|
public void testUserMappingWithDecimal() throws Exception {
|
||||||
String[] args = {
|
options.setMapColumnHive("id=STRING,value2=DECIMAL(13,5),value1=INTEGER,value3=DECIMAL(4,5),value4=VARCHAR(255)");
|
||||||
"--map-column-hive", "id=STRING,value2=DECIMAL(13,5),value1=INTEGER," +
|
|
||||||
"value3=DECIMAL(4,5),value4=VARCHAR(255)",
|
|
||||||
};
|
|
||||||
Configuration conf = new Configuration();
|
|
||||||
SqoopOptions options =
|
|
||||||
new ImportTool().parseArguments(args, null, null, false);
|
|
||||||
TableDefWriter writer = new TableDefWriter(options,
|
|
||||||
null, HsqldbTestServer.getTableName(), "outputTable", conf, false);
|
|
||||||
|
|
||||||
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
||||||
colTypes.put("id", Types.INTEGER);
|
colTypes.put("id", Types.INTEGER);
|
||||||
@ -224,7 +202,8 @@ public void testUserMappingWithDecimal() throws Exception {
|
|||||||
colTypes.put("value2", Types.DOUBLE);
|
colTypes.put("value2", Types.DOUBLE);
|
||||||
colTypes.put("value3", Types.FLOAT);
|
colTypes.put("value3", Types.FLOAT);
|
||||||
colTypes.put("value4", Types.CHAR);
|
colTypes.put("value4", Types.CHAR);
|
||||||
writer.setColumnTypes(colTypes);
|
|
||||||
|
setUpMockConnManager(HsqldbTestServer.getTableName(), colTypes);
|
||||||
|
|
||||||
String createTable = writer.getCreateTableStmt();
|
String createTable = writer.getCreateTableStmt();
|
||||||
|
|
||||||
@ -245,37 +224,20 @@ public void testUserMappingWithDecimal() throws Exception {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUserMappingFailWhenCantBeApplied() throws Exception {
|
public void testUserMappingFailWhenCantBeApplied() throws Exception {
|
||||||
String[] args = {
|
options.setMapColumnHive("id=STRING,value=INTEGER");
|
||||||
"--map-column-hive", "id=STRING,value=INTEGER",
|
|
||||||
};
|
|
||||||
Configuration conf = new Configuration();
|
|
||||||
SqoopOptions options =
|
|
||||||
new ImportTool().parseArguments(args, null, null, false);
|
|
||||||
TableDefWriter writer = new TableDefWriter(options,
|
|
||||||
null, HsqldbTestServer.getTableName(), "outputTable", conf, false);
|
|
||||||
|
|
||||||
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
||||||
colTypes.put("id", Types.INTEGER);
|
colTypes.put("id", Types.INTEGER);
|
||||||
writer.setColumnTypes(colTypes);
|
setUpMockConnManager(HsqldbTestServer.getTableName(), colTypes);
|
||||||
|
|
||||||
thrown.expect(IllegalArgumentException.class);
|
thrown.expect(IllegalArgumentException.class);
|
||||||
thrown.reportMissingExceptionWithMessage("Expected IllegalArgumentException on non applied Hive type mapping");
|
thrown.reportMissingExceptionWithMessage("Expected IllegalArgumentException on non applied Hive type mapping");
|
||||||
String createTable = writer.getCreateTableStmt();
|
writer.getCreateTableStmt();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHiveDatabase() throws Exception {
|
public void testHiveDatabase() throws Exception {
|
||||||
String[] args = {
|
options.setHiveDatabaseName("db");
|
||||||
"--hive-database", "db",
|
|
||||||
};
|
|
||||||
Configuration conf = new Configuration();
|
|
||||||
SqoopOptions options =
|
|
||||||
new ImportTool().parseArguments(args, null, null, false);
|
|
||||||
TableDefWriter writer = new TableDefWriter(options,
|
|
||||||
null, HsqldbTestServer.getTableName(), "outputTable", conf, false);
|
|
||||||
|
|
||||||
Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
|
|
||||||
writer.setColumnTypes(colTypes);
|
|
||||||
|
|
||||||
String createTable = writer.getCreateTableStmt();
|
String createTable = writer.getCreateTableStmt();
|
||||||
assertNotNull(createTable);
|
assertNotNull(createTable);
|
||||||
@ -286,4 +248,9 @@ public void testHiveDatabase() throws Exception {
|
|||||||
assertTrue(createTable.contains("`db`.`outputTable`"));
|
assertTrue(createTable.contains("`db`.`outputTable`"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setUpMockConnManager(String tableName, Map<String, Integer> typeMap) {
|
||||||
|
when(connManager.getColumnTypes(tableName)).thenReturn(typeMap);
|
||||||
|
when(connManager.getColumnNames(tableName)).thenReturn(typeMap.keySet().toArray(new String[]{}));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user