Debian Codegraph @

src/test/java/ezvcard/parameter/MediaTypeCaseClassesTest.java

raw symbols SCIP
1 package ezvcard.parameter;
2
3 import static org.junit.Assert.assertSame;
4
5 import org.junit.Before;
6 import org.junit.Test;
7
8 /*
9 Copyright (c) 2012-2023, Michael Angstadt
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
15 1. Redistributions of source code must retain the above copyright notice, this
16 list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright notice,
18 this list of conditions and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /**
34 * @author Michael Angstadt
35 */
36 public class MediaTypeCaseClassesTest {
37 private MediaTypeCaseClasses<TextMediaType> caseClasses;
38
39 @Before
40 public void before() {
41 caseClasses = new MediaTypeCaseClasses<>(TextMediaType.class);
42 }
43
44 @Test
45 public void get() {
46 assertSame(TextMediaType.XML, caseClasses.get(new String[] { "xml", null, null }));
47
48 TextMediaType rtf = caseClasses.get(new String[] { "rtf", "text/rtf", "rtf" });
49 assertSame(rtf, caseClasses.get(new String[] { "rtf", "text/rtf", "rtf" }));
50 assertSame(rtf, caseClasses.get(new String[] { "rtf", null, null }));
51 assertSame(rtf, caseClasses.get(new String[] { null, "text/rtf", null }));
52 assertSame(rtf, caseClasses.get(new String[] { null, null, "rtf" }));
53 }
54
55 @Test
56 public void find() {
57 //find() does not include runtime-created objects
58 caseClasses.get(new String[] { "rtf", "text/rtf", "rtf" });
59
60 assertFind("XmL", null, null, TextMediaType.XML);
61 assertFind("rtf", null, null, null); //not public
62 assertFind(null, "TEXT/xml", null, TextMediaType.XML);
63 assertFind(null, "foo/bar", null, null);
64 assertFind(null, null, "xML", TextMediaType.XML);
65 assertFind(null, null, "foo", null);
66 assertFind("xml", "TEXT/xml", null, TextMediaType.XML);
67 assertFind("xml", "foo/bar", null, null);
68 assertFind("xml", null, "xML", TextMediaType.XML);
69 assertFind("xml", null, "foo", null);
70 assertFind(null, "text/xml", "xML", TextMediaType.XML);
71 assertFind(null, "text/xml", "foo", null);
72 assertFind("xml", "text/xml", "xML", TextMediaType.XML);
73 assertFind("xml", "text/xml", "foo", null);
74 }
75
76 private void assertFind(String type, String mediaType, String extension, TextMediaType expected) {
77 TextMediaType actual = caseClasses.find(new String[] { type, mediaType, extension });
78 assertSame("Expected = " + expected + ", Actual = " + actual, expected, actual);
79 }
80
81 public static class TextMediaType extends MediaTypeParameter {
82 public static final TextMediaType PLAIN = new TextMediaType("plain", "text/plain", "txt");
83 public static final TextMediaType XML = new TextMediaType("xml", "text/xml", "xml");
84 public static final TextMediaType HTML = new TextMediaType("html", "text/html", "html");
85
86 private TextMediaType(String value, String mediaType, String extension) {
87 super(value, mediaType, extension);
88 }
89 }
90 }