| 1 |
package org.astrogrid.samp.web; |
| 2 |
|
| 3 |
import java.lang.reflect.Method; |
| 4 |
import java.util.HashMap; |
| 5 |
import java.util.HashSet; |
| 6 |
import java.util.Iterator; |
| 7 |
import java.util.Locale; |
| 8 |
import java.util.Map; |
| 9 |
import java.util.ResourceBundle; |
| 10 |
import java.util.Set; |
| 11 |
import junit.framework.TestCase; |
| 12 |
|
| 13 |
public class I18nTest extends TestCase { |
| 14 |
|
| 15 |
private static final Locale[] locales_ = new Locale[] { |
| 16 |
Locale.ENGLISH, |
| 17 |
Locale.ITALIAN, |
| 18 |
Locale.GERMAN, |
| 19 |
Locale.FRENCH, |
| 20 |
}; |
| 21 |
private static final String[] suffixes_ = new String[] { |
| 22 |
"_en", "_it", "_de", "_fr", |
| 23 |
}; |
| 24 |
private static final Method[] contentMethods_ = |
| 25 |
AuthResourceBundle.getContentMethods(); |
| 26 |
|
| 27 |
public void testTools() throws Exception { |
| 28 |
assertEquals( toMap( getContent( Locale.ENGLISH ) ), |
| 29 |
toMap( getContent( Locale.UK ) ) ); |
| 30 |
} |
| 31 |
|
| 32 |
public void testAuthResourceBundle() throws Exception { |
| 33 |
Set mapSet = new HashSet(); |
| 34 |
for ( int il = 0; il < locales_.length; il++ ) { |
| 35 |
Locale locale = locales_[ il ]; |
| 36 |
ResourceBundle bundle = |
| 37 |
ResourceBundle.getBundle( AuthResourceBundle.class.getName(), |
| 38 |
locale ); |
| 39 |
AuthResourceBundle.checkHasAllKeys( bundle ); |
| 40 |
AuthResourceBundle.Content content = |
| 41 |
AuthResourceBundle.getAuthContent( bundle ); |
| 42 |
Map map = toMap( content ); |
| 43 |
for ( Iterator it = map.entrySet().iterator(); it.hasNext(); ) { |
| 44 |
Map.Entry entry = (Map.Entry) it.next(); |
| 45 |
String value = (String) entry.getValue(); |
| 46 |
assertNotNull( value ); |
| 47 |
assertTrue( value.length() > 0 ); |
| 48 |
} |
| 49 |
assertTrue( "Reuse content for " + locale, |
| 50 |
! mapSet.contains( map ) ); |
| 51 |
mapSet.add( map ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* This one does something different - by picking the resources by |
| 57 |
* locale-specific name, it avoids getting a parent chain. |
| 58 |
* In this way it can check if any resources are missing |
| 59 |
* (if you do it the more obvious way, missing ones are filled in by |
| 60 |
* the parent, which we don't want for testing purposes). |
| 61 |
*/ |
| 62 |
public void testAuthSuffixes() { |
| 63 |
for ( int is = 0; is < suffixes_.length; is++ ) { |
| 64 |
ResourceBundle bundle = |
| 65 |
ResourceBundle.getBundle( AuthResourceBundle.class.getName() |
| 66 |
+ suffixes_[ is ], |
| 67 |
Locale.US ); |
| 68 |
AuthResourceBundle.checkHasAllKeys( bundle ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
private static AuthResourceBundle.Content getContent( Locale locale ) { |
| 73 |
ResourceBundle bundle = |
| 74 |
ResourceBundle.getBundle( AuthResourceBundle.class.getName(), |
| 75 |
locale ); |
| 76 |
return AuthResourceBundle.getAuthContent( bundle ); |
| 77 |
} |
| 78 |
|
| 79 |
private static Map toMap( AuthResourceBundle.Content content ) |
| 80 |
throws Exception { |
| 81 |
Map map = new HashMap(); |
| 82 |
for ( int im = 0; im < contentMethods_.length; im++ ) { |
| 83 |
Method method = contentMethods_[ im ]; |
| 84 |
map.put( method.getName(), |
| 85 |
(String) method.invoke( content, new Object[ 0 ] ) ); |
| 86 |
} |
| 87 |
return map; |
| 88 |
} |
| 89 |
} |