0001: /* Licensed to the Apache Software Foundation (ASF) under one or more
0002: * contributor license agreements. See the NOTICE file distributed with
0003: * this work for additional information regarding copyright ownership.
0004: * The ASF licenses this file to You under the Apache License, Version 2.0
0005: * (the "License"); you may not use this file except in compliance with
0006: * the License. You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.apache.harmony.luni.tests.java.util;
0017:
0018: import java.io.Closeable;
0019: import java.io.EOFException;
0020: import java.io.File;
0021: import java.io.FileInputStream;
0022: import java.io.FileNotFoundException;
0023: import java.io.FileOutputStream;
0024: import java.io.IOException;
0025: import java.io.InputStream;
0026: import java.io.OutputStream;
0027: import java.io.PipedInputStream;
0028: import java.io.StringReader;
0029: import java.math.BigDecimal;
0030: import java.math.BigInteger;
0031: import java.net.InetSocketAddress;
0032: import java.net.ServerSocket;
0033: import java.net.Socket;
0034: import java.net.SocketAddress;
0035: import java.nio.CharBuffer;
0036: import java.nio.channels.FileChannel;
0037: import java.nio.channels.ReadableByteChannel;
0038: import java.nio.channels.SocketChannel;
0039: import java.nio.charset.Charset;
0040: import java.util.Arrays;
0041: import java.util.InputMismatchException;
0042: import java.util.Locale;
0043: import java.util.NoSuchElementException;
0044: import java.util.Scanner;
0045: import java.util.regex.MatchResult;
0046: import java.util.regex.Pattern;
0047:
0048: import junit.framework.TestCase;
0049:
0050: public class ScannerTest extends TestCase {
0051:
0052: private Scanner s;
0053:
0054: private ServerSocket server;
0055:
0056: private SocketAddress address;
0057:
0058: private SocketChannel client;
0059:
0060: private Socket serverSocket;
0061:
0062: private OutputStream os;
0063:
0064: private static class MockCloseable implements Closeable, Readable {
0065:
0066: public void close() throws IOException {
0067: throw new IOException();
0068: }
0069:
0070: public int read(CharBuffer cb) throws IOException {
0071: throw new EOFException();
0072: }
0073:
0074: }
0075:
0076: /**
0077: * @tests java.util.Scanner#Scanner(File)
0078: */
0079: public void test_ConstructorLjava_io_File() throws IOException {
0080: File tmpFile = File
0081: .createTempFile("TestFileForScanner", ".tmp");
0082: s = new Scanner(tmpFile);
0083: assertNotNull(s);
0084: s.close();
0085: assertTrue(tmpFile.delete());
0086:
0087: try {
0088: s = new Scanner(tmpFile);
0089: fail("should throw FileNotFoundException");
0090: } catch (FileNotFoundException e) {
0091: // expected
0092: }
0093:
0094: tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
0095: FileOutputStream fos = new FileOutputStream(tmpFile);
0096: fos.write("test".getBytes());
0097:
0098: s = new Scanner(tmpFile);
0099: tmpFile.delete();
0100:
0101: // Scanner(File = null)
0102: try {
0103: s = new Scanner((File) null);
0104: fail("Should throw NullPointerException");
0105: } catch (NullPointerException e) {
0106: // expected
0107: }
0108:
0109: // TODO: test if the default charset is used.
0110: }
0111:
0112: /**
0113: * @tests java.util.Scanner#Scanner(File, String)
0114: */
0115: public void test_ConstructorLjava_io_FileLjava_lang_String()
0116: throws IOException {
0117: File tmpFile = File
0118: .createTempFile("TestFileForScanner", ".tmp");
0119: s = new Scanner(tmpFile, Charset.defaultCharset().name());
0120: assertNotNull(s);
0121: s.close();
0122: assertTrue(tmpFile.delete());
0123:
0124: try {
0125: s = new Scanner(tmpFile, Charset.defaultCharset().name());
0126: fail("should throw FileNotFoundException");
0127: } catch (FileNotFoundException e) {
0128: // expected
0129: }
0130:
0131: try {
0132: s = new Scanner(tmpFile, null);
0133: fail("should throw FileNotFoundException");
0134: } catch (FileNotFoundException e) {
0135: // expected
0136: }
0137:
0138: tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
0139: try {
0140: s = new Scanner(tmpFile, "invalid charset");
0141: fail("should throw IllegalArgumentException");
0142: } catch (IllegalArgumentException e) {
0143: // expected
0144: }
0145:
0146: //fail on RI. File is opened but not closed when exception is thrown on
0147: // RI.
0148: assertTrue(tmpFile.delete());
0149:
0150: // Scanner(File = null, Charset = null)
0151: try {
0152: s = new Scanner((File) null, null);
0153: fail("Should throw NullPointerException");
0154: } catch (NullPointerException e) {
0155: // expected
0156: }
0157:
0158: // Scanner(File = null, Charset = UTF-8)
0159: try {
0160: s = new Scanner((File) null, "UTF-8");
0161: fail("Should throw NullPointerException");
0162: } catch (NullPointerException e) {
0163: // expected
0164: }
0165:
0166: // Scanner(File = null, Charset = invalid)
0167: try {
0168: s = new Scanner((File) null, "invalid");
0169: fail("Should throw NullPointerException");
0170: } catch (NullPointerException e) {
0171: // expected
0172: }
0173:
0174: // Scanner(File, Charset = null)
0175: try {
0176: File f = File.createTempFile("test", ".tmp");
0177: s = new Scanner(f, null);
0178: fail("Should throw IllegalArgumentException");
0179: } catch (IllegalArgumentException e) {
0180: // expected
0181: }
0182:
0183: // TODO: test if the specified charset is used.
0184: }
0185:
0186: /**
0187: * @tests java.util.Scanner#Scanner(InputStream)
0188: */
0189: public void test_ConstructorLjava_io_InputStream() {
0190: s = new Scanner(new PipedInputStream());
0191: assertNotNull(s);
0192: s.close();
0193:
0194: // Scanner(InputStream)
0195: try {
0196: s = new Scanner((InputStream) null);
0197: fail("Should throw NullPointerException");
0198: } catch (NullPointerException e) {
0199: // expected
0200: }
0201:
0202: // TODO: test if the default charset is used.
0203: }
0204:
0205: /**
0206: * @tests java.util.Scanner#Scanner(InputStream, String)
0207: */
0208: public void test_ConstructorLjava_io_InputStreamLjava_lang_String() {
0209: s = new Scanner(new PipedInputStream(), Charset
0210: .defaultCharset().name());
0211: assertNotNull(s);
0212: s.close();
0213:
0214: try {
0215: s = new Scanner((PipedInputStream) null, "invalid charset");
0216: fail("should throw NullPointerException");
0217: } catch (NullPointerException e) {
0218: // expected
0219: }
0220:
0221: try {
0222: s = new Scanner(new PipedInputStream(), null);
0223: fail("should throw NullPointerException");
0224: } catch (NullPointerException e) {
0225: // expected
0226: }
0227:
0228: try {
0229: s = new Scanner(new PipedInputStream(), "invalid charset");
0230: fail("should throw IllegalArgumentException");
0231: } catch (IllegalArgumentException e) {
0232: // expected
0233: }
0234:
0235: // TODO: test if the specified charset is used.
0236: }
0237:
0238: /**
0239: * @tests java.util.Scanner#Scanner(Readable)
0240: */
0241: public void test_ConstructorLjava_lang_Readable() {
0242: s = new Scanner(new StringReader("test string"));
0243: assertNotNull(s);
0244: s.close();
0245:
0246: // Scanner(Readable)
0247: try {
0248: s = new Scanner((Readable) null);
0249: fail("Should throw NullPointerException");
0250: } catch (NullPointerException e) {
0251: // expected
0252: }
0253: }
0254:
0255: /**
0256: * @tests java.util.Scanner#Scanner(ReadableByteChannel)
0257: */
0258: public void test_ConstructorLjava_nio_channels_ReadableByteChannel()
0259: throws IOException {
0260: File tmpFile = File
0261: .createTempFile("TestFileForScanner", ".tmp");
0262: FileChannel fc = new FileOutputStream(tmpFile).getChannel();
0263: s = new Scanner(fc);
0264: assertNotNull(s);
0265: s.close();
0266: assertTrue(tmpFile.delete());
0267:
0268: // Scanner(ReadableByteChannel)
0269: try {
0270: s = new Scanner((ReadableByteChannel) null);
0271: fail("Should throw NullPointerException");
0272: } catch (NullPointerException e) {
0273: // expected
0274: }
0275:
0276: // Test if the default charset is used.
0277: String sampleData = "1 2 3 4 5 6 7 8 9 10";
0278: File tempFile = File.createTempFile("harmony", "test");
0279: tempFile.deleteOnExit();
0280: FileOutputStream os = new FileOutputStream(tempFile);
0281: os.write(sampleData.getBytes());
0282: os.close();
0283:
0284: FileInputStream is = new FileInputStream(tempFile);
0285: FileChannel channel = is.getChannel();
0286:
0287: Scanner s = new Scanner(channel);
0288: int count = 0;
0289: while (s.hasNextInt()) {
0290: s.nextInt();
0291: count++;
0292: }
0293: channel.close();
0294: assertEquals(10, count);
0295: }
0296:
0297: /**
0298: * @tests java.util.Scanner#Scanner(ReadableByteChannel, String)
0299: */
0300: public void test_ConstructorLjava_nio_channels_ReadableByteChannelLjava_lang_String()
0301: throws IOException {
0302: File tmpFile = File
0303: .createTempFile("TestFileForScanner", ".tmp");
0304: FileChannel fc = new FileOutputStream(tmpFile).getChannel();
0305: s = new Scanner(fc, Charset.defaultCharset().name());
0306: assertNotNull(s);
0307: s.close();
0308:
0309: fc = new FileOutputStream(tmpFile).getChannel();
0310: try {
0311: s = new Scanner(fc, "invalid charset");
0312: fail("should throw IllegalArgumentException");
0313: } catch (IllegalArgumentException e) {
0314: // expected
0315: }
0316: fc.close();
0317: assertTrue(tmpFile.delete());
0318:
0319: // Scanner(ReadableByteChannel = null, Charset = null)
0320: try {
0321: s = new Scanner((ReadableByteChannel) null, null);
0322: fail("Should throw NullPointerException");
0323: } catch (NullPointerException e) {
0324: // expected
0325: }
0326:
0327: // Scanner(ReadableByteChannel = null, Charset = invalid)
0328: try {
0329: s = new Scanner((ReadableByteChannel) null, "invalid");
0330: fail("Should throw NullPointerException");
0331: } catch (NullPointerException e) {
0332: // expected
0333: }
0334:
0335: // Scanner(ReadableByteChannel, Charset = null)
0336: try {
0337: s = new Scanner(fc, null);
0338: fail("Should throw IllegalArgumentException");
0339: } catch (IllegalArgumentException e) {
0340: // expected
0341: }
0342: // TODO: test if the specified charset is used.
0343: }
0344:
0345: /**
0346: * @tests java.util.Scanner#Scanner(String)
0347: */
0348: public void test_ConstructorLjava_lang_String() {
0349: s = new Scanner("test string");
0350: assertNotNull(s);
0351: s.close();
0352:
0353: // Scanner(String)
0354: try {
0355: s = new Scanner((String) null);
0356: fail("Should throw NullPointerException");
0357: } catch (NullPointerException e) {
0358: // expected
0359: }
0360: }
0361:
0362: /**
0363: * @tests java.util.Scanner#close()
0364: */
0365: public void test_close() throws IOException {
0366: File tmpFile = File
0367: .createTempFile("TestFileForScanner", ".tmp");
0368: FileOutputStream fos = new FileOutputStream(tmpFile);
0369: FileChannel fc = fos.getChannel();
0370: s = new Scanner(fc);
0371:
0372: // Write out a int before the scanner is closed, should be OK.
0373: fos.write(12);
0374:
0375: s.close();
0376: assertFalse(fc.isOpen());
0377:
0378: // Write out a int after the scanner is closed, IOException should be
0379: // thrown out.
0380: try {
0381: fos.write(12);
0382: fail("Should throw IOException");
0383: } catch (IOException e) {
0384: // expected
0385: }
0386:
0387: s.close(); // no exception should be thrown
0388: assertTrue(tmpFile.delete());
0389: }
0390:
0391: /**
0392: * @tests java.util.Scanner#ioException()
0393: */
0394: public void test_ioException() throws IOException {
0395: MockCloseable mc = new MockCloseable();
0396: s = new Scanner(mc);
0397: assertNull(s.ioException()); // No operation, no exception
0398:
0399: s.close(); // IOException should be cached
0400: assertNotNull(s.ioException());
0401: assertTrue(s.ioException() instanceof IOException);
0402: }
0403:
0404: /**
0405: * @tests java.util.Scanner#delimiter()
0406: */
0407: public void test_delimiter() {
0408: s = new Scanner("test");
0409: Pattern pattern = s.delimiter();
0410: assertEquals("\\p{javaWhitespace}+", pattern.toString());
0411: }
0412:
0413: /**
0414: * @tests java.util.Scanner#useDelimiter(Pattern)
0415: */
0416: public void test_useDelimiter_LPattern() {
0417: s = new Scanner("test");
0418: s.useDelimiter(Pattern.compile("\\w+"));
0419: assertEquals("\\w+", s.delimiter().toString());
0420:
0421: s = new Scanner("test");
0422: s.useDelimiter((Pattern) null);
0423: assertNull(s.delimiter());
0424: }
0425:
0426: /**
0427: * @tests java.util.Scanner#useDelimiter(String)
0428: */
0429: public void test_useDelimiter_String() {
0430: s = new Scanner("test");
0431: try {
0432: s.useDelimiter((String) null);
0433: fail("Should throw NullPointerException");
0434: } catch (NullPointerException e) {
0435: // expected
0436: }
0437:
0438: s = new Scanner("test");
0439: s.useDelimiter("\\w+");
0440: assertEquals("\\w+", s.delimiter().toString());
0441: }
0442:
0443: /**
0444: * @tests java.util.Scanner#locale()
0445: */
0446: public void test_locale() {
0447: s = new Scanner("test");
0448: assertEquals(Locale.getDefault(), s.locale());
0449: }
0450:
0451: /**
0452: * @tests java.util.Scanner#useLocale(Locale)
0453: */
0454: public void test_useLocale_LLocale() {
0455: s = new Scanner("test");
0456: try {
0457: s.useLocale(null);
0458: fail("Should throw NullPointerException");
0459: } catch (NullPointerException e) {
0460: // expected
0461: }
0462:
0463: s.useLocale(new Locale("test", "test"));
0464: assertEquals(new Locale("test", "test"), s.locale());
0465: }
0466:
0467: /**
0468: * @tests java.util.Scanner#radix()
0469: */
0470: public void test_radix() {
0471: s = new Scanner("test");
0472: assertEquals(10, s.radix());
0473: }
0474:
0475: /**
0476: * @tests java.util.Scanner#useRadix()
0477: */
0478: public void test_useRadix_I() {
0479: s = new Scanner("test");
0480: try {
0481: s.useRadix(Character.MIN_RADIX - 1);
0482: fail("Should throw IllegalArgumentException");
0483: } catch (IllegalArgumentException e) {
0484: // expected
0485: }
0486: try {
0487: s.useRadix(Character.MAX_RADIX + 1);
0488: fail("Should throw IllegalArgumentException");
0489: } catch (IllegalArgumentException e) {
0490: // expected
0491: }
0492: s.useRadix(11);
0493: assertEquals(11, s.radix());
0494: }
0495:
0496: /**
0497: * @tests java.util.Scanner#remove()
0498: */
0499: public void test_remove() {
0500: s = new Scanner("aab*b*").useDelimiter("\\*");
0501: try {
0502: s.remove();
0503: fail("should throw UnsupportedOperationException");
0504: } catch (UnsupportedOperationException e) {
0505: //Expected
0506: }
0507: }
0508:
0509: /**
0510: * @tests java.util.Scanner#match()
0511: */
0512: public void test_match() {
0513: MatchResult result;
0514: s = new Scanner("1 2 ");
0515: try {
0516: s.match();
0517: fail("should throw IllegalStateException");
0518: } catch (IllegalStateException e) {
0519: // Expected
0520: }
0521: assertEquals("1", s.next());
0522: assertEquals("2", s.next());
0523: result = s.match();
0524: assertEquals(2, result.start());
0525: assertEquals(3, result.end());
0526: assertEquals(2, result.start(0));
0527: assertEquals(3, result.end(0));
0528: assertEquals("2", result.group());
0529: assertEquals("2", result.group(0));
0530: assertEquals(0, result.groupCount());
0531: try {
0532: result.start(1);
0533: fail("should throw IndexOutOfBoundsException");
0534: } catch (IndexOutOfBoundsException e) {
0535: // Expected
0536: }
0537: try {
0538: s.next();
0539: fail("should throw NoSuchElementException");
0540: } catch (NoSuchElementException e) {
0541: // Expected
0542: }
0543: try {
0544: s.match();
0545: fail("should throw IllegalStateException");
0546: } catch (IllegalStateException e) {
0547: // Expected
0548: }
0549:
0550: s = new Scanner("True faLse");
0551: try {
0552: s.match();
0553: fail("should throw IllegalStateException");
0554: } catch (IllegalStateException e) {
0555: // Expected
0556: }
0557: assertTrue(s.nextBoolean());
0558: result = s.match();
0559: assertEquals(0, result.start());
0560: assertEquals(4, result.end());
0561: assertEquals(0, result.start(0));
0562: assertEquals(4, result.end(0));
0563: assertEquals("True", result.group());
0564: assertEquals(0, result.groupCount());
0565: assertFalse(s.nextBoolean());
0566: try {
0567: s.nextBoolean();
0568: fail("should throw NoSuchElementException");
0569: } catch (NoSuchElementException e) {
0570: // Expected
0571: }
0572: try {
0573: s.match();
0574: fail("should throw IllegalStateException");
0575: } catch (IllegalStateException e) {
0576: // Expected
0577: }
0578:
0579: s = new Scanner("True faLse");
0580: assertTrue(s.nextBoolean());
0581: result = s.match();
0582: assertEquals(0, result.start());
0583: assertEquals(4, result.end());
0584: assertEquals(0, result.start(0));
0585: assertEquals(4, result.end(0));
0586: assertEquals("True", result.group());
0587: assertEquals(0, result.groupCount());
0588: s.close();
0589: try {
0590: s.nextBoolean();
0591: fail("should throw IllegalStateException");
0592: } catch (IllegalStateException e) {
0593: // Expected
0594: }
0595: result = s.match();
0596: assertEquals(0, result.start());
0597: assertEquals(4, result.end());
0598: assertEquals(0, result.start(0));
0599: assertEquals(4, result.end(0));
0600: assertEquals("True", result.group());
0601: assertEquals(0, result.groupCount());
0602:
0603: s = new Scanner("True fase");
0604: assertTrue(s.nextBoolean());
0605: assertEquals(0, result.groupCount());
0606: try {
0607: s.nextBoolean();
0608: fail("Should throw InputMismatchException");
0609: } catch (InputMismatchException e) {
0610: // expected
0611: }
0612: try {
0613: s.match();
0614: fail("should throw IllegalStateException");
0615: } catch (IllegalStateException e) {
0616: // Expected
0617: }
0618:
0619: s = new Scanner("True fase");
0620: assertTrue(s.nextBoolean());
0621: try {
0622: s.next((Pattern) null);
0623: fail("Should throw NullPointerException");
0624: } catch (NullPointerException e) {
0625: // Expected
0626: }
0627: result = s.match();
0628: assertEquals(0, result.start());
0629: assertEquals(4, result.end());
0630: assertEquals(0, result.start(0));
0631: assertEquals(4, result.end(0));
0632: assertEquals("True", result.group());
0633: assertEquals(0, result.groupCount());
0634:
0635: }
0636:
0637: /**
0638: * @throws IOException
0639: * @tests java.util.Scanner#next()
0640: */
0641: public void test_next() throws IOException {
0642: // use special delimiter
0643: s = new Scanner("1**2").useDelimiter("\\*");
0644: assertEquals("1", s.next());
0645: assertEquals("", s.next());
0646: assertEquals("2", s.next());
0647:
0648: s = new Scanner(" \t 1 \t 2").useDelimiter("\\s*");
0649: assertEquals("1", s.next());
0650: assertEquals("2", s.next());
0651: try {
0652: s.next();
0653: fail("should throw NoSuchElementException");
0654: } catch (NoSuchElementException e) {
0655: // Expected
0656: }
0657:
0658: s = new Scanner("a").useDelimiter("a?");
0659: try {
0660: s.next();
0661: fail("should throw NoSuchElementException");
0662: } catch (NoSuchElementException e) {
0663: // Expected
0664: }
0665:
0666: s = new Scanner("aa").useDelimiter("a?");
0667: assertEquals("", s.next());
0668: try {
0669: s.next();
0670: fail("should throw NoSuchElementException");
0671: } catch (NoSuchElementException e) {
0672: // Expected
0673: }
0674:
0675: s = new Scanner("word( )test( )").useDelimiter("\\( \\)");
0676: assertEquals("word", s.next());
0677: assertEquals("test", s.next());
0678:
0679: s = new Scanner("? next ").useDelimiter("( )");
0680: assertEquals("?", s.next());
0681: assertEquals("next", s.next());
0682: assertEquals("", s.next());
0683:
0684: s = new Scanner("word1 word2 ");
0685: assertEquals("word1", s.next());
0686: assertEquals("word2", s.next());
0687: // test boundary case
0688: try {
0689: s.next();
0690: fail("should throw NoSuchElementException");
0691: } catch (NoSuchElementException e) {
0692: // Expected
0693: }
0694:
0695: // just delimiter exists in this scanner
0696: s = new Scanner(" ");
0697: try {
0698: s.next();
0699: fail("Should throw NoSuchElementException");
0700: } catch (NoSuchElementException e) {
0701: // Expected
0702: }
0703:
0704: // nothing exists in this scanner
0705: s = new Scanner("");
0706: try {
0707: s.next();
0708: fail("Should throw NoSuchElementException");
0709: } catch (NoSuchElementException e) {
0710: // Expected
0711: }
0712:
0713: // no delimiter exists in this scanner
0714: s = new Scanner("test");
0715: assertEquals("test", s.next());
0716:
0717: // input resourse starts with delimiter
0718: s = new Scanner(" test");
0719: assertEquals("test", s.next());
0720:
0721: // input resource ends with delimiter
0722: s = new Scanner(" test ");
0723: assertEquals("test", s.next());
0724:
0725: // Harmony uses 1024 as default buffer size,
0726: // What if a sentence can not be read in all in once.
0727: StringBuilder longSentence = new StringBuilder(1025);
0728: for (int i = 0; i < 11; i++) {
0729: longSentence.append(" ");
0730: }
0731: for (int i = 11; i < 1026; i++) {
0732: longSentence.append("a");
0733: }
0734: s = new Scanner(longSentence.toString());
0735: assertEquals(longSentence.toString().trim(), s.next());
0736:
0737: s = new Scanner(" test test");
0738: assertEquals("test", s.next());
0739: assertEquals("test", s.next());
0740:
0741: // What if use a delimiter of length 0.
0742: s = new Scanner("test\ntest").useDelimiter(Pattern.compile("^",
0743: Pattern.MULTILINE));
0744: assertEquals("test\n", s.next());
0745: assertEquals("test", s.next());
0746:
0747: s = new Scanner("").useDelimiter(Pattern.compile("^",
0748: Pattern.MULTILINE));
0749: try {
0750: s.next();
0751: fail("should throw NoSuchElementException");
0752: } catch (NoSuchElementException e) {
0753: // Expected
0754: }
0755:
0756: s = new Scanner("").useDelimiter(Pattern.compile("^*",
0757: Pattern.MULTILINE));
0758: try {
0759: s.next();
0760: fail("should throw NoSuchElementException");
0761: } catch (NoSuchElementException e) {
0762: // Expected
0763: }
0764:
0765: s = new Scanner("test\ntest").useDelimiter(Pattern.compile(
0766: "^*", Pattern.MULTILINE));
0767: assertEquals("t", s.next());
0768: assertEquals("e", s.next());
0769:
0770: s = new Scanner("\ntest\ntest").useDelimiter(Pattern.compile(
0771: "$", Pattern.MULTILINE));
0772: assertEquals("\ntest", s.next());
0773: assertEquals("\ntest", s.next());
0774:
0775: // test socket inputStream
0776: // Harmony uses 1024 as default buffer size,
0777: // what if the leading delimiter is larger than 1023
0778: for (int i = 0; i < 1024; i++) {
0779: os.write(" ".getBytes());
0780: }
0781: os.write(" 1 2 ".getBytes());
0782: s = new Scanner(client);
0783: assertEquals("1", s.next());
0784: assertEquals("2", s.next());
0785: os.write(" 1 2".getBytes());
0786: serverSocket.close();
0787: assertEquals("1", s.next());
0788: assertEquals("2", s.next());
0789: try {
0790: s.next();
0791: fail("should throw NoSuchElementException");
0792: } catch (NoSuchElementException e) {
0793: // Expected
0794: }
0795:
0796: }
0797:
0798: /**
0799: * @throws IOException
0800: * @tests java.util.Scanner#next(Pattern)
0801: */
0802: public void test_nextLPattern() throws IOException {
0803: Pattern pattern;
0804: s = new Scanner("aab*2*").useDelimiter("\\*");
0805: pattern = Pattern.compile("a*b");
0806: assertEquals("aab", s.next(pattern));
0807: try {
0808: s.next(pattern);
0809: fail("should throw InputMismatchException");
0810: } catch (InputMismatchException e) {
0811: // Expected
0812: }
0813:
0814: s = new Scanner("word ? ");
0815: pattern = Pattern.compile("\\w+");
0816: assertEquals("word", s.next(pattern));
0817: try {
0818: s.next(pattern);
0819: fail("should throw InputMismatchException");
0820: } catch (InputMismatchException e) {
0821: // Expected
0822: }
0823:
0824: s = new Scanner("word1 word2 ");
0825: pattern = Pattern.compile("\\w+");
0826: assertEquals("word1", s.next(pattern));
0827: assertEquals("word2", s.next(pattern));
0828: // test boundary case
0829: try {
0830: s.next(pattern);
0831: fail("should throw NoSuchElementException");
0832: } catch (NoSuchElementException e) {
0833: // Expected
0834: }
0835:
0836: // test socket inputStream
0837:
0838: os.write("aab 2".getBytes());
0839: serverSocket.close();
0840:
0841: s = new Scanner(client);
0842: pattern = Pattern.compile("a*b");
0843: assertEquals("aab", s.next(pattern));
0844: try {
0845: s.next(pattern);
0846: fail("should throw InputMismatchException");
0847: } catch (InputMismatchException e) {
0848: // Expected
0849: }
0850: }
0851:
0852: /**
0853: * @throws IOException
0854: * @tests java.util.Scanner#next(String)
0855: */
0856: public void test_nextLString() throws IOException {
0857: s = new Scanner("b*a*").useDelimiter("\\*");
0858: assertEquals("b", s.next("a*b"));
0859: try {
0860: s.next("a*b");
0861: fail("should throw InputMismatchException");
0862: } catch (InputMismatchException e) {
0863: // Expected
0864: }
0865:
0866: s = new Scanner("word ? ");
0867: assertEquals("word", s.next("\\w+"));
0868: try {
0869: s.next("\\w+");
0870: fail("should throw InputMismatchException");
0871: } catch (InputMismatchException e) {
0872: // Expected
0873: }
0874:
0875: s = new Scanner("word1 next ");
0876: assertEquals("word1", s.next("\\w+"));
0877: assertEquals("next", s.next("\\w+"));
0878: // test boundary case
0879: try {
0880: s.next("\\w+");
0881: fail("should throw NoSuchElementException");
0882: } catch (NoSuchElementException e) {
0883: // Expected
0884: }
0885:
0886: // test socket inputStream
0887: os.write("aab 2".getBytes());
0888: serverSocket.close();
0889:
0890: s = new Scanner(client);
0891: assertEquals("aab", s.next("a*b"));
0892: try {
0893: s.next("a*b");
0894: fail("should throw InputMismatchException");
0895: } catch (InputMismatchException e) {
0896: // Expected
0897: }
0898: }
0899:
0900: /**
0901: * @throws IOException
0902: * @tests java.util.Scanner#nextBoolean()
0903: */
0904: public void test_nextBoolean() throws IOException {
0905: // case insensitive
0906: s = new Scanner("TRue");
0907: assertTrue(s.nextBoolean());
0908:
0909: s = new Scanner("tRue false");
0910: assertTrue(s.nextBoolean());
0911: assertFalse(s.nextBoolean());
0912: try {
0913: s.nextBoolean();
0914: fail("Should throw NoSuchElementException");
0915: } catch (NoSuchElementException e) {
0916: // Expected
0917: }
0918:
0919: s = new Scanner("true1");
0920: try {
0921: s.nextBoolean();
0922: fail("Should throw InputMismatchException");
0923: } catch (InputMismatchException e) {
0924: // Expected
0925: }
0926:
0927: try {
0928: s = new Scanner("");
0929: s.nextBoolean();
0930: fail("Should throw NoSuchElementException");
0931: } catch (NoSuchElementException e) {
0932: // Expected
0933: }
0934:
0935: // test socket inputStream
0936: os.write("true false".getBytes());
0937: serverSocket.close();
0938:
0939: s = new Scanner(client);
0940: assertTrue(s.nextBoolean());
0941: assertFalse(s.nextBoolean());
0942:
0943: // ues '*' as delimiter
0944: s = new Scanner("true**false").useDelimiter("\\*");
0945: assertTrue(s.nextBoolean());
0946: try {
0947: s.nextBoolean();
0948: fail("should throw NoSuchElementException");
0949: } catch (NoSuchElementException e) {
0950: // Expected
0951: }
0952:
0953: s = new Scanner("false( )").useDelimiter("\\( \\)");
0954: assertFalse(s.nextBoolean());
0955:
0956: }
0957:
0958: /**
0959: * @throws IOException
0960: * @tests java.util.Scanner#nextInt(int)
0961: */
0962: public void test_nextIntI() throws IOException {
0963: s = new Scanner("123 456");
0964: assertEquals(123, s.nextInt(10));
0965: assertEquals(456, s.nextInt(10));
0966: try {
0967: s.nextInt(10);
0968: fail("Should throw NoSuchElementException");
0969: } catch (NoSuchElementException e) {
0970: // Expected
0971: }
0972:
0973: // If the radix is different from 10
0974: s = new Scanner("123 456");
0975: assertEquals(38, s.nextInt(5));
0976: try {
0977: s.nextInt(5);
0978: fail("Should throw InputMismatchException");
0979: } catch (InputMismatchException e) {
0980: // Expected
0981: }
0982:
0983: // If the number is out of range
0984: s = new Scanner("123456789123456789123456789123456789");
0985: try {
0986: s.nextInt(10);
0987: fail("Should throw InputMismatchException");
0988: } catch (InputMismatchException e) {
0989: // Expected
0990: }
0991:
0992: /*
0993: * Different locale can only recognize corresponding locale sensitive
0994: * string. ',' is used in many locales as group separator.
0995: */
0996: s = new Scanner("23,456 23,456");
0997: s.useLocale(Locale.GERMANY);
0998: try {
0999: s.nextInt(10);
1000: fail("Should throw InputMismatchException");
1001: } catch (InputMismatchException e) {
1002: // expected
1003: }
1004: s.useLocale(Locale.ENGLISH);
1005: // If exception is thrown out, input will not be advanced.
1006: assertEquals(23456, s.nextInt(10));
1007: assertEquals(23456, s.nextInt(10));
1008:
1009: /*
1010: * ''' is used in many locales as group separator.
1011: */
1012: s = new Scanner("23'456 23'456");
1013: s.useLocale(Locale.GERMANY);
1014: try {
1015: s.nextInt(10);
1016: fail("Should throw InputMismatchException");
1017: } catch (InputMismatchException e) {
1018: // expected
1019: }
1020: s.useLocale(new Locale("it", "CH"));
1021: // If exception is thrown out, input will not be advanced.
1022: assertEquals(23456, s.nextInt(10));
1023: assertEquals(23456, s.nextInt(10));
1024:
1025: /*
1026: * The input string has Arabic-Indic digits.
1027: */
1028: s = new Scanner("1\u06602 1\u06662");
1029: assertEquals(102, s.nextInt(10));
1030: try {
1031: s.nextInt(5);
1032: fail("Should throw InputMismatchException");
1033: } catch (InputMismatchException e) {
1034: // Expected
1035: }
1036: assertEquals(162, s.nextInt(10));
1037:
1038: /*
1039: * '.' is used in many locales as group separator. The input string
1040: * has Arabic-Indic digits .
1041: */
1042: s = new Scanner("23.45\u0666 23.456");
1043: s.useLocale(Locale.CHINESE);
1044: try {
1045: s.nextInt(10);
1046: fail("Should throw InputMismatchException");
1047: } catch (InputMismatchException e) {
1048: // expected
1049: }
1050: s.useLocale(Locale.GERMANY);
1051: // If exception is thrown out, input will not be advanced.
1052: assertEquals(23456, s.nextInt(10));
1053: assertEquals(23456, s.nextInt(10));
1054:
1055: // The input string starts with zero
1056: s = new Scanner("03,456");
1057: s.useLocale(Locale.ENGLISH);
1058: try {
1059: s.nextInt(10);
1060: fail("Should throw InputMismatchException");
1061: } catch (InputMismatchException e) {
1062: // expected
1063: }
1064:
1065: s = new Scanner("03456");
1066: assertEquals(3456, s.nextInt(10));
1067:
1068: s = new Scanner("\u06603,456");
1069: s.useLocale(Locale.ENGLISH);
1070: assertEquals(3456, s.nextInt(10));
1071:
1072: s = new Scanner("E3456");
1073: assertEquals(930902, s.nextInt(16));
1074: // The following test case fails on RI, because RI does not support
1075: // letter as leading digit
1076: s = new Scanner("E3,456");
1077: s.useLocale(Locale.ENGLISH);
1078: assertEquals(930902, s.nextInt(16));
1079:
1080: /*
1081: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
1082: * respectively, but they are not differentiated.
1083: */
1084: s = new Scanner("12300");
1085: s.useLocale(Locale.CHINESE);
1086: assertEquals(12300, s.nextInt(10));
1087:
1088: s = new Scanner("123\u0966\u0966");
1089: s.useLocale(Locale.CHINESE);
1090: assertEquals(12300, s.nextInt(10));
1091:
1092: s = new Scanner("123\u0e50\u0e50");
1093: s.useLocale(Locale.CHINESE);
1094: assertEquals(12300, s.nextInt(10));
1095:
1096: /*
1097: * There are three types of negative prefix all in all. '' '-' '(' There
1098: * are three types of negative suffix all in all. '' '-' ')' '(' and ')'
1099: * must be used togethor. Prefix '-' and suffix '-' must be used
1100: * exclusively.
1101: */
1102:
1103: /*
1104: * According to Integer regular expression: Integer :: = ( [-+]? (*
1105: * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix |
1106: * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be
1107: * recognized by scanner with locale ar_AE, (123) shouble be recognized
1108: * by scanner with locale mk_MK. But this is not the case on RI.
1109: */
1110: s = new Scanner("-123 123- -123-");
1111: s.useLocale(new Locale("ar", "AE"));
1112: assertEquals(-123, s.nextInt(10));
1113: // The following test case fails on RI
1114: assertEquals(-123, s.nextInt(10));
1115: try {
1116: s.nextInt(10);
1117: fail("Should throw InputMismatchException");
1118: } catch (InputMismatchException e) {
1119: // expected
1120: }
1121:
1122: s = new Scanner("-123 123- (123)");
1123: s.useLocale(new Locale("mk", "MK"));
1124: assertEquals(-123, s.nextInt(10));
1125: try {
1126: s.nextInt();
1127: fail("Should throw InputMismatchException");
1128: } catch (InputMismatchException e) {
1129: // expected
1130: }
1131: // Skip the un-recognizable token 123-.
1132: assertEquals("123-", s.next());
1133: // The following test case fails on RI
1134: assertEquals(-123, s.nextInt(10));
1135:
1136: // If the parameter radix is illegal, the following test cases fail on
1137: // RI
1138: try {
1139: s.nextInt(Character.MIN_RADIX - 1);
1140: fail("Should throw IllegalArgumentException");
1141: } catch (IllegalArgumentException e) {
1142: // Expected
1143: }
1144: try {
1145: s.nextInt(Character.MAX_RADIX + 1);
1146: fail("Should throw IllegalArgumentException");
1147: } catch (IllegalArgumentException e) {
1148: // Expected
1149: }
1150: }
1151:
1152: /**
1153: * @throws IOException
1154: * @tests java.util.Scanner#nextInt()
1155: */
1156: public void test_nextInt() throws IOException {
1157: s = new Scanner("123 456");
1158: assertEquals(123, s.nextInt());
1159: assertEquals(456, s.nextInt());
1160: try {
1161: s.nextInt();
1162: fail("Should throw NoSuchElementException");
1163: } catch (NoSuchElementException e) {
1164: // Expected
1165: }
1166:
1167: // If the radix is different from 10
1168: s = new Scanner("123 456");
1169: s.useRadix(5);
1170: assertEquals(38, s.nextInt());
1171: try {
1172: s.nextInt();
1173: fail("Should throw InputMismatchException");
1174: } catch (InputMismatchException e) {
1175: // Expected
1176: }
1177:
1178: // If the number is out of range
1179: s = new Scanner("123456789123456789123456789123456789");
1180: try {
1181: s.nextInt();
1182: fail("Should throw InputMismatchException");
1183: } catch (InputMismatchException e) {
1184: // Expected
1185: }
1186:
1187: /*
1188: * Different locale can only recognize corresponding locale sensitive
1189: * string. ',' is used in many locales as group separator.
1190: */
1191: s = new Scanner("23,456 23,456");
1192: s.useLocale(Locale.GERMANY);
1193: try {
1194: s.nextInt();
1195: fail("Should throw InputMismatchException");
1196: } catch (InputMismatchException e) {
1197: // expected
1198: }
1199: s.useLocale(Locale.ENGLISH);
1200: // If exception is thrown out, input will not be advanced.
1201: assertEquals(23456, s.nextInt());
1202: assertEquals(23456, s.nextInt());
1203:
1204: /*
1205: * ''' is used in many locales as group separator.
1206: */
1207: s = new Scanner("23'456 23'456");
1208: s.useLocale(Locale.GERMANY);
1209: try {
1210: s.nextInt();
1211: fail("Should throw InputMismatchException");
1212: } catch (InputMismatchException e) {
1213: // expected
1214: }
1215: s.useLocale(new Locale("it", "CH"));
1216: // If exception is thrown out, input will not be advanced.
1217: assertEquals(23456, s.nextInt());
1218: assertEquals(23456, s.nextInt());
1219:
1220: /*
1221: * The input string has Arabic-Indic digits.
1222: */
1223: s = new Scanner("1\u06602 1\u06662");
1224: assertEquals(102, s.nextInt());
1225: s.useRadix(5);
1226: try {
1227: s.nextInt();
1228: fail("Should throw InputMismatchException");
1229: } catch (InputMismatchException e) {
1230: // Expected
1231: }
1232: s.useRadix(10);
1233: assertEquals(162, s.nextInt());
1234:
1235: /*
1236: * '.' is used in many locales as group separator. The input string
1237: * has Arabic-Indic digits .
1238: */
1239: s = new Scanner("23.45\u0666 23.456");
1240: s.useLocale(Locale.CHINESE);
1241: try {
1242: s.nextInt();
1243: fail("Should throw InputMismatchException");
1244: } catch (InputMismatchException e) {
1245: // expected
1246: }
1247: s.useLocale(Locale.GERMANY);
1248: // If exception is thrown out, input will not be advanced.
1249: assertEquals(23456, s.nextInt());
1250: assertEquals(23456, s.nextInt());
1251:
1252: // The input string starts with zero
1253: s = new Scanner("03,456");
1254: s.useLocale(Locale.ENGLISH);
1255: try {
1256: s.nextInt();
1257: fail("Should throw InputMismatchException");
1258: } catch (InputMismatchException e) {
1259: // expected
1260: }
1261:
1262: s = new Scanner("03456");
1263: assertEquals(3456, s.nextInt());
1264:
1265: s = new Scanner("\u06603,456");
1266: s.useLocale(Locale.ENGLISH);
1267: assertEquals(3456, s.nextInt());
1268:
1269: s = new Scanner("E3456");
1270: s.useRadix(16);
1271: assertEquals(930902, s.nextInt());
1272:
1273: // The following test case fails on RI, because RI does not support
1274: // letter as leading digit
1275: s = new Scanner("E3,456");
1276: s.useLocale(Locale.ENGLISH);
1277: s.useRadix(16);
1278: assertEquals(930902, s.nextInt());
1279:
1280: /*
1281: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
1282: * respectively, but they are not differentiated.
1283: */
1284: s = new Scanner("12300");
1285: s.useLocale(Locale.CHINESE);
1286: assertEquals(12300, s.nextInt());
1287:
1288: s = new Scanner("123\u0966\u0966");
1289: s.useLocale(Locale.CHINESE);
1290: assertEquals(12300, s.nextInt());
1291:
1292: s = new Scanner("123\u0e50\u0e50");
1293: s.useLocale(Locale.CHINESE);
1294: assertEquals(12300, s.nextInt());
1295:
1296: /*
1297: * There are three types of negative prefix all in all. '' '-' '(' There
1298: * are three types of negative suffix all in all. '' '-' ')' '(' and ')'
1299: * must be used togethor. Prefix '-' and suffix '-' must be used
1300: * exclusively.
1301: */
1302:
1303: /*
1304: * According to Integer regular expression: Integer :: = ( [-+]? (*
1305: * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix |
1306: * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be
1307: * recognized by scanner with locale ar_AE, (123) shouble be recognized
1308: * by scanner with locale mk_MK. But this is not the case on RI.
1309: */
1310: s = new Scanner("-123 123- -123-");
1311: s.useLocale(new Locale("ar", "AE"));
1312: assertEquals(-123, s.nextInt());
1313: // The following test case fails on RI
1314: assertEquals(-123, s.nextInt());
1315: try {
1316: s.nextInt();
1317: fail("Should throw InputMismatchException");
1318: } catch (InputMismatchException e) {
1319: // expected
1320: }
1321:
1322: s = new Scanner("-123 123- (123)");
1323: s.useLocale(new Locale("mk", "MK"));
1324: assertEquals(-123, s.nextInt());
1325: try {
1326: s.nextInt();
1327: fail("Should throw InputMismatchException");
1328: } catch (InputMismatchException e) {
1329: // expected
1330: }
1331: // Skip the un-recognizable token 123-.
1332: assertEquals("123-", s.next());
1333: // The following test case fails on RI
1334: assertEquals(-123, s.nextInt());
1335: }
1336:
1337: /**
1338: * @throws IOException
1339: * @tests java.util.Scanner#nextByte(int)
1340: */
1341: public void test_nextByteI() throws IOException {
1342: s = new Scanner("123 126");
1343: assertEquals(123, s.nextByte(10));
1344: assertEquals(126, s.nextByte(10));
1345: try {
1346: s.nextByte(10);
1347: fail("Should throw NoSuchElementException");
1348: } catch (NoSuchElementException e) {
1349: // Expected
1350: }
1351:
1352: // If the radix is different from 10
1353: s = new Scanner("123 126");
1354: assertEquals(38, s.nextByte(5));
1355: try {
1356: s.nextByte(5);
1357: fail("Should throw InputMismatchException");
1358: } catch (InputMismatchException e) {
1359: // Expected
1360: }
1361:
1362: // If the number is out of range
1363: s = new Scanner("1234");
1364: try {
1365: s.nextByte(10);
1366: fail("Should throw InputMismatchException");
1367: } catch (InputMismatchException e) {
1368: // Expected
1369: }
1370:
1371: /*
1372: * The input string has Arabic-Indic digits.
1373: */
1374: s = new Scanner("1\u06602 12\u0666");
1375: assertEquals(102, s.nextByte(10));
1376: try {
1377: s.nextByte(5);
1378: fail("Should throw InputMismatchException");
1379: } catch (InputMismatchException e) {
1380: // Expected
1381: }
1382: assertEquals(126, s.nextByte(10));
1383:
1384: s = new Scanner("012");
1385: assertEquals(12, s.nextByte(10));
1386:
1387: s = new Scanner("E");
1388: assertEquals(14, s.nextByte(16));
1389:
1390: /*
1391: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
1392: * respectively, but they are not differentiated.
1393: */
1394: s = new Scanner("100");
1395: s.useLocale(Locale.CHINESE);
1396: assertEquals(100, s.nextByte(10));
1397:
1398: s = new Scanner("1\u0966\u0966");
1399: s.useLocale(Locale.CHINESE);
1400: assertEquals(100, s.nextByte(10));
1401:
1402: s = new Scanner("1\u0e50\u0e50");
1403: s.useLocale(Locale.CHINESE);
1404: assertEquals(100, s.nextByte(10));
1405:
1406: s = new Scanner("-123");
1407: s.useLocale(new Locale("ar", "AE"));
1408: assertEquals(-123, s.nextByte(10));
1409:
1410: s = new Scanner("-123");
1411: s.useLocale(new Locale("mk", "MK"));
1412: assertEquals(-123, s.nextByte(10));
1413: }
1414:
1415: /**
1416: * @throws IOException
1417: * @tests java.util.Scanner#nextByte()
1418: */
1419: public void test_nextByte() throws IOException {
1420: s = new Scanner("123 126");
1421: assertEquals(123, s.nextByte());
1422: assertEquals(126, s.nextByte());
1423: try {
1424: s.nextByte();
1425: fail("Should throw NoSuchElementException");
1426: } catch (NoSuchElementException e) {
1427: // Expected
1428: }
1429:
1430: // If the radix is different from 10
1431: s = new Scanner("123 126");
1432: s.useRadix(5);
1433: assertEquals(38, s.nextByte());
1434: try {
1435: s.nextByte();
1436: fail("Should throw InputMismatchException");
1437: } catch (InputMismatchException e) {
1438: // Expected
1439: }
1440:
1441: // If the number is out of range
1442: s = new Scanner("1234");
1443: try {
1444: s.nextByte();
1445: fail("Should throw InputMismatchException");
1446: } catch (InputMismatchException e) {
1447: // Expected
1448: }
1449:
1450: /*
1451: * The input string has Arabic-Indic digits.
1452: */
1453: s = new Scanner("1\u06602 12\u0666");
1454: assertEquals(102, s.nextByte());
1455: s.useRadix(5);
1456: try {
1457: s.nextByte();
1458: fail("Should throw InputMismatchException");
1459: } catch (InputMismatchException e) {
1460: // Expected
1461: }
1462: s.useRadix(10);
1463: assertEquals(126, s.nextByte());
1464:
1465: s = new Scanner("012");
1466: assertEquals(12, s.nextByte());
1467:
1468: s = new Scanner("E");
1469: s.useRadix(16);
1470: assertEquals(14, s.nextByte());
1471:
1472: /*
1473: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
1474: * respectively, but they are not differentiated.
1475: */
1476: s = new Scanner("100");
1477: s.useLocale(Locale.CHINESE);
1478: assertEquals(100, s.nextByte());
1479:
1480: s = new Scanner("1\u0966\u0966");
1481: s.useLocale(Locale.CHINESE);
1482: assertEquals(100, s.nextByte());
1483:
1484: s = new Scanner("1\u0e50\u0e50");
1485: s.useLocale(Locale.CHINESE);
1486: assertEquals(100, s.nextByte());
1487:
1488: s = new Scanner("-123");
1489: s.useLocale(new Locale("ar", "AE"));
1490: assertEquals(-123, s.nextByte());
1491:
1492: s = new Scanner("-123");
1493: s.useLocale(new Locale("mk", "MK"));
1494: assertEquals(-123, s.nextByte());
1495: }
1496:
1497: /**
1498: * @throws IOException
1499: * @tests java.util.Scanner#nextFloat()
1500: */
1501: public void test_nextFloat() throws IOException {
1502: s = new Scanner("123 45\u0666. 123.4 .123 ");
1503: s.useLocale(Locale.ENGLISH);
1504: assertEquals((float) 123.0, s.nextFloat());
1505: assertEquals((float) 456.0, s.nextFloat());
1506: assertEquals((float) 123.4, s.nextFloat());
1507: assertEquals((float) 0.123, s.nextFloat());
1508: try {
1509: s.nextFloat();
1510: fail("Should throw NoSuchElementException");
1511: } catch (NoSuchElementException e) {
1512: // Expected
1513: }
1514:
1515: s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
1516: s.useLocale(Locale.ENGLISH);
1517: assertEquals((float) 123.4, s.nextFloat());
1518: assertEquals((float) -456.7, s.nextFloat());
1519: assertEquals((float) 123456.789, s.nextFloat());
1520: try {
1521: s.nextFloat();
1522: fail("Should throw InputMismatchException");
1523: } catch (InputMismatchException e) {
1524: // Expected
1525: }
1526:
1527: // Scientific notation
1528: s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
1529: s.useLocale(Locale.ENGLISH);
1530: assertEquals((float) 1.234E12, s.nextFloat());
1531: assertEquals((float) -4.567E14, s.nextFloat());
1532: assertEquals((float) 1.23456789E-5, s.nextFloat());
1533:
1534: s = new Scanner("NaN Infinity -Infinity");
1535: assertEquals(Float.NaN, s.nextFloat());
1536: assertEquals(Float.POSITIVE_INFINITY, s.nextFloat());
1537: assertEquals(Float.NEGATIVE_INFINITY, s.nextFloat());
1538:
1539: String str = String.valueOf(Float.MAX_VALUE * 2);
1540: s = new Scanner(str);
1541: assertEquals(Float.POSITIVE_INFINITY, s.nextFloat());
1542:
1543: /*
1544: * Different locale can only recognize corresponding locale sensitive
1545: * string. ',' is used in many locales as group separator.
1546: */
1547: s = new Scanner("23,456 23,456");
1548: s.useLocale(Locale.ENGLISH);
1549: assertEquals((float) 23456.0, s.nextFloat());
1550: s.useLocale(Locale.GERMANY);
1551: assertEquals((float) 23.456, s.nextFloat());
1552:
1553: s = new Scanner("23.456 23.456");
1554: s.useLocale(Locale.ENGLISH);
1555: assertEquals((float) 23.456, s.nextFloat());
1556: s.useLocale(Locale.GERMANY);
1557: assertEquals((float) 23456.0, s.nextFloat());
1558:
1559: s = new Scanner("23,456.7 23.456,7");
1560: s.useLocale(Locale.ENGLISH);
1561: assertEquals((float) 23456.7, s.nextFloat());
1562: s.useLocale(Locale.GERMANY);
1563: assertEquals((float) 23456.7, s.nextFloat());
1564:
1565: s = new Scanner("-123.4 123.4- -123.4-");
1566: s.useLocale(new Locale("ar", "AE"));
1567: assertEquals((float) -123.4, s.nextFloat());
1568: //The following test case fails on RI
1569: assertEquals((float) -123.4, s.nextFloat());
1570: try {
1571: s.nextFloat();
1572: fail("Should throw InputMismatchException");
1573: } catch (InputMismatchException e) {
1574: // Expected
1575: }
1576:
1577: s = new Scanner("(123) 123- -123");
1578: s.useLocale(new Locale("mk", "MK"));
1579: assertEquals((float) -123.0, s.nextFloat());
1580: try {
1581: s.nextFloat();
1582: fail("Should throw InputMismatchException");
1583: } catch (InputMismatchException e) {
1584: // Expected
1585: }
1586: // Skip the un-recognizable token 123-.
1587: assertEquals("123-", s.next());
1588: assertEquals((float) -123.0, s.nextFloat());
1589:
1590: }
1591:
1592: /**
1593: * @throws IOException
1594: * @tests java.util.Scanner#nextBigInteger(int)
1595: */
1596: public void test_nextBigIntegerI() throws IOException {
1597: s = new Scanner("123 456");
1598: assertEquals(new BigInteger("123"), s.nextBigInteger(10));
1599: assertEquals(new BigInteger("456"), s.nextBigInteger(10));
1600: try {
1601: s.nextBigInteger(10);
1602: fail("Should throw NoSuchElementException");
1603: } catch (NoSuchElementException e) {
1604: // Expected
1605: }
1606:
1607: // If the radix is different from 10
1608: s = new Scanner("123 456");
1609: assertEquals(new BigInteger("38"), s.nextBigInteger(5));
1610: try {
1611: s.nextBigInteger(5);
1612: fail("Should throw InputMismatchException");
1613: } catch (InputMismatchException e) {
1614: // Expected
1615: }
1616:
1617: /*
1618: * Different locale can only recognize corresponding locale sensitive
1619: * string. ',' is used in many locales as group separator.
1620: */
1621: s = new Scanner("23,456 23,456");
1622: s.useLocale(Locale.GERMANY);
1623: try {
1624: s.nextBigInteger(10);
1625: fail("Should throw InputMismatchException");
1626: } catch (InputMismatchException e) {
1627: // Expected
1628: }
1629: s.useLocale(Locale.ENGLISH);
1630: // If exception is thrown out, input will not be advanced.
1631: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
1632: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
1633:
1634: /*
1635: * ''' is used in many locales as group separator.
1636: */
1637: s = new Scanner("23'456 23'456");
1638: s.useLocale(Locale.GERMANY);
1639: try {
1640: s.nextBigInteger(10);
1641: fail("Should throw InputMismatchException");
1642: } catch (InputMismatchException e) {
1643: // Expected
1644: }
1645: s.useLocale(new Locale("it", "CH"));
1646: // If exception is thrown out, input will not be advanced.
1647: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
1648: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
1649:
1650: /*
1651: * The input string has Arabic-Indic digits.
1652: */
1653: s = new Scanner("1\u06602 1\u06662");
1654: assertEquals(new BigInteger("102"), s.nextBigInteger(10));
1655: try {
1656: s.nextBigInteger(5);
1657: fail("Should throw InputMismatchException");
1658: } catch (InputMismatchException e) {
1659: // Expected
1660: }
1661: assertEquals(new BigInteger("162"), s.nextBigInteger(10));
1662:
1663: /*
1664: * '.' is used in many locales as group separator. The input string
1665: * has Arabic-Indic digits .
1666: */
1667: s = new Scanner("23.45\u0666 23.456");
1668: s.useLocale(Locale.CHINESE);
1669: try {
1670: s.nextBigInteger(10);
1671: fail("Should throw InputMismatchException");
1672: } catch (InputMismatchException e) {
1673: // Expected
1674: }
1675: s.useLocale(Locale.GERMANY);
1676: // If exception is thrown out, input will not be advanced.
1677: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
1678: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
1679:
1680: // The input string starts with zero
1681: s = new Scanner("03,456");
1682: s.useLocale(Locale.ENGLISH);
1683: try {
1684: s.nextBigInteger(10);
1685: fail("Should throw InputMismatchException");
1686: } catch (InputMismatchException e) {
1687: // Expected
1688: }
1689:
1690: s = new Scanner("03456");
1691: assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
1692:
1693: s = new Scanner("\u06603,456");
1694: s.useLocale(Locale.ENGLISH);
1695: assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
1696:
1697: s = new Scanner("E34");
1698: assertEquals(new BigInteger("3636"), s.nextBigInteger(16));
1699:
1700: /*
1701: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
1702: * respectively, but they are not differentiated.
1703: */
1704: s = new Scanner("12300");
1705: s.useLocale(Locale.CHINESE);
1706: assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
1707:
1708: s = new Scanner("123\u0966\u0966");
1709: s.useLocale(Locale.CHINESE);
1710: assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
1711:
1712: s = new Scanner("123\u0e50\u0e50");
1713: s.useLocale(Locale.CHINESE);
1714: assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
1715:
1716: s = new Scanner("-123");
1717: s.useLocale(new Locale("ar", "AE"));
1718: assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
1719:
1720: s = new Scanner("-123");
1721: s.useLocale(new Locale("mk", "MK"));
1722: assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
1723: }
1724:
1725: /**
1726: * @throws IOException
1727: * @tests java.util.Scanner#nextBigInteger()
1728: */
1729: public void test_nextBigInteger() throws IOException {
1730: s = new Scanner("123 456");
1731: assertEquals(new BigInteger("123"), s.nextBigInteger());
1732: assertEquals(new BigInteger("456"), s.nextBigInteger());
1733: try {
1734: s.nextBigInteger();
1735: fail("Should throw NoSuchElementException");
1736: } catch (NoSuchElementException e) {
1737: // Expected
1738: }
1739:
1740: // If the radix is different from 10
1741: s = new Scanner("123 456");
1742: s.useRadix(5);
1743: assertEquals(new BigInteger("38"), s.nextBigInteger());
1744: try {
1745: s.nextBigInteger();
1746: fail("Should throw InputMismatchException");
1747: } catch (InputMismatchException e) {
1748: // Expected
1749: }
1750:
1751: /*
1752: * Different locale can only recognize corresponding locale sensitive
1753: * string. ',' is used in many locales as group separator.
1754: */
1755: s = new Scanner("23,456 23,456");
1756: s.useLocale(Locale.GERMANY);
1757: try {
1758: s.nextBigInteger();
1759: fail("Should throw InputMismatchException");
1760: } catch (InputMismatchException e) {
1761: // Expected
1762: }
1763: s.useLocale(Locale.ENGLISH);
1764: // If exception is thrown out, input will not be advanced.
1765: assertEquals(new BigInteger("23456"), s.nextBigInteger());
1766: assertEquals(new BigInteger("23456"), s.nextBigInteger());
1767:
1768: /*
1769: * ''' is used in many locales as group separator.
1770: */
1771: s = new Scanner("23'456 23'456");
1772: s.useLocale(Locale.GERMANY);
1773: try {
1774: s.nextBigInteger();
1775: fail("Should throw InputMismatchException");
1776: } catch (InputMismatchException e) {
1777: // Expected
1778: }
1779: s.useLocale(new Locale("it", "CH"));
1780: // If exception is thrown out, input will not be advanced.
1781: assertEquals(new BigInteger("23456"), s.nextBigInteger());
1782: assertEquals(new BigInteger("23456"), s.nextBigInteger());
1783:
1784: /*
1785: * The input string has Arabic-Indic digits.
1786: */
1787: s = new Scanner("1\u06602 1\u06662");
1788: assertEquals(new BigInteger("102"), s.nextBigInteger());
1789: s.useRadix(5);
1790: try {
1791: s.nextBigInteger();
1792: fail("Should throw InputMismatchException");
1793: } catch (InputMismatchException e) {
1794: // Expected
1795: }
1796: s.useRadix(10);
1797: assertEquals(new BigInteger("162"), s.nextBigInteger());
1798:
1799: /*
1800: * '.' is used in many locales as group separator. The input string
1801: * has Arabic-Indic digits .
1802: */
1803: s = new Scanner("23.45\u0666 23.456");
1804: s.useLocale(Locale.CHINESE);
1805: try {
1806: s.nextBigInteger();
1807: fail("Should throw InputMismatchException");
1808: } catch (InputMismatchException e) {
1809: // Expected
1810: }
1811: s.useLocale(Locale.GERMANY);
1812: // If exception is thrown out, input will not be advanced.
1813: assertEquals(new BigInteger("23456"), s.nextBigInteger());
1814: assertEquals(new BigInteger("23456"), s.nextBigInteger());
1815:
1816: // The input string starts with zero
1817: s = new Scanner("03,456");
1818: s.useLocale(Locale.ENGLISH);
1819: try {
1820: s.nextBigInteger();
1821: fail("Should throw InputMismatchException");
1822: } catch (InputMismatchException e) {
1823: // Expected
1824: }
1825:
1826: s = new Scanner("03456");
1827: assertEquals(new BigInteger("3456"), s.nextBigInteger());
1828:
1829: s = new Scanner("\u06603,456");
1830: s.useLocale(Locale.ENGLISH);
1831: assertEquals(new BigInteger("3456"), s.nextBigInteger());
1832:
1833: s = new Scanner("E34");
1834: s.useRadix(16);
1835: assertEquals(new BigInteger("3636"), s.nextBigInteger());
1836:
1837: /*
1838: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
1839: * respectively, but they are not differentiated.
1840: */
1841: s = new Scanner("12300");
1842: s.useLocale(Locale.CHINESE);
1843: assertEquals(new BigInteger("12300"), s.nextBigInteger());
1844:
1845: s = new Scanner("123\u0966\u0966");
1846: s.useLocale(Locale.CHINESE);
1847: assertEquals(new BigInteger("12300"), s.nextBigInteger());
1848:
1849: s = new Scanner("123\u0e50\u0e50");
1850: s.useLocale(Locale.CHINESE);
1851: assertEquals(new BigInteger("12300"), s.nextBigInteger());
1852:
1853: s = new Scanner("-123");
1854: s.useLocale(new Locale("ar", "AE"));
1855: assertEquals(new BigInteger("-123"), s.nextBigInteger());
1856:
1857: s = new Scanner("-123");
1858: s.useLocale(new Locale("mk", "MK"));
1859: assertEquals(new BigInteger("-123"), s.nextBigInteger());
1860: }
1861:
1862: /**
1863: * @throws IOException
1864: * @tests java.util.Scanner#nextShort(int)
1865: */
1866: public void test_nextShortI() throws IOException {
1867: s = new Scanner("123 456");
1868: assertEquals(123, s.nextShort(10));
1869: assertEquals(456, s.nextShort(10));
1870: try {
1871: s.nextShort(10);
1872: fail("Should throw NoSuchElementException");
1873: } catch (NoSuchElementException e) {
1874: // Expected
1875: }
1876:
1877: // If the radix is different from 10
1878: s = new Scanner("123 456");
1879: assertEquals(38, s.nextShort(5));
1880: try {
1881: s.nextShort(5);
1882: fail("Should throw InputMismatchException");
1883: } catch (InputMismatchException e) {
1884: // Expected
1885: }
1886:
1887: // If the number is out of range
1888: s = new Scanner("123456789");
1889: try {
1890: s.nextShort(10);
1891: fail("Should throw InputMismatchException");
1892: } catch (InputMismatchException e) {
1893: // Expected
1894: }
1895:
1896: /*
1897: * Different locale can only recognize corresponding locale sensitive
1898: * string. ',' is used in many locales as group separator.
1899: */
1900: s = new Scanner("23,456 23,456");
1901: s.useLocale(Locale.GERMANY);
1902: try {
1903: s.nextShort(10);
1904: fail("Should throw InputMismatchException");
1905: } catch (InputMismatchException e) {
1906: // Expected
1907: }
1908: s.useLocale(Locale.ENGLISH);
1909: // If exception is thrown out, input will not be advanced.
1910: assertEquals(23456, s.nextShort(10));
1911: assertEquals(23456, s.nextShort(10));
1912:
1913: /*
1914: * ''' is used in many locales as group separator.
1915: */
1916: s = new Scanner("23'456 23'456");
1917: s.useLocale(Locale.GERMANY);
1918: try {
1919: s.nextShort(10);
1920: fail("Should throw InputMismatchException");
1921: } catch (InputMismatchException e) {
1922: // Expected
1923: }
1924: s.useLocale(new Locale("it", "CH"));
1925: // If exception is thrown out, input will not be advanced.
1926: assertEquals(23456, s.nextShort(10));
1927: assertEquals(23456, s.nextShort(10));
1928:
1929: /*
1930: * The input string has Arabic-Indic digits.
1931: */
1932: s = new Scanner("1\u06602 1\u06662");
1933: assertEquals(102, s.nextShort(10));
1934: try {
1935: s.nextShort(5);
1936: fail("Should throw InputMismatchException");
1937: } catch (InputMismatchException e) {
1938: // Expected
1939: }
1940: assertEquals(162, s.nextShort(10));
1941:
1942: /*
1943: * '.' is used in many locales as group separator. The input string
1944: * has Arabic-Indic digits .
1945: */
1946: s = new Scanner("23.45\u0666 23.456");
1947: s.useLocale(Locale.CHINESE);
1948: try {
1949: s.nextShort(10);
1950: fail("Should throw InputMismatchException");
1951: } catch (InputMismatchException e) {
1952: // Expected
1953: }
1954: s.useLocale(Locale.GERMANY);
1955: // If exception is thrown out, input will not be advanced.
1956: assertEquals(23456, s.nextShort(10));
1957: assertEquals(23456, s.nextShort(10));
1958:
1959: // The input string starts with zero
1960: s = new Scanner("03,456");
1961: s.useLocale(Locale.ENGLISH);
1962: try {
1963: s.nextShort(10);
1964: fail("Should throw InputMismatchException");
1965: } catch (InputMismatchException e) {
1966: // Expected
1967: }
1968:
1969: s = new Scanner("03456");
1970: assertEquals(3456, s.nextShort(10));
1971:
1972: s = new Scanner("\u06603,456");
1973: s.useLocale(Locale.ENGLISH);
1974: assertEquals(3456, s.nextShort(10));
1975:
1976: s = new Scanner("E34");
1977: assertEquals(3636, s.nextShort(16));
1978:
1979: /*
1980: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
1981: * respectively, but they are not differentiated.
1982: */
1983: s = new Scanner("12300");
1984: s.useLocale(Locale.CHINESE);
1985: assertEquals(12300, s.nextShort(10));
1986:
1987: s = new Scanner("123\u0966\u0966");
1988: s.useLocale(Locale.CHINESE);
1989: assertEquals(12300, s.nextShort(10));
1990:
1991: s = new Scanner("123\u0e50\u0e50");
1992: s.useLocale(Locale.CHINESE);
1993: assertEquals(12300, s.nextShort(10));
1994:
1995: s = new Scanner("-123");
1996: s.useLocale(new Locale("ar", "AE"));
1997: assertEquals(-123, s.nextShort(10));
1998:
1999: s = new Scanner("-123");
2000: s.useLocale(new Locale("mk", "MK"));
2001: assertEquals(-123, s.nextShort(10));
2002: }
2003:
2004: /**
2005: * @throws IOException
2006: * @tests java.util.Scanner#nextShort()
2007: */
2008: public void test_nextShort() throws IOException {
2009: s = new Scanner("123 456");
2010: assertEquals(123, s.nextShort());
2011: assertEquals(456, s.nextShort());
2012: try {
2013: s.nextShort();
2014: fail("Should throw NoSuchElementException");
2015: } catch (NoSuchElementException e) {
2016: // Expected
2017: }
2018:
2019: // If the radix is different from 10
2020: s = new Scanner("123 456");
2021: s.useRadix(5);
2022: assertEquals(38, s.nextShort());
2023: try {
2024: s.nextShort();
2025: fail("Should throw InputMismatchException");
2026: } catch (InputMismatchException e) {
2027: // Expected
2028: }
2029:
2030: // If the number is out of range
2031: s = new Scanner("123456789");
2032: try {
2033: s.nextShort();
2034: fail("Should throw InputMismatchException");
2035: } catch (InputMismatchException e) {
2036: // Expected
2037: }
2038:
2039: /*
2040: * Different locale can only recognize corresponding locale sensitive
2041: * string. ',' is used in many locales as group separator.
2042: */
2043: s = new Scanner("23,456 23,456");
2044: s.useLocale(Locale.GERMANY);
2045: try {
2046: s.nextShort();
2047: fail("Should throw InputMismatchException");
2048: } catch (InputMismatchException e) {
2049: // Expected
2050: }
2051: s.useLocale(Locale.ENGLISH);
2052: // If exception is thrown out, input will not be advanced.
2053: assertEquals(23456, s.nextShort());
2054: assertEquals(23456, s.nextShort());
2055:
2056: /*
2057: * ''' is used in many locales as group separator.
2058: */
2059: s = new Scanner("23'456 23'456");
2060: s.useLocale(Locale.GERMANY);
2061: try {
2062: s.nextShort();
2063: fail("Should throw InputMismatchException");
2064: } catch (InputMismatchException e) {
2065: // Expected
2066: }
2067: s.useLocale(new Locale("it", "CH"));
2068: // If exception is thrown out, input will not be advanced.
2069: assertEquals(23456, s.nextShort());
2070: assertEquals(23456, s.nextShort());
2071:
2072: /*
2073: * The input string has Arabic-Indic digits.
2074: */
2075: s = new Scanner("1\u06602 1\u06662");
2076: assertEquals(102, s.nextShort());
2077: s.useRadix(5);
2078: try {
2079: s.nextShort();
2080: fail("Should throw InputMismatchException");
2081: } catch (InputMismatchException e) {
2082: // Expected
2083: }
2084: s.useRadix(10);
2085: assertEquals(162, s.nextShort());
2086:
2087: /*
2088: * '.' is used in many locales as group separator. The input string
2089: * has Arabic-Indic digits .
2090: */
2091: s = new Scanner("23.45\u0666 23.456");
2092: s.useLocale(Locale.CHINESE);
2093: try {
2094: s.nextShort();
2095: fail("Should throw InputMismatchException");
2096: } catch (InputMismatchException e) {
2097: // Expected
2098: }
2099: s.useLocale(Locale.GERMANY);
2100: // If exception is thrown out, input will not be advanced.
2101: assertEquals(23456, s.nextShort());
2102: assertEquals(23456, s.nextShort());
2103:
2104: // The input string starts with zero
2105: s = new Scanner("03,456");
2106: s.useLocale(Locale.ENGLISH);
2107: try {
2108: s.nextShort();
2109: fail("Should throw InputMismatchException");
2110: } catch (InputMismatchException e) {
2111: // Expected
2112: }
2113:
2114: s = new Scanner("03456");
2115: assertEquals(3456, s.nextShort());
2116:
2117: s = new Scanner("\u06603,456");
2118: s.useLocale(Locale.ENGLISH);
2119: assertEquals(3456, s.nextShort());
2120:
2121: s = new Scanner("E34");
2122: s.useRadix(16);
2123: assertEquals(3636, s.nextShort());
2124:
2125: /*
2126: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
2127: * respectively, but they are not differentiated.
2128: */
2129: s = new Scanner("12300");
2130: s.useLocale(Locale.CHINESE);
2131: assertEquals(12300, s.nextShort());
2132:
2133: s = new Scanner("123\u0966\u0966");
2134: s.useLocale(Locale.CHINESE);
2135: assertEquals(12300, s.nextShort());
2136:
2137: s = new Scanner("123\u0e50\u0e50");
2138: s.useLocale(Locale.CHINESE);
2139: assertEquals(12300, s.nextShort());
2140:
2141: s = new Scanner("-123");
2142: s.useLocale(new Locale("ar", "AE"));
2143: assertEquals(-123, s.nextShort());
2144:
2145: s = new Scanner("-123");
2146: s.useLocale(new Locale("mk", "MK"));
2147: assertEquals(-123, s.nextShort());
2148: }
2149:
2150: /**
2151: * @throws IOException
2152: * @tests java.util.Scanner#nextLong(int)
2153: */
2154: public void test_nextLongI() throws IOException {
2155: s = new Scanner("123 456");
2156: assertEquals(123, s.nextLong(10));
2157: assertEquals(456, s.nextLong(10));
2158: try {
2159: s.nextLong(10);
2160: fail("Should throw NoSuchElementException");
2161: } catch (NoSuchElementException e) {
2162: // Expected
2163: }
2164:
2165: // If the radix is different from 10
2166: s = new Scanner("123 456");
2167: assertEquals(38, s.nextLong(5));
2168: try {
2169: s.nextLong(5);
2170: fail("Should throw InputMismatchException");
2171: } catch (InputMismatchException e) {
2172: // Expected
2173: }
2174:
2175: // If the number is out of range
2176: s = new Scanner("123456789123456789123456789123456789");
2177: try {
2178: s.nextLong(10);
2179: fail("Should throw InputMismatchException");
2180: } catch (InputMismatchException e) {
2181: // Expected
2182: }
2183:
2184: /*
2185: * Different locale can only recognize corresponding locale sensitive
2186: * string. ',' is used in many locales as group separator.
2187: */
2188: s = new Scanner("23,456 23,456");
2189: s.useLocale(Locale.GERMANY);
2190: try {
2191: s.nextLong(10);
2192: fail("Should throw InputMismatchException");
2193: } catch (InputMismatchException e) {
2194: // Expected
2195: }
2196: s.useLocale(Locale.ENGLISH);
2197: // If exception is thrown out, input will not be advanced.
2198: assertEquals(23456, s.nextLong(10));
2199: assertEquals(23456, s.nextLong(10));
2200:
2201: /*
2202: * ''' is used in many locales as group separator.
2203: */
2204: s = new Scanner("23'456 23'456");
2205: s.useLocale(Locale.GERMANY);
2206: try {
2207: s.nextLong(10);
2208: fail("Should throw InputMismatchException");
2209: } catch (InputMismatchException e) {
2210: // Expected
2211: }
2212: s.useLocale(new Locale("it", "CH"));
2213: // If exception is thrown out, input will not be advanced.
2214: assertEquals(23456, s.nextLong(10));
2215: assertEquals(23456, s.nextLong(10));
2216:
2217: /*
2218: * The input string has Arabic-Indic digits.
2219: */
2220: s = new Scanner("1\u06602 1\u06662");
2221: assertEquals(102, s.nextLong(10));
2222: try {
2223: s.nextLong(5);
2224: fail("Should throw InputMismatchException");
2225: } catch (InputMismatchException e) {
2226: // Expected
2227: }
2228: assertEquals(162, s.nextLong(10));
2229:
2230: /*
2231: * '.' is used in many locales as group separator. The input string
2232: * has Arabic-Indic digits .
2233: */
2234: s = new Scanner("23.45\u0666 23.456");
2235: s.useLocale(Locale.CHINESE);
2236: try {
2237: s.nextLong(10);
2238: fail("Should throw InputMismatchException");
2239: } catch (InputMismatchException e) {
2240: // Expected
2241: }
2242: s.useLocale(Locale.GERMANY);
2243: // If exception is thrown out, input will not be advanced.
2244: assertEquals(23456, s.nextLong(10));
2245: assertEquals(23456, s.nextLong(10));
2246:
2247: // The input string starts with zero
2248: s = new Scanner("03,456");
2249: s.useLocale(Locale.ENGLISH);
2250: try {
2251: s.nextLong(10);
2252: fail("Should throw InputMismatchException");
2253: } catch (InputMismatchException e) {
2254: // Expected
2255: }
2256:
2257: s = new Scanner("03456");
2258: assertEquals(3456, s.nextLong(10));
2259:
2260: s = new Scanner("\u06603,456");
2261: s.useLocale(Locale.ENGLISH);
2262: assertEquals(3456, s.nextLong(10));
2263:
2264: s = new Scanner("E34");
2265: assertEquals(3636, s.nextLong(16));
2266:
2267: /*
2268: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
2269: * respectively, but they are not differentiated.
2270: */
2271: s = new Scanner("12300");
2272: s.useLocale(Locale.CHINESE);
2273: assertEquals(12300, s.nextLong(10));
2274:
2275: s = new Scanner("123\u0966\u0966");
2276: s.useLocale(Locale.CHINESE);
2277: assertEquals(12300, s.nextLong(10));
2278:
2279: s = new Scanner("123\u0e50\u0e50");
2280: s.useLocale(Locale.CHINESE);
2281: assertEquals(12300, s.nextLong(10));
2282:
2283: s = new Scanner("-123");
2284: s.useLocale(new Locale("ar", "AE"));
2285: assertEquals(-123, s.nextLong(10));
2286:
2287: s = new Scanner("-123");
2288: s.useLocale(new Locale("mk", "MK"));
2289: assertEquals(-123, s.nextLong(10));
2290: }
2291:
2292: /**
2293: * @throws IOException
2294: * @tests java.util.Scanner#nextLong()
2295: */
2296: public void test_nextLong() throws IOException {
2297: s = new Scanner("123 456");
2298: assertEquals(123, s.nextLong());
2299: assertEquals(456, s.nextLong());
2300: try {
2301: s.nextLong();
2302: fail("Should throw NoSuchElementException");
2303: } catch (NoSuchElementException e) {
2304: // Expected
2305: }
2306:
2307: // If the radix is different from 10
2308: s = new Scanner("123 456");
2309: s.useRadix(5);
2310: assertEquals(38, s.nextLong());
2311: try {
2312: s.nextLong();
2313: fail("Should throw InputMismatchException");
2314: } catch (InputMismatchException e) {
2315: // Expected
2316: }
2317:
2318: // If the number is out of range
2319: s = new Scanner("123456789123456789123456789123456789");
2320: try {
2321: s.nextLong();
2322: fail("Should throw InputMismatchException");
2323: } catch (InputMismatchException e) {
2324: // Expected
2325: }
2326:
2327: /*
2328: * Different locale can only recognize corresponding locale sensitive
2329: * string. ',' is used in many locales as group separator.
2330: */
2331: s = new Scanner("23,456 23,456");
2332: s.useLocale(Locale.GERMANY);
2333: try {
2334: s.nextLong();
2335: fail("Should throw InputMismatchException");
2336: } catch (InputMismatchException e) {
2337: // Expected
2338: }
2339: s.useLocale(Locale.ENGLISH);
2340: // If exception is thrown out, input will not be advanced.
2341: assertEquals(23456, s.nextLong());
2342: assertEquals(23456, s.nextLong());
2343:
2344: /*
2345: * ''' is used in many locales as group separator.
2346: */
2347: s = new Scanner("23'456 23'456");
2348: s.useLocale(Locale.GERMANY);
2349: try {
2350: s.nextLong();
2351: fail("Should throw InputMismatchException");
2352: } catch (InputMismatchException e) {
2353: // Expected
2354: }
2355: s.useLocale(new Locale("it", "CH"));
2356: // If exception is thrown out, input will not be advanced.
2357: assertEquals(23456, s.nextLong());
2358: assertEquals(23456, s.nextLong());
2359:
2360: /*
2361: * The input string has Arabic-Indic digits.
2362: */
2363: s = new Scanner("1\u06602 1\u06662");
2364: assertEquals(102, s.nextLong());
2365: s.useRadix(5);
2366: try {
2367: s.nextLong();
2368: fail("Should throw InputMismatchException");
2369: } catch (InputMismatchException e) {
2370: // Expected
2371: }
2372: s.useRadix(10);
2373: assertEquals(162, s.nextLong());
2374:
2375: /*
2376: * '.' is used in many locales as group separator. The input string
2377: * has Arabic-Indic digits .
2378: */
2379: s = new Scanner("23.45\u0666 23.456");
2380: s.useLocale(Locale.CHINESE);
2381: try {
2382: s.nextLong();
2383: fail("Should throw InputMismatchException");
2384: } catch (InputMismatchException e) {
2385: // Expected
2386: }
2387: s.useLocale(Locale.GERMANY);
2388: // If exception is thrown out, input will not be advanced.
2389: assertEquals(23456, s.nextLong());
2390: assertEquals(23456, s.nextLong());
2391:
2392: // The input string starts with zero
2393: s = new Scanner("03,456");
2394: s.useLocale(Locale.ENGLISH);
2395: try {
2396: s.nextLong();
2397: fail("Should throw InputMismatchException");
2398: } catch (InputMismatchException e) {
2399: // Expected
2400: }
2401:
2402: s = new Scanner("03456");
2403: assertEquals(3456, s.nextLong());
2404:
2405: s = new Scanner("\u06603,456");
2406: s.useLocale(Locale.ENGLISH);
2407: assertEquals(3456, s.nextLong());
2408:
2409: s = new Scanner("E34");
2410: s.useRadix(16);
2411: assertEquals(3636, s.nextLong());
2412:
2413: /*
2414: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
2415: * respectively, but they are not differentiated.
2416: */
2417: s = new Scanner("12300");
2418: s.useLocale(Locale.CHINESE);
2419: assertEquals(12300, s.nextLong());
2420:
2421: s = new Scanner("123\u0966\u0966");
2422: s.useLocale(Locale.CHINESE);
2423: assertEquals(12300, s.nextLong());
2424:
2425: s = new Scanner("123\u0e50\u0e50");
2426: s.useLocale(Locale.CHINESE);
2427: assertEquals(12300, s.nextLong());
2428:
2429: s = new Scanner("-123");
2430: s.useLocale(new Locale("ar", "AE"));
2431: assertEquals(-123, s.nextLong());
2432:
2433: s = new Scanner("-123");
2434: s.useLocale(new Locale("mk", "MK"));
2435: assertEquals(-123, s.nextLong());
2436: }
2437:
2438: /**
2439: * @throws IOException
2440: * @tests java.util.Scanner#hasNext()
2441: */
2442: public void test_hasNext() throws IOException {
2443: s = new Scanner("1##2").useDelimiter("\\#");
2444: assertTrue(s.hasNext());
2445: assertEquals("1", s.next());
2446: assertEquals("", s.next());
2447: assertEquals("2", s.next());
2448: assertFalse(s.hasNext());
2449: s.close();
2450: try {
2451: s.hasNext();
2452: fail("should throw IllegalStateException");
2453: } catch (IllegalStateException e) {
2454: // expected
2455: }
2456:
2457: s = new Scanner("1( )2( )").useDelimiter("\\( \\)");
2458: assertTrue(s.hasNext());
2459: assertTrue(s.hasNext());
2460: assertEquals("1", s.next());
2461: assertEquals("2", s.next());
2462:
2463: s = new Scanner("1 2 ").useDelimiter("( )");
2464: assertEquals("1", s.next());
2465: assertEquals("2", s.next());
2466: assertTrue(s.hasNext());
2467: assertEquals("", s.next());
2468:
2469: s = new Scanner("1\n2 ");
2470: assertEquals("1", s.next());
2471: assertTrue(s.hasNext());
2472: assertEquals("2", s.next());
2473: assertFalse(s.hasNext());
2474: // test boundary case
2475: try {
2476: s.next();
2477: fail("should throw NoSuchElementException");
2478: } catch (NoSuchElementException e) {
2479: // Expected
2480: }
2481:
2482: s = new Scanner("1'\n'2 ");
2483: assertEquals("1'", s.next());
2484: assertTrue(s.hasNext());
2485: assertEquals("'2", s.next());
2486: assertFalse(s.hasNext());
2487: // test boundary case
2488: try {
2489: s.next();
2490: fail("should throw NoSuchElementException");
2491: } catch (NoSuchElementException e) {
2492: // Expected
2493: }
2494:
2495: s = new Scanner(" ");
2496: assertFalse(s.hasNext());
2497:
2498: // test socket inputStream
2499:
2500: os.write("1 2".getBytes());
2501: serverSocket.close();
2502:
2503: s = new Scanner(client);
2504: assertEquals("1", s.next());
2505: assertTrue(s.hasNext());
2506: assertEquals("2", s.next());
2507: assertFalse(s.hasNext());
2508: try {
2509: s.next();
2510: fail("should throw NoSuchElementException");
2511: } catch (NoSuchElementException e) {
2512: // Expected
2513: }
2514: }
2515:
2516: /**
2517: * @throws IOException
2518: * @tests java.util.Scanner#hasNext(Pattern)
2519: */
2520: public void test_hasNextLPattern() throws IOException {
2521: Pattern pattern;
2522: s = new Scanner("aab@2@abb@").useDelimiter("\\@");
2523: pattern = Pattern.compile("a*b");
2524: assertTrue(s.hasNext(pattern));
2525: assertEquals("aab", s.next(pattern));
2526: assertFalse(s.hasNext(pattern));
2527: try {
2528: s.next(pattern);
2529: fail("should throw InputMismatchException");
2530: } catch (InputMismatchException e) {
2531: // Expected
2532: }
2533:
2534: s = new Scanner("word ? ");
2535: pattern = Pattern.compile("\\w+");
2536: assertTrue(s.hasNext(pattern));
2537: assertEquals("word", s.next(pattern));
2538: assertFalse(s.hasNext(pattern));
2539: try {
2540: s.next(pattern);
2541: fail("should throw InputMismatchException");
2542: } catch (InputMismatchException e) {
2543: // Expected
2544: }
2545:
2546: s = new Scanner("word1 WorD2 ");
2547: pattern = Pattern.compile("\\w+");
2548: assertTrue(s.hasNext(pattern));
2549: assertEquals("word1", s.next(pattern));
2550: assertTrue(s.hasNext(pattern));
2551: assertEquals("WorD2", s.next(pattern));
2552: assertFalse(s.hasNext(pattern));
2553: try {
2554: s.next(pattern);
2555: fail("should throw NoSuchElementException");
2556: } catch (NoSuchElementException e) {
2557: // Expected
2558: }
2559:
2560: s = new Scanner("word1 WorD2 ");
2561: pattern = Pattern.compile("\\w+");
2562: try {
2563: s.hasNext((Pattern) null);
2564: fail("Should throw NullPointerException");
2565: } catch (NullPointerException e) {
2566: // expected
2567: }
2568: s.close();
2569: try {
2570: s.hasNext(pattern);
2571: fail("should throw IllegalStateException");
2572: } catch (IllegalStateException e) {
2573: // expected
2574: }
2575:
2576: // test socket inputStream
2577: os.write("aab b".getBytes());
2578: serverSocket.close();
2579:
2580: s = new Scanner(client);
2581: pattern = Pattern.compile("a+b");
2582: assertTrue(s.hasNext(pattern));
2583: assertEquals("aab", s.next(pattern));
2584: assertFalse(s.hasNext(pattern));
2585: try {
2586: s.next(pattern);
2587: fail("should throw InputMismatchException");
2588: } catch (InputMismatchException e) {
2589: // Expected
2590: }
2591: }
2592:
2593: /**
2594: * @throws IOException
2595: * @tests java.util.Scanner#hasNext(String)
2596: */
2597: public void test_hasNextLString() throws IOException {
2598: s = new Scanner("aab@2@abb@").useDelimiter("\\@");
2599: try {
2600: s.hasNext((String) null);
2601: fail("Should throw NullPointerException");
2602: } catch (NullPointerException e) {
2603: // expected
2604: }
2605:
2606: s = new Scanner("aab*b*").useDelimiter("\\*");
2607: assertTrue(s.hasNext("a+b"));
2608: assertEquals("aab", s.next("a+b"));
2609: assertFalse(s.hasNext("a+b"));
2610: try {
2611: s.next("a+b");
2612: fail("should throw InputMismatchException");
2613: } catch (InputMismatchException e) {
2614: // Expected
2615: }
2616: s.close();
2617: try {
2618: s.hasNext("a+b");
2619: fail("should throw IllegalStateException");
2620: } catch (IllegalStateException e) {
2621: // expected
2622: }
2623:
2624: s = new Scanner("WORD ? ");
2625: assertTrue(s.hasNext("\\w+"));
2626: assertEquals("WORD", s.next("\\w+"));
2627: assertFalse(s.hasNext("\\w+"));
2628: try {
2629: s.next("\\w+");
2630: fail("should throw InputMismatchException");
2631: } catch (InputMismatchException e) {
2632: // Expected
2633: }
2634:
2635: s = new Scanner("word1 word2 ");
2636: assertEquals("word1", s.next("\\w+"));
2637: assertEquals("word2", s.next("\\w+"));
2638: // test boundary case
2639: try {
2640: s.next("\\w+");
2641: fail("should throw NoSuchElementException");
2642: } catch (NoSuchElementException e) {
2643: // Expected
2644: }
2645:
2646: // test socket inputStream
2647:
2648: os.write("aab 2".getBytes());
2649: serverSocket.close();
2650:
2651: s = new Scanner(client);
2652: assertTrue(s.hasNext("a*b"));
2653: assertEquals("aab", s.next("a*b"));
2654: assertFalse(s.hasNext("a*b"));
2655: try {
2656: s.next("a*b");
2657: fail("should throw InputMismatchException");
2658: } catch (InputMismatchException e) {
2659: // Expected
2660: }
2661: }
2662:
2663: /**
2664: * @throws IOException
2665: * @tests java.util.Scanner#hasNextBoolean()
2666: */
2667: public void test_hasNextBoolean() throws IOException {
2668:
2669: s = new Scanner("TRue");
2670: assertTrue(s.hasNextBoolean());
2671: assertTrue(s.nextBoolean());
2672:
2673: s = new Scanner("tRue false");
2674: assertTrue(s.hasNextBoolean());
2675: assertTrue(s.nextBoolean());
2676: assertTrue(s.hasNextBoolean());
2677: assertFalse(s.nextBoolean());
2678:
2679: s = new Scanner("");
2680: assertFalse(s.hasNextBoolean());
2681:
2682: // test socket inputStream
2683:
2684: os.write("true false ".getBytes());
2685: serverSocket.close();
2686:
2687: s = new Scanner(client);
2688: assertTrue(s.hasNextBoolean());
2689: assertTrue(s.nextBoolean());
2690:
2691: // ues '*' as delimiter
2692: s = new Scanner("true**false").useDelimiter("\\*");
2693: assertTrue(s.hasNextBoolean());
2694: assertTrue(s.nextBoolean());
2695: assertFalse(s.hasNextBoolean());
2696: try {
2697: s.nextBoolean();
2698: fail("should throw NoSuchElementException");
2699: } catch (NoSuchElementException e) {
2700: // Expected
2701: }
2702:
2703: s = new Scanner("false( )").useDelimiter("\\( \\)");
2704: assertTrue(s.hasNextBoolean());
2705: assertFalse(s.nextBoolean());
2706: assertFalse(s.hasNextBoolean());
2707:
2708: }
2709:
2710: /**
2711: * @throws IOException
2712: * @tests java.util.Scanner#hasNextByte(int)
2713: */
2714: public void test_hasNextByteI() throws IOException {
2715: s = new Scanner("123 126");
2716: assertTrue(s.hasNextByte(10));
2717: assertEquals(123, s.nextByte(10));
2718: assertTrue(s.hasNextByte(10));
2719: assertEquals(126, s.nextByte(10));
2720: assertFalse(s.hasNextByte(10));
2721: try {
2722: s.nextByte(10);
2723: fail("Should throw NoSuchElementException");
2724: } catch (NoSuchElementException e) {
2725: // Expected
2726: }
2727:
2728: // If the radix is different from 10
2729: s = new Scanner("123 126");
2730: assertTrue(s.hasNextByte(5));
2731: assertEquals(38, s.nextByte(5));
2732: assertFalse(s.hasNextByte(5));
2733: try {
2734: s.nextByte(5);
2735: fail("Should throw InputMismatchException");
2736: } catch (InputMismatchException e) {
2737: // Expected
2738: }
2739:
2740: // If the number is out of range
2741: s = new Scanner("1234");
2742: assertFalse(s.hasNextByte(10));
2743: try {
2744: s.nextByte(10);
2745: fail("Should throw InputMismatchException");
2746: } catch (InputMismatchException e) {
2747: // Expected
2748: }
2749:
2750: /*
2751: * The input string has Arabic-Indic digits.
2752: */
2753: s = new Scanner("1\u06602 12\u0666");
2754: assertTrue(s.hasNextByte(10));
2755: assertEquals(102, s.nextByte(10));
2756: assertFalse(s.hasNextByte(5));
2757: try {
2758: s.nextByte(5);
2759: fail("Should throw InputMismatchException");
2760: } catch (InputMismatchException e) {
2761: // Expected
2762: }
2763: assertTrue(s.hasNextByte(10));
2764: assertEquals(126, s.nextByte(10));
2765:
2766: s = new Scanner("012");
2767: assertTrue(s.hasNextByte(10));
2768: assertEquals(12, s.nextByte(10));
2769:
2770: s = new Scanner("E");
2771: assertTrue(s.hasNextByte(16));
2772: assertEquals(14, s.nextByte(16));
2773:
2774: /*
2775: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
2776: * respectively, but they are not differentiated.
2777: */
2778: s = new Scanner("100");
2779: s.useLocale(Locale.CHINESE);
2780: assertTrue(s.hasNextByte(10));
2781: assertEquals(100, s.nextByte(10));
2782:
2783: s = new Scanner("1\u0966\u0966");
2784: s.useLocale(Locale.CHINESE);
2785: assertTrue(s.hasNextByte(10));
2786: assertEquals(100, s.nextByte(10));
2787:
2788: s = new Scanner("1\u0e50\u0e50");
2789: s.useLocale(Locale.CHINESE);
2790: assertTrue(s.hasNextByte(10));
2791: assertEquals(100, s.nextByte(10));
2792:
2793: s = new Scanner("-123");
2794: s.useLocale(new Locale("ar", "AE"));
2795: assertTrue(s.hasNextByte(10));
2796: assertEquals(-123, s.nextByte(10));
2797:
2798: s = new Scanner("-123");
2799: s.useLocale(new Locale("mk", "MK"));
2800: assertTrue(s.hasNextByte(10));
2801: assertEquals(-123, s.nextByte(10));
2802: }
2803:
2804: /**
2805: * @throws IOException
2806: * @tests java.util.Scanner#hasNextByte(int)
2807: */
2808: public void test_hasNextByteI_cache() throws IOException {
2809: //regression for HARMONY-2063
2810: s = new Scanner("123 45");
2811: assertTrue(s.hasNextByte(8));
2812: assertEquals(83, s.nextByte());
2813: assertEquals(45, s.nextByte());
2814:
2815: s = new Scanner("123 45");
2816: assertTrue(s.hasNextByte(10));
2817: assertTrue(s.hasNextByte(8));
2818: assertEquals(83, s.nextByte());
2819: assertEquals(45, s.nextByte());
2820:
2821: s = new Scanner("-123 -45");
2822: assertTrue(s.hasNextByte(8));
2823: assertEquals(-123, s.nextInt());
2824: assertEquals(-45, s.nextByte());
2825:
2826: s = new Scanner("123 45");
2827: assertTrue(s.hasNextByte());
2828: s.close();
2829: try {
2830: s.nextByte();
2831: fail("Should throw IllegalStateException");
2832: } catch (IllegalStateException e) {
2833: // expected
2834: }
2835: }
2836:
2837: /**
2838: * @throws IOException
2839: * @tests java.util.Scanner#hasNextByte()
2840: */
2841: public void test_hasNextByte() throws IOException {
2842: s = new Scanner("123 126");
2843: assertTrue(s.hasNextByte());
2844: assertEquals(123, s.nextByte());
2845: assertTrue(s.hasNextByte());
2846: assertEquals(126, s.nextByte());
2847: assertFalse(s.hasNextByte());
2848: try {
2849: s.nextByte();
2850: fail("Should throw NoSuchElementException");
2851: } catch (NoSuchElementException e) {
2852: // Expected
2853: }
2854:
2855: // If the radix is different from 10
2856: s = new Scanner("123 126");
2857: s.useRadix(5);
2858: assertTrue(s.hasNextByte());
2859: assertEquals(38, s.nextByte());
2860: assertFalse(s.hasNextByte());
2861: try {
2862: s.nextByte();
2863: fail("Should throw InputMismatchException");
2864: } catch (InputMismatchException e) {
2865: // Expected
2866: }
2867:
2868: // If the number is out of range
2869: s = new Scanner("1234");
2870: assertFalse(s.hasNextByte());
2871: try {
2872: s.nextByte();
2873: fail("Should throw InputMismatchException");
2874: } catch (InputMismatchException e) {
2875: // Expected
2876: }
2877:
2878: /*
2879: * The input string has Arabic-Indic digits.
2880: */
2881: s = new Scanner("1\u06602 12\u0666");
2882: assertTrue(s.hasNextByte());
2883: assertEquals(102, s.nextByte());
2884: s.useRadix(5);
2885: assertFalse(s.hasNextByte());
2886: try {
2887: s.nextByte();
2888: fail("Should throw InputMismatchException");
2889: } catch (InputMismatchException e) {
2890: // Expected
2891: }
2892: s.useRadix(10);
2893: assertTrue(s.hasNextByte());
2894: assertEquals(126, s.nextByte());
2895:
2896: s = new Scanner("012");
2897: assertEquals(12, s.nextByte());
2898:
2899: s = new Scanner("E");
2900: s.useRadix(16);
2901: assertTrue(s.hasNextByte());
2902: assertEquals(14, s.nextByte());
2903:
2904: /*
2905: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
2906: * respectively, but they are not differentiated.
2907: */
2908: s = new Scanner("100");
2909: s.useLocale(Locale.CHINESE);
2910: assertTrue(s.hasNextByte());
2911: assertEquals(100, s.nextByte());
2912:
2913: s = new Scanner("1\u0966\u0966");
2914: s.useLocale(Locale.CHINESE);
2915: assertTrue(s.hasNextByte());
2916: assertEquals(100, s.nextByte());
2917:
2918: s = new Scanner("1\u0e50\u0e50");
2919: s.useLocale(Locale.CHINESE);
2920: assertTrue(s.hasNextByte());
2921: assertEquals(100, s.nextByte());
2922:
2923: s = new Scanner("-123");
2924: s.useLocale(new Locale("ar", "AE"));
2925: assertTrue(s.hasNextByte());
2926: assertEquals(-123, s.nextByte());
2927:
2928: s = new Scanner("-123");
2929: s.useLocale(new Locale("mk", "MK"));
2930: assertTrue(s.hasNextByte());
2931: assertEquals(-123, s.nextByte());
2932: }
2933:
2934: /**
2935: * @throws IOException
2936: * @tests java.util.Scanner#hasNextBigInteger(int)
2937: */
2938: public void test_hasNextBigIntegerI() throws IOException {
2939: s = new Scanner("123 456");
2940: assertTrue(s.hasNextBigInteger(10));
2941: assertEquals(new BigInteger("123"), s.nextBigInteger(10));
2942: assertTrue(s.hasNextBigInteger(10));
2943: assertEquals(new BigInteger("456"), s.nextBigInteger(10));
2944: assertFalse(s.hasNextBigInteger(10));
2945: try {
2946: s.nextBigInteger(10);
2947: fail("Should throw NoSuchElementException");
2948: } catch (NoSuchElementException e) {
2949: // Expected
2950: }
2951:
2952: // If the radix is different from 10
2953: s = new Scanner("123 456");
2954: assertTrue(s.hasNextBigInteger(5));
2955: assertEquals(new BigInteger("38"), s.nextBigInteger(5));
2956: assertFalse(s.hasNextBigInteger(5));
2957: try {
2958: s.nextBigInteger(5);
2959: fail("Should throw InputMismatchException");
2960: } catch (InputMismatchException e) {
2961: // Expected
2962: }
2963:
2964: /*
2965: * Different locale can only recognize corresponding locale sensitive
2966: * string. ',' is used in many locales as group separator.
2967: */
2968: s = new Scanner("23,456 23,456");
2969: s.useLocale(Locale.GERMANY);
2970: assertFalse(s.hasNextBigInteger(10));
2971: try {
2972: s.nextBigInteger(10);
2973: fail("Should throw InputMismatchException");
2974: } catch (InputMismatchException e) {
2975: // Expected
2976: }
2977: s.useLocale(Locale.ENGLISH);
2978: // If exception is thrown out, input will not be advanced.
2979: assertTrue(s.hasNextBigInteger(10));
2980: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
2981: assertTrue(s.hasNextBigInteger(10));
2982: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
2983:
2984: /*
2985: * ''' is used in many locales as group separator.
2986: */
2987: s = new Scanner("23'456 23'456");
2988: s.useLocale(Locale.GERMANY);
2989: assertFalse(s.hasNextBigInteger(10));
2990: try {
2991: s.nextBigInteger(10);
2992: fail("Should throw InputMismatchException");
2993: } catch (InputMismatchException e) {
2994: // Expected
2995: }
2996: s.useLocale(new Locale("it", "CH"));
2997: // If exception is thrown out, input will not be advanced.
2998: assertTrue(s.hasNextBigInteger(10));
2999: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
3000: assertTrue(s.hasNextBigInteger(10));
3001: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
3002:
3003: /*
3004: * The input string has Arabic-Indic digits.
3005: */
3006: s = new Scanner("1\u06602 1\u06662");
3007: assertTrue(s.hasNextBigInteger(10));
3008: assertEquals(new BigInteger("102"), s.nextBigInteger(10));
3009: assertFalse(s.hasNextBigInteger(5));
3010: try {
3011: s.nextBigInteger(5);
3012: fail("Should throw InputMismatchException");
3013: } catch (InputMismatchException e) {
3014: // Expected
3015: }
3016: assertTrue(s.hasNextBigInteger(10));
3017: assertEquals(new BigInteger("162"), s.nextBigInteger(10));
3018:
3019: /*
3020: * '.' is used in many locales as group separator. The input string
3021: * has Arabic-Indic digits .
3022: */
3023: s = new Scanner("23.45\u0666 23.456");
3024: s.useLocale(Locale.CHINESE);
3025: assertFalse(s.hasNextBigInteger(10));
3026: try {
3027: s.nextBigInteger(10);
3028: fail("Should throw InputMismatchException");
3029: } catch (InputMismatchException e) {
3030: // Expected
3031: }
3032: s.useLocale(Locale.GERMANY);
3033: // If exception is thrown out, input will not be advanced.
3034: assertTrue(s.hasNextBigInteger(10));
3035: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
3036: assertTrue(s.hasNextBigInteger(10));
3037: assertEquals(new BigInteger("23456"), s.nextBigInteger(10));
3038:
3039: // The input string starts with zero
3040: s = new Scanner("03,456");
3041: s.useLocale(Locale.ENGLISH);
3042: assertFalse(s.hasNextBigInteger(10));
3043: try {
3044: s.nextBigInteger(10);
3045: fail("Should throw InputMismatchException");
3046: } catch (InputMismatchException e) {
3047: // Expected
3048: }
3049:
3050: s = new Scanner("03456");
3051: assertTrue(s.hasNextBigInteger(10));
3052: assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
3053:
3054: s = new Scanner("\u06603,456");
3055: s.useLocale(Locale.ENGLISH);
3056: assertTrue(s.hasNextBigInteger(10));
3057: assertEquals(new BigInteger("3456"), s.nextBigInteger(10));
3058:
3059: s = new Scanner("E34");
3060: assertTrue(s.hasNextBigInteger(16));
3061: assertEquals(new BigInteger("3636"), s.nextBigInteger(16));
3062:
3063: /*
3064: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
3065: * respectively, but they are not differentiated.
3066: */
3067: s = new Scanner("12300");
3068: s.useLocale(Locale.CHINESE);
3069: assertTrue(s.hasNextBigInteger(10));
3070: assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
3071:
3072: s = new Scanner("123\u0966\u0966");
3073: s.useLocale(Locale.CHINESE);
3074: assertTrue(s.hasNextBigInteger(10));
3075: assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
3076:
3077: s = new Scanner("123\u0e50\u0e50");
3078: s.useLocale(Locale.CHINESE);
3079: assertTrue(s.hasNextBigInteger(10));
3080: assertEquals(new BigInteger("12300"), s.nextBigInteger(10));
3081:
3082: s = new Scanner("-123");
3083: s.useLocale(new Locale("ar", "AE"));
3084: assertTrue(s.hasNextBigInteger(10));
3085: assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
3086:
3087: s = new Scanner("-123");
3088: s.useLocale(new Locale("mk", "MK"));
3089: assertTrue(s.hasNextBigInteger(10));
3090: assertEquals(new BigInteger("-123"), s.nextBigInteger(10));
3091: }
3092:
3093: /**
3094: * @throws IOException
3095: * @tests java.util.Scanner#hasNextBigInteger(int)
3096: */
3097: public void test_hasNextBigIntegerI_cache() throws IOException {
3098: //regression for HARMONY-2063
3099: s = new Scanner("123 123456789123456789");
3100: assertTrue(s.hasNextBigInteger(16));
3101: assertEquals(new BigInteger("291"), s.nextBigInteger());
3102: assertEquals(new BigInteger("123456789123456789"), s
3103: .nextBigInteger());
3104:
3105: s = new Scanner("123456789123456789 456");
3106: assertTrue(s.hasNextBigInteger(16));
3107: assertTrue(s.hasNextBigInteger(10));
3108: assertEquals(new BigInteger("123456789123456789"), s
3109: .nextBigInteger());
3110: assertEquals(new BigInteger("456"), s.nextBigInteger());
3111:
3112: s = new Scanner("-123 -123456789123456789");
3113: assertTrue(s.hasNextBigInteger(8));
3114: assertEquals(-123, s.nextShort());
3115: assertEquals(new BigInteger("-123456789123456789"), s
3116: .nextBigInteger());
3117:
3118: s = new Scanner("123 456");
3119: assertTrue(s.hasNextBigInteger());
3120: s.close();
3121: try {
3122: s.nextBigInteger();
3123: fail("Should throw IllegalStateException");
3124: } catch (IllegalStateException e) {
3125: // expected
3126: }
3127: }
3128:
3129: /**
3130: * @throws IOException
3131: * @tests java.util.Scanner#hasNextBigInteger()
3132: */
3133: public void test_hasNextBigInteger() throws IOException {
3134: s = new Scanner("123 456");
3135: assertTrue(s.hasNextBigInteger());
3136: assertEquals(new BigInteger("123"), s.nextBigInteger());
3137: assertTrue(s.hasNextBigInteger());
3138: assertEquals(new BigInteger("456"), s.nextBigInteger());
3139: assertFalse(s.hasNextBigInteger());
3140: try {
3141: s.nextBigInteger();
3142: fail("Should throw NoSuchElementException");
3143: } catch (NoSuchElementException e) {
3144: // Expected
3145: }
3146:
3147: // If the radix is different from 10
3148: s = new Scanner("123 456");
3149: s.useRadix(5);
3150: assertTrue(s.hasNextBigInteger());
3151: assertEquals(new BigInteger("38"), s.nextBigInteger());
3152: assertFalse(s.hasNextBigInteger());
3153: try {
3154: s.nextBigInteger();
3155: fail("Should throw InputMismatchException");
3156: } catch (InputMismatchException e) {
3157: // Expected
3158: }
3159:
3160: /*
3161: * Different locale can only recognize corresponding locale sensitive
3162: * string. ',' is used in many locales as group separator.
3163: */
3164: s = new Scanner("23,456 23,456");
3165: s.useLocale(Locale.GERMANY);
3166: assertFalse(s.hasNextBigInteger());
3167: try {
3168: s.nextBigInteger();
3169: fail("Should throw InputMismatchException");
3170: } catch (InputMismatchException e) {
3171: // Expected
3172: }
3173: s.useLocale(Locale.ENGLISH);
3174: // If exception is thrown out, input will not be advanced.
3175: assertTrue(s.hasNextBigInteger());
3176: assertEquals(new BigInteger("23456"), s.nextBigInteger());
3177: assertTrue(s.hasNextBigInteger());
3178: assertEquals(new BigInteger("23456"), s.nextBigInteger());
3179:
3180: /*
3181: * ''' is used in many locales as group separator.
3182: */
3183: s = new Scanner("23'456 23'456");
3184: s.useLocale(Locale.GERMANY);
3185: assertFalse(s.hasNextBigInteger());
3186: try {
3187: s.nextBigInteger();
3188: fail("Should throw InputMismatchException");
3189: } catch (InputMismatchException e) {
3190: // Expected
3191: }
3192: s.useLocale(new Locale("it", "CH"));
3193: // If exception is thrown out, input will not be advanced.
3194: assertTrue(s.hasNextBigInteger());
3195: assertEquals(new BigInteger("23456"), s.nextBigInteger());
3196: assertTrue(s.hasNextBigInteger());
3197: assertEquals(new BigInteger("23456"), s.nextBigInteger());
3198:
3199: /*
3200: * The input string has Arabic-Indic digits.
3201: */
3202: s = new Scanner("1\u06602 1\u06662");
3203: assertEquals(new BigInteger("102"), s.nextBigInteger());
3204: s.useRadix(5);
3205: assertFalse(s.hasNextBigInteger());
3206: try {
3207: s.nextBigInteger();
3208: fail("Should throw InputMismatchException");
3209: } catch (InputMismatchException e) {
3210: // Expected
3211: }
3212: s.useRadix(10);
3213: assertTrue(s.hasNextBigInteger());
3214: assertEquals(new BigInteger("162"), s.nextBigInteger());
3215:
3216: /*
3217: * '.' is used in many locales as group separator. The input string
3218: * has Arabic-Indic digits .
3219: */
3220: s = new Scanner("23.45\u0666 23.456");
3221: s.useLocale(Locale.CHINESE);
3222: assertFalse(s.hasNextBigInteger());
3223: try {
3224: s.nextBigInteger();
3225: fail("Should throw InputMismatchException");
3226: } catch (InputMismatchException e) {
3227: // Expected
3228: }
3229: s.useLocale(Locale.GERMANY);
3230: // If exception is thrown out, input will not be advanced.
3231: assertTrue(s.hasNextBigInteger());
3232: assertEquals(new BigInteger("23456"), s.nextBigInteger());
3233: assertTrue(s.hasNextBigInteger());
3234: assertEquals(new BigInteger("23456"), s.nextBigInteger());
3235:
3236: // The input string starts with zero
3237: s = new Scanner("03,456");
3238: s.useLocale(Locale.ENGLISH);
3239: assertFalse(s.hasNextBigInteger());
3240: try {
3241: s.nextBigInteger();
3242: fail("Should throw InputMismatchException");
3243: } catch (InputMismatchException e) {
3244: // Expected
3245: }
3246:
3247: s = new Scanner("03456");
3248: assertTrue(s.hasNextBigInteger());
3249: assertEquals(new BigInteger("3456"), s.nextBigInteger());
3250:
3251: s = new Scanner("\u06603,456");
3252: s.useLocale(Locale.ENGLISH);
3253: assertTrue(s.hasNextBigInteger());
3254: assertEquals(new BigInteger("3456"), s.nextBigInteger());
3255:
3256: s = new Scanner("E34");
3257: s.useRadix(16);
3258: assertTrue(s.hasNextBigInteger());
3259: assertEquals(new BigInteger("3636"), s.nextBigInteger());
3260:
3261: /*
3262: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
3263: * respectively, but they are not differentiated.
3264: */
3265: s = new Scanner("12300");
3266: s.useLocale(Locale.CHINESE);
3267: assertTrue(s.hasNextBigInteger());
3268: assertEquals(new BigInteger("12300"), s.nextBigInteger());
3269:
3270: s = new Scanner("123\u0966\u0966");
3271: s.useLocale(Locale.CHINESE);
3272: assertTrue(s.hasNextBigInteger());
3273: assertEquals(new BigInteger("12300"), s.nextBigInteger());
3274:
3275: s = new Scanner("123\u0e50\u0e50");
3276: s.useLocale(Locale.CHINESE);
3277: assertTrue(s.hasNextBigInteger());
3278: assertEquals(new BigInteger("12300"), s.nextBigInteger());
3279:
3280: s = new Scanner("-123");
3281: s.useLocale(new Locale("ar", "AE"));
3282: assertTrue(s.hasNextBigInteger());
3283: assertEquals(new BigInteger("-123"), s.nextBigInteger());
3284:
3285: s = new Scanner("-123");
3286: s.useLocale(new Locale("mk", "MK"));
3287: assertTrue(s.hasNextBigInteger());
3288: assertEquals(new BigInteger("-123"), s.nextBigInteger());
3289: }
3290:
3291: /**
3292: * @throws IOException
3293: * @tests java.util.Scanner#hasNextInt(int)
3294: */
3295: public void test_hasNextIntI() throws IOException {
3296: s = new Scanner("123 456");
3297: assertEquals(123, s.nextInt(10));
3298: assertTrue(s.hasNextInt(10));
3299: assertEquals(456, s.nextInt(10));
3300: assertFalse(s.hasNextInt(10));
3301: try {
3302: s.nextInt(10);
3303: fail("Should throw NoSuchElementException");
3304: } catch (NoSuchElementException e) {
3305: // Expected
3306: }
3307:
3308: // If the radix is different from 10
3309: s = new Scanner("123 456");
3310: assertTrue(s.hasNextInt(5));
3311: assertEquals(38, s.nextInt(5));
3312: assertFalse(s.hasNextInt(5));
3313: try {
3314: s.nextInt(5);
3315: fail("Should throw InputMismatchException");
3316: } catch (InputMismatchException e) {
3317: // Expected
3318: }
3319:
3320: // If the number is out of range
3321: s = new Scanner("123456789123456789123456789123456789");
3322: assertFalse(s.hasNextInt(10));
3323:
3324: /*
3325: * Different locale can only recognize corresponding locale sensitive
3326: * string. ',' is used in many locales as group separator.
3327: */
3328: s = new Scanner("23,456");
3329: s.useLocale(Locale.GERMANY);
3330: assertFalse(s.hasNextInt(10));
3331: s.useLocale(Locale.ENGLISH);
3332: // If exception is thrown out, input will not be advanced.
3333: assertTrue(s.hasNextInt(10));
3334: /*
3335: * ''' is used in many locales as group separator.
3336: */
3337: s = new Scanner("23'456");
3338: s.useLocale(Locale.GERMANY);
3339: assertFalse(s.hasNextInt(10));
3340: s.useLocale(new Locale("it", "CH"));
3341: // If exception is thrown out, input will not be advanced.
3342: assertTrue(s.hasNextInt(10));
3343:
3344: /*
3345: * The input string has Arabic-Indic digits.
3346: */
3347: s = new Scanner("1\u06662");
3348: assertTrue(s.hasNextInt(10));
3349: assertFalse(s.hasNextInt(5));
3350:
3351: /*
3352: * '.' is used in many locales as group separator. The input string
3353: * has Arabic-Indic digits .
3354: */
3355: s = new Scanner("23.45\u0666");
3356: s.useLocale(Locale.CHINESE);
3357: assertFalse(s.hasNextInt(10));
3358: try {
3359: s.nextInt(10);
3360: fail("Should throw InputMismatchException");
3361: } catch (InputMismatchException e) {
3362: // expected
3363: }
3364: s.useLocale(Locale.GERMANY);
3365: assertTrue(s.hasNextInt(10));
3366:
3367: // The input string starts with zero
3368: s = new Scanner("03,456");
3369: s.useLocale(Locale.ENGLISH);
3370: assertFalse(s.hasNextInt(10));
3371: try {
3372: s.nextInt(10);
3373: fail("Should throw InputMismatchException");
3374: } catch (InputMismatchException e) {
3375: // expected
3376: }
3377:
3378: s = new Scanner("03456");
3379: assertTrue(s.hasNextInt(10));
3380: assertEquals(3456, s.nextInt(10));
3381:
3382: s = new Scanner("\u06603,456");
3383: s.useLocale(Locale.ENGLISH);
3384: assertTrue(s.hasNextInt(10));
3385: assertEquals(3456, s.nextInt(10));
3386:
3387: s = new Scanner("E3456");
3388: assertTrue(s.hasNextInt(16));
3389: assertEquals(930902, s.nextInt(16));
3390: // The following test case fails on RI, because RI does not support
3391: // letter as leading digit
3392: s = new Scanner("E3,456");
3393: s.useLocale(Locale.ENGLISH);
3394: assertTrue(s.hasNextInt(16));
3395: assertEquals(930902, s.nextInt(16));
3396:
3397: // If parameter radix is illegal, the following test case fails on RI
3398: try {
3399: s.hasNextInt(Character.MIN_RADIX - 1);
3400: fail("Should throw IllegalArgumentException");
3401: } catch (IllegalArgumentException e) {
3402: // Expected
3403: }
3404:
3405: /*
3406: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
3407: * respectively, but they are not differentiated.
3408: */
3409: s = new Scanner("12300");
3410: s.useLocale(Locale.CHINESE);
3411: assertTrue(s.hasNextInt(10));
3412: assertEquals(12300, s.nextInt(10));
3413:
3414: s = new Scanner("123\u0966\u0966");
3415: s.useLocale(Locale.CHINESE);
3416: assertTrue(s.hasNextInt(10));
3417: assertEquals(12300, s.nextInt(10));
3418:
3419: s = new Scanner("123\u0e50\u0e50");
3420: s.useLocale(Locale.CHINESE);
3421: assertTrue(s.hasNextInt(10));
3422: assertEquals(12300, s.nextInt(10));
3423:
3424: /*
3425: * There are three types of negative prefix all in all. '' '-' '(' There
3426: * are three types of negative suffix all in all. '' '-' ')' '(' and ')'
3427: * must be used togethor. Prefix '-' and suffix '-' must be used
3428: * exclusively.
3429: */
3430:
3431: /*
3432: * According to Integer regular expression: Integer :: = ( [-+]? (*
3433: * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix |
3434: * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be
3435: * recognized by scanner with locale ar_AE, (123) shouble be recognized
3436: * by scanner with locale mk_MK. But this is not the case on RI.
3437: */
3438: s = new Scanner("-123 123- -123-");
3439: s.useLocale(new Locale("ar", "AE"));
3440: assertTrue(s.hasNextInt(10));
3441: assertEquals(-123, s.nextInt(10));
3442: // The following test case fails on RI
3443: assertTrue(s.hasNextInt(10));
3444: assertEquals(-123, s.nextInt(10));
3445: assertFalse(s.hasNextInt(10));
3446: try {
3447: s.nextInt(10);
3448: fail("Should throw InputMismatchException");
3449: } catch (InputMismatchException e) {
3450: // expected
3451: }
3452:
3453: s = new Scanner("-123 123- (123)");
3454: s.useLocale(new Locale("mk", "MK"));
3455: assertTrue(s.hasNextInt(10));
3456: assertEquals(-123, s.nextInt(10));
3457: assertFalse(s.hasNextInt(10));
3458: try {
3459: s.nextInt();
3460: fail("Should throw InputMismatchException");
3461: } catch (InputMismatchException e) {
3462: // expected
3463: }
3464: // Skip the un-recognizable token 123-.
3465: assertEquals("123-", s.next());
3466: // The following test case fails on RI
3467: assertTrue(s.hasNextInt(10));
3468: assertEquals(-123, s.nextInt(10));
3469: }
3470:
3471: /**
3472: * @throws IOException
3473: * @tests java.util.Scanner#hasNextInt(int)
3474: */
3475: public void test_hasNextIntI_cache() throws IOException {
3476: //regression for HARMONY-2063
3477: s = new Scanner("123 456");
3478: assertTrue(s.hasNextInt(16));
3479: assertEquals(291, s.nextInt(10));
3480: assertEquals(456, s.nextInt());
3481:
3482: s = new Scanner("123 456");
3483: assertTrue(s.hasNextInt(16));
3484: assertTrue(s.hasNextInt(8));
3485: assertEquals(83, s.nextInt());
3486: assertEquals(456, s.nextInt());
3487:
3488: s = new Scanner("-123 -456 -789");
3489: assertTrue(s.hasNextInt(8));
3490: assertEquals(-123, s.nextShort());
3491: assertEquals(-456, s.nextInt());
3492: assertTrue(s.hasNextShort(16));
3493: assertEquals(-789, s.nextInt());
3494:
3495: s = new Scanner("123 456");
3496: assertTrue(s.hasNextInt());
3497: s.close();
3498: try {
3499: s.nextInt();
3500: fail("Should throw IllegalStateException");
3501: } catch (IllegalStateException e) {
3502: // expected
3503: }
3504: }
3505:
3506: /**
3507: * @throws IOException
3508: * @tests java.util.Scanner#hasNextInt()
3509: */
3510: public void test_hasNextInt() throws IOException {
3511: s = new Scanner("123 456");
3512: assertTrue(s.hasNextInt());
3513: assertEquals(123, s.nextInt());
3514: assertEquals(456, s.nextInt());
3515: assertFalse(s.hasNextInt());
3516: try {
3517: s.nextInt();
3518: fail("Should throw NoSuchElementException");
3519: } catch (NoSuchElementException e) {
3520: // Expected
3521: }
3522:
3523: // If the radix is different from 10
3524: s = new Scanner("123 456");
3525: s.useRadix(5);
3526: assertTrue(s.hasNextInt());
3527: assertEquals(38, s.nextInt());
3528: assertFalse(s.hasNextInt());
3529: try {
3530: s.nextInt();
3531: fail("Should throw InputMismatchException");
3532: } catch (InputMismatchException e) {
3533: // Expected
3534: }
3535:
3536: // If the number is out of range
3537: s = new Scanner("123456789123456789123456789123456789");
3538: assertFalse(s.hasNextInt());
3539:
3540: /*
3541: * Different locale can only recognize corresponding locale sensitive
3542: * string. ',' is used in many locales as group separator.
3543: */
3544: s = new Scanner("23,456");
3545: s.useLocale(Locale.GERMANY);
3546: assertFalse(s.hasNextInt());
3547: s.useLocale(Locale.ENGLISH);
3548: assertTrue(s.hasNextInt());
3549:
3550: /*
3551: * ''' is used in many locales as group separator.
3552: */
3553: s = new Scanner("23'456");
3554: s.useLocale(Locale.GERMANY);
3555: assertFalse(s.hasNextInt());
3556: s.useLocale(new Locale("it", "CH"));
3557: assertTrue(s.hasNextInt());
3558:
3559: /*
3560: * The input string has Arabic-Indic digits.
3561: */
3562: s = new Scanner("1\u06662");
3563: s.useRadix(5);
3564: assertFalse(s.hasNextInt());
3565:
3566: /*
3567: * '.' is used in many locales as group separator. The input string
3568: * has Arabic-Indic digits .
3569: */
3570: s = new Scanner("23.45\u0666");
3571: s.useLocale(Locale.CHINESE);
3572: assertFalse(s.hasNextInt());
3573: s.useLocale(Locale.GERMANY);
3574: assertTrue(s.hasNextInt());
3575:
3576: // The input string starts with zero
3577: s = new Scanner("03,456");
3578: s.useLocale(Locale.ENGLISH);
3579: assertFalse(s.hasNextInt());
3580: try {
3581: s.nextInt();
3582: fail("Should throw InputMismatchException");
3583: } catch (InputMismatchException e) {
3584: // expected
3585: }
3586:
3587: s = new Scanner("03456");
3588: assertTrue(s.hasNextInt());
3589: assertEquals(3456, s.nextInt());
3590:
3591: s = new Scanner("\u06603,456");
3592: s.useLocale(Locale.ENGLISH);
3593: assertEquals(3456, s.nextInt());
3594:
3595: s = new Scanner("E3456");
3596: s.useRadix(16);
3597: assertTrue(s.hasNextInt());
3598: assertEquals(930902, s.nextInt());
3599:
3600: // The following test case fails on RI, because RI does not support
3601: // letter as leading digit
3602: s = new Scanner("E3,456");
3603: s.useLocale(Locale.ENGLISH);
3604: s.useRadix(16);
3605: assertTrue(s.hasNextInt());
3606: assertEquals(930902, s.nextInt());
3607:
3608: /*
3609: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
3610: * respectively, but they are not differentiated.
3611: */
3612: s = new Scanner("12300");
3613: s.useLocale(Locale.CHINESE);
3614: assertTrue(s.hasNextInt());
3615: assertEquals(12300, s.nextInt());
3616:
3617: s = new Scanner("123\u0966\u0966");
3618: s.useLocale(Locale.CHINESE);
3619: assertTrue(s.hasNextInt());
3620: assertEquals(12300, s.nextInt());
3621:
3622: s = new Scanner("123\u0e50\u0e50");
3623: s.useLocale(Locale.CHINESE);
3624: assertTrue(s.hasNextInt());
3625: assertEquals(12300, s.nextInt());
3626:
3627: /*
3628: * There are three types of negative prefix all in all. '' '-' '(' There
3629: * are three types of negative suffix all in all. '' '-' ')' '(' and ')'
3630: * must be used togethor. Prefix '-' and suffix '-' must be used
3631: * exclusively.
3632: */
3633:
3634: /*
3635: * According to Integer regular expression: Integer :: = ( [-+]? (*
3636: * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix |
3637: * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be
3638: * recognized by scanner with locale ar_AE, (123) shouble be recognized
3639: * by scanner with locale mk_MK. But this is not the case on RI.
3640: */
3641: s = new Scanner("-123 123- -123-");
3642: s.useLocale(new Locale("ar", "AE"));
3643: assertTrue(s.hasNextInt());
3644: assertEquals(-123, s.nextInt());
3645: // The following test case fails on RI
3646: assertTrue(s.hasNextInt());
3647: assertEquals(-123, s.nextInt());
3648: assertFalse(s.hasNextInt());
3649: try {
3650: s.nextInt();
3651: fail("Should throw InputMismatchException");
3652: } catch (InputMismatchException e) {
3653: // expected
3654: }
3655:
3656: s = new Scanner("-123 123- (123)");
3657: s.useLocale(new Locale("mk", "MK"));
3658: assertTrue(s.hasNextInt());
3659: assertEquals(-123, s.nextInt());
3660: try {
3661: s.nextInt();
3662: fail("Should throw InputMismatchException");
3663: } catch (InputMismatchException e) {
3664: // expected
3665: }
3666: // Skip the un-recognizable token 123-.
3667: assertEquals("123-", s.next());
3668: // The following test case fails on RI
3669: assertTrue(s.hasNextInt());
3670: assertEquals(-123, s.nextInt());
3671: }
3672:
3673: /**
3674: * @throws IOException
3675: * @tests java.util.Scanner#hasNextFloat()
3676: */
3677: public void test_hasNextFloat() throws IOException {
3678: s = new Scanner("123 45\u0666. 123.4 .123 ");
3679: s.useLocale(Locale.ENGLISH);
3680: assertTrue(s.hasNextFloat());
3681: assertEquals((float) 123.0, s.nextFloat());
3682: assertTrue(s.hasNextFloat());
3683: assertEquals((float) 456.0, s.nextFloat());
3684: assertTrue(s.hasNextFloat());
3685: assertEquals((float) 123.4, s.nextFloat());
3686: assertTrue(s.hasNextFloat());
3687: assertEquals((float) 0.123, s.nextFloat());
3688: assertFalse(s.hasNextFloat());
3689: try {
3690: s.nextFloat();
3691: fail("Should throw NoSuchElementException");
3692: } catch (NoSuchElementException e) {
3693: // Expected
3694: }
3695:
3696: s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
3697: s.useLocale(Locale.ENGLISH);
3698: assertTrue(s.hasNextFloat());
3699: assertEquals((float) 123.4, s.nextFloat());
3700: assertTrue(s.hasNextFloat());
3701: assertEquals((float) -456.7, s.nextFloat());
3702: assertTrue(s.hasNextFloat());
3703: assertEquals((float) 123456.789, s.nextFloat());
3704: assertFalse(s.hasNextFloat());
3705: try {
3706: s.nextFloat();
3707: fail("Should throw InputMismatchException");
3708: } catch (InputMismatchException e) {
3709: // Expected
3710: }
3711:
3712: // Scientific notation
3713: s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
3714: s.useLocale(Locale.ENGLISH);
3715: assertTrue(s.hasNextFloat());
3716: assertEquals((float) 1.234E12, s.nextFloat());
3717: assertTrue(s.hasNextFloat());
3718: assertEquals((float) -4.567E14, s.nextFloat());
3719: assertTrue(s.hasNextFloat());
3720: assertEquals((float) 1.23456789E-5, s.nextFloat());
3721:
3722: s = new Scanner("NaN Infinity -Infinity");
3723: assertTrue(s.hasNextFloat());
3724: assertEquals(Float.NaN, s.nextFloat());
3725: assertTrue(s.hasNextFloat());
3726: assertEquals(Float.POSITIVE_INFINITY, s.nextFloat());
3727: assertTrue(s.hasNextFloat());
3728: assertEquals(Float.NEGATIVE_INFINITY, s.nextFloat());
3729:
3730: String str = String.valueOf(Float.MAX_VALUE * 2);
3731: s = new Scanner(str);
3732: assertTrue(s.hasNextFloat());
3733: assertEquals(Float.POSITIVE_INFINITY, s.nextFloat());
3734:
3735: /*
3736: * Different locale can only recognize corresponding locale sensitive
3737: * string. ',' is used in many locales as group separator.
3738: */
3739: s = new Scanner("23,456 23,456");
3740: s.useLocale(Locale.ENGLISH);
3741: assertTrue(s.hasNextFloat());
3742: assertEquals((float) 23456.0, s.nextFloat());
3743: s.useLocale(Locale.GERMANY);
3744: assertTrue(s.hasNextFloat());
3745: assertEquals((float) 23.456, s.nextFloat());
3746:
3747: s = new Scanner("23.456 23.456");
3748: s.useLocale(Locale.ENGLISH);
3749: assertTrue(s.hasNextFloat());
3750: assertEquals((float) 23.456, s.nextFloat());
3751: s.useLocale(Locale.GERMANY);
3752: assertTrue(s.hasNextFloat());
3753: assertEquals((float) 23456.0, s.nextFloat());
3754:
3755: s = new Scanner("23,456.7 23.456,7");
3756: s.useLocale(Locale.ENGLISH);
3757: assertTrue(s.hasNextFloat());
3758: assertEquals((float) 23456.7, s.nextFloat());
3759: s.useLocale(Locale.GERMANY);
3760: assertTrue(s.hasNextFloat());
3761: assertEquals((float) 23456.7, s.nextFloat());
3762:
3763: s = new Scanner("-123.4 123.4- -123.4-");
3764: s.useLocale(new Locale("ar", "AE"));
3765: assertTrue(s.hasNextFloat());
3766: assertEquals((float) -123.4, s.nextFloat());
3767: //The following test case fails on RI
3768: assertTrue(s.hasNextFloat());
3769: assertEquals((float) -123.4, s.nextFloat());
3770: try {
3771: s.nextFloat();
3772: fail("Should throw InputMismatchException");
3773: } catch (InputMismatchException e) {
3774: // Expected
3775: }
3776:
3777: s = new Scanner("(123) 123- -123");
3778: s.useLocale(new Locale("mk", "MK"));
3779: assertTrue(s.hasNextFloat());
3780: assertEquals((float) -123.0, s.nextFloat());
3781: assertFalse(s.hasNextFloat());
3782: try {
3783: s.nextFloat();
3784: fail("Should throw InputMismatchException");
3785: } catch (InputMismatchException e) {
3786: // Expected
3787: }
3788: // Skip the un-recognizable token 123-.
3789: assertEquals("123-", s.next());
3790: assertTrue(s.hasNextFloat());
3791: assertEquals((float) -123.0, s.nextFloat());
3792:
3793: s = new Scanner("+123.4 -456.7");
3794: s.useLocale(Locale.ENGLISH);
3795: assertTrue(s.hasNextFloat());
3796: s.close();
3797: try {
3798: s.nextFloat();
3799: fail("Should throw IllegalStateException");
3800: } catch (IllegalStateException e) {
3801: //expected
3802: }
3803:
3804: }
3805:
3806: /**
3807: * @throws IOException
3808: * @tests java.util.Scanner#hasNextShort(int)
3809: */
3810: public void test_hasNextShortI() throws IOException {
3811: s = new Scanner("123 456");
3812: assertTrue(s.hasNextShort(10));
3813: assertEquals(123, s.nextShort(10));
3814: assertTrue(s.hasNextShort(10));
3815: assertEquals(456, s.nextShort(10));
3816: assertFalse(s.hasNextShort(10));
3817: try {
3818: s.nextShort(10);
3819: fail("Should throw NoSuchElementException");
3820: } catch (NoSuchElementException e) {
3821: // Expected
3822: }
3823:
3824: // If the radix is different from 10
3825: s = new Scanner("123 456");
3826: assertTrue(s.hasNextShort(5));
3827: assertEquals(38, s.nextShort(5));
3828: assertFalse(s.hasNextShort(5));
3829: try {
3830: s.nextShort(5);
3831: fail("Should throw InputMismatchException");
3832: } catch (InputMismatchException e) {
3833: // Expected
3834: }
3835:
3836: // If the number is out of range
3837: s = new Scanner("123456789");
3838: assertFalse(s.hasNextShort(10));
3839: try {
3840: s.nextShort(10);
3841: fail("Should throw InputMismatchException");
3842: } catch (InputMismatchException e) {
3843: // Expected
3844: }
3845:
3846: /*
3847: * Different locale can only recognize corresponding locale sensitive
3848: * string. ',' is used in many locales as group separator.
3849: */
3850: s = new Scanner("23,456 23,456");
3851: s.useLocale(Locale.GERMANY);
3852: assertFalse(s.hasNextShort(10));
3853: try {
3854: s.nextShort(10);
3855: fail("Should throw InputMismatchException");
3856: } catch (InputMismatchException e) {
3857: // Expected
3858: }
3859: s.useLocale(Locale.ENGLISH);
3860: // If exception is thrown out, input will not be advanced.
3861: assertTrue(s.hasNextShort(10));
3862: assertEquals(23456, s.nextInt(10));
3863: assertTrue(s.hasNextShort(10));
3864: assertEquals(23456, s.nextInt(10));
3865:
3866: /*
3867: * ''' is used in many locales as group separator.
3868: */
3869: s = new Scanner("23'456 23'456");
3870: s.useLocale(Locale.GERMANY);
3871: assertFalse(s.hasNextShort(10));
3872: try {
3873: s.nextShort(10);
3874: fail("Should throw InputMismatchException");
3875: } catch (InputMismatchException e) {
3876: // Expected
3877: }
3878: s.useLocale(new Locale("it", "CH"));
3879: // If exception is thrown out, input will not be advanced.
3880: assertTrue(s.hasNextShort(10));
3881: assertEquals(23456, s.nextShort(10));
3882: assertTrue(s.hasNextShort(10));
3883: assertEquals(23456, s.nextShort(10));
3884:
3885: /*
3886: * The input string has Arabic-Indic digits.
3887: */
3888: s = new Scanner("1\u06602 1\u06662");
3889: assertTrue(s.hasNextShort(10));
3890: assertEquals(102, s.nextShort(10));
3891: assertFalse(s.hasNextShort(5));
3892: try {
3893: s.nextShort(5);
3894: fail("Should throw InputMismatchException");
3895: } catch (InputMismatchException e) {
3896: // Expected
3897: }
3898: assertTrue(s.hasNextShort(10));
3899: assertEquals(162, s.nextShort(10));
3900:
3901: /*
3902: * '.' is used in many locales as group separator. The input string
3903: * has Arabic-Indic digits .
3904: */
3905: s = new Scanner("23.45\u0666 23.456");
3906: s.useLocale(Locale.CHINESE);
3907: assertFalse(s.hasNextShort(10));
3908: try {
3909: s.nextShort(10);
3910: fail("Should throw InputMismatchException");
3911: } catch (InputMismatchException e) {
3912: // Expected
3913: }
3914: s.useLocale(Locale.GERMANY);
3915: // If exception is thrown out, input will not be advanced.
3916: assertTrue(s.hasNextShort(10));
3917: assertEquals(23456, s.nextShort(10));
3918: assertTrue(s.hasNextShort(10));
3919: assertEquals(23456, s.nextShort(10));
3920:
3921: // The input string starts with zero
3922: s = new Scanner("03,456");
3923: s.useLocale(Locale.ENGLISH);
3924: assertFalse(s.hasNextShort(10));
3925: try {
3926: s.nextShort(10);
3927: fail("Should throw InputMismatchException");
3928: } catch (InputMismatchException e) {
3929: // Expected
3930: }
3931:
3932: s = new Scanner("03456");
3933: assertTrue(s.hasNextShort(10));
3934: assertEquals(3456, s.nextShort(10));
3935:
3936: s = new Scanner("\u06603,456");
3937: s.useLocale(Locale.ENGLISH);
3938: assertTrue(s.hasNextShort(10));
3939: assertEquals(3456, s.nextShort(10));
3940:
3941: s = new Scanner("E34");
3942: assertTrue(s.hasNextShort(16));
3943: assertEquals(3636, s.nextShort(16));
3944:
3945: /*
3946: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
3947: * respectively, but they are not differentiated.
3948: */
3949: s = new Scanner("12300");
3950: s.useLocale(Locale.CHINESE);
3951: assertTrue(s.hasNextShort(10));
3952: assertEquals(12300, s.nextShort(10));
3953:
3954: s = new Scanner("123\u0966\u0966");
3955: s.useLocale(Locale.CHINESE);
3956: assertTrue(s.hasNextShort(10));
3957: assertEquals(12300, s.nextShort(10));
3958:
3959: s = new Scanner("123\u0e50\u0e50");
3960: s.useLocale(Locale.CHINESE);
3961: assertTrue(s.hasNextShort(10));
3962: assertEquals(12300, s.nextShort(10));
3963:
3964: s = new Scanner("-123");
3965: s.useLocale(new Locale("ar", "AE"));
3966: assertTrue(s.hasNextShort(10));
3967: assertEquals(-123, s.nextShort(10));
3968:
3969: s = new Scanner("-123");
3970: s.useLocale(new Locale("mk", "MK"));
3971: assertTrue(s.hasNextShort(10));
3972: assertEquals(-123, s.nextShort(10));
3973: }
3974:
3975: /**
3976: * @throws IOException
3977: * @tests java.util.Scanner#hasNextShort()
3978: */
3979: public void test_hasNextShort() throws IOException {
3980: s = new Scanner("123 456");
3981: assertTrue(s.hasNextShort());
3982: assertEquals(123, s.nextShort());
3983: assertTrue(s.hasNextShort());
3984: assertEquals(456, s.nextShort());
3985: assertFalse(s.hasNextShort());
3986: try {
3987: s.nextShort();
3988: fail("Should throw NoSuchElementException");
3989: } catch (NoSuchElementException e) {
3990: // Expected
3991: }
3992:
3993: // If the radix is different from 10
3994: s = new Scanner("123 456");
3995: s.useRadix(5);
3996: assertTrue(s.hasNextShort());
3997: assertEquals(38, s.nextShort());
3998: assertFalse(s.hasNextShort());
3999: try {
4000: s.nextShort();
4001: fail("Should throw InputMismatchException");
4002: } catch (InputMismatchException e) {
4003: // Expected
4004: }
4005:
4006: // If the number is out of range
4007: s = new Scanner("123456789");
4008: assertFalse(s.hasNextShort());
4009: try {
4010: s.nextShort();
4011: fail("Should throw InputMismatchException");
4012: } catch (InputMismatchException e) {
4013: // Expected
4014: }
4015:
4016: /*
4017: * Different locale can only recognize corresponding locale sensitive
4018: * string. ',' is used in many locales as group separator.
4019: */
4020: s = new Scanner("23,456 23,456");
4021: s.useLocale(Locale.GERMANY);
4022: assertFalse(s.hasNextShort());
4023: try {
4024: s.nextShort();
4025: fail("Should throw InputMismatchException");
4026: } catch (InputMismatchException e) {
4027: // Expected
4028: }
4029: s.useLocale(Locale.ENGLISH);
4030: // If exception is thrown out, input will not be advanced.
4031: assertTrue(s.hasNextShort());
4032: assertEquals(23456, s.nextShort());
4033: assertTrue(s.hasNextShort());
4034: assertEquals(23456, s.nextShort());
4035:
4036: /*
4037: * ''' is used in many locales as group separator.
4038: */
4039: s = new Scanner("23'456 23'456");
4040: s.useLocale(Locale.GERMANY);
4041: assertFalse(s.hasNextShort());
4042: try {
4043: s.nextShort();
4044: fail("Should throw InputMismatchException");
4045: } catch (InputMismatchException e) {
4046: // Expected
4047: }
4048: s.useLocale(new Locale("it", "CH"));
4049: // If exception is thrown out, input will not be advanced.
4050: assertTrue(s.hasNextShort());
4051: assertEquals(23456, s.nextShort());
4052: assertTrue(s.hasNextShort());
4053: assertEquals(23456, s.nextShort());
4054:
4055: /*
4056: * The input string has Arabic-Indic digits.
4057: */
4058: s = new Scanner("1\u06602 1\u06662");
4059: assertEquals(102, s.nextShort());
4060: s.useRadix(5);
4061: assertFalse(s.hasNextShort());
4062: try {
4063: s.nextShort();
4064: fail("Should throw InputMismatchException");
4065: } catch (InputMismatchException e) {
4066: // Expected
4067: }
4068: s.useRadix(10);
4069: assertTrue(s.hasNextShort());
4070: assertEquals(162, s.nextShort());
4071:
4072: /*
4073: * '.' is used in many locales as group separator. The input string
4074: * has Arabic-Indic digits .
4075: */
4076: s = new Scanner("23.45\u0666 23.456");
4077: s.useLocale(Locale.CHINESE);
4078: assertFalse(s.hasNextShort());
4079: try {
4080: s.nextShort();
4081: fail("Should throw InputMismatchException");
4082: } catch (InputMismatchException e) {
4083: // Expected
4084: }
4085: s.useLocale(Locale.GERMANY);
4086: // If exception is thrown out, input will not be advanced.
4087: assertTrue(s.hasNextShort());
4088: assertEquals(23456, s.nextShort());
4089: assertTrue(s.hasNextShort());
4090: assertEquals(23456, s.nextShort());
4091:
4092: // The input string starts with zero
4093: s = new Scanner("03,456");
4094: s.useLocale(Locale.ENGLISH);
4095: assertFalse(s.hasNextShort());
4096: try {
4097: s.nextShort();
4098: fail("Should throw InputMismatchException");
4099: } catch (InputMismatchException e) {
4100: // Expected
4101: }
4102:
4103: s = new Scanner("03456");
4104: assertTrue(s.hasNextShort());
4105: assertEquals(3456, s.nextShort());
4106:
4107: s = new Scanner("\u06603,456");
4108: s.useLocale(Locale.ENGLISH);
4109: assertTrue(s.hasNextShort());
4110: assertEquals(3456, s.nextShort());
4111:
4112: s = new Scanner("E34");
4113: s.useRadix(16);
4114: assertTrue(s.hasNextShort());
4115: assertEquals(3636, s.nextShort());
4116:
4117: /*
4118: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
4119: * respectively, but they are not differentiated.
4120: */
4121: s = new Scanner("12300");
4122: s.useLocale(Locale.CHINESE);
4123: assertTrue(s.hasNextShort());
4124: assertEquals(12300, s.nextShort());
4125:
4126: s = new Scanner("123\u0966\u0966");
4127: s.useLocale(Locale.CHINESE);
4128: assertTrue(s.hasNextShort());
4129: assertEquals(12300, s.nextShort());
4130:
4131: s = new Scanner("123\u0e50\u0e50");
4132: s.useLocale(Locale.CHINESE);
4133: assertTrue(s.hasNextShort());
4134: assertEquals(12300, s.nextShort());
4135:
4136: s = new Scanner("-123");
4137: s.useLocale(new Locale("ar", "AE"));
4138: assertTrue(s.hasNextShort());
4139: assertEquals(-123, s.nextShort());
4140:
4141: s = new Scanner("-123");
4142: s.useLocale(new Locale("mk", "MK"));
4143: assertTrue(s.hasNextShort());
4144: assertEquals(-123, s.nextShort());
4145: }
4146:
4147: /**
4148: * @throws IOException
4149: * @tests java.util.Scanner#hasNextShort(int)
4150: */
4151: public void test_hasNextShortI_cache() throws IOException {
4152: //regression for HARMONY-2063
4153: s = new Scanner("123 456");
4154: assertTrue(s.hasNextShort(16));
4155: assertEquals(291, s.nextShort());
4156: assertEquals(456, s.nextShort());
4157:
4158: s = new Scanner("123 456");
4159: assertTrue(s.hasNextShort(16));
4160: assertTrue(s.hasNextShort(8));
4161: assertEquals(83, s.nextShort());
4162: assertEquals(456, s.nextShort());
4163:
4164: s = new Scanner("-123 -456 -789");
4165: assertTrue(s.hasNextShort(8));
4166: assertEquals(-123, s.nextInt());
4167: assertEquals(-456, s.nextShort());
4168: assertTrue(s.hasNextInt(16));
4169: assertEquals(-789, s.nextShort());
4170:
4171: s = new Scanner("123 456");
4172: assertTrue(s.hasNextShort());
4173: s.close();
4174: try {
4175: s.nextShort();
4176: fail("Should throw IllegalStateException");
4177: } catch (IllegalStateException e) {
4178: // expected
4179: }
4180: }
4181:
4182: /**
4183: * @throws IOException
4184: * @tests java.util.Scanner#hasNextLong(int)
4185: */
4186: public void test_hasNextLongI() throws IOException {
4187: s = new Scanner("123 456");
4188: assertTrue(s.hasNextLong(10));
4189: assertEquals(123, s.nextLong(10));
4190: assertTrue(s.hasNextLong(10));
4191: assertEquals(456, s.nextLong(10));
4192: assertFalse(s.hasNextLong(10));
4193: try {
4194: s.nextLong(10);
4195: fail("Should throw NoSuchElementException");
4196: } catch (NoSuchElementException e) {
4197: // Expected
4198: }
4199:
4200: // If the radix is different from 10
4201: s = new Scanner("123 456");
4202: assertTrue(s.hasNextLong(5));
4203: assertEquals(38, s.nextLong(5));
4204: assertFalse(s.hasNextLong(5));
4205: try {
4206: s.nextLong(5);
4207: fail("Should throw InputMismatchException");
4208: } catch (InputMismatchException e) {
4209: // Expected
4210: }
4211:
4212: // If the number is out of range
4213: s = new Scanner("123456789123456789123456789123456789");
4214: assertFalse(s.hasNextLong(10));
4215: try {
4216: s.nextLong(10);
4217: fail("Should throw InputMismatchException");
4218: } catch (InputMismatchException e) {
4219: // Expected
4220: }
4221:
4222: /*
4223: * Different locale can only recognize corresponding locale sensitive
4224: * string. ',' is used in many locales as group separator.
4225: */
4226: s = new Scanner("23,456 23,456");
4227: s.useLocale(Locale.GERMANY);
4228: assertFalse(s.hasNextShort(10));
4229: try {
4230: s.nextLong(10);
4231: fail("Should throw InputMismatchException");
4232: } catch (InputMismatchException e) {
4233: // Expected
4234: }
4235: s.useLocale(Locale.ENGLISH);
4236: // If exception is thrown out, input will not be advanced.
4237: assertTrue(s.hasNextLong(10));
4238: assertEquals(23456, s.nextLong(10));
4239: assertTrue(s.hasNextLong(10));
4240: assertEquals(23456, s.nextLong(10));
4241:
4242: /*
4243: * ''' is used in many locales as group separator.
4244: */
4245: s = new Scanner("23'456 23'456");
4246: s.useLocale(Locale.GERMANY);
4247: assertFalse(s.hasNextLong(10));
4248: try {
4249: s.nextLong(10);
4250: fail("Should throw InputMismatchException");
4251: } catch (InputMismatchException e) {
4252: // Expected
4253: }
4254: s.useLocale(new Locale("it", "CH"));
4255: // If exception is thrown out, input will not be advanced.
4256: assertTrue(s.hasNextLong(10));
4257: assertEquals(23456, s.nextLong(10));
4258: assertTrue(s.hasNextLong(10));
4259: assertEquals(23456, s.nextLong(10));
4260:
4261: /*
4262: * The input string has Arabic-Indic digits.
4263: */
4264: s = new Scanner("1\u06602 1\u06662");
4265: assertTrue(s.hasNextLong(10));
4266: assertEquals(102, s.nextLong(10));
4267: assertFalse(s.hasNextLong(5));
4268: try {
4269: s.nextLong(5);
4270: fail("Should throw InputMismatchException");
4271: } catch (InputMismatchException e) {
4272: // Expected
4273: }
4274: assertTrue(s.hasNextLong(10));
4275: assertEquals(162, s.nextLong(10));
4276:
4277: /*
4278: * '.' is used in many locales as group separator. The input string
4279: * has Arabic-Indic digits .
4280: */
4281: s = new Scanner("23.45\u0666 23.456");
4282: s.useLocale(Locale.CHINESE);
4283: assertFalse(s.hasNextLong(10));
4284: try {
4285: s.nextLong(10);
4286: fail("Should throw InputMismatchException");
4287: } catch (InputMismatchException e) {
4288: // Expected
4289: }
4290: s.useLocale(Locale.GERMANY);
4291: // If exception is thrown out, input will not be advanced.
4292: assertTrue(s.hasNextLong(10));
4293: assertEquals(23456, s.nextLong(10));
4294: assertTrue(s.hasNextLong(10));
4295: assertEquals(23456, s.nextLong(10));
4296:
4297: // The input string starts with zero
4298: s = new Scanner("03,456");
4299: s.useLocale(Locale.ENGLISH);
4300: assertFalse(s.hasNextLong(10));
4301: try {
4302: s.nextLong(10);
4303: fail("Should throw InputMismatchException");
4304: } catch (InputMismatchException e) {
4305: // Expected
4306: }
4307:
4308: s = new Scanner("03456");
4309: assertTrue(s.hasNextLong(10));
4310: assertEquals(3456, s.nextLong(10));
4311:
4312: s = new Scanner("\u06603,456");
4313: s.useLocale(Locale.ENGLISH);
4314: assertTrue(s.hasNextLong(10));
4315: assertEquals(3456, s.nextLong(10));
4316:
4317: s = new Scanner("E34");
4318: assertTrue(s.hasNextLong(16));
4319: assertEquals(3636, s.nextLong(16));
4320:
4321: /*
4322: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
4323: * respectively, but they are not differentiated.
4324: */
4325: s = new Scanner("12300");
4326: s.useLocale(Locale.CHINESE);
4327: assertTrue(s.hasNextLong(10));
4328: assertEquals(12300, s.nextLong(10));
4329:
4330: s = new Scanner("123\u0966\u0966");
4331: s.useLocale(Locale.CHINESE);
4332: assertTrue(s.hasNextLong(10));
4333: assertEquals(12300, s.nextLong(10));
4334:
4335: s = new Scanner("123\u0e50\u0e50");
4336: s.useLocale(Locale.CHINESE);
4337: assertTrue(s.hasNextLong(10));
4338: assertEquals(12300, s.nextLong(10));
4339:
4340: s = new Scanner("-123");
4341: s.useLocale(new Locale("ar", "AE"));
4342: assertTrue(s.hasNextLong(10));
4343: assertEquals(-123, s.nextLong(10));
4344:
4345: s = new Scanner("-123");
4346: s.useLocale(new Locale("mk", "MK"));
4347: assertTrue(s.hasNextLong(10));
4348: assertEquals(-123, s.nextLong(10));
4349: }
4350:
4351: /**
4352: * @throws IOException
4353: * @tests java.util.Scanner#hasNextLong(int)
4354: */
4355: public void test_hasNextLongI_cache() throws IOException {
4356: //regression for HARMONY-2063
4357: s = new Scanner("123 456");
4358: assertTrue(s.hasNextLong(16));
4359: assertEquals(291, s.nextLong());
4360: assertEquals(456, s.nextLong());
4361:
4362: s = new Scanner("123 456");
4363: assertTrue(s.hasNextLong(16));
4364: assertTrue(s.hasNextLong(8));
4365: assertEquals(83, s.nextLong());
4366: assertEquals(456, s.nextLong());
4367:
4368: s = new Scanner("-123 -456 -789");
4369: assertTrue(s.hasNextLong(8));
4370: assertEquals(-123, s.nextInt());
4371: assertEquals(-456, s.nextLong());
4372: assertTrue(s.hasNextShort(16));
4373: assertEquals(-789, s.nextLong());
4374:
4375: s = new Scanner("123 456");
4376: assertTrue(s.hasNextLong());
4377: s.close();
4378: try {
4379: s.nextLong();
4380: fail("Should throw IllegalStateException");
4381: } catch (IllegalStateException e) {
4382: // expected
4383: }
4384: }
4385:
4386: /**
4387: * @throws IOException
4388: * @tests java.util.Scanner#hasNextLong()
4389: */
4390: public void test_hasNextLong() throws IOException {
4391: s = new Scanner("123 456");
4392: assertTrue(s.hasNextLong());
4393: assertEquals(123, s.nextLong());
4394: assertTrue(s.hasNextLong());
4395: assertEquals(456, s.nextLong());
4396: assertFalse(s.hasNextLong());
4397: try {
4398: s.nextLong();
4399: fail("Should throw NoSuchElementException");
4400: } catch (NoSuchElementException e) {
4401: // Expected
4402: }
4403:
4404: // If the radix is different from 10
4405: s = new Scanner("123 456");
4406: s.useRadix(5);
4407: assertTrue(s.hasNextLong());
4408: assertEquals(38, s.nextLong());
4409: assertFalse(s.hasNextLong());
4410: try {
4411: s.nextLong();
4412: fail("Should throw InputMismatchException");
4413: } catch (InputMismatchException e) {
4414: // Expected
4415: }
4416:
4417: // If the number is out of range
4418: s = new Scanner("123456789123456789123456789123456789");
4419: assertFalse(s.hasNextLong());
4420: try {
4421: s.nextLong();
4422: fail("Should throw InputMismatchException");
4423: } catch (InputMismatchException e) {
4424: // Expected
4425: }
4426:
4427: /*
4428: * Different locale can only recognize corresponding locale sensitive
4429: * string. ',' is used in many locales as group separator.
4430: */
4431: s = new Scanner("23,456 23,456");
4432: s.useLocale(Locale.GERMANY);
4433: assertFalse(s.hasNextLong());
4434: try {
4435: s.nextLong();
4436: fail("Should throw InputMismatchException");
4437: } catch (InputMismatchException e) {
4438: // Expected
4439: }
4440: s.useLocale(Locale.ENGLISH);
4441: // If exception is thrown out, input will not be advanced.
4442: assertTrue(s.hasNextLong());
4443: assertEquals(23456, s.nextLong());
4444: assertTrue(s.hasNextLong());
4445: assertEquals(23456, s.nextLong());
4446:
4447: /*
4448: * ''' is used in many locales as group separator.
4449: */
4450: s = new Scanner("23'456 23'456");
4451: s.useLocale(Locale.GERMANY);
4452: assertFalse(s.hasNextLong());
4453: try {
4454: s.nextLong();
4455: fail("Should throw InputMismatchException");
4456: } catch (InputMismatchException e) {
4457: // Expected
4458: }
4459: s.useLocale(new Locale("it", "CH"));
4460: // If exception is thrown out, input will not be advanced.
4461: assertTrue(s.hasNextLong());
4462: assertEquals(23456, s.nextLong());
4463: assertTrue(s.hasNextLong());
4464: assertEquals(23456, s.nextLong());
4465:
4466: /*
4467: * The input string has Arabic-Indic digits.
4468: */
4469: s = new Scanner("1\u06602 1\u06662");
4470: assertEquals(102, s.nextLong());
4471: s.useRadix(5);
4472: assertFalse(s.hasNextLong());
4473: try {
4474: s.nextLong();
4475: fail("Should throw InputMismatchException");
4476: } catch (InputMismatchException e) {
4477: // Expected
4478: }
4479: s.useRadix(10);
4480: assertTrue(s.hasNextLong());
4481: assertEquals(162, s.nextLong());
4482:
4483: /*
4484: * '.' is used in many locales as group separator. The input string
4485: * has Arabic-Indic digits .
4486: */
4487: s = new Scanner("23.45\u0666 23.456");
4488: s.useLocale(Locale.CHINESE);
4489: assertFalse(s.hasNextLong());
4490: try {
4491: s.nextLong();
4492: fail("Should throw InputMismatchException");
4493: } catch (InputMismatchException e) {
4494: // Expected
4495: }
4496: s.useLocale(Locale.GERMANY);
4497: // If exception is thrown out, input will not be advanced.
4498: assertTrue(s.hasNextLong());
4499: assertEquals(23456, s.nextLong());
4500: assertTrue(s.hasNextLong());
4501: assertEquals(23456, s.nextLong());
4502:
4503: // The input string starts with zero
4504: s = new Scanner("03,456");
4505: s.useLocale(Locale.ENGLISH);
4506: assertFalse(s.hasNextLong());
4507: try {
4508: s.nextLong();
4509: fail("Should throw InputMismatchException");
4510: } catch (InputMismatchException e) {
4511: // Expected
4512: }
4513:
4514: s = new Scanner("03456");
4515: assertTrue(s.hasNextLong());
4516: assertEquals(3456, s.nextLong());
4517:
4518: s = new Scanner("\u06603,456");
4519: s.useLocale(Locale.ENGLISH);
4520: assertTrue(s.hasNextLong());
4521: assertEquals(3456, s.nextLong());
4522:
4523: s = new Scanner("E34");
4524: s.useRadix(16);
4525: assertTrue(s.hasNextLong());
4526: assertEquals(3636, s.nextLong());
4527:
4528: /*
4529: * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50'
4530: * respectively, but they are not differentiated.
4531: */
4532: s = new Scanner("12300");
4533: s.useLocale(Locale.CHINESE);
4534: assertTrue(s.hasNextLong());
4535: assertEquals(12300, s.nextLong());
4536:
4537: s = new Scanner("123\u0966\u0966");
4538: s.useLocale(Locale.CHINESE);
4539: assertTrue(s.hasNextLong());
4540: assertEquals(12300, s.nextLong());
4541:
4542: s = new Scanner("123\u0e50\u0e50");
4543: s.useLocale(Locale.CHINESE);
4544: assertTrue(s.hasNextLong());
4545: assertEquals(12300, s.nextLong());
4546:
4547: s = new Scanner("-123");
4548: s.useLocale(new Locale("ar", "AE"));
4549: assertTrue(s.hasNextLong());
4550: assertEquals(-123, s.nextLong());
4551:
4552: s = new Scanner("-123");
4553: s.useLocale(new Locale("mk", "MK"));
4554: assertTrue(s.hasNextLong());
4555: assertEquals(-123, s.nextLong());
4556: }
4557:
4558: /**
4559: * @throws IOException
4560: * @tests java.util.Scanner#nextDouble()
4561: */
4562: public void test_hasNextDouble() throws IOException {
4563: s = new Scanner("123 45\u0666. 123.4 .123 ");
4564: s.useLocale(Locale.ENGLISH);
4565: assertTrue(s.hasNextDouble());
4566: assertEquals(123.0, s.nextDouble());
4567: assertTrue(s.hasNextDouble());
4568: assertEquals(456.0, s.nextDouble());
4569: assertTrue(s.hasNextDouble());
4570: assertEquals(123.4, s.nextDouble());
4571: assertTrue(s.hasNextDouble());
4572: assertEquals(0.123, s.nextDouble());
4573: assertFalse(s.hasNextDouble());
4574: try {
4575: s.nextDouble();
4576: fail("Should throw NoSuchElementException");
4577: } catch (NoSuchElementException e) {
4578: // Expected
4579: }
4580:
4581: s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
4582: s.useLocale(Locale.ENGLISH);
4583: assertTrue(s.hasNextDouble());
4584: assertEquals(123.4, s.nextDouble());
4585: assertTrue(s.hasNextDouble());
4586: assertEquals(-456.7, s.nextDouble());
4587: assertTrue(s.hasNextDouble());
4588: assertEquals(123456.789, s.nextDouble());
4589: assertFalse(s.hasNextDouble());
4590: try {
4591: s.nextDouble();
4592: fail("Should throw InputMismatchException");
4593: } catch (InputMismatchException e) {
4594: // Expected
4595: }
4596:
4597: // Scientific notation
4598: s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
4599: s.useLocale(Locale.ENGLISH);
4600: assertTrue(s.hasNextDouble());
4601: assertEquals(1.234E12, s.nextDouble());
4602: assertTrue(s.hasNextDouble());
4603: assertEquals(-4.567E14, s.nextDouble());
4604: assertTrue(s.hasNextDouble());
4605: assertEquals(1.23456789E-5, s.nextDouble());
4606:
4607: s = new Scanner("NaN Infinity -Infinity");
4608: assertTrue(s.hasNextDouble());
4609: assertEquals(Double.NaN, s.nextDouble());
4610: assertTrue(s.hasNextDouble());
4611: assertEquals(Double.POSITIVE_INFINITY, s.nextDouble());
4612: assertTrue(s.hasNextDouble());
4613: assertEquals(Double.NEGATIVE_INFINITY, s.nextDouble());
4614:
4615: String str = String.valueOf(Double.MAX_VALUE * 2);
4616: s = new Scanner(str);
4617: assertTrue(s.hasNextDouble());
4618: assertEquals(Double.POSITIVE_INFINITY, s.nextDouble());
4619:
4620: /*
4621: * Different locale can only recognize corresponding locale sensitive
4622: * string. ',' is used in many locales as group separator.
4623: */
4624: s = new Scanner("23,456 23,456");
4625: s.useLocale(Locale.ENGLISH);
4626: assertTrue(s.hasNextDouble());
4627: assertEquals(23456.0, s.nextDouble());
4628: s.useLocale(Locale.GERMANY);
4629: assertTrue(s.hasNextDouble());
4630: assertEquals(23.456, s.nextDouble());
4631:
4632: s = new Scanner("23.456 23.456");
4633: s.useLocale(Locale.ENGLISH);
4634: assertTrue(s.hasNextDouble());
4635: assertEquals(23.456, s.nextDouble());
4636: s.useLocale(Locale.GERMANY);
4637: assertTrue(s.hasNextDouble());
4638: assertEquals(23456.0, s.nextDouble());
4639:
4640: s = new Scanner("23,456.7 23.456,7");
4641: s.useLocale(Locale.ENGLISH);
4642: assertTrue(s.hasNextDouble());
4643: assertEquals(23456.7, s.nextDouble());
4644: s.useLocale(Locale.GERMANY);
4645: assertTrue(s.hasNextDouble());
4646: assertEquals(23456.7, s.nextDouble());
4647:
4648: s = new Scanner("-123.4");
4649: s.useLocale(Locale.ENGLISH);
4650: assertTrue(s.hasNextDouble());
4651: assertEquals(-123.4, s.nextDouble());
4652:
4653: s = new Scanner("+123.4 -456.7");
4654: s.useLocale(Locale.ENGLISH);
4655: assertTrue(s.hasNextDouble());
4656: s.close();
4657: try {
4658: s.nextDouble();
4659: fail("Should throw IllegalStateException");
4660: } catch (IllegalStateException e) {
4661: //expected
4662: }
4663: }
4664:
4665: /**
4666: * @throws IOException
4667: * @tests java.util.Scanner#hasNextBigDecimal()
4668: */
4669: public void test_hasNextBigDecimal() throws IOException {
4670: s = new Scanner("123 45\u0666. 123.4 .123 ");
4671: s.useLocale(Locale.ENGLISH);
4672: assertTrue(s.hasNextBigDecimal());
4673: assertEquals(new BigDecimal("123"), s.nextBigDecimal());
4674: assertTrue(s.hasNextBigDecimal());
4675: assertEquals(new BigDecimal("456"), s.nextBigDecimal());
4676: assertTrue(s.hasNextBigDecimal());
4677: assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
4678: assertTrue(s.hasNextBigDecimal());
4679: assertEquals(new BigDecimal("0.123"), s.nextBigDecimal());
4680: assertFalse(s.hasNextBigDecimal());
4681: try {
4682: s.nextBigDecimal();
4683: fail("Should throw NoSuchElementException");
4684: } catch (NoSuchElementException e) {
4685: // Expected
4686: }
4687:
4688: s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
4689: s.useLocale(Locale.ENGLISH);
4690: assertTrue(s.hasNextBigDecimal());
4691: assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
4692: assertTrue(s.hasNextBigDecimal());
4693: assertEquals(new BigDecimal("-456.7"), s.nextBigDecimal());
4694: assertTrue(s.hasNextBigDecimal());
4695: assertEquals(new BigDecimal("123456.789"), s.nextBigDecimal());
4696: assertFalse(s.hasNextBigDecimal());
4697: try {
4698: s.nextBigDecimal();
4699: fail("Should throw InputMismatchException");
4700: } catch (InputMismatchException e) {
4701: // Expected
4702: }
4703:
4704: // Scientific notation
4705: s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
4706: s.useLocale(Locale.ENGLISH);
4707: assertTrue(s.hasNextBigDecimal());
4708: assertEquals(new BigDecimal("1.234E12"), s.nextBigDecimal());
4709: assertTrue(s.hasNextBigDecimal());
4710: assertEquals(new BigDecimal("-4.567E14"), s.nextBigDecimal());
4711: assertTrue(s.hasNextBigDecimal());
4712: assertEquals(new BigDecimal("1.23456789E-5"), s
4713: .nextBigDecimal());
4714:
4715: s = new Scanner("NaN");
4716: assertFalse(s.hasNextBigDecimal());
4717: try {
4718: s.nextBigDecimal();
4719: fail("Should throw InputMismatchException");
4720: } catch (InputMismatchException e) {
4721: // Expected
4722: }
4723:
4724: /*
4725: * Different locale can only recognize corresponding locale sensitive
4726: * string. ',' is used in many locales as group separator.
4727: */
4728: s = new Scanner("23,456 23,456");
4729: s.useLocale(Locale.ENGLISH);
4730: assertTrue(s.hasNextBigDecimal());
4731: assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
4732: s.useLocale(Locale.GERMANY);
4733: assertTrue(s.hasNextBigDecimal());
4734: assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
4735:
4736: s = new Scanner("23.456 23.456");
4737: s.useLocale(Locale.ENGLISH);
4738: assertTrue(s.hasNextBigDecimal());
4739: assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
4740: s.useLocale(Locale.GERMANY);
4741: assertTrue(s.hasNextBigDecimal());
4742: assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
4743:
4744: s = new Scanner("23,456.7");
4745: s.useLocale(Locale.ENGLISH);
4746: assertTrue(s.hasNextBigDecimal());
4747: assertEquals(new BigDecimal("23456.7"), s.nextBigDecimal());
4748:
4749: s = new Scanner("-123.4");
4750: s.useLocale(Locale.ENGLISH);
4751: assertTrue(s.hasNextBigDecimal());
4752: assertEquals(new BigDecimal("-123.4"), s.nextBigDecimal());
4753: }
4754:
4755: private static class MockStringReader extends StringReader {
4756:
4757: public MockStringReader(String param) {
4758: super (param);
4759: }
4760:
4761: public int read(CharBuffer target) throws IOException {
4762: target.append('t');
4763: target.append('e');
4764: target.append('s');
4765: target.append('t');
4766: throw new IOException();
4767: }
4768:
4769: }
4770:
4771: private static class MockStringReader2Read extends StringReader {
4772: private int timesRead = 1;
4773:
4774: public MockStringReader2Read(String param) {
4775: super (param);
4776: }
4777:
4778: public int read(CharBuffer target) throws IOException {
4779: if (timesRead == 1) {
4780: target.append('1');
4781: target.append('2');
4782: target.append('3');
4783: timesRead++;
4784: return 3;
4785: } else if (timesRead == 2) {
4786: target.append('t');
4787: timesRead++;
4788: return 1;
4789: } else {
4790: throw new IOException();
4791: }
4792: }
4793:
4794: }
4795:
4796: /**
4797: * @tests java.util.Scanner#findWithinHorizon(Pattern, int)
4798: */
4799: public void test_findWithinHorizon_LPatternI() {
4800:
4801: // This method searches through the input up to the specified search
4802: // horizon(exclusive).
4803: s = new Scanner("123test");
4804: String result = s.findWithinHorizon(Pattern
4805: .compile("\\p{Lower}"), 5);
4806: assertEquals("t", result);
4807: MatchResult mresult = s.match();
4808: assertEquals(3, mresult.start());
4809: assertEquals(4, mresult.end());
4810:
4811: s = new Scanner("12345test1234test next");
4812: /*
4813: * If the pattern is found the scanner advances past the input that
4814: * matched and returns the string that matched the pattern.
4815: */
4816: result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 2);
4817: assertEquals("12", result);
4818: mresult = s.match();
4819: assertEquals(0, mresult.start());
4820: assertEquals(2, mresult.end());
4821: // Position is now pointing at the bar. "12|345test1234test next"
4822:
4823: result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 6);
4824: assertEquals("345", result);
4825:
4826: mresult = s.match();
4827: assertEquals(2, mresult.start());
4828: assertEquals(5, mresult.end());
4829: // Position is now pointing at the bar. "12345|test1234test next"
4830:
4831: // If no such pattern is detected then the null is returned and the
4832: // scanner's position remains unchanged.
4833: result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 3);
4834: assertNull(result);
4835:
4836: try {
4837: s.match();
4838: fail("Should throw IllegalStateException");
4839: } catch (IllegalStateException e) {
4840: // expected
4841: }
4842: assertEquals("345", mresult.group());
4843: assertEquals(2, mresult.start());
4844: assertEquals(5, mresult.end());
4845: // Position is now still pointing at the bar. "12345|test1234test next"
4846:
4847: // If horizon is 0, then the horizon is ignored and this method
4848: // continues to search through the input looking for the specified
4849: // pattern without bound.
4850: result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 0);
4851: mresult = s.match();
4852: assertEquals(9, mresult.start());
4853: assertEquals(13, mresult.end());
4854: // Position is now pointing at the bar. "12345test1234|test next"
4855:
4856: assertEquals("test", s.next());
4857: mresult = s.match();
4858: assertEquals(13, mresult.start());
4859: assertEquals(17, mresult.end());
4860:
4861: assertEquals("next", s.next());
4862: mresult = s.match();
4863: assertEquals(18, mresult.start());
4864: assertEquals(22, mresult.end());
4865:
4866: try {
4867: s.findWithinHorizon((Pattern) null, -1);
4868: fail("Should throw NullPointerException");
4869: } catch (NullPointerException e) {
4870: // expected
4871: }
4872:
4873: try {
4874: s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), -1);
4875: fail("Should throw IllegalArgumentException");
4876: } catch (IllegalArgumentException e) {
4877: // expected
4878: }
4879:
4880: s.close();
4881: try {
4882: s.findWithinHorizon((Pattern) null, -1);
4883: fail("Should throw IllegalStateException");
4884: } catch (IllegalStateException e) {
4885: // expected
4886: }
4887:
4888: s = new Scanner("test");
4889: result = s.findWithinHorizon(Pattern.compile("\\w+"), 10);
4890: assertEquals("test", result);
4891:
4892: s = new Scanner("aa\n\rb");
4893: String patternStr = "^(a)$";
4894: result = s.findWithinHorizon(Pattern.compile("a"), 5);
4895: assertEquals("a", result);
4896: mresult = s.match();
4897: assertEquals(0, mresult.start());
4898: assertEquals(1, mresult.end());
4899:
4900: result = s.findWithinHorizon(Pattern.compile(patternStr,
4901: Pattern.MULTILINE), 5);
4902: assertNull(result);
4903:
4904: try {
4905: mresult = s.match();
4906: fail("Should throw IllegalStateException");
4907: } catch (IllegalStateException e) {
4908: // expected
4909: }
4910:
4911: s = new Scanner("");
4912: result = s.findWithinHorizon(Pattern.compile("^"), 0);
4913: assertEquals("", result);
4914: MatchResult matchResult = s.match();
4915: assertEquals(0, matchResult.start());
4916: assertEquals(0, matchResult.end());
4917:
4918: result = s.findWithinHorizon(Pattern.compile("$"), 0);
4919: assertEquals("", result);
4920: matchResult = s.match();
4921: assertEquals(0, matchResult.start());
4922: assertEquals(0, matchResult.end());
4923:
4924: s = new Scanner("1 fish 2 fish red fish blue fish");
4925: result = s.findWithinHorizon(Pattern
4926: .compile("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)"),
4927: 10);
4928: assertNull(result);
4929:
4930: try {
4931: mresult = s.match();
4932: fail("Should throw IllegalStateException");
4933: } catch (IllegalStateException e) {
4934: // expected
4935: }
4936: assertEquals(0, mresult.groupCount());
4937:
4938: result = s.findWithinHorizon(Pattern
4939: .compile("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)"),
4940: 100);
4941: assertEquals("1 fish 2 fish red fish blue", result);
4942: mresult = s.match();
4943: assertEquals(4, mresult.groupCount());
4944: assertEquals("1", mresult.group(1));
4945: assertEquals("2", mresult.group(2));
4946: assertEquals("red", mresult.group(3));
4947: assertEquals("blue", mresult.group(4));
4948:
4949: s = new Scanner("test");
4950: s.close();
4951: try {
4952: s.findWithinHorizon(Pattern.compile("test"), 1);
4953: fail("Should throw IllegalStateException");
4954: } catch (IllegalStateException e) {
4955: // expected
4956: }
4957:
4958: s = new Scanner("word1 WorD2 ");
4959: s.close();
4960: try {
4961: s.findWithinHorizon(Pattern.compile("\\d+"), 10);
4962: fail("should throw IllegalStateException");
4963: } catch (IllegalStateException e) {
4964: // expected
4965: }
4966:
4967: s = new Scanner("word1 WorD2 wOrd3 ");
4968: Pattern pattern = Pattern.compile("\\d+");
4969: assertEquals("1", s.findWithinHorizon(pattern, 10));
4970: assertEquals("WorD2", s.next());
4971: assertEquals("3", s.findWithinHorizon(pattern, 15));
4972:
4973: // Regression test
4974: s = new Scanner(new MockStringReader("MockStringReader"));
4975: pattern = Pattern.compile("test");
4976: result = s.findWithinHorizon(pattern, 10);
4977: assertEquals("test", result);
4978:
4979: // Test the situation when input length is longer than buffer size.
4980: StringBuilder stringBuilder = new StringBuilder();
4981: for (int i = 0; i < 1026; i++) {
4982: stringBuilder.append('a');
4983: }
4984: s = new Scanner(stringBuilder.toString());
4985: pattern = Pattern.compile("\\p{Lower}+");
4986: result = s.findWithinHorizon(pattern, 1026);
4987: assertEquals(stringBuilder.toString(), result);
4988:
4989: // Test the situation when input length is longer than buffer size and
4990: // set horizon to buffer size.
4991: stringBuilder = new StringBuilder();
4992: for (int i = 0; i < 1026; i++) {
4993: stringBuilder.append('a');
4994: }
4995: s = new Scanner(stringBuilder.toString());
4996: pattern = Pattern.compile("\\p{Lower}+");
4997: result = s.findWithinHorizon(pattern, 1022);
4998: assertEquals(1022, result.length());
4999: assertEquals(stringBuilder.subSequence(0, 1022), result);
5000:
5001: // Test the situation, under which pattern is clipped by buffer.
5002: stringBuilder = new StringBuilder();
5003: for (int i = 0; i < 1022; i++) {
5004: stringBuilder.append(' ');
5005: }
5006: stringBuilder.append("bbc");
5007: assertEquals(1025, stringBuilder.length());
5008: s = new Scanner(stringBuilder.toString());
5009: pattern = Pattern.compile("bbc");
5010: result = s.findWithinHorizon(pattern, 1025);
5011: assertEquals(3, result.length());
5012: assertEquals(stringBuilder.subSequence(1022, 1025), result);
5013:
5014: stringBuilder = new StringBuilder();
5015: for (int i = 0; i < 1026; i++) {
5016: stringBuilder.append('a');
5017: }
5018: s = new Scanner(stringBuilder.toString());
5019: pattern = Pattern.compile("\\p{Lower}+");
5020: result = s.findWithinHorizon(pattern, 0);
5021: assertEquals(stringBuilder.toString(), result);
5022:
5023: stringBuilder = new StringBuilder();
5024: for (int i = 0; i < 10240; i++) {
5025: stringBuilder.append('-');
5026: }
5027: stringBuilder.replace(0, 2, "aa");
5028: s = new Scanner(stringBuilder.toString());
5029: result = s.findWithinHorizon(Pattern.compile("aa"), 0);
5030: assertEquals("aa", result);
5031:
5032: s = new Scanner("aaaa");
5033: result = s.findWithinHorizon(Pattern.compile("a*"), 0);
5034: assertEquals("aaaa", result);
5035: }
5036:
5037: /**
5038: * @tests java.util.Scanner#findInLine(Pattern)
5039: */
5040: public void test_findInLine_LPattern() {
5041:
5042: Scanner s = new Scanner("");
5043: try {
5044: s.findInLine((Pattern) null);
5045: fail("Should throw NullPointerException");
5046: } catch (NullPointerException e) {
5047: // Expected
5048: }
5049: String result = s.findInLine(Pattern.compile("^"));
5050: assertEquals("", result);
5051: MatchResult matchResult = s.match();
5052: assertEquals(0, matchResult.start());
5053: assertEquals(0, matchResult.end());
5054:
5055: result = s.findInLine(Pattern.compile("$"));
5056: assertEquals("", result);
5057: matchResult = s.match();
5058: assertEquals(0, matchResult.start());
5059: assertEquals(0, matchResult.end());
5060:
5061: /*
5062: * When we use the operation of findInLine(Pattern), the match region
5063: * should not span the line separator.
5064: */
5065: s = new Scanner("aa\nb.b");
5066: result = s.findInLine(Pattern.compile("a\nb*"));
5067: assertNull(result);
5068:
5069: s = new Scanner("aa\nbb.b");
5070: result = s.findInLine(Pattern.compile("\\."));
5071: assertNull(result);
5072:
5073: s = new Scanner("abcd1234test\n");
5074: result = s.findInLine(Pattern.compile("\\p{Lower}+"));
5075: assertEquals("abcd", result);
5076: matchResult = s.match();
5077: assertEquals(0, matchResult.start());
5078: assertEquals(4, matchResult.end());
5079:
5080: result = s.findInLine(Pattern.compile("\\p{Digit}{5}"));
5081: assertNull(result);
5082: try {
5083: matchResult = s.match();
5084: fail("Should throw IllegalStateException");
5085: } catch (IllegalStateException e) {
5086: // expected
5087: }
5088: assertEquals(0, matchResult.start());
5089: assertEquals(4, matchResult.end());
5090:
5091: result = s.findInLine(Pattern.compile("\\p{Lower}+"));
5092: assertEquals("test", result);
5093: matchResult = s.match();
5094: assertEquals(8, matchResult.start());
5095: assertEquals(12, matchResult.end());
5096:
5097: char[] chars = new char[2048];
5098: Arrays.fill(chars, 'a');
5099: StringBuilder stringBuilder = new StringBuilder();
5100: stringBuilder.append(chars);
5101: stringBuilder.append("1234");
5102: s = new Scanner(stringBuilder.toString());
5103: result = s.findInLine(Pattern.compile("\\p{Digit}+"));
5104: assertEquals("1234", result);
5105: matchResult = s.match();
5106: assertEquals(2048, matchResult.start());
5107: assertEquals(2052, matchResult.end());
5108:
5109: s = new Scanner("test");
5110: s.close();
5111: try {
5112: s.findInLine((Pattern) null);
5113: fail("Should throw IllegalStateException");
5114: } catch (IllegalStateException e) {
5115: // expected
5116: }
5117:
5118: s = new Scanner("test1234\n1234 test");
5119: result = s.findInLine(Pattern.compile("test"));
5120: assertEquals("test", result);
5121: matchResult = s.match();
5122: assertEquals(0, matchResult.start());
5123: assertEquals(4, matchResult.end());
5124:
5125: int number = s.nextInt();
5126: assertEquals(1234, number);
5127: matchResult = s.match();
5128: assertEquals(4, matchResult.start());
5129: assertEquals(8, matchResult.end());
5130:
5131: result = s.next();
5132: assertEquals("1234", result);
5133: matchResult = s.match();
5134: assertEquals(9, matchResult.start());
5135: assertEquals(13, matchResult.end());
5136:
5137: result = s.findInLine(Pattern.compile("test"));
5138: assertEquals("test", result);
5139: matchResult = s.match();
5140: assertEquals(14, matchResult.start());
5141: assertEquals(18, matchResult.end());
5142:
5143: s = new Scanner("test\u0085\ntest");
5144: result = s.findInLine("est");
5145: assertEquals("est", result);
5146: result = s.findInLine("est");
5147: assertEquals("est", result);
5148:
5149: s = new Scanner("test\ntest");
5150: result = s.findInLine("est");
5151: assertEquals("est", result);
5152: result = s.findInLine("est");
5153: assertEquals("est", result);
5154:
5155: s = new Scanner("test\n123\ntest");
5156: result = s.findInLine("est");
5157: assertEquals("est", result);
5158: result = s.findInLine("est");
5159: // RI fails. It is a RI's bug.
5160: assertNull(result);
5161: }
5162:
5163: /**
5164: * @tests java.util.Scanner#findInLine(String)
5165: */
5166: public void test_findInLine_LString() {
5167: s = new Scanner("test");
5168: try {
5169: s.findInLine((String) null);
5170: fail("Should throw NullPointerException");
5171: } catch (NullPointerException e) {
5172: // expected
5173: }
5174:
5175: s.close();
5176: try {
5177: s.findInLine((String) null);
5178: fail("Should throw NullPointerException");
5179: } catch (NullPointerException e) {
5180: // expected
5181: }
5182: try {
5183: s.findInLine("test");
5184: fail("Should throw IllegalStateException");
5185: } catch (IllegalStateException e) {
5186: // exptected
5187: }
5188: }
5189:
5190: /**
5191: * @tests java.util.Scanner#skip(Pattern)
5192: */
5193: public void test_skip_LPattern() {
5194: s = new Scanner("test");
5195: try {
5196: s.skip((String) null);
5197: fail("Should throw NullPointerException");
5198: } catch (NullPointerException e) {
5199: // expected
5200: }
5201:
5202: // If pattern does not match, NoSuchElementException will be thrown out.
5203: s = new Scanner("1234");
5204: try {
5205: s.skip(Pattern.compile("\\p{Lower}"));
5206: fail("Should throw NoSuchElementException");
5207: } catch (NoSuchElementException e) {
5208: // expected
5209: }
5210: // Then, no matchResult will be thrown out.
5211: try {
5212: s.match();
5213: fail("Should throw IllegalStateException");
5214: } catch (IllegalStateException e) {
5215: // expected
5216: }
5217:
5218: s.skip(Pattern.compile("\\p{Digit}"));
5219: MatchResult matchResult = s.match();
5220: assertEquals(0, matchResult.start());
5221: assertEquals(1, matchResult.end());
5222:
5223: s.skip(Pattern.compile("\\p{Digit}+"));
5224: matchResult = s.match();
5225: assertEquals(1, matchResult.start());
5226: assertEquals(4, matchResult.end());
5227:
5228: s.close();
5229: try {
5230: s.skip(Pattern.compile("test"));
5231: fail("Should throw IllegalStateException");
5232: } catch (IllegalStateException e) {
5233: // expected
5234: }
5235:
5236: MockStringReader2Read reader = new MockStringReader2Read("test");
5237: s = new Scanner(reader);
5238: try {
5239: s.skip(Pattern.compile("\\p{Digit}{4}"));
5240: fail("Should throw NoSuchElementException");
5241: } catch (NoSuchElementException e) {
5242: // expected
5243: }
5244: try {
5245: s.match();
5246: fail("Should throw IllegalStateException");
5247: } catch (IllegalStateException e) {
5248: // expected
5249: }
5250: s.skip(Pattern.compile("\\p{Digit}{3}\\p{Lower}"));
5251: matchResult = s.match();
5252: assertEquals(0, matchResult.start());
5253: assertEquals(4, matchResult.end());
5254:
5255: s.close();
5256: try {
5257: s.skip((Pattern) null);
5258: fail("Should throw IllegalStateException");
5259: } catch (IllegalStateException e) {
5260: // expected
5261: }
5262:
5263: StringBuilder stringBuilder = new StringBuilder();
5264: char[] chars = new char[1024];
5265: Arrays.fill(chars, 'a');
5266: stringBuilder.append(chars);
5267: stringBuilder.append('3');
5268: s = new Scanner(stringBuilder.toString());
5269: s.skip(Pattern.compile("\\p{Lower}+\\p{Digit}"));
5270: matchResult = s.match();
5271: assertEquals(0, matchResult.start());
5272: assertEquals(1025, matchResult.end());
5273:
5274: // Large amount of input may be cached
5275: chars = new char[102400];
5276: Arrays.fill(chars, 'a');
5277: stringBuilder = new StringBuilder();
5278: stringBuilder.append(chars);
5279: s = new Scanner(stringBuilder.toString());
5280: s.skip(Pattern.compile(".*"));
5281: matchResult = s.match();
5282: assertEquals(0, matchResult.start());
5283: assertEquals(102400, matchResult.end());
5284:
5285: // skip something without risking a NoSuchElementException
5286: s.skip(Pattern.compile("[ \t]*"));
5287: matchResult = s.match();
5288: assertEquals(102400, matchResult.start());
5289: assertEquals(102400, matchResult.end());
5290: }
5291:
5292: /**
5293: * @tests java.util.Scanner#skip(String)
5294: */
5295: public void test_skip_LString() {
5296: s = new Scanner("test");
5297: try {
5298: s.skip((String) null);
5299: fail("Should throw NullPointerException");
5300: } catch (NullPointerException e) {
5301: // expected
5302: }
5303: }
5304:
5305: /**
5306: * @throws IOException
5307: * @tests java.util.Scanner#nextDouble()
5308: */
5309: public void test_nextDouble() throws IOException {
5310: s = new Scanner("123 45\u0666. 123.4 .123 ");
5311: s.useLocale(Locale.ENGLISH);
5312: assertEquals(123.0, s.nextDouble());
5313: assertEquals(456.0, s.nextDouble());
5314: assertEquals(123.4, s.nextDouble());
5315: assertEquals(0.123, s.nextDouble());
5316: try {
5317: s.nextDouble();
5318: fail("Should throw NoSuchElementException");
5319: } catch (NoSuchElementException e) {
5320: // Expected
5321: }
5322:
5323: s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
5324: s.useLocale(Locale.ENGLISH);
5325: assertEquals(123.4, s.nextDouble());
5326: assertEquals(-456.7, s.nextDouble());
5327: assertEquals(123456.789, s.nextDouble());
5328: try {
5329: s.nextDouble();
5330: fail("Should throw InputMismatchException");
5331: } catch (InputMismatchException e) {
5332: // Expected
5333: }
5334:
5335: // Scientific notation
5336: s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
5337: s.useLocale(Locale.ENGLISH);
5338: assertEquals(1.234E12, s.nextDouble());
5339: assertEquals(-4.567E14, s.nextDouble());
5340: assertEquals(1.23456789E-5, s.nextDouble());
5341:
5342: s = new Scanner("NaN Infinity -Infinity");
5343: assertEquals(Double.NaN, s.nextDouble());
5344: assertEquals(Double.POSITIVE_INFINITY, s.nextDouble());
5345: assertEquals(Double.NEGATIVE_INFINITY, s.nextDouble());
5346:
5347: //The following test case fails on RI
5348: s = new Scanner("\u221e");
5349: s.useLocale(Locale.ENGLISH);
5350: assertEquals(Double.POSITIVE_INFINITY, s.nextDouble());
5351:
5352: String str = String.valueOf(Double.MAX_VALUE * 2);
5353: s = new Scanner(str);
5354: assertEquals(Double.POSITIVE_INFINITY, s.nextDouble());
5355:
5356: /*
5357: * Different locale can only recognize corresponding locale sensitive
5358: * string. ',' is used in many locales as group separator.
5359: */
5360: s = new Scanner("23,456 23,456");
5361: s.useLocale(Locale.ENGLISH);
5362: assertEquals(23456.0, s.nextDouble());
5363: s.useLocale(Locale.GERMANY);
5364: assertEquals(23.456, s.nextDouble());
5365:
5366: s = new Scanner("23.456 23.456");
5367: s.useLocale(Locale.ENGLISH);
5368: assertEquals(23.456, s.nextDouble());
5369: s.useLocale(Locale.GERMANY);
5370: assertEquals(23456.0, s.nextDouble());
5371:
5372: s = new Scanner("23,456.7 23.456,7");
5373: s.useLocale(Locale.ENGLISH);
5374: assertEquals(23456.7, s.nextDouble());
5375: s.useLocale(Locale.GERMANY);
5376: assertEquals(23456.7, s.nextDouble());
5377:
5378: s = new Scanner("-123.4");
5379: s.useLocale(Locale.ENGLISH);
5380: assertEquals(-123.4, s.nextDouble());
5381: }
5382:
5383: /**
5384: * @throws IOException
5385: * @tests java.util.Scanner#nextBigDecimal()
5386: */
5387: public void test_nextBigDecimal() throws IOException {
5388: s = new Scanner("123 45\u0666. 123.4 .123 ");
5389: s.useLocale(Locale.ENGLISH);
5390: assertEquals(new BigDecimal("123"), s.nextBigDecimal());
5391: assertEquals(new BigDecimal("456"), s.nextBigDecimal());
5392: assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
5393: assertEquals(new BigDecimal("0.123"), s.nextBigDecimal());
5394: try {
5395: s.nextBigDecimal();
5396: fail("Should throw NoSuchElementException");
5397: } catch (NoSuchElementException e) {
5398: // Expected
5399: }
5400:
5401: s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4");
5402: s.useLocale(Locale.ENGLISH);
5403: assertEquals(new BigDecimal("123.4"), s.nextBigDecimal());
5404: assertEquals(new BigDecimal("-456.7"), s.nextBigDecimal());
5405: assertEquals(new BigDecimal("123456.789"), s.nextBigDecimal());
5406: try {
5407: s.nextBigDecimal();
5408: fail("Should throw InputMismatchException");
5409: } catch (InputMismatchException e) {
5410: // Expected
5411: }
5412:
5413: // Scientific notation
5414: s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10");
5415: s.useLocale(Locale.ENGLISH);
5416: assertEquals(new BigDecimal("1.234E12"), s.nextBigDecimal());
5417: assertEquals(new BigDecimal("-4.567E14"), s.nextBigDecimal());
5418: assertEquals(new BigDecimal("1.23456789E-5"), s
5419: .nextBigDecimal());
5420:
5421: s = new Scanner("NaN");
5422: try {
5423: s.nextBigDecimal();
5424: fail("Should throw InputMismatchException");
5425: } catch (InputMismatchException e) {
5426: // Expected
5427: }
5428:
5429: /*
5430: * Different locale can only recognize corresponding locale sensitive
5431: * string. ',' is used in many locales as group separator.
5432: */
5433: s = new Scanner("23,456 23,456");
5434: s.useLocale(Locale.ENGLISH);
5435: assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
5436: s.useLocale(Locale.GERMANY);
5437: assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
5438:
5439: s = new Scanner("23.456 23.456");
5440: s.useLocale(Locale.ENGLISH);
5441: assertEquals(new BigDecimal("23.456"), s.nextBigDecimal());
5442: s.useLocale(Locale.GERMANY);
5443: assertEquals(new BigDecimal("23456"), s.nextBigDecimal());
5444:
5445: s = new Scanner("23,456.7");
5446: s.useLocale(Locale.ENGLISH);
5447: assertEquals(new BigDecimal("23456.7"), s.nextBigDecimal());
5448:
5449: s = new Scanner("-123.4");
5450: s.useLocale(Locale.ENGLISH);
5451: assertEquals(new BigDecimal("-123.4"), s.nextBigDecimal());
5452: }
5453:
5454: /**
5455: * @tests java.util.Scanner#toString()
5456: */
5457: public void test_toString() {
5458: s = new Scanner("test");
5459: assertNotNull(s.toString());
5460: }
5461:
5462: /**
5463: * @tests java.util.Scanner#nextLine()
5464: */
5465: public void test_nextLine() {
5466: s = new Scanner("");
5467: s.close();
5468: try {
5469: s.nextLine();
5470: fail("Should throw IllegalStateException");
5471: } catch (IllegalStateException e) {
5472: // expected
5473: }
5474:
5475: s = new Scanner("test\r\ntest");
5476: String result = s.nextLine();
5477: assertEquals("test", result);
5478: MatchResult matchResult = s.match();
5479: assertEquals(0, matchResult.start());
5480: assertEquals(6, matchResult.end());
5481:
5482: s = new Scanner("\u0085");
5483: result = s.nextLine();
5484: assertEquals("", result);
5485: matchResult = s.match();
5486: assertEquals(0, matchResult.start());
5487: assertEquals(1, matchResult.end());
5488:
5489: s = new Scanner("\u2028");
5490: result = s.nextLine();
5491: assertEquals("", result);
5492: matchResult = s.match();
5493: assertEquals(0, matchResult.start());
5494: assertEquals(1, matchResult.end());
5495:
5496: s = new Scanner("\u2029");
5497: result = s.nextLine();
5498: assertEquals("", result);
5499: matchResult = s.match();
5500: assertEquals(0, matchResult.start());
5501: assertEquals(1, matchResult.end());
5502:
5503: s = new Scanner("");
5504: try {
5505: result = s.nextLine();
5506: fail("Should throw NoSuchElementException");
5507: } catch (NoSuchElementException e) {
5508: // expected
5509: }
5510: try {
5511: s.match();
5512: fail("Should throw IllegalStateException");
5513: } catch (IllegalStateException e) {
5514: // expected
5515: }
5516:
5517: s = new Scanner("Ttest");
5518: result = s.nextLine();
5519: assertEquals("Ttest", result);
5520: matchResult = s.match();
5521: assertEquals(0, matchResult.start());
5522: assertEquals(5, matchResult.end());
5523:
5524: s = new Scanner("\r\n");
5525: result = s.nextLine();
5526: assertEquals("", result);
5527: matchResult = s.match();
5528: assertEquals(0, matchResult.start());
5529: assertEquals(2, matchResult.end());
5530:
5531: char[] chars = new char[1024];
5532: Arrays.fill(chars, 'a');
5533: StringBuilder stringBuilder = new StringBuilder();
5534: stringBuilder.append(chars);
5535: chars = new char[] { '+', '-' };
5536: stringBuilder.append(chars);
5537: stringBuilder.append("\u2028");
5538: s = new Scanner(stringBuilder.toString());
5539: result = s.nextLine();
5540:
5541: assertEquals(stringBuilder.toString().substring(0, 1026),
5542: result);
5543: matchResult = s.match();
5544: assertEquals(0, matchResult.start());
5545: assertEquals(1027, matchResult.end());
5546:
5547: chars = new char[1023];
5548: Arrays.fill(chars, 'a');
5549: stringBuilder = new StringBuilder();
5550: stringBuilder.append(chars);
5551: stringBuilder.append("\r\n");
5552: s = new Scanner(stringBuilder.toString());
5553: result = s.nextLine();
5554:
5555: assertEquals(stringBuilder.toString().substring(0, 1023),
5556: result);
5557: matchResult = s.match();
5558: assertEquals(0, matchResult.start());
5559: assertEquals(1025, matchResult.end());
5560:
5561: s = new Scanner(" ");
5562: result = s.nextLine();
5563: assertEquals(" ", result);
5564:
5565: s = new Scanner("test\n\n\n");
5566: result = s.nextLine();
5567: assertEquals("test", result);
5568: matchResult = s.match();
5569: assertEquals(0, matchResult.start());
5570: assertEquals(5, matchResult.end());
5571: result = s.nextLine();
5572: matchResult = s.match();
5573: assertEquals(5, matchResult.start());
5574: assertEquals(6, matchResult.end());
5575:
5576: s = new Scanner("\n\n\n");
5577: result = s.nextLine();
5578: assertEquals("", result);
5579: matchResult = s.match();
5580: assertEquals(0, matchResult.start());
5581: assertEquals(1, matchResult.end());
5582: result = s.nextLine();
5583: matchResult = s.match();
5584: assertEquals(1, matchResult.start());
5585: assertEquals(2, matchResult.end());
5586:
5587: s = new Scanner("123 test\n ");
5588: int value = s.nextInt();
5589: assertEquals(123, value);
5590:
5591: result = s.nextLine();
5592: assertEquals(" test", result);
5593:
5594: s = new Scanner("test\n ");
5595: result = s.nextLine();
5596: assertEquals("test", result);
5597:
5598: // Regression test for Harmony-4774
5599: class CountReadable implements Readable {
5600: int counter = 0;
5601:
5602: public int read(CharBuffer charBuffer) throws IOException {
5603: counter++;
5604: charBuffer.append("hello\n");
5605: return 6;
5606: }
5607: }
5608: CountReadable cr = new CountReadable();
5609: s = new Scanner(cr);
5610: result = s.nextLine();
5611: // We expect read() to be called only once, otherwise we see the problem
5612: // when reading from System.in described in Harmony-4774
5613: assertEquals(1, cr.counter);
5614: assertEquals("hello", result);
5615: }
5616:
5617: /**
5618: * @tests java.util.Scanner#hasNextLine()
5619: */
5620: public void test_hasNextLine() {
5621:
5622: s = new Scanner("");
5623: s.close();
5624: try {
5625: s.hasNextLine();
5626: fail("Should throw IllegalStateException");
5627: } catch (IllegalStateException e) {
5628: // expected
5629: }
5630:
5631: s = new Scanner("test\r\ntest");
5632: boolean result = s.hasNextLine();
5633: assertTrue(result);
5634: MatchResult matchResult = s.match();
5635: assertEquals(0, matchResult.start());
5636: assertEquals(6, matchResult.end());
5637:
5638: s = new Scanner("\u0085");
5639: result = s.hasNextLine();
5640: assertTrue(result);
5641: matchResult = s.match();
5642: assertEquals(0, matchResult.start());
5643: assertEquals(1, matchResult.end());
5644:
5645: s = new Scanner("\u2028");
5646: result = s.hasNextLine();
5647: assertTrue(result);
5648: matchResult = s.match();
5649: assertEquals(0, matchResult.start());
5650: assertEquals(1, matchResult.end());
5651:
5652: s = new Scanner("\u2029");
5653: result = s.hasNextLine();
5654: assertTrue(result);
5655: matchResult = s.match();
5656: assertEquals(0, matchResult.start());
5657: assertEquals(1, matchResult.end());
5658:
5659: s = new Scanner("test\n");
5660: assertTrue(s.hasNextLine());
5661: matchResult = s.match();
5662: assertEquals(0, matchResult.start());
5663: assertEquals(5, matchResult.end());
5664:
5665: char[] chars = new char[2048];
5666: Arrays.fill(chars, 'a');
5667: StringBuilder stringBuilder = new StringBuilder();
5668: stringBuilder.append(chars);
5669: s = new Scanner(stringBuilder.toString());
5670: result = s.hasNextLine();
5671: assertTrue(result);
5672:
5673: matchResult = s.match();
5674: assertEquals(0, matchResult.start());
5675: assertEquals(2048, matchResult.end());
5676:
5677: s = new Scanner("\n\n\n");
5678: assertTrue(s.hasNextLine());
5679: matchResult = s.match();
5680: assertEquals(0, matchResult.start());
5681: assertEquals(1, matchResult.end());
5682:
5683: // The scanner will not advance any input.
5684: assertTrue(s.hasNextLine());
5685: matchResult = s.match();
5686: assertEquals(0, matchResult.start());
5687: assertEquals(1, matchResult.end());
5688: }
5689:
5690: protected void setUp() throws Exception {
5691: super .setUp();
5692:
5693: server = new ServerSocket(0);
5694: address = new InetSocketAddress("127.0.0.1", server
5695: .getLocalPort());
5696:
5697: client = SocketChannel.open();
5698: client.connect(address);
5699: serverSocket = server.accept();
5700:
5701: os = serverSocket.getOutputStream();
5702: }
5703:
5704: protected void tearDown() throws Exception {
5705: super .tearDown();
5706:
5707: try {
5708: serverSocket.close();
5709: } catch (Exception e) {
5710:
5711: }
5712: try {
5713: client.close();
5714: } catch (Exception e) {
5715: // do nothing
5716: }
5717: try {
5718: server.close();
5719: } catch (Exception e) {
5720: // do nothing
5721: }
5722: }
5723: }
|