This project has retired. For details please refer to its Attic page.
InternationalStringTest xref
View Javadoc
1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.ws.scout.registry.infomodel;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Locale;
22  
23  import javax.xml.registry.JAXRException;
24  import javax.xml.registry.infomodel.InternationalString;
25  import javax.xml.registry.infomodel.LocalizedString;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * @version $Revision$ $Date$
31   */
32  public class InternationalStringTest extends TestCase {
33      private Locale defaultLocale;
34      private LocalizedString defaultString;
35      private LocalizedString usString;
36      private LocalizedString ukString;
37      private LocalizedString caString;
38  
39      public void testAddLocalizedString() throws JAXRException {
40          InternationalString is = new InternationalStringImpl(Locale.US, "USA", LocalizedString.DEFAULT_CHARSET_NAME);
41          Collection localizedStrings = is.getLocalizedStrings();
42          assertEquals(1, localizedStrings.size());
43          assertTrue(localizedStrings.contains(usString));
44          is.addLocalizedString(ukString);
45          localizedStrings = is.getLocalizedStrings();
46          assertEquals(2, localizedStrings.size());
47          assertTrue(localizedStrings.contains(usString));
48          assertTrue(localizedStrings.contains(ukString));
49  
50          // add again and see that we overwrite
51          is.addLocalizedString(ukString);
52          localizedStrings = is.getLocalizedStrings();
53          assertEquals(2, localizedStrings.size());
54          assertTrue(localizedStrings.contains(usString));
55          assertTrue(localizedStrings.contains(ukString));
56      }
57  
58      public void testAddLocalizedStrings() throws JAXRException {
59          InternationalString is = new InternationalStringImpl(Locale.US, "USA", LocalizedString.DEFAULT_CHARSET_NAME);
60          Collection localizedStrings = is.getLocalizedStrings();
61          assertEquals(1, localizedStrings.size());
62          assertTrue(localizedStrings.contains(usString));
63          ArrayList<LocalizedString> strings = new ArrayList<LocalizedString>(2);
64          strings.add(ukString);
65          strings.add(caString);
66          is.addLocalizedStrings(strings);
67          localizedStrings = is.getLocalizedStrings();
68          assertEquals(3, localizedStrings.size());
69          assertTrue(localizedStrings.contains(usString));
70          assertTrue(localizedStrings.contains(caString));
71          assertTrue(localizedStrings.contains(ukString));
72      }
73  
74      public void testGetValue() throws JAXRException {
75          InternationalString is = new InternationalStringImpl();
76          is.addLocalizedString(usString);
77          is.addLocalizedString(defaultString);
78          is.addLocalizedString(ukString);
79          assertEquals("default", is.getValue());
80      }
81  
82      public void testSetValue() throws JAXRException {
83          InternationalString is = new InternationalStringImpl();
84          is.addLocalizedString(defaultString);
85          is.setValue("foo");
86          assertEquals("foo", is.getValue());
87      }
88  
89      public void testGetValueWithLocale() throws JAXRException {
90          InternationalString is = new InternationalStringImpl();
91          is.addLocalizedString(usString);
92          is.addLocalizedString(ukString);
93          assertEquals("USA", is.getValue(Locale.US));
94      }
95  
96      public void testGetValueWithUndefinedLocale() throws JAXRException {
97          InternationalString is = new InternationalStringImpl();
98          is.addLocalizedString(usString);
99          assertNull(is.getValue(Locale.UK));
100     }
101 
102     public void testSetValueWithLocale() throws JAXRException {
103         InternationalString is = new InternationalStringImpl();
104         is.addLocalizedString(usString);
105         is.addLocalizedString(ukString);
106         is.setValue(Locale.US, "foo");
107         assertEquals("foo", is.getValue(Locale.US));
108         assertEquals("England", is.getValue(Locale.UK));
109         assertEquals(2, is.getLocalizedStrings().size());
110     }
111 
112     public void testRemoveLocalizedString() throws JAXRException {
113         InternationalString is = new InternationalStringImpl();
114         is.addLocalizedString(usString);
115         is.addLocalizedString(ukString);
116         is.addLocalizedString(caString);
117         assertEquals(3, is.getLocalizedStrings().size());
118         is.removeLocalizedString(ukString);
119         Collection localizedStrings = is.getLocalizedStrings();
120         assertEquals(2, localizedStrings.size());
121         assertTrue(localizedStrings.contains(usString));
122         assertFalse(localizedStrings.contains(ukString));
123         assertTrue(localizedStrings.contains(caString));
124     }
125 
126     public void testRemoveLocalizedStrings() throws JAXRException {
127         InternationalString is = new InternationalStringImpl();
128         is.addLocalizedString(usString);
129         is.addLocalizedString(ukString);
130         is.addLocalizedString(caString);
131         assertEquals(3, is.getLocalizedStrings().size());
132         Collection<LocalizedString> localizedStrings = new ArrayList<LocalizedString>();
133         localizedStrings.add(usString);
134         localizedStrings.add(caString);
135         is.removeLocalizedStrings(localizedStrings);
136         localizedStrings = is.getLocalizedStrings();
137         assertEquals(1, localizedStrings.size());
138         assertFalse(localizedStrings.contains(usString));
139         assertTrue(localizedStrings.contains(ukString));
140         assertFalse(localizedStrings.contains(caString));
141     }
142 
143     public void testGetLocalizedString() throws JAXRException {
144         InternationalString is = new InternationalStringImpl();
145         is.addLocalizedString(usString);
146         assertEquals(usString, is.getLocalizedString(Locale.US, LocalizedString.DEFAULT_CHARSET_NAME));
147         assertNull(is.getLocalizedString(Locale.US, "US-ASCII"));
148     }
149 
150     protected void setUp() throws Exception {
151         super.setUp();
152         defaultLocale = Locale.getDefault();
153         defaultString = new LocalizedStringImpl(defaultLocale, "default", LocalizedString.DEFAULT_CHARSET_NAME);
154         usString = new LocalizedStringImpl(Locale.US, "USA", LocalizedString.DEFAULT_CHARSET_NAME);
155         ukString = new LocalizedStringImpl(Locale.UK, "England", LocalizedString.DEFAULT_CHARSET_NAME);
156         caString = new LocalizedStringImpl(Locale.CANADA, "Canada", LocalizedString.DEFAULT_CHARSET_NAME);
157     }
158 }