Debian Codegraph @

src/test/java/ezvcard/MessagesTest.java

raw symbols SCIP
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 Copyright (c) 2012-2023, Michael Angstadt
19 All rights reserved.
20
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are met:
23
24 1. Redistributions of source code must retain the above copyright notice, this
25 list of conditions and the following disclaimer.
26 2. Redistributions in binary form must reproduce the above copyright notice,
27 this list of conditions and the following disclaimer in the documentation
28 and/or other materials provided with the distribution.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
34 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /**
43 * @author Michael Angstadt
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 }