Home | History | Annotate | Download | only in charset
      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 tests.api.java.nio.charset;
     19 
     20 import java.nio.ByteBuffer;
     21 import java.nio.CharBuffer;
     22 import java.nio.charset.Charset;
     23 
     24 import junit.framework.TestCase;
     25 
     26 /**
     27  * Super class for concrete charset test suites.
     28  */
     29 public abstract class AbstractCharsetTestCase extends TestCase {
     30 
     31 	// the canonical name of this charset
     32 	protected final String canonicalName;
     33 
     34 	// the aliases set
     35 	protected final String[] aliases;
     36 
     37 	// canEncode
     38 	protected final boolean canEncode;
     39 
     40 	// isRegistered
     41 	protected final boolean isRegistered;
     42 
     43 	// charset instance
     44 	protected Charset testingCharset;
     45 
     46 	/*
     47 	 * Initialize the field "testingCharset" here.
     48 	 *
     49 	 * @see TestCase#setUp()
     50 	 */
     51 	protected void setUp() throws Exception {
     52 		super.setUp();
     53 		this.testingCharset = Charset.forName(this.canonicalName);
     54 	}
     55 
     56 	/*
     57 	 * @see TestCase#tearDown()
     58 	 */
     59 	protected void tearDown() throws Exception {
     60 		super.tearDown();
     61 	}
     62 
     63 	/**
     64 	 * Constructor for ConcreteCharsetTest.
     65 	 *
     66 	 */
     67 	public AbstractCharsetTestCase(String arg0, String canonicalName,
     68 			String[] aliases, boolean canEncode, boolean isRegistered) {
     69 		super(arg0);
     70 		this.canonicalName = canonicalName;
     71 		this.canEncode = canEncode;
     72 		this.isRegistered = isRegistered;
     73 		this.aliases = aliases;
     74 	}
     75 
     76 	/*
     77 	 * Test canEncode.
     78 	 */
     79 	public void testCanEncode() {
     80 		assertEquals(this.canEncode, this.testingCharset.canEncode());
     81 	}
     82 
     83 	/*
     84 	 * Test isRegistered.
     85 	 */
     86 	public void testIsRegistered() {
     87 		assertEquals(this.isRegistered, this.testingCharset.isRegistered());
     88 	}
     89 
     90 	/*
     91 	 * Test name.
     92 	 */
     93 	public void testName() {
     94 		assertEquals(this.canonicalName, this.testingCharset.name());
     95 		// assertEquals(this.canonicalName, this.testingCharset.displayName());
     96 		// assertEquals(this.canonicalName,
     97 		// this.testingCharset.displayName(null));
     98 	}
     99 
    100 	/*
    101 	 * Test aliases.
    102 	 */
    103 	public void testAliases() {
    104 		for (int i = 0; i < this.aliases.length; i++) {
    105 			Charset c = Charset.forName(this.aliases[i]);
    106 			assertEquals(this.canonicalName, c.name());
    107 			// TODO
    108 			// assertTrue(this.testingCharset.aliases().contains(this.aliases[i]));
    109 		}
    110 	}
    111 
    112 	/*
    113 	 * Test the method encode(String) with null.
    114 	 */
    115 	public void testEncode_String_Null() {
    116 		try {
    117 			this.testingCharset.encode((String) null);
    118 			fail("Should throw NullPointerException!");
    119 		} catch (NullPointerException e) {
    120 			// expected
    121 		}
    122 	}
    123 
    124 	/*
    125 	 * Test the method encode(CharBuffer) with null.
    126 	 */
    127 	public void testEncode_CharBuffer_Null() {
    128 		try {
    129 			this.testingCharset.encode((CharBuffer) null);
    130 			fail("Should throw NullPointerException!");
    131 		} catch (NullPointerException e) {
    132 			// expected
    133 		}
    134 	}
    135 
    136 	/*
    137 	 * Test encoding.
    138 	 */
    139 	protected void internalTestEncode(String input, byte[] output) {
    140 		ByteBuffer bb = this.testingCharset.encode(input);
    141 		int i = 0;
    142 		bb.rewind();
    143 		while (bb.hasRemaining() && i < output.length) {
    144 			assertEquals(output[i], bb.get());
    145 			i++;
    146 		}
    147 		assertFalse(bb.hasRemaining());
    148 		assertEquals(output.length, i);
    149 	}
    150 
    151 	/*
    152 	 * Test encoding.
    153 	 */
    154 	public abstract void testEncode_Normal();
    155 
    156 	/*
    157 	 * Test decoding.
    158 	 */
    159 	protected void internalTestDecode(byte[] input, char[] output) {
    160 		CharBuffer chb = this.testingCharset.decode(ByteBuffer.wrap(input));
    161 		int i = 0;
    162 		chb.rewind();
    163 		while (chb.hasRemaining() && i < output.length) {
    164 			assertEquals(output[i], chb.get());
    165 			i++;
    166 		}
    167 		assertFalse(chb.hasRemaining());
    168 		assertEquals(output.length, i);
    169 	}
    170 
    171 	/*
    172 	 * Test decoding.
    173 	 */
    174 	public abstract void testDecode_Normal();
    175 }
    176