harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ghar...@apache.org
Subject svn commit: r416323 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Scanner.java main/java/org/apache/harmony/luni/util/ExternalMessages.properties test/java/tests/api/java/util/ScannerTest.java
Date Thu, 22 Jun 2006 10:38:04 GMT
Author: gharley
Date: Thu Jun 22 03:38:03 2006
New Revision: 416323

URL: http://svn.apache.org/viewvc?rev=416323&view=rev
Log:
HARMONY 611 : Implement some new methods in Class java.util.Scanner

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
    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
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Scanner.java?rev=416323&r1=416322&r2=416323&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
Thu Jun 22 03:38:03 2006
@@ -52,12 +52,26 @@
  */
 public final class Scanner implements Iterator<String> {
 
+    // Default delimiting pattern
+    private final static Pattern DEFAULT_DELIMITER = Pattern
+            .compile("\\p{javaWhitespace}+"); //$NON-NLS-1$
+
+    //Default radix
+    private final static int DEFAULT_RADIX = 10;
+
+    //The input source of scanner
     private Readable input;
 
-    private IOException lastIOException;
+    private Pattern delimiter = DEFAULT_DELIMITER;
+
+    private int radix = DEFAULT_RADIX;
+
+    private Locale locale = Locale.getDefault();
 
     private boolean closed = false; // used by find and nextXXX operation
 
+    private IOException lastIOException;
+
     /**
      * Constructs a scanner that uses File as its input. The default charset is
      * applied when reading the file.
@@ -81,9 +95,19 @@
      *            the name of the encoding type of the file
      * @throws FileNotFoundException
      *             if the specified file is not found
+     * @throws IllegalArgumentException
+     *            if the specified coding does not exist
      */
     public Scanner(File src, String charsetName) throws FileNotFoundException {
+        if (null == src) {
+            throw new NullPointerException(org.apache.harmony.luni.util.Msg
+                    .getString("KA00a"));
+        }
         FileInputStream fis = new FileInputStream(src);
+        if (null == charsetName) {
+            throw new IllegalArgumentException(org.apache.harmony.luni.util.Msg
+                    .getString("KA009"));
+        }
         try {
             input = new InputStreamReader(fis, charsetName);
         } catch (UnsupportedEncodingException e) {
@@ -125,8 +149,14 @@
      *            the input stream to be scanned
      * @param charsetName
      *            the encoding type of the input stream
+     * @throws IllegalArgumentException
+     *            if the specified character set is not found
      */
     public Scanner(InputStream src, String charsetName) {
+        if (null == src) {
+            throw new NullPointerException(org.apache.harmony.luni.util.Msg
+                    .getString("KA00b"));
+        }
         try {
             input = new InputStreamReader(src, charsetName);
         } catch (UnsupportedEncodingException e) {
@@ -141,6 +171,10 @@
      *            the Readable to be scanned
      */
     public Scanner(Readable src) {
+        if (null == src) {
+            throw new NullPointerException(org.apache.harmony.luni.util.Msg
+                    .getString("KA00c"));
+        }
         input = src;
     }
 
@@ -163,8 +197,18 @@
      *            the ReadableByteChannel to be scanned
      * @param charsetName
      *            the encoding type of the content in the ReadableByteChannel
+     * @throws IllegalArgumentException
+     *            if the specified character set is not found           
      */
     public Scanner(ReadableByteChannel src, String charsetName) {
+        if (null == src) {
+            throw new NullPointerException(org.apache.harmony.luni.util.Msg
+                    .getString("KA00d"));
+        }
+        if (null == charsetName) {
+            throw new IllegalArgumentException(org.apache.harmony.luni.util.Msg
+                    .getString("KA009"));
+        }
         try {
             input = new InputStreamReader(Channels.newInputStream(src),
                     charsetName);
@@ -180,7 +224,10 @@
      * 
      */
     public void close() {
-        if (input instanceof Closeable && !closed) {
+        if (closed == true) {
+            return;
+        }
+        if (input instanceof Closeable) {
             try {
                 ((Closeable) input).close();
             } catch (IOException e) {
@@ -190,94 +237,121 @@
         closed = true;
     }
 
+    /**
+     * Returns the <code>Pattern</code> in use by this scanner.
+     * 
+     * @return the <code>Pattern</code> presently in use by this scanner
+     */
     public Pattern delimiter() {
-        throw new NotYetImplementedException();
+        return delimiter;
     }
 
+    //TODO: To implement this feature
     public String findInLine(Pattern pattern) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public String findInLine(String pattern) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public String findWithinHorizon(Pattern pattern, int horizon) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public String findWithinHorizon(String pattern, int horizon) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNext() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNext(Pattern pattern) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNext(String pattern) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextBigDecimal() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextBigInteger() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextBigInteger(int radix) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextBoolean() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextByte() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextByte(int radix) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextDouble() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextFloat() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextInt() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextInt(int radix) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextLine() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextLong() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextLong(int radix) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextShort() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean hasNextShort(int radix) {
         throw new NotYetImplementedException();
     }
@@ -292,118 +366,195 @@
         return lastIOException;
     }
 
+    /**
+     * return the locale of this scanner.
+     * 
+     * @return 
+     *             the locale of this scanner
+     */
     public Locale locale() {
-        throw new NotYetImplementedException();
+        return locale;
     }
 
+    //TODO: To implement this feature
     public MatchResult match() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public String next() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public String next(Pattern pattern) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public String next(String pattern) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public BigDecimal nextBigDecimal() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public BigInteger nextBigInteger() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public BigInteger nextBigInteger(int radix) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public boolean nextBoolean() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public byte nextByte() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public byte nextByte(int radix) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public double nextDouble() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public float nextFloat() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public int nextInt() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public int nextInt(int radix) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public String nextLine() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public long nextLong() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public long nextLong(int radix) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public short nextShort() {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public short nextShort(int radix) {
         throw new NotYetImplementedException();
     }
 
+    /**
+     * return the radix of this scanner.
+     * 
+     * @return
+     *            the radix of this scanner
+     */
     public int radix() {
-        throw new NotYetImplementedException();
+        return radix;
     }
 
+    //TODO: To implement this feature
     public Scanner skip(Pattern pattern) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public Scanner skip(String pattern) {
         throw new NotYetImplementedException();
     }
 
+    //TODO: To implement this feature
     public String toString() {
         throw new NotYetImplementedException();
     }
 
+    /**
+     * Set the delimiting pattern of this scanner
+     * 
+     * @param pattern
+     *            the delimiting pattern to use
+     * @return this scanner
+     */
     public Scanner useDelimiter(Pattern pattern) {
-        throw new NotYetImplementedException();
+        delimiter = pattern;
+        return this;
     }
 
+    /**
+     * Set the delimiting pattern of this scanner with a pattern compiled from
+     * the supplied string value
+     * 
+     * @param pattern
+     *            a string from which a <code>Pattern</code> can be compiled
+     * @return this scanner
+     */
     public Scanner useDelimiter(String pattern) {
-        throw new NotYetImplementedException();
+        return useDelimiter(Pattern.compile(pattern));
     }
 
+    /**
+     * 
+     * set the locale of this scanner to a specified locale. 
+     *
+     * @param locale
+     *              the specified locale to use
+     * @return
+     *              this scanner
+     */
     public Scanner useLocale(Locale locale) {
-        throw new NotYetImplementedException();
+        if( null == locale )
+            throw new NullPointerException();
+        this.locale = locale;
+        return this;
     }
 
+    /**
+     * 
+     * set the radix of this scanner to a specified radix.
+     * 
+     * @param radix
+     *             the specified radix to use
+     * @return
+     *             this scanner
+     */
     public Scanner useRadix(int radix) {
-        throw new NotYetImplementedException();
+        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
+            throw new IllegalArgumentException(org.apache.harmony.luni.util.Msg
+                    .getString("KA008", radix));
+        }
+        this.radix = radix;
+        return this;
     }
 
+    //TODO: To implement this feature
     public void remove() {
         throw new UnsupportedOperationException();
     }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties?rev=416323&r1=416322&r2=416323&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
Thu Jun 22 03:38:03 2006
@@ -293,4 +293,10 @@
 KA005={0} is not an enum type
 KA006={0} is not a constant in the enum type {1}
 KA007=field is null
+KA008={0} is an illegal radix
+KA009=CharsetName is illegal
+KA00a=File is null
+KA00b=InputStream is null
+KA00c=Readable is null
+KA00d=ReadableByteChannel is null
 

Modified: 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=416323&r1=416322&r2=416323&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/ScannerTest.java
Thu Jun 22 03:38:03 2006
@@ -15,16 +15,21 @@
 package tests.api.java.util;
 
 import java.io.Closeable;
+import java.io.EOFException;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.PipedInputStream;
 import java.io.StringReader;
 import java.nio.CharBuffer;
 import java.nio.channels.FileChannel;
+import java.nio.channels.ReadableByteChannel;
 import java.nio.charset.Charset;
+import java.util.Locale;
 import java.util.Scanner;
+import java.util.regex.Pattern;
 
 import junit.framework.TestCase;
 
@@ -39,7 +44,7 @@
         }
 
         public int read(CharBuffer cb) throws IOException {
-            return 0;
+            throw new EOFException();
         }
 
     }
@@ -61,6 +66,21 @@
             // expected
         }
 
+        tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
+        FileOutputStream fos = new FileOutputStream(tmpFile);
+        fos.write("test".getBytes());
+
+        s = new Scanner(tmpFile);
+        tmpFile.delete();
+
+        // Scanner(File = null)
+        try {
+            s = new Scanner((File) null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
         // TODO: test if the default charset is used.
     }
 
@@ -81,6 +101,13 @@
         } catch (FileNotFoundException e) {
             // expected
         }
+        
+        try {
+            s = new Scanner(tmpFile, null);
+            fail("should throw FileNotFoundException");
+        } catch (FileNotFoundException e) {
+            // expected
+        }
 
         tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
         try {
@@ -89,9 +116,43 @@
         } catch (IllegalArgumentException e) {
             // expected
         }
-        // fail on RI. File is opened but not closed when exception is thrown on
+
+        //fail on RI. File is opened but not closed when exception is thrown on
         // RI.
         assertTrue(tmpFile.delete());
+        
+        // Scanner(File = null, Charset = null)
+        try {
+            s = new Scanner((File) null, null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        // Scanner(File = null, Charset = UTF-8)
+        try {
+            s = new Scanner((File) null, "UTF-8");
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        // Scanner(File = null, Charset = invalid)
+        try {
+            s = new Scanner((File) null, "invalid");
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        // Scanner(File, Charset = null)
+        try {
+            File f = File.createTempFile("test", ".tmp");
+            s = new Scanner(f, null);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
 
         // TODO: test if the specified charset is used.
     }
@@ -104,6 +165,14 @@
         assertNotNull(s);
         s.close();
 
+        // Scanner(InputStream)
+        try {
+            s = new Scanner((InputStream) null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
         // TODO: test if the default charset is used.
     }
 
@@ -116,6 +185,20 @@
         s.close();
 
         try {
+            s = new Scanner((PipedInputStream) null, "invalid charset");
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
+            s = new Scanner(new PipedInputStream(), null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        try {
             s = new Scanner(new PipedInputStream(), "invalid charset");
             fail("should throw IllegalArgumentException");
         } catch (IllegalArgumentException e) {
@@ -132,6 +215,14 @@
         s = new Scanner(new StringReader("test string"));
         assertNotNull(s);
         s.close();
+
+        // Scanner(Readable)
+        try {
+            s = new Scanner((Readable) null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
     }
 
     /**
@@ -146,6 +237,14 @@
         s.close();
         assertTrue(tmpFile.delete());
 
+        // Scanner(ReadableByteChannel)
+        try {
+            s = new Scanner((ReadableByteChannel) null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
         // TODO: test if the default charset is used.
     }
 
@@ -170,6 +269,29 @@
         fc.close();
         assertTrue(tmpFile.delete());
 
+        // Scanner(ReadableByteChannel = null, Charset = null)
+        try {
+            s = new Scanner((ReadableByteChannel) null, null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        // Scanner(ReadableByteChannel = null, Charset = invalid)
+        try {
+            s = new Scanner((ReadableByteChannel) null, "invalid");
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        // Scanner(ReadableByteChannel, Charset = null)
+        try {
+            s = new Scanner(fc, null);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
         // TODO: test if the specified charset is used.
     }
 
@@ -180,6 +302,14 @@
         s = new Scanner("test string");
         assertNotNull(s);
         s.close();
+
+        // Scanner(String)
+        try {
+            s = new Scanner((String) null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
     }
 
     /**
@@ -187,14 +317,25 @@
      */
     public void test_close() throws IOException {
         File tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
-        FileChannel fc = new FileOutputStream(tmpFile).getChannel();
+        FileOutputStream fos = new FileOutputStream(tmpFile);
+        FileChannel fc = fos.getChannel();
         s = new Scanner(fc);
+
+        // Write out a int before the scanner is closed, should be OK.
+        fos.write(12);
+
         s.close();
         assertFalse(fc.isOpen());
-        s.close(); // no exception should be thrown
 
-        // TODO: test if invoking scan operation will raise
-        // IllegalStateException
+        // Write out a int after the scanner is closed, IOException should be
+        // thrown out.
+        try {
+            fos.write(12);
+        } catch (IOException e) {
+            // expected
+        }
+
+        s.close(); // no exception should be thrown
         assertTrue(tmpFile.delete());
     }
 
@@ -204,8 +345,102 @@
     public void test_ioException() throws IOException {
         MockCloseable mc = new MockCloseable();
         s = new Scanner(mc);
-        assertNull(s.ioException());
-        s.close();
+        assertNull(s.ioException()); // No operation, no exception
+
+        s.close(); // IOException should be cached
         assertNotNull(s.ioException());
+        assertTrue(s.ioException() instanceof IOException);
+    }
+    
+    /**
+     * @tests java.util.Scanner#delimiter()
+     */
+    public void test_delimiter() {
+        s = new Scanner("test");
+        Pattern pattern = s.delimiter();
+        assertEquals("\\p{javaWhitespace}+", pattern.toString());
+    }
+
+    /**
+     * @tests java.util.Scanner#useDelimiter(Pattern)
+     */
+    public void test_useDelimiter_LPattern() {
+        s = new Scanner("test");
+        s.useDelimiter(Pattern.compile("\\w+"));
+        assertEquals("\\w+", s.delimiter().toString());
+        
+        s = new Scanner("test");
+        s.useDelimiter((Pattern) null);
+        assertNull(s.delimiter());
+    }
+
+    /**
+     * @tests java.util.Scanner#useDelimiter(String)
+     */
+    public void test_useDelimiter_String() {
+        s = new Scanner("test");
+        try {
+            s.useDelimiter((String) null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+        s = new Scanner("test");
+        s.useDelimiter("\\w+");
+        assertEquals("\\w+", s.delimiter().toString());
+    }
+
+    /**
+     * @tests java.util.Scanner#locale()
+     */
+    public void test_locale() {
+        s = new Scanner("test");
+        assertEquals(Locale.getDefault(), s.locale());
+    }
+
+    /**
+     * @tests java.util.Scanner#useLocale(Locale)
+     */
+    public void test_useLocale_LLocale() {
+        s = new Scanner("test");
+        try {
+            s.useLocale(null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        s.useLocale(new Locale("test", "test"));
+        assertEquals(new Locale("test", "test"), s.locale());
+    }
+
+    /**
+     * @tests java.util.Scanner#radix()
+     */
+    public void test_radix() {
+        s = new Scanner("test");
+        assertEquals(10, s.radix());
+    }
+
+    /**
+     * @tests java.util.Scanner#useRadix()
+     */
+    public void test_useRadix_I() {
+        s = new Scanner("test");
+        try {
+            s.useRadix(Character.MIN_RADIX - 1);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        try {
+            s.useRadix(Character.MAX_RADIX + 1);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        s.useRadix(11);
+        assertEquals(11, s.radix());
     }
 }



Mime
View raw message