gzz-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gzz-commits] gzz/lava gzz/storm/BlockOutputStream.java gzz/s...


From: Benja Fallenstein
Subject: [Gzz-commits] gzz/lava gzz/storm/BlockOutputStream.java gzz/s...
Date: Wed, 06 Nov 2002 17:02:56 -0500

CVSROOT:        /cvsroot/gzz
Module name:    gzz
Changes by:     Benja Fallenstein <address@hidden>      02/11/06 17:02:56

Modified files:
        lava/gzz/storm : BlockOutputStream.java StormPool.java 
        lava/gzz/storm/impl: TransientPool.java 
        lava/test/gzz/storm: StormPoolTest.java 
        lava/test/gzz/storm/impl: TransientPool.test 

Log message:
        More Storm work (test-first + implementation)

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/BlockOutputStream.java.diff?tr1=1.5&tr2=1.6&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/StormPool.java.diff?tr1=1.4&tr2=1.5&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/impl/TransientPool.java.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/test/gzz/storm/StormPoolTest.java.diff?tr1=1.4&tr2=1.5&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/test/gzz/storm/impl/TransientPool.test.diff?tr1=1.2&tr2=1.3&r1=text&r2=text

Patches:
Index: gzz/lava/gzz/storm/BlockOutputStream.java
diff -u gzz/lava/gzz/storm/BlockOutputStream.java:1.5 
gzz/lava/gzz/storm/BlockOutputStream.java:1.6
--- gzz/lava/gzz/storm/BlockOutputStream.java:1.5       Tue Nov  5 12:35:44 2002
+++ gzz/lava/gzz/storm/BlockOutputStream.java   Wed Nov  6 17:02:56 2002
@@ -58,6 +58,8 @@
     /** Get the header of this block.
      *  The header is set when this object is created, because it must be
      *  the first thing written to the stream (obviously <code>;-)</code>).
+     *  <p>
+     *  XXX not sure this is really needed...
      */
     abstract public Header822 getHeader() throws IOException;
 }
Index: gzz/lava/gzz/storm/StormPool.java
diff -u gzz/lava/gzz/storm/StormPool.java:1.4 
gzz/lava/gzz/storm/StormPool.java:1.5
--- gzz/lava/gzz/storm/StormPool.java:1.4       Wed Nov  6 12:38:40 2002
+++ gzz/lava/gzz/storm/StormPool.java   Wed Nov  6 17:02:56 2002
@@ -102,7 +102,7 @@
      *  The header for the new block is passed explicitly.
      *  @see BlockOutputStream
      */
-    BlockOutputStream getBlockOutputStream(HeaderLines822 hdr) throws 
IOException;
+    BlockOutputStream getBlockOutputStream(Header822 hdr) throws IOException;
 
 
     // GETTING POOL NAMES
Index: gzz/lava/gzz/storm/impl/TransientPool.java
diff -u gzz/lava/gzz/storm/impl/TransientPool.java:1.7 
gzz/lava/gzz/storm/impl/TransientPool.java:1.8
--- gzz/lava/gzz/storm/impl/TransientPool.java:1.7      Wed Nov  6 12:41:22 2002
+++ gzz/lava/gzz/storm/impl/TransientPool.java  Wed Nov  6 17:02:56 2002
@@ -28,8 +28,9 @@
 import java.util.*;
 
 /** A StormPool whose contents are exclusively stored in memory.
+ *  Should become an IndexedPool eventually.
  */
-public class TransientPool implements IndexedPool {
+public class TransientPool implements StormPool {
     /** The blocks in this pool.
      *  A mapping from <code>BlockId</code> objects
      *  to <code>TransientBlock</code>s.
@@ -55,7 +56,7 @@
     }
 
     protected class TransientBlock implements Block {
-        protected byte[] bytes;
+        protected byte[] bytes, rawBytes;
         protected Header822 h;
 
        protected TransientBlock(byte[] b, Header822 h) {
@@ -68,8 +69,18 @@
        public InputStream getInputStream() {
            return new ByteArrayInputStream(bytes);
        }
-       public InputStream getRawInputStream() { return null; }
-
+       public InputStream getRawInputStream() throws IOException {
+           if(rawBytes == null) {
+               ByteArrayOutputStream bos = new ByteArrayOutputStream();
+               h.writeTo(bos);
+               bos.close();
+               byte[] header = bos.toByteArray();
+               rawBytes = new byte[header.length + bytes.length];
+               System.arraycopy(header, 0, rawBytes, 0, header.length);
+               System.arraycopy(bytes, 0, rawBytes, header.length, 
bytes.length);
+           }
+           return new ByteArrayInputStream(rawBytes);
+       }
     }
 
     public Block get(BlockId id) {
@@ -77,18 +88,15 @@
     }
     public void add(Block b) {}
     public void delete(Block b) {}
-    public Set getIds() { return null; }
+    public Set getIds() { return blocks.keySet(); }
     public BlockOutputStream getBlockOutputStream(String contentType) {
        Header822 hdr = new UniqueHeader822();
        hdr.add("Content-Type", contentType);
        return new TransientBlockOutputStream(hdr);
     }
-    public BlockOutputStream getBlockOutputStream(HeaderLines822 hdr) {
-       return null;
+    public BlockOutputStream getBlockOutputStream(Header822 hdr) {
+       return new TransientBlockOutputStream(new VerbatimHeader822(hdr));
     }
     public String getDefaultPoolName() { return null; }
     public Set getPoolNames() { return null; }
-    public Pointer getPointer(String uri) { return null; }
-    public Set getDiffsFrom(BlockId id) { return null; }
-    public Set getDiffsTo(BlockId id) { return null; }
 }
Index: gzz/lava/test/gzz/storm/StormPoolTest.java
diff -u gzz/lava/test/gzz/storm/StormPoolTest.java:1.4 
gzz/lava/test/gzz/storm/StormPoolTest.java:1.5
--- gzz/lava/test/gzz/storm/StormPoolTest.java:1.4      Wed Nov  6 12:31:30 2002
+++ gzz/lava/test/gzz/storm/StormPoolTest.java  Wed Nov  6 17:02:56 2002
@@ -23,35 +23,132 @@
 package gzz.storm;
 
 import gzz.util.*;
-import java.io.*;
 import gzz.util.headers.*;
+import java.io.*;
+import java.util.*;
 
-public class StormPoolTest{
-
-       public void testNewBlock(StormPool pool) throws IOException{
-
-               BlockOutputStream bos = pool.getBlockOutputStream("text/plain");
-               OutputStreamWriter osw = new OutputStreamWriter( bos );
-               osw.write("Hallo, Welt!");
-               osw.close();
-               Block b = pool.get(bos.getBlockId());
-
-               InputStreamReader isr = new InputStreamReader(
-                                               b.getInputStream());
-               char[] array = new char[12];
-               isr.read(array);                  
-
-               if(isr.read() != -1)
-                       throw new Error("The block contains too much data!");
-
-               if(!(new String(array).equals("Hallo, Welt!")))
-                       throw new Error();
-
-               isr.close();
-
-               Header822 h = b.getHeader();
+public class StormPoolTest {
 
-               if(!h.get("Content-type").equals("text/plain"))
-                       throw new Error("wrong content-type");
-       }
+    /** Create a new block in the pool, request it from the pool by ID,
+     *  check its body (contents) and its Content-Type.
+     */
+    public void testNewBlock(StormPool pool) throws IOException{
+       
+       BlockOutputStream bos = pool.getBlockOutputStream("text/plain");
+       OutputStreamWriter osw = new OutputStreamWriter( bos );
+       osw.write("Hallo, Welt!");
+       osw.close();
+       Block b = pool.get(bos.getBlockId());
+               
+       InputStreamReader isr = new InputStreamReader(b.getInputStream());
+       char[] array = new char[12];
+       isr.read(array);                  
+       
+       if(isr.read() != -1)
+           throw new Error("The block contains too much data!");
+       
+       if(!(new String(array).equals("Hallo, Welt!")))
+           throw new Error();
+       
+       isr.close();
+       
+       Header822 h = b.getHeader();
+
+       if(!h.get("Content-type").equals("text/plain"))
+           throw new Error("wrong content-type");
+    }
+
+    /** Test creating a block with our own header.
+     */
+    public void testOwnHeader(StormPool pool) throws IOException {
+       Header822 hl = new VerbatimHeader822();
+       hl.add("Foo", "Bar");
+       hl.add("Baz", "beng");
+       hl.add("Content-TypE", "application/octet-stream");
+
+       byte[] expectedBody = new byte[] {0,7,4,21};
+
+       BlockOutputStream bos = pool.getBlockOutputStream(hl);
+       bos.write(expectedBody);
+       hl.add("This-should", "not appear in the block");
+       bos.close();
+
+       Block block = bos.getBlock();
+       InputStream is = block.getInputStream();
+       for(int i=0; i<expectedBody.length; i++)
+           if(is.read() != expectedBody[i])
+               throw new Error("Bad data in block at position "+i);
+
+       if(is.read() >= 0)
+           throw new Error("Too much data in block");
+
+       is.close();
+
+       /*
+       System.out.println(new String(
+           gzz.util.CopyUtil.readBytes(block.getRawInputStream())));
+       */
+
+       is = block.getRawInputStream();
+
+       byte[] expectedHeader = 
+           ("Foo: Bar\r\nBaz: beng\r\nContent-TypE: "+
+            "application/octet-stream\r\n\r\n").getBytes("US-ASCII");
+       
+       for(int i=0; i<expectedHeader.length; i++)
+           if(is.read() != expectedHeader[i])
+               throw new Error("Bad header at byte: "+i);
+
+       for(int i=0; i<expectedBody.length; i++)
+           if(is.read() != expectedBody[i])
+               throw new Error("Bad header at byte: "+i);
+
+       if(is.read() >= 0)
+           throw new Error("Too much data in block");
+
+       Header822 h = block.getHeader();
+       
+       if(!h.get("content-tYPE").equals("application/octet-stream"))
+           throw new Error("Bad content-type");
+       if(!(h.get("foo").equals("Bar") && h.get("Baz").equals("beng")))
+           throw new Error("Bad header");
+       
+       try {
+           h.get("This-should");
+
+           throw new Error("Header was not copied but referenced");
+       } catch(NoSuchElementException _) {}
+    }
+
+    /** Assert that getIds() does not return null.
+     *  This is required of all pools. (They can return
+     *  the canonical empty set from java.util.Collections
+     *  at no extra cost, after all...)
+     */
+    public void testIdsNotNull(StormPool pool) throws IOException {
+       if(pool.getIds() == null)
+           throw new Error("pool.getIds() is null");
+    }
+
+    /** Create a new block, check that the ID appears
+     *  in the pool's getIds() set.
+     *  NOTE: getIds() is NOT REQUIRED to return all ids in the pool;
+     *  this is implementation-dependent. Obviously, this method
+     *  should only be called for pools that guarantee the id to be
+     *  in getIds().
+     */
+    public void testAddId(StormPool pool) throws IOException {
+       Set oldIds = new HashSet(pool.getIds());
+
+       BlockOutputStream bos = pool.getBlockOutputStream("text/plain");
+       bos.close();
+
+       Set newIds = new HashSet(pool.getIds());
+
+       if(oldIds.equals(newIds))
+           throw new Error("No new id was added");
+
+       if(!newIds.contains(bos.getBlockId()))
+           throw new Error("Id was not added");
+    }
 }
Index: gzz/lava/test/gzz/storm/impl/TransientPool.test
diff -u gzz/lava/test/gzz/storm/impl/TransientPool.test:1.2 
gzz/lava/test/gzz/storm/impl/TransientPool.test:1.3
--- gzz/lava/test/gzz/storm/impl/TransientPool.test:1.2 Wed Nov  6 12:41:22 2002
+++ gzz/lava/test/gzz/storm/impl/TransientPool.test     Wed Nov  6 17:02:56 2002
@@ -18,7 +18,12 @@
 
 import java, gzz
 
-def testNewBlock():
-    s = gzz.storm.StormPoolTest()
-    p = gzz.storm.impl.TransientPool()
-    s.testNewBlock(p)
+s = gzz.storm.StormPoolTest()
+
+def p():
+    return gzz.storm.impl.TransientPool()
+
+def testNewBlock(): s.testNewBlock(p())
+def testOwnHeader(): s.testOwnHeader(p())
+def testIdsNotNull(): s.testIdsNotNull(p())
+def testAddId(): s.testAddId(p())




reply via email to

[Prev in Thread] Current Thread [Next in Thread]