Browse Source

Formatter unit tests

Fix NPE when formatting unused null references

git-svn-id: http://svn.dmdirc.com/trunk@3404 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 years ago
parent
commit
b67d2e0edb

+ 3
- 3
src/com/dmdirc/ui/messages/Formatter.java View File

111
             switch (chr) {
111
             switch (chr) {
112
             case 'b': case 'B': case 'h': case 'H': case 's': case 'S':
112
             case 'b': case 'B': case 'h': case 'H': case 's': case 'S':
113
                 // General (strings)
113
                 // General (strings)
114
-                res[i] = args[i].toString();
114
+                res[i] = String.valueOf(args[i]);
115
                 break;
115
                 break;
116
             case 'c': case 'C':
116
             case 'c': case 'C':
117
                 // Character
117
                 // Character
118
-                res[i] = ((String) args[i]).charAt(0);
118
+                res[i] = String.valueOf(args[i]).charAt(0);
119
                 break;
119
                 break;
120
             case 'd': case 'o': case 'x': case 'X':
120
             case 'd': case 'o': case 'x': case 'X':
121
                 // Integers
121
                 // Integers
136
                 break;
136
                 break;
137
             case 'u':
137
             case 'u':
138
                 // Duration hacks
138
                 // Duration hacks
139
-                res[i] = formatDuration(Integer.valueOf(args[i].toString()));
139
+                res[i] = formatDuration(Integer.valueOf(String.valueOf(args[i].toString())));
140
                 break;
140
                 break;
141
             default:
141
             default:
142
                 res[i] = args[i];
142
                 res[i] = args[i];

+ 45
- 1
test/com/dmdirc/ui/messages/FormatterTest.java View File

47
         assertEquals("10", Formatter.formatMessage(mcm, "1%1$d", "10"));
47
         assertEquals("10", Formatter.formatMessage(mcm, "1%1$d", "10"));
48
         assertEquals("111999", Formatter.formatMessage(mcm, "1%1$s", "111999"));
48
         assertEquals("111999", Formatter.formatMessage(mcm, "1%1$s", "111999"));
49
     }
49
     }
50
-
50
+    
51
+    @Test
52
+    public void testCaching() {
53
+        try {
54
+            assertEquals("H", Formatter.formatMessage(mcm, "1%1$C", "Hello!"));
55
+            assertEquals("H", Formatter.formatMessage(mcm, "1%1$C", "Hello!", 123, null));
56
+            assertEquals("HELLO!", Formatter.formatMessage(mcm, "1%1$S", "Hello!", 123, null));
57
+            assertEquals("HELLO!", Formatter.formatMessage(mcm, "1%1$S", "Hello!"));
58
+        } catch (Exception ex) {
59
+            ex.printStackTrace();
60
+            assertTrue(false);
61
+        }
62
+    }
63
+    
64
+    @Test
65
+    public void testFormatDuration() {
66
+        assertEquals("1 minute, 1 second", Formatter.formatMessage(mcm, "1%1$u", "61"));
67
+    }
68
+    
69
+    @Test
70
+    public void testFormatDurationSeconds() {
71
+        assertEquals("1 second", Formatter.formatDuration(1));
72
+        assertEquals("2 seconds", Formatter.formatDuration(2));
73
+    }
74
+    
75
+    @Test
76
+    public void testFormatDurationMinutes() {
77
+        assertEquals("1 minute", Formatter.formatDuration(60));
78
+        assertEquals("1 minute, 1 second", Formatter.formatDuration(61));
79
+        assertEquals("1 minute, 2 seconds", Formatter.formatDuration(62));
80
+        assertEquals("2 minutes, 2 seconds", Formatter.formatDuration(122));
81
+    }
82
+    
83
+    @Test
84
+    public void testFormatDurationHours() {
85
+        assertEquals("1 hour", Formatter.formatDuration(3600));
86
+        assertEquals("1 hour, 1 second", Formatter.formatDuration(3601));
87
+        assertEquals("2 hours, 1 minute, 5 seconds", Formatter.formatDuration(7265));
88
+    }
89
+    
90
+    @Test
91
+    public void testFormatDurationDays() {
92
+        assertEquals("1 day", Formatter.formatDuration(86400));
93
+        assertEquals("1 day, 10 minutes, 1 second", Formatter.formatDuration(87001));
94
+    }
51
 
95
 
52
     private class MyConfigManager extends ConfigManager {
96
     private class MyConfigManager extends ConfigManager {
53
         
97
         

Loading…
Cancel
Save