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 */ 16 17package tests.api.java.nio.charset; 18 19import java.nio.BufferOverflowException; 20import java.nio.BufferUnderflowException; 21import java.nio.charset.CoderResult; 22import java.nio.charset.MalformedInputException; 23import java.nio.charset.UnmappableCharacterException; 24 25import junit.framework.TestCase; 26 27/** 28 * Test class java.nio.charset.CoderResult. 29 */ 30public class CoderResultTest extends TestCase { 31 32 /* 33 * @see TestCase#setUp() 34 */ 35 protected void setUp() throws Exception { 36 super.setUp(); 37 } 38 39 /* 40 * @see TestCase#tearDown() 41 */ 42 protected void tearDown() throws Exception { 43 super.tearDown(); 44 } 45 46 /* 47 * Test the constant OVERFLOW and UNDERFLOW. 48 */ 49 public void testConstants() throws Exception { 50 assertNotSame(CoderResult.OVERFLOW, CoderResult.UNDERFLOW); 51 52 assertNotNull(CoderResult.OVERFLOW); 53 assertFalse(CoderResult.OVERFLOW.isError()); 54 assertFalse(CoderResult.OVERFLOW.isMalformed()); 55 assertFalse(CoderResult.OVERFLOW.isUnderflow()); 56 assertFalse(CoderResult.OVERFLOW.isUnmappable()); 57 assertTrue(CoderResult.OVERFLOW.isOverflow()); 58 assertTrue(CoderResult.OVERFLOW.toString().indexOf("OVERFLOW") != -1); 59 try { 60 CoderResult.OVERFLOW.throwException(); 61 fail("Should throw BufferOverflowException"); 62 } catch (BufferOverflowException ex) { 63 // expected 64 } 65 try { 66 CoderResult.OVERFLOW.length(); 67 fail("Should throw UnsupportedOperationException"); 68 } catch (UnsupportedOperationException ex) { 69 // expected 70 } 71 72 assertNotNull(CoderResult.UNDERFLOW); 73 assertFalse(CoderResult.UNDERFLOW.isError()); 74 assertFalse(CoderResult.UNDERFLOW.isMalformed()); 75 assertTrue(CoderResult.UNDERFLOW.isUnderflow()); 76 assertFalse(CoderResult.UNDERFLOW.isUnmappable()); 77 assertFalse(CoderResult.UNDERFLOW.isOverflow()); 78 assertTrue(CoderResult.UNDERFLOW.toString().indexOf("UNDERFLOW") != -1); 79 try { 80 CoderResult.UNDERFLOW.throwException(); 81 fail("Should throw BufferOverflowException"); 82 } catch (BufferUnderflowException ex) { 83 // expected 84 } 85 try { 86 CoderResult.UNDERFLOW.length(); 87 fail("Should throw UnsupportedOperationException"); 88 } catch (UnsupportedOperationException ex) { 89 // expected 90 } 91 } 92 93 /** 94 * Test method isError(). 95 * 96 */ 97 public void testIsError() { 98 assertFalse(CoderResult.UNDERFLOW.isError()); 99 assertFalse(CoderResult.OVERFLOW.isError()); 100 assertTrue(CoderResult.malformedForLength(1).isError()); 101 assertTrue(CoderResult.unmappableForLength(1).isError()); 102 } 103 104 /** 105 * Test method isMalformed(). 106 * 107 */ 108 public void testIsMalformed() { 109 assertFalse(CoderResult.UNDERFLOW.isMalformed()); 110 assertFalse(CoderResult.OVERFLOW.isMalformed()); 111 assertTrue(CoderResult.malformedForLength(1).isMalformed()); 112 assertFalse(CoderResult.unmappableForLength(1).isMalformed()); 113 } 114 115 /** 116 * Test method isMalformed(). 117 * 118 */ 119 public void testIsUnmappable() { 120 assertFalse(CoderResult.UNDERFLOW.isUnmappable()); 121 assertFalse(CoderResult.OVERFLOW.isUnmappable()); 122 assertFalse(CoderResult.malformedForLength(1).isUnmappable()); 123 assertTrue(CoderResult.unmappableForLength(1).isUnmappable()); 124 } 125 126 /** 127 * Test method isOverflow(). 128 * 129 */ 130 public void testIsOverflow() { 131 assertFalse(CoderResult.UNDERFLOW.isOverflow()); 132 assertTrue(CoderResult.OVERFLOW.isOverflow()); 133 assertFalse(CoderResult.malformedForLength(1).isOverflow()); 134 assertFalse(CoderResult.unmappableForLength(1).isOverflow()); 135 } 136 137 /** 138 * Test method isUnderflow(). 139 * 140 */ 141 public void testIsUnderflow() { 142 assertTrue(CoderResult.UNDERFLOW.isUnderflow()); 143 assertFalse(CoderResult.OVERFLOW.isUnderflow()); 144 assertFalse(CoderResult.malformedForLength(1).isUnderflow()); 145 assertFalse(CoderResult.unmappableForLength(1).isUnderflow()); 146 } 147 148 /** 149 * Test method length(). 150 * 151 */ 152 public void testLength() { 153 try { 154 CoderResult.UNDERFLOW.length(); 155 fail("Should throw UnsupportedOperationException"); 156 } catch (UnsupportedOperationException ex) { 157 // expected 158 } 159 try { 160 CoderResult.OVERFLOW.length(); 161 fail("Should throw UnsupportedOperationException"); 162 } catch (UnsupportedOperationException ex) { 163 // expected 164 } 165 166 assertEquals(CoderResult.malformedForLength(1).length(), 1); 167 assertEquals(CoderResult.unmappableForLength(1).length(), 1); 168 } 169 170 /** 171 * Test method malformedForLength(int). 172 * 173 */ 174 public void testMalformedForLength() { 175 assertNotNull(CoderResult.malformedForLength(Integer.MAX_VALUE)); 176 assertNotNull(CoderResult.malformedForLength(1)); 177 assertSame(CoderResult.malformedForLength(1), CoderResult 178 .malformedForLength(1)); 179 assertNotSame(CoderResult.malformedForLength(1), CoderResult 180 .unmappableForLength(1)); 181 assertNotSame(CoderResult.malformedForLength(2), CoderResult 182 .malformedForLength(1)); 183 try { 184 CoderResult.malformedForLength(-1); 185 fail("Should throw IllegalArgumentException"); 186 } catch (IllegalArgumentException ex) { 187 // expected 188 } 189 try { 190 CoderResult.malformedForLength(0); 191 fail("Should throw IllegalArgumentException"); 192 } catch (IllegalArgumentException ex) { 193 // expected 194 } 195 } 196 197 /** 198 * Test method unmappableForLength(int). 199 * 200 */ 201 public void testUnmappableForLength() { 202 assertNotNull(CoderResult.unmappableForLength(Integer.MAX_VALUE)); 203 assertNotNull(CoderResult.unmappableForLength(1)); 204 assertSame(CoderResult.unmappableForLength(1), CoderResult 205 .unmappableForLength(1)); 206 assertNotSame(CoderResult.unmappableForLength(2), CoderResult 207 .unmappableForLength(1)); 208 try { 209 CoderResult.unmappableForLength(-1); 210 fail("Should throw IllegalArgumentException"); 211 } catch (IllegalArgumentException ex) { 212 // expected 213 } 214 try { 215 CoderResult.unmappableForLength(0); 216 fail("Should throw IllegalArgumentException"); 217 } catch (IllegalArgumentException ex) { 218 // expected 219 } 220 } 221 222 /** 223 * Test method throwException(). 224 * 225 */ 226 public void testThrowException() throws Exception { 227 try { 228 CoderResult.OVERFLOW.throwException(); 229 fail("Should throw BufferOverflowException"); 230 } catch (BufferOverflowException ex) { 231 // expected 232 } 233 try { 234 CoderResult.UNDERFLOW.throwException(); 235 fail("Should throw BufferOverflowException"); 236 } catch (BufferUnderflowException ex) { 237 // expected 238 } 239 try { 240 CoderResult.malformedForLength(1).throwException(); 241 fail("Should throw MalformedInputException"); 242 } catch (MalformedInputException ex) { 243 assertEquals(ex.getInputLength(), 1); 244 } 245 try { 246 CoderResult.unmappableForLength(1).throwException(); 247 fail("Should throw UnmappableCharacterException"); 248 } catch (UnmappableCharacterException ex) { 249 assertEquals(ex.getInputLength(), 1); 250 } 251 } 252 253 /** 254 * Test method toString(). 255 * 256 */ 257 public void testToString() throws Exception { 258 assertTrue(CoderResult.OVERFLOW.toString().indexOf("OVERFLOW") != -1); 259 assertTrue(CoderResult.UNDERFLOW.toString().indexOf("UNDERFLOW") != -1); 260 assertTrue(CoderResult.malformedForLength(666).toString() 261 .indexOf("666") != -1); 262 assertTrue(CoderResult.unmappableForLength(666).toString().indexOf( 263 "666") != -1); 264 } 265} 266