NameMatcher extends Matcher { @Override public Object[] getMatchingData(Object[] fields) { if (getToSchema().isEmpty()) { return fields; } Object[] out = new Object[getToSchema().getColumnsCount()]; int i = 0; for (Column toCol : getToSchema().getColumnsList()) { Integer fromIndex = fromColNameIndexMap.get(toCol.getName()); if (fromIndex != null && fromIndex < fields.length) { out[i] = fields[fromIndex]; } else { tryFillNullInArrayForUnexpectedColumn(toCol, out, i); } i++; } return out; } NameMatcher(Schema from, Schema to); @Override Object[] getMatchingData(Object[] fields);  }
@Test public void testPerfectMatch() { matcher = new NameMatcher( SchemaFixture.createSchema1("from"), SchemaFixture.createSchema1("to")); Object[] fields = SchemaFixture.createNotNullRecordForSchema1(); Object[] actual = matcher.getMatchingData(fields); assertArrayEquals(fields, actual); }
@Test public void testDirectFieldsCopy() { matcher = new NameMatcher( NullSchema.getInstance(), SchemaFixture.createSchema1("to")); Object[] fields = SchemaFixture.createNotNullRecordForSchema1(); Object[] actual = matcher.getMatchingData(fields); assertArrayEquals(fields, actual); }
@Test public void testConvertWhenToSchemaIsLongerThanFromSchema() { matcher = new NameMatcher( SchemaFixture.createSchema("from", new String[]{"text1", "text2"}), SchemaFixture.createSchema("to", new String[]{"text3", "text1", "text2"})); Object[] fields = new Object[] {"t1", "t2"}; Object[] actual = matcher.getMatchingData(fields); assertArrayEquals(new Object[] {null, "t1", "t2"}, actual); }
@Test (expectedExceptions = SqoopException.class) public void testConvertWhenToSchemaIsLongerThanFromSchemaFail() { Schema from = SchemaFixture.createSchema("from", new String[]{"text1", "text2"}); Schema to = SchemaFixture.createSchema("to", new String[]{"text4", "text3", "text2", "text1"}); to.getColumnsList().get(0).setNullable(true); to.getColumnsList().get(1).setNullable(false); matcher = new NameMatcher(from, to); Object[] fields = new Object[] {"t1", "t2"}; matcher.getMatchingData(fields); }