| 1 |
package ezvcard; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertEquals; |
| 4 |
import static org.junit.Assert.assertNull; |
| 5 |
import static org.junit.Assert.fail; |
| 6 |
|
| 7 |
import java.io.BufferedReader; |
| 8 |
import java.io.IOException; |
| 9 |
import java.io.InputStream; |
| 10 |
import java.io.InputStreamReader; |
| 11 |
import java.util.HashSet; |
| 12 |
import java.util.Properties; |
| 13 |
import java.util.Set; |
| 14 |
|
| 15 |
import org.junit.Test; |
| 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 |
public class MessagesTest { |
| 46 |
private final Messages messages = Messages.INSTANCE; |
| 47 |
|
| 48 |
@Test |
| 49 |
public void getValidationWarning() { |
| 50 |
String actual = messages.getValidationWarning(0); |
| 51 |
String expected = messages.getMessage("validate.0"); |
| 52 |
assertEquals(expected, actual); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void getValidationWarning_does_not_exist() { |
| 57 |
assertNull(messages.getValidationWarning(5000)); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void getParseMessage() { |
| 62 |
String actual = messages.getParseMessage(0); |
| 63 |
String expected = messages.getMessage("parse.0"); |
| 64 |
assertEquals(expected, actual); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void getParseMessage_does_not_exist() { |
| 69 |
assertNull(messages.getParseMessage(5000)); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void getExceptionMessage() { |
| 74 |
String actual = messages.getExceptionMessage(1); |
| 75 |
String expected = messages.getMessage("exception.0", 1, messages.getMessage("exception.1")); |
| 76 |
assertEquals(expected, actual); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void getExceptionMessage_does_not_exist() { |
| 81 |
assertNull(messages.getExceptionMessage(5000)); |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void getIllegalArgumentException() { |
| 86 |
String actual = messages.getIllegalArgumentException(1).getMessage(); |
| 87 |
String expected = messages.getMessage("exception.0", 1, messages.getMessage("exception.1")); |
| 88 |
assertEquals(expected, actual); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void getIllegalArgumentException_does_not_exist() { |
| 93 |
assertNull(messages.getIllegalArgumentException(5000)); |
| 94 |
} |
| 95 |
|
| 96 |
@Test |
| 97 |
public void getMessage() throws Exception { |
| 98 |
String expected = load().getProperty("parse.0"); |
| 99 |
String actual = messages.getMessage("parse.0"); |
| 100 |
assertEquals(expected, actual); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void getMessage_does_not_exist() { |
| 105 |
assertNull(messages.getMessage("does-not-exist")); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void duplicate_keys() throws Exception { |
| 110 |
Set<String> keys = new HashSet<>(); |
| 111 |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream()))) { |
| 112 |
String line; |
| 113 |
while ((line = reader.readLine()) != null) { |
| 114 |
line = line.trim(); |
| 115 |
if (line.isEmpty() || line.charAt(0) == '#') { |
| 116 |
continue; |
| 117 |
} |
| 118 |
|
| 119 |
int pos = line.indexOf('='); |
| 120 |
if (pos < 0) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
String key = line.substring(0, pos); |
| 125 |
if (!keys.add(key)) { |
| 126 |
fail("Key \"" + key + "\" appears more than once."); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
private Properties load() throws IOException { |
| 133 |
Properties properties = new Properties(); |
| 134 |
try (InputStream in = inputStream()) { |
| 135 |
properties.load(in); |
| 136 |
} |
| 137 |
return properties; |
| 138 |
} |
| 139 |
|
| 140 |
private InputStream inputStream() { |
| 141 |
return getClass().getResourceAsStream("/ezvcard/messages.properties"); |
| 142 |
} |
| 143 |
} |