You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by gh...@apache.org on 2006/07/06 12:47:05 UTC

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

Author: gharley
Date: Thu Jul  6 03:47:05 2006
New Revision: 419514

URL: http://svn.apache.org/viewvc?rev=419514&view=rev
Log:
HARMONY 781 : [luni] Implementation of new methods hasNextBoolean(),nextBoolean(),match() and remove() in 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/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=419514&r1=419513&r2=419514&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 Jul  6 03:47:05 2006
@@ -60,6 +60,10 @@
     private static final Pattern DEFAULT_DELIMITER = Pattern
             .compile("\\p{javaWhitespace}+"); //$NON-NLS-1$
     
+    //The boolean's pattern
+    private static final Pattern BOOLEAN_PATTERN = Pattern.compile(
+            "true|false", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
+    
     // The pattern matching anything
     private static final Pattern ANY_PATTERN = Pattern.compile("(?s).*");
 
@@ -96,6 +100,8 @@
     private boolean closed = false;
 
     private IOException lastIOException;
+    
+    private boolean matchSuccessful = false;
 
     /**
      * Constructs a scanner that uses File as its input. The default charset is
@@ -378,9 +384,18 @@
         throw new NotYetImplementedException();
     }
 
-    //TODO: To implement this feature
+    /**
+     * Returns true if this scanner's next token can be translated into a valid
+     * boolean value. The scanner does not advance past the input that matched.
+     * 
+     * @return true 
+     *             iff the next token in this scanner's input can be translated
+     *         into a valid boolean value
+     * @throws IllegalStateException
+     *             if the scanner has been closed
+     */
     public boolean hasNextBoolean() {
-        throw new NotYetImplementedException();
+        return hasNext(BOOLEAN_PATTERN);
     }
 
     //TODO: To implement this feature
@@ -458,9 +473,29 @@
         return locale;
     }
 
-    //TODO: To implement this feature
+    /**
+     * Returns the match result of this scanner's last match operation.This
+     * method throws IllegalStateException if no match operation has been
+     * performed, or if the last match was unsuccessful.
+     * 
+     * The various nextXXX methods of Scanner provide a match result if they do
+     * not complete with throwing an exception. For example, after an invocation
+     * of the nextBoolean() method which returned a boolean value, this method
+     * returns a match result for the search of the Boolean regular expression
+     * defined above. In the same way,the findInLine(java.lang.String),
+     * findWithinHorizon(java.lang.String, int), and
+     * skip(java.util.regex.Pattern) methods will provide a match result if they
+     * are successful.
+     * 
+     * @return the match result of the last match operation
+     * @throws IllegalStateException
+     *             if the match result is available
+     */
     public MatchResult match() {
-        throw new NotYetImplementedException();
+        if (!matchSuccessful) {
+            throw new IllegalStateException();
+        }
+        return matcher.toMatchResult();
     }
 
     /**
@@ -500,6 +535,10 @@
      */
     public String next(Pattern pattern) {
         checkClosed();
+        if (null == pattern) {
+            throw new NullPointerException();
+        }
+        matchSuccessful = false;
         if (isInputExhausted()) {
             throw new NoSuchElementException();
         }
@@ -510,7 +549,7 @@
             throw new NoSuchElementException();
         }
         matcher.usePattern(pattern);
-        if (matcher.matches()) {
+        if (matchSuccessful = matcher.matches()) {
             return matcher.group(0);
         } else {
             recoverPreviousStatus();
@@ -556,11 +595,26 @@
         throw new NotYetImplementedException();
     }
 
-    //TODO: To implement this feature
+    /**
+     * Translates the next token in this scanner's input into a boolean value and
+     * returns this value. This method will throw InputMismatchException if the
+     * next token can not be interpreted as a boolean value with a case
+     * insensitive pattern created from the string "true|false". If this match
+     * succeeds, the scanner advances past the input that matched.
+     * 
+     * @return the boolean value scanned from the input
+     * @throws IllegalStateException
+     *             if this scanner has been closed
+     * @throws NoSuchElementException
+     *             if input has been exhausted
+     * @throws InputMismatchException
+     *             if the next token can not be translated into a valid boolean
+     *             value
+     */
     public boolean nextBoolean() {
-        throw new NotYetImplementedException();
+        return Boolean.parseBoolean(next(BOOLEAN_PATTERN));
     }
-
+    
     //TODO: To implement this feature
     public byte nextByte() {
         throw new NotYetImplementedException();
@@ -699,7 +753,14 @@
         return this;
     }
 
-    //TODO: To implement this feature
+    /**
+     * 
+     * The operation of remove is not supported by this implementation of
+     * Iterator.
+     * 
+     * @return UnsupportedOperationException 
+     *            if this method is invoked
+     */
     public void remove() {
         throw new UnsupportedOperationException();
     }
@@ -862,8 +923,10 @@
         boolean findComplete = false;
         while (!findComplete) {
             if (findComplete = matcher.find()) {
-                tokenEndIndex = matcher.start();
-                findStartIndex = matcher.start();
+                if (matcher.start() == findStartIndex
+                        && matcher.start() == matcher.end()) {
+                    findComplete = false;
+                }
             } else {
                 if (readMore()) {
                     resetMatcher();
@@ -872,6 +935,8 @@
                 }
             }
         }
+        tokenEndIndex = matcher.start();
+        findStartIndex = matcher.start();
         return tokenEndIndex;
     }
 

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=419514&r1=419513&r2=419514&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 Jul  6 03:47:05 2006
@@ -37,23 +37,24 @@
 import java.util.Locale;
 import java.util.NoSuchElementException;
 import java.util.Scanner;
+import java.util.regex.MatchResult;
 import java.util.regex.Pattern;
 
 import junit.framework.TestCase;
 
 public class ScannerTest extends TestCase {
 
-    Scanner s;
+    private Scanner s;
 
-    ServerSocket server;
+    private ServerSocket server;
 
-    SocketAddress address;
+    private SocketAddress address;
 
-    SocketChannel client;
+    private SocketChannel client;
 
-    Socket serverSocket;
+    private Socket serverSocket;
 
-    OutputStream os;
+    private OutputStream os;
 
     private static class MockCloseable implements Closeable, Readable {
 
@@ -463,6 +464,147 @@
     }
 
     /**
+     * @tests java.util.Scanner#remove()
+     */
+    public void test_remove() {
+        s = new Scanner("aab*b*").useDelimiter("\\*");
+        try {
+            s.remove();
+            fail("should throw UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {
+            //Expected
+        }
+    }
+
+    /**
+     * @tests java.util.Scanner#match()
+     */
+    public void test_match() {
+        MatchResult result ;
+        s = new Scanner("1 2 ");
+        try {
+            s.match();
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        assertEquals("1", s.next());
+        assertEquals("2", s.next());
+        result = s.match();
+        assertEquals(2, result.start());
+        assertEquals(3, result.end());
+        assertEquals(2, result.start(0));
+        assertEquals(3, result.end(0));
+        assertEquals("2", result.group());
+        assertEquals("2", result.group(0));
+        assertEquals(0, result.groupCount());
+        try {
+            result.start(1);
+            fail("should throw IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // Expected
+        }
+        try {
+            s.next();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+        try {
+            s.match();
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        
+        s = new Scanner("True faLse");
+        try {
+            s.match();
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        assertEquals(true, s.nextBoolean());
+        result = s.match();
+        assertEquals(0, result.start());
+        assertEquals(4, result.end());
+        assertEquals(0, result.start(0));
+        assertEquals(4, result.end(0));
+        assertEquals("True", result.group());
+        assertEquals(0, result.groupCount());
+        assertEquals(false, s.nextBoolean());
+        try {
+            s.nextBoolean();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+        try {
+            s.match();
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        
+        s = new Scanner("True faLse");
+        assertEquals(true, s.nextBoolean());
+        result = s.match();
+        assertEquals(0, result.start());
+        assertEquals(4, result.end());
+        assertEquals(0, result.start(0));
+        assertEquals(4, result.end(0));
+        assertEquals("True", result.group());
+        assertEquals(0, result.groupCount());
+        s.close();
+        try {
+            s.nextBoolean();
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        result = s.match();
+        assertEquals(0, result.start());
+        assertEquals(4, result.end());
+        assertEquals(0, result.start(0));
+        assertEquals(4, result.end(0));
+        assertEquals("True", result.group());
+        assertEquals(0, result.groupCount());
+        
+        s = new Scanner("True fase");
+        assertEquals(true, s.nextBoolean());
+        assertEquals(0, result.groupCount());
+        try {
+            s.nextBoolean();
+            fail("Should throw InputMismatchException");
+        } catch (InputMismatchException e) {
+            // expected
+        }
+        try {
+            s.match();
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // Expected
+        }
+        
+        s = new Scanner("True fase");
+        assertEquals(true, s.nextBoolean());
+        try {
+            s.next((Pattern)null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+        result = s.match();
+        assertEquals(0, result.start());
+        assertEquals(4, result.end());
+        assertEquals(0, result.start(0));
+        assertEquals(4, result.end(0));
+        assertEquals("True", result.group());
+        assertEquals(0, result.groupCount());
+        
+    }
+     
+    /**
      * @throws IOException
      * @tests java.util.Scanner#next()
      */
@@ -473,6 +615,34 @@
         assertEquals("", s.next());
         assertEquals("2", s.next());
 
+        s = new Scanner(" \t 1 \t 2").useDelimiter("\\s*");
+        assertEquals("1", s.next());
+        assertEquals("2", s.next());
+        try {
+            s.next();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+        
+        s = new Scanner("a").useDelimiter("a?");
+        try {
+            s.next();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+        
+        s = new Scanner("aa").useDelimiter("a?");
+        assertEquals("", s.next());
+        try {
+            s.next();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+        
+
         s = new Scanner("word( )test( )").useDelimiter("\\( \\)");
         assertEquals("word", s.next());
         assertEquals("test", s.next());
@@ -511,7 +681,7 @@
             // Expected
         }
 
-        // no delimiter exites in this scanner
+        // no delimiter exists in this scanner
         s = new Scanner("test");
         assertEquals("test", s.next());
 
@@ -523,6 +693,7 @@
         s = new Scanner("  test  ");
         assertEquals("test", s.next());
 
+        // Harmony uses 1024 as default buffer size,
         // What if a sentence can not be read in all in once.
         StringBuilder longSentence = new StringBuilder(1025);
         for (int i = 0; i <= 10; i++) {
@@ -543,6 +714,29 @@
                 Pattern.MULTILINE));
         assertEquals("test\n", s.next());
         assertEquals("test", s.next());
+        
+        s = new Scanner("").useDelimiter(Pattern.compile("^",
+                Pattern.MULTILINE));
+        try {
+            s.next();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+        
+        s = new Scanner("").useDelimiter(Pattern.compile("^*",
+                Pattern.MULTILINE));
+        try {
+            s.next();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+
+        s = new Scanner("test\ntest").useDelimiter(Pattern.compile("^*",
+                Pattern.MULTILINE));
+        assertEquals("t", s.next());
+        assertEquals("e", s.next());
 
         s = new Scanner("\ntest\ntest").useDelimiter(Pattern.compile("$",
                 Pattern.MULTILINE));
@@ -676,6 +870,64 @@
     
     /**
      * @throws IOException
+     * @tests java.util.Scanner#nextBoolean()
+     */
+    public void test_nextBoolean() throws IOException {
+        // case insensitive
+        s = new Scanner("TRue");
+        assertTrue(s.nextBoolean());
+
+        s = new Scanner("tRue false");
+        assertTrue(s.nextBoolean());
+        assertFalse(s.nextBoolean());
+        try {
+            s.nextBoolean();
+            fail("Should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+
+        s = new Scanner("true1");
+        try {
+            s.nextBoolean();
+            fail("Should throw InputMismatchException");
+        } catch (InputMismatchException e) {
+            // Expected
+        }
+
+        try {
+            s = new Scanner("");
+            s.nextBoolean();
+            fail("Should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+
+        // test socket inputStream
+        os.write("true false".getBytes());
+        serverSocket.close();
+
+        s = new Scanner(client);
+        assertTrue(s.nextBoolean());
+        assertFalse(s.nextBoolean());
+
+        // ues '*' as delimiter
+        s = new Scanner("true**false").useDelimiter("\\*");
+        assertTrue(s.nextBoolean());
+        try {
+            s.nextBoolean();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+
+        s = new Scanner("false( )").useDelimiter("\\( \\)");
+        assertFalse(s.nextBoolean());
+
+    }
+    
+    /**
+     * @throws IOException
      * @tests java.util.Scanner#hasNext()
      */
     public void test_hasNext() throws IOException {
@@ -899,7 +1151,55 @@
         }
     }
     
-    public void setUp() throws Exception {
+    /**
+     * @throws IOException
+     * @tests java.util.Scanner#hasNextBoolean()
+     */
+    public void test_hasNextBoolean() throws IOException {
+
+        s = new Scanner("TRue");
+        assertTrue(s.hasNextBoolean());
+        assertTrue(s.nextBoolean());
+
+        s = new Scanner("tRue false");
+        assertTrue(s.hasNextBoolean());
+        assertTrue(s.nextBoolean());
+        assertTrue(s.hasNextBoolean());
+        assertFalse(s.nextBoolean());
+
+        s = new Scanner("");
+        assertFalse(s.hasNextBoolean());
+
+        // test socket inputStream
+
+        os.write("true false ".getBytes());
+        serverSocket.close();
+
+        s = new Scanner(client);
+        assertTrue(s.hasNextBoolean());
+        assertTrue(s.nextBoolean());
+
+        // ues '*' as delimiter
+        s = new Scanner("true**false").useDelimiter("\\*");
+        assertTrue(s.hasNextBoolean());
+        assertTrue(s.nextBoolean());
+        assertFalse(s.hasNextBoolean());
+        try {
+            s.nextBoolean();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // Expected
+        }
+
+        s = new Scanner("false( )").useDelimiter("\\( \\)");
+        assertTrue(s.hasNextBoolean());
+        assertFalse(s.nextBoolean());
+        assertFalse(s.hasNextBoolean());
+
+    }
+
+    
+    protected void setUp() throws Exception {
         super.setUp();
 
         server = new ServerSocket(0);
@@ -912,7 +1212,7 @@
         os = serverSocket.getOutputStream();
     }
 
-    public void tearDown() throws Exception {
+    protected void tearDown() throws Exception {
         super.tearDown();
 
         try {