Skip to content

Commit f68d9c6

Browse files
committed
Update tests.
1 parent 653733a commit f68d9c6

2 files changed

Lines changed: 13 additions & 25 deletions

File tree

kilo-client/src/main/java/org/httprpc/kilo/io/CSVEncoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public CSVEncoder(Collection<String> keys) {
6161
* The type to format.
6262
*
6363
* @param type
64-
* The type to format.
64+
* The type to format. The type must be final or {@link Date}.
6565
*
6666
* @param formatter
6767
* The formatter to apply to instances of the given type.
@@ -72,7 +72,7 @@ public <T> void format(Class<T> type, Function<? super T, String> formatter) {
7272
throw new IllegalArgumentException();
7373
}
7474

75-
if (type != Date.class && (type.getModifiers() & Modifier.FINAL) == 0) {
75+
if ((type.getModifiers() & Modifier.FINAL) == 0 && type != Date.class) {
7676
throw new IllegalArgumentException();
7777
}
7878

kilo-client/src/test/java/org/httprpc/kilo/io/CSVEncoderTest.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.text.DateFormat;
2222
import java.text.FieldPosition;
2323
import java.text.Format;
24-
import java.text.NumberFormat;
2524
import java.text.ParsePosition;
2625
import java.time.LocalDateTime;
2726
import java.time.format.DateTimeFormatter;
@@ -138,27 +137,19 @@ public void testRecords() throws IOException {
138137

139138
@Test
140139
public void testFormat() throws IOException {
141-
var integer = 1000;
142140
var flag = true;
143-
var date = new Date();
144141
var localDateTime = LocalDateTime.now();
142+
var date = new Date();
145143

146144
var rows = listOf(
147145
mapOf(
148-
entry("a", integer),
149-
entry("b", flag),
150-
entry("c", date),
151-
entry("d", localDateTime)
146+
entry("a", flag),
147+
entry("b", localDateTime),
148+
entry("c", date)
152149
)
153150
);
154151

155-
var csvEncoder = new CSVEncoder(listOf("a", "b", "c", "d"));
156-
157-
var integerFormat = NumberFormat.getNumberInstance();
158-
159-
integerFormat.setGroupingUsed(false);
160-
161-
csvEncoder.format(Integer.class, integerFormat::format);
152+
var csvEncoder = new CSVEncoder(listOf("a", "b", "c"));
162153

163154
var booleanFormat = new Format() {
164155
@Override
@@ -172,25 +163,22 @@ public Object parseObject(String source, ParsePosition pos) {
172163
}
173164
};
174165

175-
csvEncoder.format(Boolean.class, booleanFormat::format);
166+
var dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT);
176167

177168
var dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
178169

179-
csvEncoder.format(Date.class, dateFormat::format);
180-
181-
var dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT);
182-
170+
csvEncoder.format(Boolean.class, booleanFormat::format);
183171
csvEncoder.format(LocalDateTime.class, dateTimeFormatter::format);
172+
csvEncoder.format(Date.class, dateFormat::format);
184173

185174
var writer = new StringWriter();
186175

187176
csvEncoder.write(rows, writer);
188177

189-
var expected = "\"a\",\"b\",\"c\",\"d\"\r\n"
190-
+ "\"" + integerFormat.format(integer) + "\","
178+
var expected = "\"a\",\"b\",\"c\"\r\n"
191179
+ "\"" + booleanFormat.format(flag) + "\","
192-
+ "\"" + dateFormat.format(date) + "\","
193-
+ "\"" + dateTimeFormatter.format(localDateTime) + "\"\r\n";
180+
+ "\"" + dateTimeFormatter.format(localDateTime) + "\","
181+
+ "\"" + dateFormat.format(date) + "\"\r\n";
194182

195183
assertEquals(expected, writer.toString());
196184
}

0 commit comments

Comments
 (0)