1 33 34 package org.slf4j.helpers; 35 36 import org.slf4j.helpers.MessageFormatter; 37 38 import junit.framework.TestCase; 39 40 41 45 public class MessageFormatterTest extends TestCase { 46 47 Integer i1 = new Integer (1); 48 Integer i2 = new Integer (2); 49 Integer i3 = new Integer (3); 50 51 public void testNull() { 52 String result; 53 result = MessageFormatter.format(null, i1); 54 assertEquals(null, result); 55 } 56 57 public void testNullParam() { 58 String result; 59 60 result = MessageFormatter.format("Value is {}.", null); 61 assertEquals("Value is null.", result); 62 63 result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, null); 64 assertEquals("Val1 is null, val2 is null.", result); 65 66 result = MessageFormatter.format("Val1 is {}, val2 is {}.", i1, null); 67 assertEquals("Val1 is 1, val2 is null.", result); 68 69 result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, i2); 70 assertEquals("Val1 is null, val2 is 2.", result); 71 72 result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer []{null, null, null}); 73 assertEquals("Val1 is null, val2 is null, val3 is null", result); 74 75 result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer []{null, i2, i3}); 76 assertEquals("Val1 is null, val2 is 2, val3 is 3", result); 77 78 result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer []{null, null, i3}); 79 assertEquals("Val1 is null, val2 is null, val3 is 3", result); 80 } 81 82 83 public void test1Param() { 84 String result; 85 86 result = MessageFormatter.format("Value is {}.", i3); 87 assertEquals("Value is 3.", result); 88 89 result = MessageFormatter.format("Value is {", i3); 90 assertEquals("Value is {", result); 91 92 result = MessageFormatter.format("{} is larger than 2.", i3); 93 assertEquals("3 is larger than 2.", result); 94 95 result = MessageFormatter.format("No subst", i3); 96 assertEquals("No subst", result); 97 98 result = MessageFormatter.format("Incorrect {subst", i3); 99 assertEquals("Incorrect {subst", result); 100 101 result = MessageFormatter.format("Escaped \\{} subst", i3); 102 assertEquals("Escaped {} subst", result); 103 104 result = MessageFormatter.format("\\{Escaped", i3); 105 assertEquals("{Escaped", result); 106 107 result = MessageFormatter.format("\\{}Escaped", i3); 108 assertEquals("{}Escaped", result); 109 110 result = MessageFormatter.format("File name is \\{{}}.", "App folder.zip"); 111 assertEquals("File name is {App folder.zip}.", result); 112 } 113 114 public void test2Param() { 115 String result; 116 117 118 result = MessageFormatter.format("Value {} is smaller than {}.", i1, i2); 119 assertEquals("Value 1 is smaller than 2.", result); 120 121 result = MessageFormatter.format("Value {} is smaller than {}", i1, i2); 122 assertEquals("Value 1 is smaller than 2", result); 123 124 result = MessageFormatter.format("{}{}", i1, i2); 125 assertEquals("12", result); 126 127 result = MessageFormatter.format("Val1={}, Val2={", i1, i2); 128 assertEquals("Val1=1, Val2={", result); 129 130 result = MessageFormatter.format("Value {} is smaller than \\{}", i1, i2); 131 assertEquals("Value 1 is smaller than {}", result); 132 133 result = MessageFormatter.format("Value {} is smaller than \\{} tail", i1, i2); 134 assertEquals("Value 1 is smaller than {} tail", result); 135 136 result = MessageFormatter.format("Value {} is smaller than \\{", i1, i2); 137 assertEquals("Value 1 is smaller than \\{", result); 138 139 result = MessageFormatter.format("Value {} is smaller than \\{tail", i1, i2); 140 assertEquals("Value 1 is smaller than {tail", result); 141 142 143 result = MessageFormatter.format("Value \\{} is smaller than {}", i1, i2); 144 assertEquals("Value {} is smaller than 1", result); 145 } 146 147 public void testArray() { 148 String result; 149 150 Integer [] ia = new Integer [] {i1, i2, i3}; 151 152 result = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia); 153 assertEquals("Value 1 is smaller than 2 and 3.", result); 154 155 result = MessageFormatter.arrayFormat("{}{}{}", ia); 156 assertEquals("123", result); 157 158 result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia); 159 assertEquals("Value 1 is smaller than 2.", result); 160 161 result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia); 162 assertEquals("Value 1 is smaller than 2", result); 163 164 result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia); 165 assertEquals("Val=1, {, Val={}", result); 166 167 result = MessageFormatter.arrayFormat("Val={}, \\{, Val={}", ia); 168 assertEquals("Val=1, {, Val=2", result); 169 170 171 result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia); 172 assertEquals("Val1=1, Val2={", result); 173 174 175 } 176 177 } 178 | Popular Tags |