View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * @version $Id: StringEncoderAbstractTest.html 889935 2013-12-11 05:05:13Z ggregory $
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              // An exception should be thrown
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 }