View Javadoc

1   /*
2    * Copyright (c) 2009 QOS.ch All rights reserved.
3    * 
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   * 
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   * 
14   * THE SOFTWARE IS PROVIDED "AS  IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package ch.qos.cal10n;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.fail;
27  
28  import java.util.Locale;
29  
30  import ch.qos.cal10n.sample.Labels;
31  import org.junit.Test;
32  
33  import ch.qos.cal10n.sample.Colors;
34  import ch.qos.cal10n.sample.Minimal;
35  import ch.qos.cal10n.sample.Host.OtherColors;
36  
37  public class MessageConveyorTest {
38  
39    @Test
40    public void smoke_EN() {
41      MessageConveyor rbbmc = new MessageConveyor(Locale.UK);
42      String val;
43  
44      val = rbbmc.getMessage(Colors.BLUE);
45      assertEquals("violets are blue", val);
46  
47      val = rbbmc.getMessage(Colors.GREEN, "apples");
48      assertEquals("apples are green", val);
49    }
50  
51    // see http://jira.qos.ch/browse/CAL-1
52    @Test
53    public void nestedEnum_EN() {
54      MessageConveyor rbbmc = new MessageConveyor(Locale.UK);
55      String val;
56  
57      val = rbbmc.getMessage(Colors.RED);
58      assertEquals("roses are red", val);
59  
60      val = rbbmc.getMessage(OtherColors.RED);
61      assertEquals("roses are red", val);
62  
63      val = rbbmc.getMessage(OtherColors.BLUE);
64      assertEquals("violets are blue", val);
65    }
66  
67    @Test
68    public void smoke_FR() {
69      MessageConveyor rbbmc = new MessageConveyor(Locale.FRANCE);
70      String val;
71  
72      val = rbbmc.getMessage(Colors.BLUE);
73      assertEquals("les violettes sont bleues", val);
74  
75      // lemon=citron in french. This illustrates the problem of
76      // translating the parameters of a message
77      val = rbbmc.getMessage(Colors.GREEN, "pommes");
78      assertEquals("les pommes sont verts", val);
79    }
80  
81    @Test
82    public void mpo() {
83      MessageConveyor rbbmc = new MessageConveyor(Locale.UK);
84      MessageParameterObj mpo;
85      String val;
86  
87      mpo = new MessageParameterObj(Colors.BLUE);
88      val = rbbmc.getMessage(mpo);
89      assertEquals("violets are blue", val);
90  
91      mpo = new MessageParameterObj(Colors.GREEN, "apples");
92      val = rbbmc.getMessage(mpo);
93      assertEquals("apples are green", val);
94    }
95  
96    @Test
97    public void failedRBLookup() {
98  
99      MessageConveyor mc = new MessageConveyor(Locale.CHINA);
100     try {
101       mc.getMessage(Colors.BLUE);
102       fail("missing exception");
103     } catch (MessageConveyorException e) {
104       assertEquals(
105           "Failed to locate resource bundle [colors] for locale [zh_CN] for enum type [ch.qos.cal10n.sample.Colors]",
106           e.getMessage());
107     }
108   }
109   
110   @Test
111   public void minimal() {
112     MessageConveyor mc = new MessageConveyor(Locale.ENGLISH);
113     assertEquals("A", mc.getMessage(Minimal.A));
114   }
115 
116 
117   @Test
118   public void specialCharacters() {
119     MessageConveyor mc = new MessageConveyor(Locale.ENGLISH);
120     assertEquals("A label \n with linefeed and unicode", mc.getMessage(Labels.L0));
121     assertEquals("Another \nlabel", mc.getMessage(Labels.L1));
122   }
123 
124 }