View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package net.sourceforge.jwebunit.tests;
20  
21  import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt;
22  import static net.sourceforge.jwebunit.junit.JWebUnit.getMessage;
23  import static net.sourceforge.jwebunit.junit.JWebUnit.getTestContext;
24  import static net.sourceforge.jwebunit.junit.JWebUnit.getTester;
25  import static net.sourceforge.jwebunit.junit.JWebUnit.setBaseUrl;
26  import static org.junit.Assert.assertEquals;
27  
28  import java.text.NumberFormat;
29  import java.util.Locale;
30  
31  import org.junit.Test;
32  
33  /**
34   * Tests for MessageBundle related assertions
35   * @author Chris Eldredge
36   */
37  public class ResourceBundleAssertionsTest extends JWebUnitAPITestCase {
38  
39      static final String resourceBundleName = "MessageBundle";
40  
41      static final String pageTitle = "Keyed Message Title - Main Page";
42  
43      static final String text = "This is a test page.";
44  
45      public void setUp() throws Exception {
46          super.setUp();
47          setBaseUrl(HOST_PATH + "/ResourceBundleAssertionsTest");
48          getTestContext().setResourceBundleName(resourceBundleName);
49          beginAt("/testPage.html");
50      }
51  
52      @Test public void testGetMessage() {
53          assertEquals(pageTitle, getMessage("title.fixed"));
54      }
55  
56      @Test public void testGetMessageWithArg() {
57          assertEquals(pageTitle, getMessage("title.with.args", new String[] { "Main Page" }));
58      }
59  
60      /**
61       * Verify number formatting works in Default Locale
62       */
63      @Test public void testGetFormattedMessage() {
64          final Double amount = new Double(1234.56);
65          NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.getDefault());
66          final String expectedValue = numberFormat.format(amount);
67  
68          assertEquals(expectedValue, getTester()
69                  .getMessage("currency.with.args", new Object[] { amount }));
70      }
71  
72      /**
73       * Verify Test uses the Locale specified by framework
74       */
75      @Test public void testGetLocalizedFormattedMessage() {
76          final Double amount = new Double(1234.56);
77          NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
78          final String expectedValue = numberFormat.format(amount);
79  
80          getTestContext().setLocale(Locale.FRANCE);
81  
82          assertEquals(expectedValue, getTester()
83                  .getMessage("currency.with.args", new Object[] { amount }));
84      }
85  
86      @Test public void testAssertTitleEqualsKey() throws Throwable {
87          assertPassFail("assertTitleEqualsKey", "title.fixed", "title.not.used");
88      }
89  
90      @Test public void testAssertTitleEqualsKeyWithArg() throws Throwable {
91          //assertTitleEqualsKey("title.with.args", new String[] {"Main Page"});
92          assertPassFail("assertTitleEqualsKey", new Object[] { "title.with.args",
93                  new Object[] { "Main Page" } }, new Object[] { "title.with.args",
94                  new Object[] { "Wrong" } });
95      }
96  
97      @Test public void testAssertKeyPresent() throws Throwable {
98          assertPassFail("assertKeyPresent", "text.fixed", "title.not.used");
99      }
100 
101     @Test public void testAssertKeyPresentWithArg() throws Throwable {
102         assertPassFail("assertKeyPresent",
103                 new Object[] { "text.with.args", new Object[] { "test" } }, new Object[] {
104                         "text.with.args", new Object[] { "wrong" } });
105     }
106 
107     @Test public void testAssertTextNotPresent() throws Throwable {
108         assertPassFail("assertTextNotPresent", "no such text", text);
109     }
110 
111     @Test public void testAssertNotKeyPresent() throws Throwable {
112         assertPassFail("assertKeyNotPresent", "title.not.used", "text.fixed");
113     }
114 
115     @Test public void testAssertKeyNotPresentWithArg() throws Throwable {
116         assertPassFail("assertKeyNotPresent", new Object[] { "text.with.args",
117                 new Object[] { "wrong" } }, new Object[] { "text.with.args",
118                 new Object[] { "test" } });
119     }
120 
121     @Test public void testAssertKeyInTable() throws Throwable {
122         assertPassFail("assertKeyInTable", new Object[] { "testTable", "table.fixed" },
123                 new Object[] { "testTable", "title.not.used" });
124     }
125 
126     @Test public void testAssertKeyInTableWithArgs() throws Throwable {
127         assertPassFail("assertKeyInTable", new Object[] { "testTable", "table.with.args",
128                 new Object[] { "Data", "Table" } }, new Object[] { "testTable", "table.with.args",
129                 new Object[] { "wrong" } });
130     }
131 
132     @Test public void testAssertKeyNotInTable() throws Throwable {
133         assertPassFail("assertKeyNotInTable", new Object[] { "testTable", "title.not.used" },
134                 new Object[] { "testTable", "table.fixed" });
135     }
136 
137     @Test public void testAssertKeysInTable() throws Throwable {
138         assertPassFail("assertKeysInTable", new Object[] { "testTable",
139                 new String[] { "table.fixed", "table2.fixed" } }, new Object[] { "testTable",
140                 new String[] { "table.fixed", "title.not.used" } });
141     }
142 
143     @Test public void testAssertKeysInTableWithArgs() throws Throwable {
144         // okay, it's not pretty but somebody *might* want to use it...
145         assertPassFail("assertKeysInTable",
146                 new Object[] {
147                         "testTable",
148                         new String[] { "table.with.args", "table2.with.args" },
149                         new Object[][] { new Object[] { "Data", "Table" },
150                                 new Object[] { "Data", "In" } } }, new Object[] { "testTable",
151                         new String[] { "table.with.args", "table2.with.args" },
152                         new Object[][] { new Object[] { "Wrong" }, new Object[] { "Wrong" } } });
153     }
154 
155 }