You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/06/08 06:18:31 UTC

svn commit: r412645 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Scanner.java test/java/tests/api/java/util/AllTests.java test/java/tests/api/java/util/ScannerTest.java

Author: mloenko
Date: Wed Jun  7 21:18:29 2006
New Revision: 412645

URL: http://svn.apache.org/viewvc?rev=412645&view=rev
Log:
improvement from HARMONY-567
[classlib][luni]java.util.Scanner constructors not implemented

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java?rev=412645&r1=412644&r2=412645&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java Wed Jun  7 21:18:29 2006
@@ -14,11 +14,20 @@
  */
 package java.util;
 
+import java.io.Closeable;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.channels.Channels;
 import java.nio.channels.ReadableByteChannel;
+import java.nio.charset.Charset;
 import java.util.regex.MatchResult;
 import java.util.regex.Pattern;
 
@@ -43,40 +52,142 @@
  */
 public final class Scanner implements Iterator<String> {
 
+    private Readable input;
+
+    private IOException lastIOException;
+
+    private boolean closed = false; // used by find and nextXXX operation
+
+    /**
+     * Constructs a scanner that uses File as its input. The default charset is
+     * applied when reading the file.
+     * 
+     * @param src
+     *            the file to be scanned
+     * @throws FileNotFoundException
+     *             if the specified file is not found
+     */
     public Scanner(File src) throws FileNotFoundException {
-        throw new NotYetImplementedException();
+        this(src, Charset.defaultCharset().name());
     }
 
+    /**
+     * Constructs a scanner that uses File as its input. The specified charset
+     * is applied when reading the file.
+     * 
+     * @param src
+     *            the file to be scanned
+     * @param charsetName
+     *            the name of the encoding type of the file
+     * @throws FileNotFoundException
+     *             if the specified file is not found
+     */
     public Scanner(File src, String charsetName) throws FileNotFoundException {
-        throw new NotYetImplementedException();
-    }
-
+        FileInputStream fis = new FileInputStream(src);
+        try {
+            input = new InputStreamReader(fis, charsetName);
+        } catch (UnsupportedEncodingException e) {
+            try {
+                fis.close();
+            } catch (IOException ioException) {
+                // ignore
+            }
+            throw new IllegalArgumentException(e.getMessage());
+        }
+    }
+
+    /**
+     * Constructs a scanner that uses String as its input.
+     * 
+     * @param src
+     *            the string to be scanned
+     */
     public Scanner(String src) {
-        throw new NotYetImplementedException();
+        input = new StringReader(src);
     }
 
+    /**
+     * Constructs a scanner that uses InputStream as its input. The default
+     * charset is applied when decoding the input.
+     * 
+     * @param src
+     *            the input stream to be scanned
+     */
     public Scanner(InputStream src) {
-        throw new NotYetImplementedException();
+        this(src, Charset.defaultCharset().name());
     }
 
+    /**
+     * Constructs a scanner that uses InputStream as its input. The specified
+     * charset is applied when decoding the input.
+     * 
+     * @param src
+     *            the input stream to be scanned
+     * @param charsetName
+     *            the encoding type of the input stream
+     */
     public Scanner(InputStream src, String charsetName) {
-        throw new NotYetImplementedException();
+        try {
+            input = new InputStreamReader(src, charsetName);
+        } catch (UnsupportedEncodingException e) {
+            throw new IllegalArgumentException(e.getMessage());
+        }
     }
 
-    public Scanner(ReadableByteChannel src) {
-        throw new NotYetImplementedException();
+    /**
+     * Constructs a scanner that uses Readable as its input.
+     * 
+     * @param src
+     *            the Readable to be scanned
+     */
+    public Scanner(Readable src) {
+        input = src;
     }
 
-    public Scanner(Readable src) {
-        throw new NotYetImplementedException();
+    /**
+     * Constructs a scanner that uses ReadableByteChannel as its input. The
+     * default charset is applied when decoding the input.
+     * 
+     * @param src
+     *            the ReadableByteChannel to be scanned
+     */
+    public Scanner(ReadableByteChannel src) {
+        this(src, Charset.defaultCharset().name());
     }
 
+    /**
+     * Constructs a scanner that uses ReadableByteChannel as its input. The
+     * specified charset is applied when decoding the input.
+     * 
+     * @param src
+     *            the ReadableByteChannel to be scanned
+     * @param charsetName
+     *            the encoding type of the content in the ReadableByteChannel
+     */
     public Scanner(ReadableByteChannel src, String charsetName) {
-        throw new NotYetImplementedException();
+        try {
+            input = new InputStreamReader(Channels.newInputStream(src),
+                    charsetName);
+        } catch (UnsupportedEncodingException e) {
+            throw new IllegalArgumentException(e.getMessage());
+        }
     }
 
+    /**
+     * Closes the underlying input if the input implements Closeable. If the
+     * scanner has been closed, this method will take no effect. The scanning
+     * operation after calling this method will throw IllegalStateException
+     * 
+     */
     public void close() {
-        throw new NotYetImplementedException();
+        if (input instanceof Closeable && !closed) {
+            try {
+                ((Closeable) input).close();
+            } catch (IOException e) {
+                lastIOException = e;
+            }
+        }
+        closed = true;
     }
 
     public Pattern delimiter() {
@@ -171,8 +282,14 @@
         throw new NotYetImplementedException();
     }
 
+    /**
+     * returns the last IOException thrown when reading the underlying input. If
+     * no exception is thrown, return null.
+     * 
+     * @return the last IOException thrown
+     */
     public IOException ioException() {
-        throw new NotYetImplementedException();
+        return lastIOException;
     }
 
     public Locale locale() {
@@ -195,15 +312,15 @@
         throw new NotYetImplementedException();
     }
 
-    public boolean nextBigDecimal() {
+    public BigDecimal nextBigDecimal() {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextBigInteger() {
+    public BigInteger nextBigInteger() {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextBigInteger(int radix) {
+    public BigInteger nextBigInteger(int radix) {
         throw new NotYetImplementedException();
     }
 
@@ -211,47 +328,47 @@
         throw new NotYetImplementedException();
     }
 
-    public boolean nextByte() {
+    public byte nextByte() {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextByte(int radix) {
+    public byte nextByte(int radix) {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextDouble() {
+    public double nextDouble() {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextFloat() {
+    public float nextFloat() {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextInt() {
+    public int nextInt() {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextInt(int radix) {
+    public int nextInt(int radix) {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextLine() {
+    public String nextLine() {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextLong() {
+    public long nextLong() {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextLong(int radix) {
+    public long nextLong(int radix) {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextShort() {
+    public short nextShort() {
         throw new NotYetImplementedException();
     }
 
-    public boolean nextShort(int radix) {
+    public short nextShort(int radix) {
         throw new NotYetImplementedException();
     }
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java?rev=412645&r1=412644&r2=412645&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java Wed Jun  7 21:18:29 2006
@@ -78,6 +78,7 @@
 		suite.addTestSuite(PropertyResourceBundleTest.class);
 		suite.addTestSuite(RandomTest.class);
 		suite.addTestSuite(ResourceBundleTest.class);
+        suite.addTestSuite(ScannerTest.class);
 		suite.addTestSuite(SimpleTimeZoneTest.class);
 		suite.addTestSuite(StackTest.class);
 		suite.addTestSuite(StringTokenizerTest.class);

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java?rev=412645&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java Wed Jun  7 21:18:29 2006
@@ -0,0 +1,211 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.util;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PipedInputStream;
+import java.io.StringReader;
+import java.nio.CharBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.charset.Charset;
+import java.util.Scanner;
+
+import junit.framework.TestCase;
+
+public class ScannerTest extends TestCase {
+
+    Scanner s;
+
+    private static class MockCloseable implements Closeable, Readable {
+
+        public void close() throws IOException {
+            throw new IOException();
+        }
+
+        public int read(CharBuffer cb) throws IOException {
+            return 0;
+        }
+
+    }
+
+    /**
+     * @tests java.util.Scanner#Scanner(File)
+     */
+    public void test_ConstructorLjava_io_File() throws IOException {
+        File tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
+        s = new Scanner(tmpFile);
+        assertNotNull(s);
+        s.close();
+        assertTrue(tmpFile.delete());
+
+        try {
+            s = new Scanner(tmpFile);
+            fail("should throw FileNotFoundException");
+        } catch (FileNotFoundException e) {
+            // expected
+        }
+
+        // TODO: test if the default charset is used.
+    }
+
+    /**
+     * @tests java.util.Scanner#Scanner(File, String)
+     */
+    public void test_ConstructorLjava_io_FileLjava_lang_String()
+            throws IOException {
+        File tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
+        s = new Scanner(tmpFile, Charset.defaultCharset().name());
+        assertNotNull(s);
+        s.close();
+        assertTrue(tmpFile.delete());
+
+        try {
+            s = new Scanner(tmpFile, Charset.defaultCharset().name());
+            fail("should throw FileNotFoundException");
+        } catch (FileNotFoundException e) {
+            // expected
+        }
+
+        tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
+        try {
+            s = new Scanner(tmpFile, "invalid charset");
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        // fail on RI. File is opened but not closed when exception is thrown on
+        // RI.
+        assertTrue(tmpFile.delete());
+
+        // TODO: test if the specified charset is used.
+    }
+
+    /**
+     * @tests java.util.Scanner#Scanner(InputStream)
+     */
+    public void test_ConstructorLjava_io_InputStream() {
+        s = new Scanner(new PipedInputStream());
+        assertNotNull(s);
+        s.close();
+
+        // TODO: test if the default charset is used.
+    }
+
+    /**
+     * @tests java.util.Scanner#Scanner(InputStream, String)
+     */
+    public void test_ConstructorLjava_io_InputStreamLjava_lang_String() {
+        s = new Scanner(new PipedInputStream(), Charset.defaultCharset().name());
+        assertNotNull(s);
+        s.close();
+
+        try {
+            s = new Scanner(new PipedInputStream(), "invalid charset");
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        // TODO: test if the specified charset is used.
+    }
+
+    /**
+     * @tests java.util.Scanner#Scanner(Readable)
+     */
+    public void test_ConstructorLjava_lang_Readable() {
+        s = new Scanner(new StringReader("test string"));
+        assertNotNull(s);
+        s.close();
+    }
+
+    /**
+     * @tests java.util.Scanner#Scanner(ReadableByteChannel)
+     */
+    public void test_ConstructorLjava_nio_channels_ReadableByteChannel()
+            throws IOException {
+        File tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
+        FileChannel fc = new FileOutputStream(tmpFile).getChannel();
+        s = new Scanner(fc);
+        assertNotNull(s);
+        s.close();
+        assertTrue(tmpFile.delete());
+
+        // TODO: test if the default charset is used.
+    }
+
+    /**
+     * @tests java.util.Scanner#Scanner(ReadableByteChannel, String)
+     */
+    public void test_ConstructorLjava_nio_channels_ReadableByteChannelLjava_lang_String()
+            throws IOException {
+        File tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
+        FileChannel fc = new FileOutputStream(tmpFile).getChannel();
+        s = new Scanner(fc, Charset.defaultCharset().name());
+        assertNotNull(s);
+        s.close();
+
+        fc = new FileOutputStream(tmpFile).getChannel();
+        try {
+            s = new Scanner(fc, "invalid charset");
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        fc.close();
+        assertTrue(tmpFile.delete());
+
+        // TODO: test if the specified charset is used.
+    }
+
+    /**
+     * @tests java.util.Scanner#Scanner(String)
+     */
+    public void test_ConstructorLjava_lang_String() {
+        s = new Scanner("test string");
+        assertNotNull(s);
+        s.close();
+    }
+
+    /**
+     * @tests java.util.Scanner#close()
+     */
+    public void test_close() throws IOException {
+        File tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
+        FileChannel fc = new FileOutputStream(tmpFile).getChannel();
+        s = new Scanner(fc);
+        s.close();
+        assertFalse(fc.isOpen());
+        s.close(); // no exception should be thrown
+
+        // TODO: test if invoking scan operation will raise
+        // IllegalStateException
+        assertTrue(tmpFile.delete());
+    }
+
+    /**
+     * @tests java.util.Scanner#ioException()
+     */
+    public void test_ioException() throws IOException {
+        MockCloseable mc = new MockCloseable();
+        s = new Scanner(mc);
+        assertNull(s.ioException());
+        s.close();
+        assertNotNull(s.ioException());
+    }
+}