/
AbstractDataLoaderTest.java
183 lines (157 loc) · 7.39 KB
/
AbstractDataLoaderTest.java
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Eric Long <erilong@users.sourceforge.net>,
* Chris Henson <chenson42@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/
package org.jumpmind.symmetric.load;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.AbstractTest;
import org.jumpmind.symmetric.common.TestConstants;
import org.jumpmind.symmetric.common.csv.CsvConstants;
import org.springframework.dao.EmptyResultDataAccessException;
import org.testng.Assert;
import com.csvreader.CsvWriter;
public abstract class AbstractDataLoaderTest extends AbstractTest {
protected final static String TEST_TABLE = "test_dataloader_table";
protected final static String[] TEST_KEYS = { "id" };
protected final static String[] TEST_COLUMNS = { "id", "string_value", "string_required_value",
"char_value", "char_required_value", "date_value", "time_value", "boolean_value",
"integer_value", "decimal_value" };
protected static int batchId = 0;
protected static int sequenceId = 9;
@SuppressWarnings("unchecked")
public void testSimple(String dmlType, String[] values, String[] expectedValues) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
CsvWriter writer = getWriter(out);
writer.writeRecord(new String[] { CsvConstants.NODEID, TestConstants.TEST_CLIENT_EXTERNAL_ID });
writeTable(writer, TEST_TABLE, TEST_KEYS, TEST_COLUMNS);
String nextBatchId = getNextBatchId();
writer.writeRecord(new String[] { CsvConstants.BATCH, nextBatchId });
writer.write(dmlType);
writer.writeRecord(values, true);
writer.writeRecord(new String[] { CsvConstants.COMMIT, nextBatchId });
writer.close();
load(out);
assertTestTableEquals(values[0], expectedValues);
}
protected CsvWriter getWriter(OutputStream out) {
CsvWriter writer = new CsvWriter(new OutputStreamWriter(out), ',');
writer.setEscapeMode(CsvWriter.ESCAPE_MODE_BACKSLASH);
return writer;
}
protected abstract void load(ByteArrayOutputStream out) throws Exception;
protected void writeTable(CsvWriter writer, String tableName, String[] keys, String[] columns)
throws IOException {
writer.writeRecord(new String[] { "table", tableName });
writer.write("keys");
writer.writeRecord(keys);
writer.write("columns");
writer.writeRecord(columns);
}
@SuppressWarnings("unchecked")
protected void assertTestTableEquals(String testTableId, String[] expectedValues) {
String sql = "select " + getSelect(TEST_COLUMNS) + " from " + TEST_TABLE + " where " + getWhere(TEST_KEYS);
Map<String, Object> results = null;
try {
results = getJdbcTemplate().queryForMap(sql, new Object[] { testTableId });
} catch (EmptyResultDataAccessException e) {
}
if (expectedValues != null) {
expectedValues[1] = translateExpectedString(expectedValues[1], false);
expectedValues[2] = translateExpectedString(expectedValues[2], true);
expectedValues[3] = translateExpectedCharString(expectedValues[3], 50, false);
expectedValues[4] = translateExpectedCharString(expectedValues[4], 50, true);
}
assertEquals(TEST_COLUMNS, expectedValues, results);
}
protected void assertEquals(String[] name, String[] expected, Map<String, Object> results) {
if (expected == null) {
Assert.assertNull(results, "Expected empty results");
} else {
Assert.assertNotNull(results, "Expected non-empty results");
for (int i = 0; i < expected.length; i++) {
Object resultObj = results.get(name[i]);
String resultValue = null;
if (resultObj instanceof BigDecimal && expected[i].indexOf(".") != -1) {
DecimalFormat df = new DecimalFormat("#.00");
resultValue = df.format(resultObj);
} else if (resultObj instanceof Date) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0");
resultValue = df.format(resultObj);
} else if (resultObj != null) {
resultValue = resultObj.toString();
}
Assert.assertEquals(resultValue, expected[i], name[i]);
}
}
}
protected String getSelect(String[] columns) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < columns.length; i++) {
str.append(columns[i]).append(i + 1 < columns.length ? ", " : "");
}
return str.toString();
}
protected String getWhere(String[] columns) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < columns.length; i++) {
str.append(columns[i]).append(" = ?").append(i + 1 < columns.length ? "," : "");
}
return str.toString();
}
protected String translateExpectedString(String value, boolean isRequired) {
if (isRequired && (value == null || (value.equals("") && getDbDialect().isEmptyStringNulled()))) {
return TableTemplate.REQUIRED_FIELD_NULL_SUBSTITUTE;
} else if (value != null && value.equals("") && getDbDialect().isEmptyStringNulled()) {
return null;
}
return value;
}
protected String translateExpectedCharString(String value, int size, boolean isRequired) {
if (isRequired && value == null) {
value = TableTemplate.REQUIRED_FIELD_NULL_SUBSTITUTE;
}
if (value != null && getDbDialect().isCharSpacePadded()) {
return StringUtils.rightPad(value, size);
} else if (value != null && getDbDialect().isCharSpaceTrimmed()) {
return value.replaceFirst(" *$", "");
}
return value;
}
protected synchronized String getNextBatchId() {
return Integer.toString(++batchId);
}
protected synchronized String getBatchId() {
return Integer.toString(batchId);
}
protected synchronized String getNextId() {
return Integer.toString(++sequenceId);
}
protected synchronized String getId() {
return Integer.toString(sequenceId);
}
}