1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package tests.api.java.nio.charset; 17 18import dalvik.annotation.TestTargetClass; 19import dalvik.annotation.TestTargets; 20import dalvik.annotation.TestTargetNew; 21import dalvik.annotation.TestLevel; 22 23import java.nio.ByteBuffer; 24import java.nio.CharBuffer; 25import java.nio.charset.CharacterCodingException; 26import java.nio.charset.Charset; 27import java.nio.charset.CoderResult; 28import java.nio.charset.CodingErrorAction; 29import java.nio.charset.UnmappableCharacterException; 30 31/** 32 * test case specific activity of iso-8859-1 charset encoder 33 */ 34@TestTargetClass(java.nio.charset.CharsetEncoder.class) 35public class ISOCharsetEncoderTest extends AbstractCharsetEncoderTestCase { 36 37 // charset for iso-8859-1 38 private static final Charset CS = Charset.forName("iso-8859-1"); 39 40 /* 41 * @see CharsetEncoderTest#setUp() 42 */ 43 protected void setUp() throws Exception { 44 cs = CS; 45 super.setUp(); 46 } 47 48 /* 49 * @see CharsetEncoderTest#tearDown() 50 */ 51 protected void tearDown() throws Exception { 52 super.tearDown(); 53 } 54 55 @TestTargetNew( 56 level = TestLevel.PARTIAL, 57 notes = "IllegalStateException checking missed.", 58 method = "canEncode", 59 args = {java.lang.CharSequence.class} 60 ) 61 public void testCanEncodeCharSequence() { 62 // normal case for isoCS 63 assertTrue(encoder.canEncode("\u0077")); 64 assertFalse(encoder.canEncode("\uc2a3")); 65 assertFalse(encoder.canEncode("\ud800\udc00")); 66 try { 67 encoder.canEncode(null); 68 } catch (NullPointerException e) { 69 } 70 assertTrue(encoder.canEncode("")); 71 } 72 73 @TestTargets({ 74 @TestTargetNew( 75 level = TestLevel.PARTIAL, 76 notes = "Regression test. IllegalStateException checking missed.", 77 method = "canEncode", 78 args = {char.class} 79 ), 80 @TestTargetNew( 81 level = TestLevel.PARTIAL, 82 notes = "Regression test. IllegalStateException checking missed.", 83 method = "canEncode", 84 args = {java.lang.CharSequence.class} 85 ) 86 }) 87 public void testCanEncodeICUBug() { 88 assertFalse(encoder.canEncode((char) '\ud800')); 89 assertFalse(encoder.canEncode((String) "\ud800")); 90 } 91 92 @TestTargetNew( 93 level = TestLevel.PARTIAL, 94 notes = "IllegalStateException checking missed.", 95 method = "canEncode", 96 args = {char.class} 97 ) 98 public void testCanEncodechar() throws CharacterCodingException { 99 assertTrue(encoder.canEncode('\u0077')); 100 assertFalse(encoder.canEncode('\uc2a3')); 101 } 102 103 @TestTargets({ 104 @TestTargetNew( 105 level = TestLevel.COMPLETE, 106 notes = "", 107 method = "averageBytesPerChar", 108 args = {} 109 ), 110 @TestTargetNew( 111 level = TestLevel.COMPLETE, 112 notes = "", 113 method = "maxBytesPerChar", 114 args = {} 115 ) 116 }) 117 public void testSpecificDefaultValue() { 118 assertEquals(1, encoder.averageBytesPerChar(), 0.001); 119 assertEquals(1, encoder.maxBytesPerChar(), 0.001); 120 } 121 122 CharBuffer getMalformedCharBuffer() { 123 return CharBuffer.wrap("\ud800 buffer"); 124 } 125 126 CharBuffer getUnmapCharBuffer() { 127 return CharBuffer.wrap("\ud800\udc00 buffer"); 128 } 129 130 CharBuffer getExceptionCharBuffer() { 131 return null; 132 } 133 134 protected byte[] getIllegalByteArray() { 135 return null; 136 } 137 138 @TestTargets({ 139 @TestTargetNew( 140 level = TestLevel.COMPLETE, 141 notes = "Checks also: flush & encode, but not covers exceptions.", 142 method = "onMalformedInput", 143 args = {java.nio.charset.CodingErrorAction.class} 144 ), 145 @TestTargetNew( 146 level = TestLevel.COMPLETE, 147 notes = "Checks also: flush & encode, but not covers exceptions.", 148 method = "onUnmappableCharacter", 149 args = {java.nio.charset.CodingErrorAction.class} 150 ), 151 @TestTargetNew( 152 level = TestLevel.COMPLETE, 153 notes = "Checks also: flush & encode, but not covers exceptions.", 154 method = "reset", 155 args = {} 156 ) 157 }) 158 public void testMultiStepEncode() throws CharacterCodingException { 159 encoder.onMalformedInput(CodingErrorAction.REPORT); 160 encoder.onUnmappableCharacter(CodingErrorAction.REPORT); 161 try { 162 encoder.encode(CharBuffer.wrap("\ud800\udc00")); 163 fail("should unmappable"); 164 } catch (UnmappableCharacterException e) { 165 } 166 encoder.reset(); 167 ByteBuffer out = ByteBuffer.allocate(10); 168 assertTrue(encoder.encode(CharBuffer.wrap("\ud800"), out, true) 169 .isMalformed()); 170 encoder.flush(out); 171 encoder.reset(); 172 out = ByteBuffer.allocate(10); 173 assertSame(CoderResult.UNDERFLOW, encoder.encode(CharBuffer 174 .wrap("\ud800"), out, false)); 175 assertTrue(encoder.encode(CharBuffer.wrap("\udc00"), out, true) 176 .isMalformed()); 177 } 178} 179