2
* $Id: XLocalizeTest.java 3473 2009-08-27 13:17:10Z kleopatra $
4
* Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
package org.jdesktop.swingx;
24
import java.awt.Component;
25
import java.awt.GraphicsEnvironment;
26
import java.awt.event.ActionEvent;
27
import java.beans.PropertyChangeEvent;
28
import java.beans.PropertyChangeListener;
29
import java.util.Locale;
30
import java.util.logging.Logger;
32
import javax.swing.Action;
33
import javax.swing.JComponent;
35
import org.jdesktop.swingx.action.AbstractActionExt;
36
import org.jdesktop.swingx.plaf.LookAndFeelAddons;
37
import org.jdesktop.swingx.plaf.UIManagerExt;
38
import org.jdesktop.swingx.search.PatternModel;
39
import org.jdesktop.test.PropertyChangeReport;
40
import org.jdesktop.test.TestUtils;
41
import org.junit.Test;
42
import org.junit.runner.RunWith;
43
import org.junit.runners.JUnit4;
47
* Test to expose known issues around <code>Locale</code> setting.
49
* Ideally, there would be at least one failing test method per open
50
* Issue in the issue tracker. Plus additional failing test methods for
51
* not fully specified or not yet decided upon features/behaviour.
53
* @author Jeanette Winzenburg
55
@RunWith(JUnit4.class)
56
public class XLocalizeTest extends InteractiveTestCase {
57
@SuppressWarnings("all")
58
private static final Logger LOG = Logger.getLogger(XLocalizeTest.class
60
private static final Locale A_LOCALE = Locale.FRENCH;
61
private static final Locale OTHER_LOCALE = Locale.GERMAN;
64
private Locale originalLocale;
65
// test scope is static anyway...
67
// force the addon to load
68
LookAndFeelAddons.getAddon();
70
public static void main(String[] args) {
72
XLocalizeTest test = new XLocalizeTest();
74
test.runInteractiveTests();
75
// test.runInteractiveTests("interactive.*TwoTable.*");
76
} catch (Exception e) {
77
System.err.println("exception when executing interactive tests:");
84
protected void setUp() throws Exception {
85
originalLocale = Locale.getDefault();
92
protected void tearDown() throws Exception {
93
Locale.setDefault(originalLocale);
98
* test correct PropertyChangeNotification: must fire after
99
* all internal state is set ... dooohhh.
101
* Here: test JXDialog.
104
* test correct PropertyChangeNotification: must fire after
105
* all internal state is set ...
107
* Here: test FindPanel
110
public void testLocaleDialogPropertyNotificationInListener() {
111
// This test will not work in a headless configuration.
112
if (GraphicsEnvironment.isHeadless()) {
113
LOG.fine("cannot run localeDialogPropertyNotificationInListener - headless environment");
116
final String prefix = PatternModel.SEARCH_PREFIX;
117
final JXFindPanel findPanel = new JXFindPanel();
118
final JXDialog dialog = new JXDialog(findPanel);
119
final String titleKey = AbstractPatternPanel.SEARCH_TITLE;
120
// JW: arrrgghh ... dirty! Consequence of dirty initialization
121
// of AbstractPatternPanel subclasses ...
122
findPanel.addNotify();
123
String name = dialog.getTitle();
124
String uiValue = UIManagerExt.getString(prefix + titleKey, findPanel
127
assertNotNull(uiValue);
128
assertEquals(name, uiValue);
129
final Locale alternative = getAlternativeLocale(dialog);
130
PropertyChangeListener report = new PropertyChangeListener() {
132
public void propertyChange(PropertyChangeEvent evt) {
134
// wrong assumption: find widgets name is changed as well
135
// assertTrue("locale property changed, instead: " + evt.getPropertyName(), "locale".equals(evt.getPropertyName()));
136
if (!"locale".equals(evt.getPropertyName())) return;
137
String altUIValue = UIManagerExt.getString(prefix + titleKey,
139
String altName = dialog.getTitle();
140
assertEquals("name must be updated before fire propertyChange",
141
altUIValue, altName);
145
dialog.addPropertyChangeListener(report);
146
PropertyChangeReport r = new PropertyChangeReport();
147
dialog.addPropertyChangeListener(r);
148
dialog.setLocale(alternative);
149
// sanity: guarantee that we got a locale change notification
150
assertEquals(1, r.getEventCount("locale"));
155
* test correct PropertyChangeNotification: must fire after
156
* all internal state is set ... dooohhh.
158
* Here: test FindBar.
161
public void testLocaleFindBarPropertyNotificationInListener() {
162
final String prefix = PatternModel.SEARCH_PREFIX;
163
final JXFindBar findPanel = new JXFindBar();
164
final String actionCommand = JXFindBar.FIND_NEXT_ACTION_COMMAND;
165
// JW: arrrgghh ... dirty! Consequence of dirty initialization
166
// of AbstractPatternPanel subclasses ...
167
findPanel.addNotify();
169
final Action action = findPanel.getActionMap().get(actionCommand);
170
String name = (String) action.getValue(Action.NAME);
171
String uiValue = UIManagerExt.getString(prefix + actionCommand, findPanel
174
assertNotNull(uiValue);
175
assertEquals(name, uiValue);
176
final Locale alternative = getAlternativeLocale(findPanel);
177
PropertyChangeListener report = new PropertyChangeListener() {
179
public void propertyChange(PropertyChangeEvent evt) {
181
// assertTrue("locale property changed", "locale".equals(evt.getPropertyName()));
182
if (!"locale".equals(evt.getPropertyName())) return;
183
String altUIValue = UIManagerExt.getString(prefix + actionCommand,
185
String altName = (String) action.getValue(Action.NAME);
186
assertEquals("name must be updated before fire propertyChange",
187
altUIValue, altName);
191
PropertyChangeReport r = new PropertyChangeReport();
192
findPanel.addPropertyChangeListener(r);
193
findPanel.setLocale(alternative);
194
assertEquals(1, r.getEventCount("locale"));
199
* test correct PropertyChangeNotification: must fire after
200
* all internal state is set ...
202
* Here: test FindPanel
205
public void testLocaleFindPanelPropertyNotificationInListener() {
206
final String prefix = PatternModel.SEARCH_PREFIX;
207
final JXFindPanel findPanel = new JXFindPanel();
208
final String actionCommand = AbstractPatternPanel.MATCH_ACTION_COMMAND;
209
// JW: arrrgghh ... dirty! Consequence of dirty initialization
210
// of AbstractPatternPanel subclasses ...
211
findPanel.addNotify();
213
final Action action = findPanel.getActionMap().get(actionCommand);
214
String name = (String) action.getValue(Action.NAME);
215
String uiValue = UIManagerExt.getString(prefix + actionCommand, findPanel
218
assertNotNull(uiValue);
219
assertEquals(name, uiValue);
220
final Locale alternative = getAlternativeLocale(findPanel);
221
PropertyChangeListener report = new PropertyChangeListener() {
223
public void propertyChange(PropertyChangeEvent evt) {
225
// wrong assumption: find widgets name is changed as well
226
// assertTrue("locale property changed, instead: " + evt.getPropertyName(), "locale".equals(evt.getPropertyName()));
227
if (!"locale".equals(evt.getPropertyName())) return;
228
String altUIValue = UIManagerExt.getString(prefix + actionCommand,
230
String altName = (String) action.getValue(Action.NAME);
231
assertEquals("name must be updated before fire propertyChange",
232
altUIValue, altName);
236
findPanel.addPropertyChangeListener(report);
237
PropertyChangeReport r = new PropertyChangeReport();
238
findPanel.addPropertyChangeListener(r);
239
findPanel.setLocale(alternative);
240
assertEquals(1, r.getEventCount("locale"));
244
* test correct PropertyChangeNotification: must fire after
245
* all internal state is set ... dooohhh.
248
public void testLocaleTablePropertyNotification() {
249
String prefix = "JXTable.";
250
JXTable table = new JXTable(10, 2);
251
String actionCommand = JXTable.HORIZONTALSCROLL_ACTION_COMMAND;
253
Action action = table.getActionMap().get(actionCommand);
254
String name = (String) action.getValue(Action.NAME);
255
String uiValue = UIManagerExt.getString(prefix + actionCommand, table
258
assertNotNull(uiValue);
259
assertEquals(name, uiValue);
260
Locale old = table.getLocale();
261
Locale alternative = getAlternativeLocale(table);
262
PropertyChangeReport report = new PropertyChangeReport();
263
table.addPropertyChangeListener(report);
264
table.setLocale(alternative);
265
TestUtils.assertPropertyChangeEvent(report, "locale", old, alternative);
269
* test correct PropertyChangeNotification: must fire after
270
* all internal state is set ...
272
* Here: test JXTable.
275
public void testLocaleTablePropertyNotificationInListener() {
276
final String prefix = "JXTable.";
277
final JXTable table = new JXTable(10, 2);
278
final String actionCommand = JXTable.HORIZONTALSCROLL_ACTION_COMMAND;
280
final Action action = table.getActionMap().get(actionCommand);
281
String name = (String) action.getValue(Action.NAME);
282
String uiValue = UIManagerExt.getString(prefix + actionCommand, table
285
assertNotNull(uiValue);
286
assertEquals(name, uiValue);
287
final Locale alternative = getAlternativeLocale(table);
288
PropertyChangeListener report = new PropertyChangeListener() {
290
public void propertyChange(PropertyChangeEvent evt) {
292
assertTrue("locale property changed", "locale".equals(evt.getPropertyName()));
293
String altUIValue = UIManagerExt.getString(prefix + actionCommand,
295
String altName = (String) action.getValue(Action.NAME);
296
assertEquals("name must be updated before fire propertyChange",
297
altUIValue, altName);
301
table.addPropertyChangeListener(report);
302
table.setLocale(alternative);
305
private Locale getAlternativeLocale(final Component table) {
306
Locale alternative = OTHER_LOCALE;
307
if (alternative.getLanguage().equals(table.getLocale().getLanguage())) {
308
alternative = A_LOCALE;
315
* Issue #635-swingx: find widgets must support dynamic localization
316
* Here: test findPanel's actions (incomplete ..)
319
public void testLocaleFindPanel() {
320
JXFindPanel panel = new JXFindPanel();
321
// JW: arrrgghh ... dirty! Consequence of dirty initialization
322
// of AbstractPatternPanel subclasses ...
324
String prefix = PatternModel.SEARCH_PREFIX;
325
assertLocaleActionUpdate(panel, prefix, AbstractPatternPanel.MATCH_ACTION_COMMAND);
326
assertLocaleActionUpdate(panel, prefix, JXFindPanel.FIND_NEXT_ACTION_COMMAND);
327
assertLocaleActionUpdate(panel, prefix, JXFindPanel.FIND_PREVIOUS_ACTION_COMMAND);
331
* Issue #459-swingx: JXTable setLocale doesn't update localized column
332
* control properties. <p>
334
* Pass/fail expectation:
336
* <li> fails always with jdk5 independent of LookAndFeelAddon resource
337
* bundle registration.
338
* <li> fails with jdk6 and LookAndFeelAddon copy resource bundle values.
339
* <li> passes with jdk6 and LookAndFeelAddon addResourceBundle.
343
public void testLocaleColumnControl() {
344
String prefix = "JXTable.";
345
JXTable table = new JXTable(10, 2);
346
assertLocaleActionUpdate(table, prefix, JXTable.HORIZONTALSCROLL_ACTION_COMMAND);
347
assertLocaleActionUpdate(table, prefix, JXTable.PACKALL_ACTION_COMMAND);
348
assertLocaleActionUpdate(table, prefix, JXTable.PACKSELECTED_ACTION_COMMAND);
351
private void assertLocaleActionUpdate(JComponent table, String prefix, String actionCommand) {
352
Action action = table.getActionMap().get(actionCommand);
353
String name = (String) action.getValue(Action.NAME);
354
String uiValue = UIManagerExt.getString(prefix + actionCommand, table
357
assertNotNull(uiValue);
358
assertEquals(name, uiValue);
359
Locale alternative = OTHER_LOCALE;
360
if (alternative.getLanguage().equals(table.getLocale().getLanguage())) {
361
alternative = A_LOCALE;
363
table.setLocale(alternative);
364
String altUIValue = UIManagerExt.getString(prefix + actionCommand,
367
assertNotNull(altUIValue);
368
// sanity to track unexpected failure during refactoring
369
assertFalse("new uiValue must be different: " + uiValue + "/"
370
+ altUIValue, uiValue.equals(altUIValue));
371
String altName = (String) action.getValue(Action.NAME);
372
// here are the real asserts
373
assertFalse("new action name must be different: " + name + "/"
374
+ altName, name.equals(altName));
375
assertEquals(altName, altUIValue);
379
* Issue #466-swingx: locale-dependent values not accessible. This looks
380
* like a side-effect of the first go on #159-swingx (not all values of
381
* resourceBundle available).
384
* Could be core problem (in jdk5, fixed in jdk6) around classloader and
386
* href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4834404">
387
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4834404 </a>
392
public void testGetLocaleUIDefaults() {
393
String key = "JXTable.column.packAll";
394
Object alternativeValue = UIManagerExt.getString(key, OTHER_LOCALE);
395
// sanity - the value must be available
396
assertNotNull(alternativeValue);
397
Object defaultValue = UIManagerExt.getString(key, A_LOCALE);
398
// sanity - the value must be available
399
assertNotNull(defaultValue);
400
assertFalse("values must be different: " + defaultValue + "/" + alternativeValue, defaultValue.equals(alternativeValue));
404
* Issue #459-swingx: columnControl properties not updated on locale setting.
407
public void interactiveLocaleColumnControl() {
408
final JXTable table = new JXTable(10, 4);
409
table.setColumnControlVisible(true);
410
table.getColumnExt(0).setTitle(table.getLocale().getLanguage());
411
Action toggleLocale = new AbstractActionExt("toggleLocale") {
413
public void actionPerformed(ActionEvent e) {
414
Locale old = table.getLocale();
415
table.setLocale(old == A_LOCALE ? OTHER_LOCALE : A_LOCALE);
416
table.getColumnExt(0).setTitle(table.getLocale().getLanguage());
421
JXFrame frame = wrapWithScrollingInFrame(table, "toggle locale on table - column control not updated");
422
addAction(frame, toggleLocale);
423
frame.setVisible(true);
427
* Issue #459-swingx: columnControl properties not updated on locale setting.
431
public void interactiveLocaleColumnControlTwoTables() {
432
final JXTable table = new JXTable(10, 4);
433
table.setColumnControlVisible(true);
434
table.getColumnExt(0).setTitle(table.getLocale().getLanguage());
435
JXTable other = new JXTable(10, 4);
436
other.setColumnControlVisible(true);
437
other.setLocale(A_LOCALE);
438
other.getColumnExt(0).setTitle(other.getLocale().getLanguage());
439
JXFrame frame = wrapWithScrollingInFrame(table, other, "different locals: de <--> vs fr");
440
addSearchModeToggle(frame);
441
addMessage(frame, "Find panel/bar should be localized per-table");
443
frame.setVisible(true);