Repository navigation
Expand file tree
/
Copy pathGetterLocale309Test.java
More file actions
147 lines (123 loc) · 4.6 KB
/
Copy pathGetterLocale309Test.java
File metadata and controls
147 lines (123 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.github.mustachejava.reflection;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.reflect.SimpleObjectHandler;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class GetterLocale309Test {
@Parameterized.Parameters(name = "locale={0}, simple={1}")
public static Collection<Object[]> locales() {
return Arrays.asList(new Object[][] {
{Locale.ROOT, false}, {Locale.ENGLISH, false}, {new Locale("tr", "TR"), false}, {new Locale("az", "AZ"), false},
{Locale.ROOT, true}, {Locale.ENGLISH, true}, {new Locale("tr", "TR"), true}, {new Locale("az", "AZ"), true}
});
}
private final Locale locale;
private final boolean simple;
private Locale previous;
private Locale previousDisplay;
private Locale previousFormat;
public GetterLocale309Test(Locale locale, boolean simple) {
this.locale = locale;
this.simple = simple;
}
@Before
public void setLocale() {
previous = Locale.getDefault();
previousDisplay = Locale.getDefault(Locale.Category.DISPLAY);
previousFormat = Locale.getDefault(Locale.Category.FORMAT);
Locale.setDefault(locale);
}
@After
public void restoreLocale() {
Locale.setDefault(previous);
Locale.setDefault(Locale.Category.DISPLAY, previousDisplay);
Locale.setDefault(Locale.Category.FORMAT, previousFormat);
}
private String render(String template, Object scope) {
Mustache mustache = factory().compile(new StringReader(template), "getter-locale");
return execute(mustache, scope);
}
private DefaultMustacheFactory factory() {
DefaultMustacheFactory factory = new DefaultMustacheFactory();
if (simple) factory.setObjectHandler(new SimpleObjectHandler());
return factory;
}
private String execute(Mustache mustache, Object scope) {
StringWriter out = new StringWriter();
mustache.execute(out, scope);
return out.toString();
}
public static class Bean {
public String getIngredients() { return "cheese"; }
public String getSauces() { return "tomato"; }
public boolean isIndexed() { return true; }
public boolean isIdle() { return false; }
}
public static class InheritedBean extends Bean {
}
public interface Ingredients {
String getIngredients();
}
private static class InterfaceBean implements Ingredients {
@Override
public String getIngredients() { return "interface"; }
}
public static class PriorityBean {
public String ingredients = "field";
public String getIngredients() { return "getter"; }
public String sauces() { return "direct"; }
public String getSauces() { return "getter"; }
public String onlyField = "fallback";
}
public static class DifferentBean {
public String getIngredients() { return "different"; }
}
public static class UnicodeBean {
public String getSSName() { return "unicode"; }
}
@Test
public void getterNamesDoNotDependOnLocale() {
assertEquals("cheese/tomato", render("{{ingredients}}/{{sauces}}", new Bean()));
}
@Test
public void booleanGetterNamesDoNotDependOnLocale() {
assertEquals("true/false", render("{{#indexed}}true{{/indexed}}{{^idle}}/false{{/idle}}", new Bean()));
}
@Test
public void inheritedGetterNamesDoNotDependOnLocale() {
assertEquals("cheese", render("{{ingredients}}", new InheritedBean()));
}
@Test
public void interfaceGetterNamesDoNotDependOnLocale() {
assertEquals("interface", render("{{ingredients}}", new InterfaceBean()));
}
@Test
public void memberLookupPriorityIsPreserved() {
assertEquals("getter/direct/fallback", render("{{ingredients}}/{{sauces}}/{{onlyField}}", new PriorityBean()));
}
@Test
public void cachedTemplateResolvesNewClassesAfterLocaleChanges() {
Mustache mustache = factory().compile(new StringReader("{{ingredients}}"), "cached-getter-locale");
Locale.setDefault(Locale.ENGLISH);
assertEquals("cheese", execute(mustache, new Bean()));
Locale.setDefault(new Locale("tr", "TR"));
assertEquals("different", execute(mustache, new DifferentBean()));
Locale.setDefault(new Locale("az", "AZ"));
assertEquals("cheese", execute(mustache, new Bean()));
}
@Test
public void multiCharacterUppercaseMappingIsPreserved() {
assertEquals("unicode", render("{{ßName}}", new UnicodeBean()));
}
}