Commit 0e73af665408bd7aa6b47c399d75590aa56c64eb
1 parent
94c14ba6
Exists in
master
Refatoração dos testes unitários da classe ResourceBundle
Showing
2 changed files
with
124 additions
and
142 deletions
Show diff stats
impl/core/src/test/java/br/gov/frameworkdemoiselle/util/ResourceBundleTest.java
1 | -///* | |
2 | -// * Demoiselle Framework | |
3 | -// * Copyright (C) 2010 SERPRO | |
4 | -// * ---------------------------------------------------------------------------- | |
5 | -// * This file is part of Demoiselle Framework. | |
6 | -// * | |
7 | -// * Demoiselle Framework is free software; you can redistribute it and/or | |
8 | -// * modify it under the terms of the GNU Lesser General Public License version 3 | |
9 | -// * as published by the Free Software Foundation. | |
10 | -// * | |
11 | -// * This program 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 version 3 | |
17 | -// * along with this program; if not, see <http://www.gnu.org/licenses/> | |
18 | -// * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
19 | -// * Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | -// * ---------------------------------------------------------------------------- | |
21 | -// * Este arquivo é parte do Framework Demoiselle. | |
22 | -// * | |
23 | -// * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
24 | -// * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
25 | -// * do Software Livre (FSF). | |
26 | -// * | |
27 | -// * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
28 | -// * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
29 | -// * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
30 | -// * para maiores detalhes. | |
31 | -// * | |
32 | -// * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
33 | -// * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
34 | -// * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
35 | -// * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
36 | -// */ | |
37 | -//package br.gov.frameworkdemoiselle.util; | |
38 | -// | |
39 | -//import static org.easymock.EasyMock.createMock; | |
40 | -//import static org.easymock.EasyMock.replay; | |
41 | -//import static org.easymock.EasyMock.verify; | |
42 | -//import static org.junit.Assert.assertEquals; | |
43 | -//import static org.junit.Assert.assertFalse; | |
44 | -//import static org.junit.Assert.assertNull; | |
45 | -//import static org.junit.Assert.assertTrue; | |
46 | -// | |
47 | -//import java.util.Enumeration; | |
48 | -//import java.util.ListResourceBundle; | |
49 | -// | |
50 | -//import junit.framework.Assert; | |
51 | -// | |
52 | -//import org.junit.Before; | |
53 | -//import org.junit.Test; | |
54 | -// | |
55 | -//public class ResourceBundleTest { | |
56 | -// | |
57 | -// /** | |
58 | -// * This is a workaround to mock java.util.ResourceBundle. Since getString(key) method is defined as final, there is | |
59 | -// * no way to extend and override it. For that reason, setting expectations (i.e. expect(...)) won't work. | |
60 | -// */ | |
61 | -// class MockResourceBundle extends ListResourceBundle { | |
62 | -// | |
63 | -// private Object[][] contents = new Object[][] { { "msgWithoutParams", "no params" }, | |
64 | -// { "msgWithParams", "params: {0}, {1}" } }; | |
65 | -// | |
66 | -// protected Object[][] getContents() { | |
67 | -// return contents; | |
68 | -// } | |
69 | -// | |
70 | -// }; | |
71 | -// | |
72 | -// private ResourceBundle resourceBundle; | |
73 | -// | |
74 | -// private java.util.ResourceBundle mockResourceBundle; | |
75 | -// | |
76 | -// @Before | |
77 | -// public void setUp() throws Exception { | |
78 | -// mockResourceBundle = new MockResourceBundle(); | |
79 | -// resourceBundle = new ResourceBundle(mockResourceBundle); | |
80 | -// } | |
81 | -// | |
82 | -// @Test | |
83 | -// public void containsKey() { | |
84 | -// assertTrue(resourceBundle.containsKey("msgWithoutParams")); | |
85 | -// | |
86 | -// assertFalse(resourceBundle.containsKey("inexistentKey")); | |
87 | -// } | |
88 | -// | |
89 | -// @Test | |
90 | -// public void getKeys() { | |
91 | -// int keyCount = 0; | |
92 | -// | |
93 | -// Enumeration<String> e = resourceBundle.getKeys(); | |
94 | -// | |
95 | -// while (e.hasMoreElements()) { | |
96 | -// keyCount++; | |
97 | -// e.nextElement(); | |
98 | -// } | |
99 | -// | |
100 | -// assertEquals(resourceBundle.keySet().size(), keyCount); | |
101 | -// } | |
102 | -// | |
103 | -// @Test | |
104 | -// public void testGetLocale() { | |
105 | -// assertNull(resourceBundle.getLocale()); | |
106 | -// } | |
107 | -// | |
108 | -// @Test | |
109 | -// public void testKeySet() { | |
110 | -// assertEquals(2, resourceBundle.keySet().size()); | |
111 | -// } | |
112 | -// | |
113 | -// @Test | |
114 | -// public void getString() { | |
115 | -// assertEquals("no params", resourceBundle.getString("msgWithoutParams")); | |
116 | -// | |
117 | -// assertEquals("params: a, b", resourceBundle.getString("msgWithParams", "a", "b")); | |
118 | -// | |
119 | -// assertEquals("params: {0}, {1}", resourceBundle.getString("msgWithParams")); | |
120 | -// } | |
121 | -// | |
122 | -// /** | |
123 | -// * For this test, java.util.ResourceBundle is mocked to force an exception. Since the getString method is called | |
124 | -// * from the actual ResourceBundle, not from the mock, it tries to find a handleGetObject method that doesn't exist. | |
125 | -// * | |
126 | -// * @throws Exception | |
127 | -// */ | |
128 | -// @Test(expected = RuntimeException.class) | |
129 | -// public void getStringWhenHandleGetObjectThrowsException() { | |
130 | -// mockResourceBundle = createMock(java.util.ResourceBundle.class); | |
131 | -// resourceBundle = new ResourceBundle(mockResourceBundle); | |
132 | -// | |
133 | -// replay(mockResourceBundle); | |
134 | -// | |
135 | -// resourceBundle.getString("msgWithParams"); | |
136 | -// | |
137 | -// verify(mockResourceBundle); | |
138 | -// | |
139 | -// Assert.fail(); | |
140 | -// } | |
141 | -// | |
142 | -//} | |
1 | +/* | |
2 | + * Demoiselle Framework | |
3 | + * Copyright (C) 2010 SERPRO | |
4 | + * ---------------------------------------------------------------------------- | |
5 | + * This file is part of Demoiselle Framework. | |
6 | + * | |
7 | + * Demoiselle Framework is free software; you can redistribute it and/or | |
8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | |
9 | + * as published by the Free Software Foundation. | |
10 | + * | |
11 | + * This program 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 version 3 | |
17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | |
18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | |
19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | + * ---------------------------------------------------------------------------- | |
21 | + * Este arquivo é parte do Framework Demoiselle. | |
22 | + * | |
23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | |
24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | |
25 | + * do Software Livre (FSF). | |
26 | + * | |
27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | |
28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | |
29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | |
30 | + * para maiores detalhes. | |
31 | + * | |
32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | |
33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | |
34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | |
35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | |
36 | + */ | |
37 | +package br.gov.frameworkdemoiselle.util; | |
38 | + | |
39 | +import static org.junit.Assert.assertEquals; | |
40 | +import static org.junit.Assert.assertFalse; | |
41 | +import static org.junit.Assert.assertTrue; | |
42 | + | |
43 | +import java.util.Enumeration; | |
44 | +import java.util.Locale; | |
45 | + | |
46 | +import org.junit.Before; | |
47 | +import org.junit.Test; | |
48 | + | |
49 | +public class ResourceBundleTest { | |
50 | + | |
51 | + /** | |
52 | + * This is a workaround to mock java.util.ResourceBundle. Since getString(key) method is defined as final, there is | |
53 | + * no way to extend and override it. For that reason, setting expectations (i.e. expect(...)) won't work. | |
54 | + */ | |
55 | + private ResourceBundle resourceBundle; | |
56 | + | |
57 | + @Before | |
58 | + public void setUp() throws Exception { | |
59 | + resourceBundle = new ResourceBundle("resource-bundle", new Locale("pt")); | |
60 | + } | |
61 | + | |
62 | + @Test | |
63 | + public void containsKey() { | |
64 | + assertTrue(resourceBundle.containsKey("msgWithoutParams")); | |
65 | + | |
66 | + assertFalse(resourceBundle.containsKey("inexistentKey")); | |
67 | + } | |
68 | + | |
69 | + @Test | |
70 | + public void getKeys() { | |
71 | + int keyCount = 0; | |
72 | + | |
73 | + Enumeration<String> e = resourceBundle.getKeys(); | |
74 | + | |
75 | + while (e.hasMoreElements()) { | |
76 | + keyCount++; | |
77 | + e.nextElement(); | |
78 | + } | |
79 | + | |
80 | + assertEquals(resourceBundle.keySet().size(), keyCount); | |
81 | + } | |
82 | + | |
83 | + @Test | |
84 | + public void testGetLocale() { | |
85 | + assertEquals(resourceBundle.getLocale(), new Locale("pt")); | |
86 | + } | |
87 | + | |
88 | + @Test | |
89 | + public void testKeySet() { | |
90 | + assertEquals(2, resourceBundle.keySet().size()); | |
91 | + } | |
92 | + | |
93 | + @Test | |
94 | + public void getString() { | |
95 | + assertEquals("no params", resourceBundle.getString("msgWithoutParams")); | |
96 | + | |
97 | + assertEquals("params: a, b", resourceBundle.getString("msgWithParams", "a", "b")); | |
98 | + | |
99 | + assertEquals("params: {0}, {1}", resourceBundle.getString("msgWithParams")); | |
100 | + } | |
101 | + | |
102 | + /** | |
103 | + * For this test, java.util.ResourceBundle is mocked to force an exception. Since the getString method is called | |
104 | + * from the actual ResourceBundle, not from the mock, it tries to find a handleGetObject method that doesn't exist. | |
105 | + * | |
106 | + * @throws Exception | |
107 | + */ | |
108 | + // @Test(expected = RuntimeException.class) | |
109 | + // public void getStringWhenHandleGetObjectThrowsException() { | |
110 | + // mockResourceBundle = createMock(java.util.ResourceBundle.class); | |
111 | + // resourceBundle = new ResourceBundle(mockResourceBundle); | |
112 | + // | |
113 | + // replay(mockResourceBundle); | |
114 | + // | |
115 | + // resourceBundle.getString("msgWithParams"); | |
116 | + // | |
117 | + // verify(mockResourceBundle); | |
118 | + // | |
119 | + // Assert.fail(); | |
120 | + // } | |
121 | + | |
122 | +} | ... | ... |
impl/core/src/test/resources/resource-bundle_pt.properties
0 → 100644