incubator-isis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmatth...@apache.org
Subject svn commit: r1124417 - in /incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src: main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/ test/java/org/apache/isis/run...
Date Wed, 18 May 2011 20:45:30 GMT
Author: rmatthews
Date: Wed May 18 20:45:29 2011
New Revision: 1124417

URL: http://svn.apache.org/viewvc?rev=1124417&view=rev
Log:
Added checksums to data stored/transfered to fileserver.

Added:
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileReader.java
  (contents, props changed)
      - copied, changed from r1099954, incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataReader.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileWriter.java
  (contents, props changed)
      - copied, changed from r1099954, incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataWriter.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnectionTest.java
  (with props)
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/TestChecksumming.java
  (with props)
Removed:
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataReader.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataWriter.java
Modified:
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnection.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/FileClientCommandContext.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/FileServerDb.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/FileServerProcessor.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/LogWriter.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataReaderTest.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataWriterTest.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/FileServerTest.java
    incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/LogWriterTest.java

Modified: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnection.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnection.java?rev=1124417&r1=1124416&r2=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnection.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnection.java
Wed May 18 20:45:29 2011
@@ -26,8 +26,6 @@ import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
-import java.net.Socket;
-import java.net.UnknownHostException;
 
 import org.apache.isis.runtimes.dflt.objectstores.nosql.NoSqlStoreException;
 import org.apache.isis.runtimes.dflt.objectstores.nosql.file.server.Util;
@@ -39,31 +37,19 @@ public class ClientConnection {
 
     private static final Logger LOG = Logger.getLogger(ClientConnection.class);
 
-    private Socket socket;
-    private PrintWriter writer;
-    private BufferedReader reader;
+    private final InputStream inputStream;
+    private final OutputStream outputStream;
+    private final PrintWriter writer;
+    private final BufferedReader reader;
     private String[] headers;
     private int header;
 
-    private InputStream inputStream;
-    private OutputStream outputStream;
-
-    public ClientConnection(final String host, final int port, final int timeout) {
-        try {
-            socket = new Socket(host, port);
-            socket.setSoTimeout(timeout);
-            outputStream = socket.getOutputStream();
-            outputStream = Util.trace(outputStream, true);
-            writer = new PrintWriter(new OutputStreamWriter(outputStream, Util.ENCODING));
-            inputStream = socket.getInputStream();
-            inputStream = Util.trace(inputStream, true);
-            reader = new BufferedReader(new InputStreamReader(inputStream, Util.ENCODING));
-        } catch (final UnknownHostException e) {
-            throw new NoSqlStoreException("Unknow host " + host, e);
-        } catch (final IOException e) {
-            throw new NoSqlStoreException("Failed to connect to " + host + ":" + port, e);
-        }
 
+    public ClientConnection(final InputStream input, final OutputStream output) {
+        outputStream = Util.trace(output, true);
+        writer = new PrintWriter(new OutputStreamWriter(outputStream, Util.ENCODING));
+        inputStream = Util.trace(input, true);
+        reader = new BufferedReader(new InputStreamReader(inputStream, Util.ENCODING));
     }
 
     public void close() {

Modified: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/FileClientCommandContext.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/FileClientCommandContext.java?rev=1124417&r1=1124416&r2=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/FileClientCommandContext.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/FileClientCommandContext.java
Wed May 18 20:45:29 2011
@@ -19,6 +19,8 @@
 
 package org.apache.isis.runtimes.dflt.objectstores.nosql.file;
 
+import java.util.zip.CRC32;
+
 import org.apache.isis.runtimes.dflt.objectstores.nosql.NoSqlCommandContext;
 import org.apache.isis.runtimes.dflt.objectstores.nosql.StateWriter;
 
@@ -61,7 +63,15 @@ class FileClientCommandContext implement
 
     private void write(final char command, final JsonStateWriter writer) {
         connection.request(command, writer.getRequest());
-        connection.requestData(writer.getData());
+        String data = writer.getData();
+
+        CRC32 inputChecksum = new CRC32();
+        inputChecksum.update(data.getBytes());
+        inputChecksum.update('\n');
+        long checksum = inputChecksum.getValue();
+        String code = Long.toHexString(checksum);
+        
+        connection.requestData("00000000".substring(0, 8 - code.length()) + code + data);
         connection.endRequestSection();
     }
 

Modified: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/FileServerDb.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/FileServerDb.java?rev=1124417&r1=1124416&r2=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/FileServerDb.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/FileServerDb.java
Wed May 18 20:45:29 2011
@@ -19,12 +19,17 @@
 
 package org.apache.isis.runtimes.dflt.objectstores.nosql.file;
 
+import java.io.IOException;
+import java.net.Socket;
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.zip.CRC32;
 
 import org.apache.isis.runtimes.dflt.objectstores.nosql.NoSqlCommandContext;
 import org.apache.isis.runtimes.dflt.objectstores.nosql.NoSqlDataDatabase;
+import org.apache.isis.runtimes.dflt.objectstores.nosql.NoSqlStoreException;
 import org.apache.isis.runtimes.dflt.objectstores.nosql.StateReader;
 import org.apache.isis.runtimes.dflt.runtime.persistence.ConcurrencyException;
 import org.apache.isis.runtimes.dflt.runtime.persistence.objectstore.transaction.PersistenceCommand;
@@ -47,7 +52,17 @@ public class FileServerDb implements NoS
 
     // TODO pool connection and reuse
     private ClientConnection getConnection() {
-        return new ClientConnection(host, port, timeout);
+        try {
+            final Socket socket;
+            socket = new Socket(host, port);
+            socket.setSoTimeout(timeout);
+            return new ClientConnection(socket.getInputStream(), socket.getOutputStream());
+        } catch (final UnknownHostException e) {
+            throw new NoSqlStoreException("Unknow host " + host, e);
+        } catch (final IOException e) {
+            throw new NoSqlStoreException("Failed to connect to " + host + ":" + port, e);
+        }
+
     }
 
     // TODO pool connection and reuse
@@ -76,6 +91,7 @@ public class FileServerDb implements NoS
             abortConnection(connection);
             throw e;
         }
+        data = checkData(data);
         final JsonStateReader reader = new JsonStateReader(data);
         returnConnection(connection);
         return reader;
@@ -91,6 +107,7 @@ public class FileServerDb implements NoS
             connection.validateRequest();
             String data;
             while ((data = connection.getResponseData()).length() > 0) {
+                data = checkData(data);
                 final JsonStateReader reader = new JsonStateReader(data);
                 instances.add(reader);
             }
@@ -104,6 +121,23 @@ public class FileServerDb implements NoS
 
     }
 
+    private String checkData(final String data) {
+        String objectData = data.substring(8);
+        
+        CRC32 inputChecksum = new CRC32();
+        inputChecksum.update(objectData.getBytes());
+        long actualChecksum = inputChecksum.getValue();
+        
+        String encodedChecksum = data.substring(0, 8);
+        long expectedChecksum = Long.valueOf(encodedChecksum, 16);
+        
+        if (actualChecksum != expectedChecksum) {
+            throw new NoSqlStoreException("Data integrity error; checksums different");
+        }
+
+        return objectData;
+    }
+
     @Override
     public void write(final List<PersistenceCommand> commands) {
         final ClientConnection connection = getConnection();

Copied: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileReader.java
(from r1099954, incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataReader.java)
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileReader.java?p2=incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileReader.java&p1=incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataReader.java&r1=1099954&r2=1124417&rev=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataReader.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileReader.java
Wed May 18 20:45:29 2011
@@ -27,8 +27,8 @@ import java.io.InputStreamReader;
 
 import org.apache.log4j.Logger;
 
-public class DataReader {
-    private static final Logger LOG = Logger.getLogger(DataReader.class);
+public class DataFileReader {
+    private static final Logger LOG = Logger.getLogger(DataFileReader.class);
 
     private final BufferedReader reader;
     private final String id;
@@ -38,7 +38,7 @@ public class DataReader {
      * Opens the file for the specified id. The top line contains: type id version newline
The remainder contains the
      * data.
      */
-    public DataReader(final String type, final String id) throws IOException {
+    public DataFileReader(final String type, final String id) throws IOException {
         final File file = Util.dataFile(type, id);
         reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
         final String line = reader.readLine();
@@ -78,6 +78,7 @@ public class DataReader {
             String line;
             while ((line = reader.readLine()) != null) {
                 buffer.append(line);
+                buffer.append("\n");
             }
             return buffer.toString();
         } catch (final IOException e) {

Propchange: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileReader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileWriter.java
(from r1099954, incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataWriter.java)
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileWriter.java?p2=incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileWriter.java&p1=incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataWriter.java&r1=1099954&r2=1124417&rev=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataWriter.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileWriter.java
Wed May 18 20:45:29 2011
@@ -24,7 +24,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.List;
 
-public class DataWriter {
+public class DataFileWriter {
 
     // private static final Logger LOG = Logger.getLogger(DataWriter.class);
 
@@ -32,7 +32,7 @@ public class DataWriter {
 
     private final List<FileContent> files;
 
-    public DataWriter(final List<FileContent> files) {
+    public DataFileWriter(final List<FileContent> files) {
         this.files = files;
     }
 

Propchange: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataFileWriter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/FileServerProcessor.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/FileServerProcessor.java?rev=1124417&r1=1124416&r2=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/FileServerProcessor.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/FileServerProcessor.java
Wed May 18 20:45:29 2011
@@ -123,10 +123,9 @@ public class FileServerProcessor {
                 for (final File file : listFiles) {
                     final String fileName = file.getName();
                     final String id = fileName.substring(0, fileName.length() - 5);
-                    final DataReader reader = findInstance(type, id, connection);
+                    final DataFileReader reader = findInstance(type, id, connection);
                     readInstance(reader, connection);
                     locks.release(id, getTransactionId());
-                    connection.endBlock();
                     if (limit-- < 0) {
                         break;
                     }
@@ -158,7 +157,7 @@ public class FileServerProcessor {
             connection.endCommand();
             type = connection.getRequest();
             id = connection.getRequest();
-            final DataReader reader = findInstance(type, id, connection);
+            final DataFileReader reader = findInstance(type, id, connection);
             if (reader == null) {
                 connection.notFound(Util.FILE_NOT_FOUND + " for " + type + "/" + id);
             } else {
@@ -173,19 +172,19 @@ public class FileServerProcessor {
 
     }
 
-    private DataReader findInstance(final String type, final String id, final ServerConnection
connection)
+    private DataFileReader findInstance(final String type, final String id, final ServerConnection
connection)
         throws IOException {
         LOG.debug("reading file " + id);
         locks.acquireRead(id, getTransactionId());
         try {
-            return new DataReader(type, id);
+            return new DataFileReader(type, id);
         } catch (final FileNotFoundException e) {
             LOG.error(Util.FILE_NOT_FOUND + " for " + type + "/" + id, e);
             return null;
         }
     }
 
-    private void readInstance(final DataReader reader, final ServerConnection connection)
{
+    private void readInstance(final DataFileReader reader, final ServerConnection connection)
{
         final String data = reader.getData();
         reader.close();
         connection.responseData(data);
@@ -198,7 +197,7 @@ public class FileServerProcessor {
             final String error = acquireLocks(files);
             if (error == null) {
                 logger.log(files);
-                final DataWriter content = new DataWriter(files);
+                final DataFileWriter content = new DataFileWriter(files);
                 content.writeData();
                 connection.ok();
             } else {
@@ -238,7 +237,7 @@ public class FileServerProcessor {
                 return item.type + " being changed by another user, please try again\n" +
item.data;
             }
             if (Util.shouldFileExist(item.command)) {
-                final DataReader dataReader = new DataReader(item.type, item.id);
+                final DataFileReader dataReader = new DataFileReader(item.type, item.id);
                 final String version = dataReader.getVersion();
                 if (!version.equals(item.currentVersion)) {
                     // String data = dataReader.getData();

Modified: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/LogWriter.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/LogWriter.java?rev=1124417&r1=1124416&r2=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/LogWriter.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/main/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/LogWriter.java
Wed May 18 20:45:29 2011
@@ -55,6 +55,7 @@ public class LogWriter {
                 writer.write(' ');
                 writer.write(content.currentVersion.getBytes("utf-8"));
                 writer.write(' ');
+                writer.write('\n');
                 writer.write(content.data.getBytes("utf-8"));
                 writer.write('\n');
                 writer.write('\n');

Added: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnectionTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnectionTest.java?rev=1124417&view=auto
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnectionTest.java
(added)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnectionTest.java
Wed May 18 20:45:29 2011
@@ -0,0 +1,128 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.runtimes.dflt.objectstores.nosql.file;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+import org.apache.isis.core.commons.lang.IoUtils;
+import org.apache.isis.runtimes.dflt.runtime.persistence.ConcurrencyException;
+import org.apache.isis.runtimes.dflt.runtime.persistence.ObjectNotFoundException;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+
+public class ClientConnectionTest {
+
+    private InputStream input;
+    private ByteArrayOutputStream output;
+    private ClientConnection connection;
+
+    @Before
+    public void setup() throws Exception {
+        Logger.getRootLogger().setLevel(Level.OFF);
+    
+        input = IoUtils.asUtf8ByteStream("org.domain.Class false true 1025\n{data...}\n\n102334");
+        output = new ByteArrayOutputStream();
+        connection = new ClientConnection(input, output);
+    }
+    
+    @Test
+    public void testRequest() throws Exception {
+        connection.request('D', "xxx yyy");
+        connection.close();
+        assertEquals("Dxxx yyy\n", output.toString());
+    }
+    
+    @Test
+    public void testRequestData() throws Exception {
+        connection.requestData("{data...}");
+        connection.close();
+ //       assertEquals("{data...}\n\n5de98274", output.toString());
+    }
+    
+    @Test
+    public void testResponseHeaders() throws Exception {
+        connection.getReponseHeader();
+        assertEquals("org.domain.Class", connection.getResponse());
+        assertEquals(false, connection.getResponseAsBoolean());
+        assertEquals(true, connection.getResponseAsBoolean());
+        assertEquals(1025L, connection.getResponseAsLong());
+    }
+    
+    @Test
+    public void tooManyResponseHeadersExpected() throws Exception {
+        connection.getReponseHeader();
+        connection.getResponse();
+        connection.getResponse();
+        connection.getResponse();
+        connection.getResponse();
+        try {
+            connection.getResponse();
+            fail();
+        }catch (RemotingException e) {
+            assertThat(e.getMessage(), containsString("are only 4"));
+        }
+    }
+    
+    @Test
+    public void testResponseData() throws Exception {
+        connection.getReponseHeader();
+        String data = connection.getResponseData();
+        assertEquals("{data...}\n", data);
+    }
+    
+    @Test
+    public void validateResponseOk() throws Exception {
+        input = IoUtils.asUtf8ByteStream("ok xx xx\n{data...}");
+        connection = new ClientConnection(input, output);
+        connection.validateRequest();
+    }
+    
+    @Test(expected=RemotingException.class) 
+    public void validateResponseError() throws Exception {
+        input = IoUtils.asUtf8ByteStream("error message about it\n");
+        connection = new ClientConnection(input, output);
+        connection.validateRequest();
+    }
+    
+    @Test(expected=ObjectNotFoundException.class) 
+    public void validateObjectNotFound() throws Exception {
+        input = IoUtils.asUtf8ByteStream("not-found message about it\n");
+        connection = new ClientConnection(input, output);
+        connection.validateRequest();
+    }
+    
+    @Test(expected=ConcurrencyException.class) 
+    public void validateConcurrencyException() throws Exception {
+        input = IoUtils.asUtf8ByteStream("concurrency message about it\n");
+        connection = new ClientConnection(input, output);
+        connection.validateRequest();
+    }
+    
+    
+}
+

Propchange: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/ClientConnectionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/TestChecksumming.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/TestChecksumming.java?rev=1124417&view=auto
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/TestChecksumming.java
(added)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/TestChecksumming.java
Wed May 18 20:45:29 2011
@@ -0,0 +1,142 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.runtimes.dflt.objectstores.nosql.file;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.util.zip.CRC32;
+import java.util.zip.CheckedInputStream;
+
+import org.apache.isis.runtimes.dflt.objectstores.nosql.file.server.Util;
+
+
+public class TestChecksumming {
+
+    public static void main(String[] args) throws Exception {
+
+        CRC32 inputChecksum = new CRC32();
+        CheckedInputStream in = new CheckedInputStream(new FileInputStream("test.data"),
inputChecksum);
+        BufferedReader reader = new BufferedReader(new InputStreamReader(in, Util.ENCODING));
+
+        for (int i = 0; i < 30; i++) {
+            long time = System.currentTimeMillis();
+            StringBuffer buf = null;
+            for (int j = 0; j < 1000; j++) {
+                buf = readFile(i);
+            }
+            time = System.currentTimeMillis() - time;
+            System.out.print(time);
+            System.out.print("   ");
+
+            time = System.currentTimeMillis();
+            for (int j = 0; j < 1000; j++) {
+                readChecksummedFile(i);
+            }
+            time = System.currentTimeMillis() - time;
+            System.out.print(time);
+            System.out.print("   ");
+
+            time = System.currentTimeMillis();
+            for (int j = 0; j < 1000; j++) {
+                testArray(buf);
+            }
+            time = System.currentTimeMillis() - time;
+            System.out.print(time);
+            System.out.print("   ");
+
+            byte[] data = null;
+            time = System.currentTimeMillis();
+            for (int j = 0; j < 1000; j++) {
+                data = extractArray(buf);
+            }
+            time = System.currentTimeMillis() - time;
+            System.out.print(time);
+            System.out.print("   ");
+
+            time = System.currentTimeMillis();
+            for (int j = 0; j < 1000; j++) {
+                checksumArray(data);
+            }
+            time = System.currentTimeMillis() - time;
+            System.out.println(time);
+        }
+
+    }
+
+    private static void testArray(StringBuffer buf) {
+        byte[] data = buf.toString().getBytes();
+
+        CRC32 inputChecksum = new CRC32();
+        inputChecksum.reset();
+        inputChecksum.update(data);
+
+        // System.out.println(inputChecksum.getValue());
+    }
+
+
+    private static byte[] extractArray(StringBuffer buf) {
+        byte[] data = buf.toString().getBytes();
+        return data;
+    }
+    
+    private static void checksumArray(byte[] data) {
+        CRC32 inputChecksum = new CRC32();
+        inputChecksum.reset();
+        inputChecksum.update(data);
+
+        // System.out.println(inputChecksum.getValue());
+    }
+
+    private static StringBuffer readChecksummedFile(int i) throws Exception {
+        CRC32 inputChecksum = new CRC32();
+        CheckedInputStream in = new CheckedInputStream(new FileInputStream("test" + i % 3
+ ".data"), inputChecksum);
+        BufferedReader reader = new BufferedReader(new InputStreamReader(in, Util.ENCODING));
+
+        StringBuffer buf = new StringBuffer();
+        String line;
+        while ((line = reader.readLine()) != null) {
+            buf.append(line);
+            buf.append('\n');
+        }
+
+        // System.out.println(inputChecksum.getValue());
+
+        reader.close();
+
+        return buf;
+    }
+
+    private static StringBuffer readFile(int i) throws Exception {
+        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test"
+ i % 3 + ".data"), Util.ENCODING));
+
+        StringBuffer buf = new StringBuffer();
+        String line;
+        while ((line = reader.readLine()) != null) {
+            buf.append(line);
+            buf.append('\n');
+        }
+
+        // System.out.println(inputChecksum.getValue());
+
+        reader.close();
+
+        return buf;
+    }
+}

Propchange: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/TestChecksumming.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataReaderTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataReaderTest.java?rev=1124417&r1=1124416&r2=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataReaderTest.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataReaderTest.java
Wed May 18 20:45:29 2011
@@ -33,7 +33,7 @@ import org.junit.Test;
 
 public class DataReaderTest {
 
-    private DataReader reader;
+    private DataFileReader reader;
 
     @BeforeClass
     public static void setUp() throws Exception {
@@ -45,7 +45,7 @@ public class DataReaderTest {
     @Test
     public void noFileCausesException() throws Exception {
         try {
-            new DataReader("type", "noexistant");
+            new DataFileReader("type", "nonexistant");
             fail();
         } catch (final FileNotFoundException expected) {
         }
@@ -58,7 +58,7 @@ public class DataReaderTest {
         writer.close();
 
         try {
-            reader = new DataReader("type", "0013");
+            reader = new DataFileReader("type", "0013");
             fail();
         } catch (final FileServerException expected) {
             assertThat(expected.getMessage(), startsWith("No data in file:"));
@@ -72,13 +72,13 @@ public class DataReaderTest {
         writer.write("class.type 0012 17\n{data}");
         writer.close();
 
-        reader = new DataReader("type", "0012");
+        reader = new DataFileReader("type", "0012");
 
         assertEquals("0012", reader.getId());
         assertEquals("17", reader.getVersion());
 
         final String data = reader.getData();
-        assertEquals("{data}", data);
+        assertEquals("{data}\n", data);
         reader.close();
     }
 }

Modified: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataWriterTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataWriterTest.java?rev=1124417&r1=1124416&r2=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataWriterTest.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/DataWriterTest.java
Wed May 18 20:45:29 2011
@@ -27,6 +27,8 @@ import java.util.List;
 
 import junit.framework.Assert;
 
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -36,10 +38,11 @@ public class DataWriterTest {
     private static final String TARGET_DIRECTORY = "target/test/";
     private static final String FULLY_QUALIFIED_CLASSNAME = "org.domain.Class";
     private static final String FULLY_QUALIFIED_CLASSNAME_2 = "org.domain.Class2";
-    private DataWriter writer;
+    private DataFileWriter writer;
 
     @Before
     public void setUp() throws Exception {
+        Logger.getRootLogger().setLevel(Level.OFF);
         Util.setDirectory(TARGET_DIRECTORY, "services", "logs");
         Util.ensureDirectoryExists();
     }
@@ -62,7 +65,7 @@ public class DataWriterTest {
         files.add(new FileContent('I', "1023", "1", "2", FULLY_QUALIFIED_CLASSNAME, "{data1}"));
         files.add(new FileContent('U', "1024", "21", "22", FULLY_QUALIFIED_CLASSNAME, "{data2}"));
         files.add(new FileContent('D', "1030", "66", "", FULLY_QUALIFIED_CLASSNAME, ""));
-        final DataWriter writer = new DataWriter(files);
+        final DataFileWriter writer = new DataFileWriter(files);
         writer.writeData();
 
         BufferedReader reader =
@@ -85,7 +88,7 @@ public class DataWriterTest {
 
         final List<FileContent> files = new ArrayList<FileContent>();
         files.add(new FileContent('I', "1023", "1", "2", FULLY_QUALIFIED_CLASSNAME_2, "{data1}"));
-        writer = new DataWriter(files);
+        writer = new DataFileWriter(files);
         writer.writeData();
 
         Assert.assertTrue(file.exists());

Modified: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/FileServerTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/FileServerTest.java?rev=1124417&r1=1124416&r2=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/FileServerTest.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/FileServerTest.java
Wed May 18 20:45:29 2011
@@ -223,7 +223,7 @@ public class FileServerTest {
             new ServerConnection(IoUtils.asUtf8ByteStream("Rorg.domain.Class 2025\n\n"),
out);
         server.process(connection);
 
-        assertThat(out.toString(), is(equalTo(lineSeparated("ok\n{data1}\n"))));
+        assertThat(out.toString(), is(equalTo(lineSeparated("ok\n{data1}\n\n"))));
     }
 
     @Test
@@ -247,7 +247,7 @@ public class FileServerTest {
             new ServerConnection(IoUtils.asUtf8ByteStream("Rorg.domain.Class 2025\n\n"),
out);
         server.process(connection);
 
-        assertThat(out.toString(), is(equalTo(lineSeparated("ok\n{data1}\n"))));
+        assertThat(out.toString(), is(equalTo(lineSeparated("ok\n{data1}\n\n"))));
     }
 
     @Test
@@ -261,7 +261,30 @@ public class FileServerTest {
             new ServerConnection(IoUtils.asUtf8ByteStream("Rorg.domain.Class 2025\n\n"),
out);
         server.process(connection);
 
-        assertThat(out.toString(), is(equalTo(lineSeparated("ok\n{data1}\n"))));
+        assertThat(out.toString(), is(equalTo(lineSeparated("ok\n{data1}\n\n"))));
+    }
+
+    @Test
+    public void hasNoInstances() throws Exception {
+        final ServerConnection connection =
+            new ServerConnection(IoUtils.asUtf8ByteStream("Iorg.domain.None\n\n"), out);
+        server.process(connection);
+
+        assertThat(out.toString(), is(equalTo(lineSeparated("ok false\n"))));
+    }
+
+    @Test
+    public void hasInstances() throws Exception {
+        final File file1 = new File("target/test/org.domain.Class", "2025.data");
+        final FileWriter fileWriter = new FileWriter(file1);
+        fileWriter.write("type 1025 1\n{data1}");
+        fileWriter.close();
+
+        final ServerConnection connection =
+            new ServerConnection(IoUtils.asUtf8ByteStream("Iorg.domain.Class\n\n"), out);
+        server.process(connection);
+
+        assertThat(out.toString(), is(equalTo(lineSeparated("ok true\n"))));
     }
 
 }

Modified: incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/LogWriterTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/LogWriterTest.java?rev=1124417&r1=1124416&r2=1124417&view=diff
==============================================================================
--- incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/LogWriterTest.java
(original)
+++ incubator/isis/trunk/runtimes/dflt/objectstores/nosql/src/test/java/org/apache/isis/runtimes/dflt/objectstores/nosql/file/server/LogWriterTest.java
Wed May 18 20:45:29 2011
@@ -61,7 +61,7 @@ public class LogWriterTest {
 
         items = new ArrayList<FileContent>();
         items.add(new FileContent('U', "20", "6", "7", "type", "{data}"));
-        new DataWriter(items);
+        new DataFileWriter(items);
     }
 
     @After
@@ -84,9 +84,11 @@ public class LogWriterTest {
         final BufferedReader reader = new BufferedReader(new FileReader(logFile1));
         String line = reader.readLine();
         line = reader.readLine();
+        Assert.assertEquals("Utype 20 6 ", line);
+        line = reader.readLine();
+        Assert.assertEquals("{data}", line);
         reader.close();
 
-        Assert.assertEquals("Utype 20 6 {data}", line);
     }
 
     @Test



Mime
View raw message