harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhouke...@apache.org
Subject svn commit: r964341 - in /harmony/enhanced/java/trunk/classlib/modules/luni/src: main/java/java/util/Scanner.java test/api/common/org/apache/harmony/luni/tests/java/util/ScannerTest.java
Date Thu, 15 Jul 2010 08:40:52 GMT
Author: zhoukevin
Date: Thu Jul 15 08:40:51 2010
New Revision: 964341

URL: http://svn.apache.org/viewvc?rev=964341&view=rev
Log:
java.util.Scanner.hasNextLine() behaves differently from RI. Given a simple test scenario
as follows,

1 Scanner scanner = new Scanner(System.in);
2 while(scanner.hasNextLine()){
3     System.out.println(scanner.nextLine());
4 }

When user inputs the 1st line ends with ENTER, HARMONY will blocks on line 2 of the above
code and doesn't outputs the 1st string, while RI does.
This patch fixes the above problem and adds a test case for coverage.

Modified:
    harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Scanner.java
    harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ScannerTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Scanner.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Scanner.java?rev=964341&r1=964340&r2=964341&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Scanner.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/util/Scanner.java
Thu Jul 15 08:40:51 2010
@@ -844,7 +844,8 @@ public final class Scanner implements It
         boolean hasNextLine = false;
         while (true) {
             if (matcher.find()) {
-                if (inputExhausted || matcher.end() != bufferLength) {
+                if (inputExhausted || matcher.end() != bufferLength
+                        || bufferLength < buffer.capacity()) {
                     matchSuccessful = true;
                     hasNextLine = true;
                     break;

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ScannerTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ScannerTest.java?rev=964341&r1=964340&r2=964341&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ScannerTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ScannerTest.java
Thu Jul 15 08:40:51 2010
@@ -25,6 +25,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
 import java.io.StringReader;
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -37,8 +38,10 @@ import java.nio.channels.FileChannel;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.channels.SocketChannel;
 import java.nio.charset.Charset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.InputMismatchException;
+import java.util.List;
 import java.util.Locale;
 import java.util.NoSuchElementException;
 import java.util.Scanner;
@@ -5614,7 +5617,6 @@ public class ScannerTest extends TestCas
      * @tests java.util.Scanner#hasNextLine()
      */
     public void test_hasNextLine() {
-        
         s = new Scanner("");
         s.close();
         try {
@@ -5682,7 +5684,40 @@ public class ScannerTest extends TestCas
         assertEquals(0, matchResult.start());
         assertEquals(1, matchResult.end());
     }
-    
+
+    public void test_hasNextLine_sequence() throws IOException {
+        final PipedInputStream pis = new PipedInputStream();
+        final PipedOutputStream pos = new PipedOutputStream();
+        final Scanner scanner = new Scanner(pis);
+        pis.connect(pos);
+        final List<String> result = new ArrayList<String>();
+        Thread thread = new Thread(new Runnable() {
+            public void run() {
+                while (scanner.hasNextLine()) {
+                    result.add(scanner.nextLine());
+                }
+            }
+        });
+        thread.start();
+        for (int index = 0; index < 5; index++) {
+            pos.write(("line" + index + "\n").getBytes());
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException e) {
+                // Ignored
+            }
+            assertEquals(index + 1, result.size());
+        }
+        pis.close();
+        pos.close();
+        try {
+            thread.join(1000);
+        } catch (InterruptedException e) {
+            // Ignored
+        }
+        assertFalse(scanner.hasNextLine());
+    }
+
     protected void setUp() throws Exception {
         super.setUp();
 



Mime
View raw message