KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > example > TestFormatter


1 package cintoo.messages.example;
2
3 import cintoo.messages.format.Formatter;
4 import org.testng.Assert;
5 import org.testng.annotations.Configuration;
6 import org.testng.annotations.Test;
7
8 import java.util.*;
9
10 public class TestFormatter {
11   private ResourceBundle bundle;
12
13   @Configuration(beforeTestMethod = true)
14   public void setUp() {
15     final Map<String JavaDoc, String JavaDoc> properties = new HashMap<String JavaDoc, String JavaDoc>();
16     properties.put("testkey", "testvalue");
17     properties.put("testkey1", "testvalue1 {0}");
18     properties.put("testkey2", "testvalue2 {0} {1}");
19     properties.put("testkey3", "testvalue3 {0} {1} {2}");
20
21     bundle = new ResourceBundle() {
22       protected Object JavaDoc handleGetObject(String JavaDoc key) {
23         return properties.get(key);
24       }
25
26       public Enumeration<String JavaDoc> getKeys() {
27         return Collections.enumeration(properties.keySet());
28       }
29     };
30
31   }
32
33   @Test
34   public void testFormat() {
35     Formatter formatter = new Formatter();
36     Assert.assertEquals("testvalue", formatter.format(bundle, "testkey"));
37   }
38
39   @Test
40   public void testFormatInt() {
41     Formatter formatter = new Formatter();
42     Assert.assertEquals("testvalue1 1", formatter.format(bundle, "testkey1", 1));
43     Assert.assertEquals("testvalue2 1 2", formatter.format(bundle, "testkey2", 1, 2));
44     Assert.assertEquals("testvalue3 1 2 3", formatter.format(bundle, "testkey3", 1, 2, 3));
45   }
46
47   @Test
48   public void testNoExistingKey() {
49     Formatter formatter = new Formatter();
50     Assert.assertEquals("!notExisting!", formatter.format(bundle, "notExisting", 1));
51   }
52
53   @Test
54   public void testBundleNull() {
55     Formatter formatter = new Formatter();
56     Assert.assertEquals("!nullBundleKey!", formatter.format(null, "nullBundleKey", 1));
57   }
58
59 }
60
Popular Tags