1 /*
2 * *************************************************************************************************************************************************************
3 *
4 * SteelBlue: DCI User Interfaces
5 * http://tidalwave.it/projects/steelblue
6 *
7 * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8 *
9 * *************************************************************************************************************************************************************
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18 *
19 * *************************************************************************************************************************************************************
20 *
21 * git clone https://bitbucket.org/tidalwave/steelblue-src
22 * git clone https://github.com/tidalwave-it/steelblue-src
23 *
24 * *************************************************************************************************************************************************************
25 */
26 package it.tidalwave.ui.core.role;
27
28 import jakarta.annotation.Nonnull;
29 import java.util.Locale;
30 import java.util.function.Consumer;
31 import java.util.regex.Pattern;
32 import java.nio.file.Path;
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Test;
35 import static org.mockito.Mockito.*;
36 import static org.hamcrest.CoreMatchers.is;
37 import static org.hamcrest.MatcherAssert.assertThat;
38
39 /***************************************************************************************************************************************************************
40 *
41 * @author Fabrizio Giudici
42 *
43 **************************************************************************************************************************************************************/
44 public class DisplayableTest
45 {
46 @Test
47 public void test_of_String()
48 {
49 // when
50 final var underTest = Displayable.of("foo bar");
51 // then
52 assertThat(underTest.getDisplayName(), is("foo bar"));
53 assertThat(underTest.toString(), Pattern.matches("\\?\\?\\?@.*Displayable\\[\\]", underTest.toString()), is(true));
54 }
55
56 /**********************************************************************************************************************************************************/
57 @Test
58 public void test_of_String_with_toString_name()
59 {
60 // when
61 final var underTest = Displayable.of("foo bar", "[toString]");
62 // then
63 assertThat(underTest.getDisplayName(), is("foo bar"));
64 assertThat(Pattern.matches("\\[toString\\]@.*Displayable\\[\\]", underTest.toString()), is(true));
65 }
66
67 /**********************************************************************************************************************************************************/
68 @Test
69 public void test_of_supplier()
70 {
71 // when
72 final var underTest = Displayable.of(() -> "foo bar");
73 // then
74 assertThat(underTest.getDisplayName(), is("foo bar"));
75 // assertThat(underTest.toString(), is("???"));
76 }
77
78 /**********************************************************************************************************************************************************/
79 @Test
80 public void test_of_function()
81 {
82 // given
83 final var path = Path.of("foo bar");
84 // when
85 final var underTest = Displayable.of(Path::toString, path);
86 // then
87 assertThat(underTest.getDisplayName(), is("foo bar"));
88 // assertThat(underTest.toString(), is("???"));
89 }
90
91 /**********************************************************************************************************************************************************/
92 @Test @SuppressWarnings("unchecked")
93 public void test_renderTo()
94 {
95 // given
96 final var consumer = mock(Consumer.class);
97 final var underTest = Displayable.of("foo bar");
98 // when
99 underTest.renderTo(consumer);
100 // then
101 verify(consumer).accept(eq("foo bar"));
102 }
103
104 /**********************************************************************************************************************************************************/
105 @Test
106 public void test_render()
107 {
108 // given
109 final var underTest = Displayable.of("foo bar");
110 // when
111 final var result = underTest.render();
112 // then
113 assertThat(result, is("foo bar"));
114 }
115
116 /**********************************************************************************************************************************************************/
117 @Test(dataProvider = "i8n")
118 public void must_read_the_displayName_from_the_bundle (@Nonnull final Locale locale, @Nonnull final String expected)
119 {
120 // given
121 final var save = Locale.getDefault();
122 Locale.setDefault(locale);
123 // when
124 final var displayable = Displayable.fromBundle(DisplayableTest.class, "key");
125 // then
126 // assertThat(displayable.getLocales(), is(new TreeSet<>(List.of(Locale.ENGLISH, Locale.ITALIAN, Locale.FRENCH))));
127 // assertThat(displayable.getDisplayNames(), is(Map.of(Locale.ENGLISH, "English",
128 // Locale.ITALIAN, "Italian",
129 // Locale.FRENCH, "French")));
130 assertThat(displayable.getDisplayName(), is(expected));
131 Locale.setDefault(save);
132 // assertThat(displayable.getDisplayName(Locale.ITALIAN), is("Italian"));
133 // assertThat(displayable.getDisplayName(Locale.FRENCH), is("French"));
134 }
135
136 @DataProvider
137 private static Object[][] i8n()
138 {
139 return new Object[][]
140 {
141 { Locale.ENGLISH, "English"},
142 { Locale.ITALIAN, "Italian"},
143 { Locale.FRENCH, "French"}
144 };
145 }
146 }