001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.codec; 019 020 import java.util.Locale; 021 022 import junit.framework.Assert; 023 024 import org.junit.Test; 025 026 /** 027 * @version $Id: StringEncoderAbstractTest.html 889935 2013-12-11 05:05:13Z ggregory $ 028 */ 029 public abstract class StringEncoderAbstractTest { 030 031 protected StringEncoder stringEncoder = this.createStringEncoder(); 032 033 public void checkEncoding(String expected, String source) throws EncoderException { 034 Assert.assertEquals("Source: " + source, expected, this.getStringEncoder().encode(source)); 035 } 036 037 protected void checkEncodings(String[][] data) throws EncoderException { 038 for (String[] element : data) { 039 this.checkEncoding(element[1], element[0]); 040 } 041 } 042 043 protected void checkEncodingVariations(String expected, String data[]) throws EncoderException { 044 for (String element : data) { 045 this.checkEncoding(expected, element); 046 } 047 } 048 049 protected abstract StringEncoder createStringEncoder(); 050 051 public StringEncoder getStringEncoder() { 052 return this.stringEncoder; 053 } 054 055 @Test 056 public void testEncodeEmpty() throws Exception { 057 Encoder encoder = this.getStringEncoder(); 058 encoder.encode(""); 059 encoder.encode(" "); 060 encoder.encode("\t"); 061 } 062 063 @Test 064 public void testEncodeNull() throws Exception { 065 StringEncoder encoder = this.getStringEncoder(); 066 try { 067 encoder.encode(null); 068 } catch (EncoderException ee) { 069 // An exception should be thrown 070 } 071 } 072 073 @Test 074 public void testEncodeWithInvalidObject() throws Exception { 075 boolean exceptionThrown = false; 076 try { 077 StringEncoder encoder = this.getStringEncoder(); 078 encoder.encode(new Float(3.4)); 079 } catch (Exception e) { 080 exceptionThrown = true; 081 } 082 Assert.assertTrue("An exception was not thrown when we tried to encode " + "a Float object", exceptionThrown); 083 } 084 085 @Test 086 public void testLocaleIndependence() throws Exception { 087 StringEncoder encoder = this.getStringEncoder(); 088 089 String[] data = {"I", "i",}; 090 091 Locale orig = Locale.getDefault(); 092 Locale[] locales = {Locale.ENGLISH, new Locale("tr"), Locale.getDefault()}; 093 094 try { 095 for (String element : data) { 096 String ref = null; 097 for (int j = 0; j < locales.length; j++) { 098 Locale.setDefault(locales[j]); 099 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 }