1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec;
19
20 import java.util.Locale;
21
22 import junit.framework.Assert;
23
24 import org.junit.Test;
25
26
27
28
29 public abstract class StringEncoderAbstractTest {
30
31 protected StringEncoder stringEncoder = this.createStringEncoder();
32
33 public void checkEncoding(String expected, String source) throws EncoderException {
34 Assert.assertEquals("Source: " + source, expected, this.getStringEncoder().encode(source));
35 }
36
37 protected void checkEncodings(String[][] data) throws EncoderException {
38 for (String[] element : data) {
39 this.checkEncoding(element[1], element[0]);
40 }
41 }
42
43 protected void checkEncodingVariations(String expected, String data[]) throws EncoderException {
44 for (String element : data) {
45 this.checkEncoding(expected, element);
46 }
47 }
48
49 protected abstract StringEncoder createStringEncoder();
50
51 public StringEncoder getStringEncoder() {
52 return this.stringEncoder;
53 }
54
55 @Test
56 public void testEncodeEmpty() throws Exception {
57 Encoder encoder = this.getStringEncoder();
58 encoder.encode("");
59 encoder.encode(" ");
60 encoder.encode("\t");
61 }
62
63 @Test
64 public void testEncodeNull() throws Exception {
65 StringEncoder encoder = this.getStringEncoder();
66 try {
67 encoder.encode(null);
68 } catch (EncoderException ee) {
69
70 }
71 }
72
73 @Test
74 public void testEncodeWithInvalidObject() throws Exception {
75 boolean exceptionThrown = false;
76 try {
77 StringEncoder encoder = this.getStringEncoder();
78 encoder.encode(new Float(3.4));
79 } catch (Exception e) {
80 exceptionThrown = true;
81 }
82 Assert.assertTrue("An exception was not thrown when we tried to encode " + "a Float object", exceptionThrown);
83 }
84
85 @Test
86 public void testLocaleIndependence() throws Exception {
87 StringEncoder encoder = this.getStringEncoder();
88
89 String[] data = {"I", "i",};
90
91 Locale orig = Locale.getDefault();
92 Locale[] locales = {Locale.ENGLISH, new Locale("tr"), Locale.getDefault()};
93
94 try {
95 for (String element : data) {
96 String ref = null;
97 for (int j = 0; j < locales.length; j++) {
98 Locale.setDefault(locales[j]);
99 if (j <= 0) {
100 ref = encoder.encode(element);
101 } else {
102 String cur = null;
103 try {
104 cur = encoder.encode(element);
105 } catch (Exception e) {
106 Assert.fail(Locale.getDefault().toString() + ": " + e.getMessage());
107 }
108 Assert.assertEquals(Locale.getDefault().toString() + ": ", ref, cur);
109 }
110 }
111 }
112 } finally {
113 Locale.setDefault(orig);
114 }
115 }
116
117 }