incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r586537 - in /incubator/sling/whiteboard/microsling/src: main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java test/java/org/apache/sling/microsling/scripting/helpers/EspReaderTest.java
Date Fri, 19 Oct 2007 16:56:56 GMT
Author: fmeschbe
Date: Fri Oct 19 09:56:56 2007
New Revision: 586537

URL: http://svn.apache.org/viewvc?rev=586537&view=rev
Log:
SLING-69 Fix EspReader.read(char[], int, int) method which does not handle offset and length
and read counting correctly. Add unit tests.

Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java
    incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/EspReaderTest.java

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java?rev=586537&r1=586536&r2=586537&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/helpers/EspReader.java
Fri Oct 19 09:56:56 2007
@@ -274,15 +274,16 @@
         }
 
         int i;
-        int c;
-        for (i = off; i < len; i++) {
-            if ((c = doRead()) < 0) {
+        for (i = 0; i < len; i++, off++) {
+            int c = doRead();
+            if (c < 0) {
                 break;
             }
-            cbuf[i] = (char) c;
+            cbuf[off] = (char) c;
         }
 
-        return (i == off) ? -1 : i - off;
+        // return EOF (-1) if none have been read, else return the number read
+        return (i == 0) ? -1 : i;
     }
 
     /**

Modified: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/EspReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/EspReaderTest.java?rev=586537&r1=586536&r2=586537&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/EspReaderTest.java
(original)
+++ incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/scripting/helpers/EspReaderTest.java
Fri Oct 19 09:56:56 2007
@@ -29,6 +29,58 @@
  */
 public class EspReaderTest extends TestCase {
 
+    /** Test read() method */
+    public void testReadSingle() throws IOException {
+        String src = "<%var%>"; // expect var on reader
+
+        Reader reader = new EspReader(new StringReader(src));
+
+        assertTrue("Character 1 must be 'v'", 'v' == reader.read());
+        assertTrue("Character 2 must be 'a'", 'a' == reader.read());
+        assertTrue("Character 3 must be 'r'", 'r' == reader.read());
+        assertTrue("Character 4 must be -1", -1 == reader.read());
+    }
+
+    /** Test read(char[], int, int) method */
+    public void testReadArrayAll() throws IOException {
+        String src = "<%var%>"; // expect var on reader
+
+        Reader reader = new EspReader(new StringReader(src));
+        char[] buf = new char[3];
+        int rd = reader.read(buf, 0, buf.length);
+
+        assertEquals(3, rd);
+        assertEquals("var", new String(buf, 0, rd));
+
+        // nothing more to read, expect EOF
+        rd = reader.read(buf, 0, buf.length);
+        assertEquals(-1, rd);
+    }
+
+    /** Test read(char[], int, int) method */
+    public void testReadArrayOffset() throws IOException {
+        String jsSrc = "var x = 0;";
+        String src = "<%" + jsSrc + "%>";
+
+        Reader reader = new EspReader(new StringReader(src));
+        char[] buf = new char[10];
+        int off = 2;
+        int len = 3;
+        int rd = reader.read(buf, off, len);
+        assertEquals(len, rd);
+        assertEquals("var", new String(buf, off, rd));
+
+        off = 2;
+        len = 7;
+        rd = reader.read(buf, off, len);
+        assertEquals(len, rd);
+        assertEquals(" x = 0;", new String(buf, off, rd));
+
+        // nothing more to read, expect EOF
+        rd = reader.read(buf, 0, buf.length);
+        assertEquals(-1, rd);
+    }
+
     /** Test standard template text */
     public void testTemplate() throws IOException {
         assertEquals("out=response.writer;out.write(\"test\");", parse("test"));



Mime
View raw message