Author: manik.surtani(a)jboss.com
Date: 2009-02-25 12:29:45 -0500 (Wed, 25 Feb 2009)
New Revision: 7788
Modified:
core/branches/flat/src/main/java/org/horizon/loader/CacheStore.java
core/branches/flat/src/test/java/org/horizon/loader/BaseCacheStoreTest.java
Log:
More detailed fromStream()/toStream() contract and appropriate test
Modified: core/branches/flat/src/main/java/org/horizon/loader/CacheStore.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/CacheStore.java 2009-02-25
16:42:15 UTC (rev 7787)
+++ core/branches/flat/src/main/java/org/horizon/loader/CacheStore.java 2009-02-25
17:29:45 UTC (rev 7788)
@@ -29,7 +29,14 @@
* implementation-specific format, typically generated using {@link
#toStream(java.io.OutputStream)}. While not a
* requirement, it is recommended that implementations make use of the {@link
org.horizon.marshall.Marshaller} when
* dealing with the stream to make use of efficient marshalling.
- *
+ * <p />
+ * It is imperative that implementations <b><i>do not</i></b>
close the stream after finishing with it.
+ * <p />
+ * It is also <b><i>recommended</b></i> that implementations
use their own start and end markers on the stream
+ * since other processes may write additional data to the stream after the cache store
has written to it. As such,
+ * either markers or some other mechanism to prevent the store from reading too much
information should be employed
+ * when writing to the stream in {@link #fromStream(java.io.InputStream)} to prevent
data corruption.
+ * <p />
* @param inputStream stream to read from
* @throws CacheLoaderException in the event of problems writing to the store
*/
@@ -41,7 +48,14 @@
* <p/>
* While not a requirement, it is recommended that implementations make use of the
{@link
* org.horizon.marshall.Marshaller} when dealing with the stream to make use of
efficient marshalling.
- *
+ * <p />
+ * It is imperative that implementations <b><i>do not</i></b>
close the stream after finishing with it.
+ * <p />
+ * It is also <b><i>recommended</b></i> that implementations
use their own start and end markers on the stream
+ * since other processes may write additional data to the stream after the cache store
has written to it. As such,
+ * either markers or some other mechanism to prevent the store from reading too much
information in {@link #fromStream(java.io.InputStream)}
+ * should be employed, to prevent data corruption.
+ * <p />
* @param outputStream stream to write to
* @throws CacheLoaderException in the event of problems reading from the store
*/
Modified: core/branches/flat/src/test/java/org/horizon/loader/BaseCacheStoreTest.java
===================================================================
--- core/branches/flat/src/test/java/org/horizon/loader/BaseCacheStoreTest.java 2009-02-25
16:42:15 UTC (rev 7787)
+++ core/branches/flat/src/test/java/org/horizon/loader/BaseCacheStoreTest.java 2009-02-25
17:29:45 UTC (rev 7788)
@@ -248,7 +248,6 @@
ByteArrayOutputStream out = new ByteArrayOutputStream();
cs.toStream(out);
- out.flush();
out.close();
cs.clear();
cs.fromStream(new ByteArrayInputStream(out.toByteArray()));
@@ -264,6 +263,42 @@
assert expected.isEmpty();
}
+ public void testStreamingAPIReusingStreams() throws IOException,
ClassNotFoundException, CacheLoaderException {
+ cs.store(new StoredEntry("k1", "v1", -1, -1));
+ cs.store(new StoredEntry("k2", "v2", -1, -1));
+ cs.store(new StoredEntry("k3", "v3", -1, -1));
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ byte[] dummyStartBytes = {1,2,3,4,5,6,7,8};
+ byte[] dummyEndBytes = {8,7,6,5,4,3,2,1};
+ out.write(dummyStartBytes);
+ cs.toStream(out);
+ out.write(dummyEndBytes);
+ out.close();
+ cs.clear();
+
+ // first pop the start bytes
+ byte[] dummy = new byte[8];
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ int bytesRead = in.read(dummy, 0, 8);
+ assert bytesRead == 8;
+ for (int i=1; i<9; i++) assert dummy[i - 1] == i : "Start byte stream
corrupted!";
+ cs.fromStream(in);
+ bytesRead = in.read(dummy, 0, 8);
+ assert bytesRead == 8;
+ for (int i=8; i>0; i--) assert dummy[8 - i] == i : "Start byte stream
corrupted!";
+
+ Set<StoredEntry> set = cs.loadAll();
+
+ assert set.size() == 3;
+ Set expected = new HashSet();
+ expected.add("k1");
+ expected.add("k2");
+ expected.add("k3");
+ for (StoredEntry se : set) assert expected.remove(se.getKey());
+ assert expected.isEmpty();
+ }
+
public void testConfigFile() throws Exception {
Class<? extends CacheLoaderConfig> cfgClass = cs.getConfigurationClass();
CacheLoaderConfig clc = Util.getInstance(cfgClass);
Show replies by date