1 package com.opencsv.bean;
2
3 import com.opencsv.exceptions.CsvDataTypeMismatchException;
4 import org.junit.jupiter.api.BeforeEach;
5 import org.junit.jupiter.api.DisplayName;
6 import org.junit.jupiter.api.Test;
7 import org.junit.jupiter.params.ParameterizedTest;
8 import org.junit.jupiter.params.provider.Arguments;
9 import org.junit.jupiter.params.provider.MethodSource;
10 import org.junit.jupiter.params.provider.NullAndEmptySource;
11 import org.junit.jupiter.params.provider.ValueSource;
12
13 import java.util.Locale;
14 import java.util.Objects;
15 import java.util.UUID;
16 import java.util.stream.Stream;
17
18 import static org.junit.jupiter.api.Assertions.*;
19
20 public class ConverterUUIDTest {
21 private static final String UUID_STRING = "7F804F85-0064-4E96-8260-3FD47EA6A8BB";
22 private static final UUID EXPECTED_UUID = UUID.fromString(UUID_STRING);
23
24 private ConverterUUID converter;
25
26 @BeforeEach
27 public void setUp() {
28 converter = new ConverterUUID(Locale.getDefault());
29 }
30
31 public static Stream<Arguments> buildLegalUUIDValues() {
32 return Stream.of(
33 Arguments.of(UUID_STRING),
34 Arguments.of(UUID_STRING.toLowerCase()),
35 Arguments.of(UUID_STRING.toUpperCase()),
36 Arguments.of(UUID_STRING + " "),
37 Arguments.of(" " + UUID_STRING),
38 Arguments.of(" " + UUID_STRING + " ")
39 );
40 }
41
42 @DisplayName("Can convert UUID values to an java.util.UUID object.")
43 @ParameterizedTest
44 @MethodSource("buildLegalUUIDValues")
45 public void convertToRead(String uuidValue) throws CsvDataTypeMismatchException {
46 assertEquals(EXPECTED_UUID, converter.convertToRead(uuidValue));
47 }
48
49 @DisplayName("Convert uuid to a string")
50 @Test
51 public void convertToWrite() throws CsvDataTypeMismatchException {
52 assertEquals(UUID_STRING.toUpperCase(), converter.convertToWrite(EXPECTED_UUID).toUpperCase());
53 }
54
55 @DisplayName("convertToWrite handles null UUID object.")
56 @Test
57 public void convertToWriteWithNull() throws CsvDataTypeMismatchException {
58 assertEquals("", converter.convertToWrite(null));
59 }
60
61 public static Stream<Arguments> buildIllegalUUIDValues() {
62 return Stream.of(
63 Arguments.of("17F804F85-0064-4E96-8260-3FD47EA6A8BB"),
64 Arguments.of("7F804F85-10064-4E96-8260-3FD47EA6A8BB"),
65 Arguments.of("7F804F85-0064-14E96-8260-3FD47EA6A8BB"),
66 Arguments.of("7F804F85-0064-4E96-18260-13FD47EA6A8BB"),
67 Arguments.of("7G804F85-0064-4E96-8260-3FD47EA6A8BB"),
68 Arguments.of("7F804F85-006G-4E96-8260-3FD47EA6A8BB"),
69 Arguments.of("7F804F85-0064-4G96-8260-3FD47EA6A8BB"),
70 Arguments.of("7F804F85-0064-4E96-G260-3FD47EA6A8BB"),
71 Arguments.of("7F804F85-0064-4E96-8260-3GD47EA6A8BB")
72 );
73 }
74
75 @DisplayName("convertToRead handles illegal values.")
76 @ParameterizedTest
77 @MethodSource("buildIllegalUUIDValues")
78 public void convertToReadWithBadValues(String uuidValue) {
79 UUID uuid = null;
80 boolean pass = false;
81 try {
82 uuid = (UUID) converter.convertToRead(uuidValue);
83 } catch (CsvDataTypeMismatchException exception) {
84 pass = true;
85 } catch (Throwable t) {
86 fail(String.format("Expected a CsvDataTypeMismatch exception when converting %s but a %s was thrown with a message of %s", uuidValue, t.getClass(), t.getMessage()));
87 }
88
89 if (!pass) {
90 fail(String.format("Expected a CsvDataTypeMismatch exception when converting %s but got a UUID with a value of %s", uuidValue, Objects.toString(uuid, "null")));
91 }
92 }
93
94 @DisplayName("convertToRead handles null and empty string values")
95 @ParameterizedTest
96 @NullAndEmptySource
97 @ValueSource(strings = {" ", "\t", "\n"})
98 public void convertToReadWithNullEmptyOrWhiteSpace(String uuidValue) throws CsvDataTypeMismatchException {
99 assertNull(converter.convertToRead(uuidValue));
100 }
101 }