Author: gdusbabek
Date: Tue Feb  9 14:05:00 2010
New Revision: 908038

URL: http://svn.apache.org/viewvc?rev=908038&view=rev
Log:
Embedded cassandra service to facilitate testing apps that depend on cassandra. 
Patch by Ran Tavory, reviewed by Gary Dusbabek. CASSANDRA-740.

Added:
    
incubator/cassandra/trunk/src/java/org/apache/cassandra/service/EmbeddedCassandraService.java
    
incubator/cassandra/trunk/test/unit/org/apache/cassandra/service/EmbeddedCassandraServiceTest.java

Added: 
incubator/cassandra/trunk/src/java/org/apache/cassandra/service/EmbeddedCassandraService.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/service/EmbeddedCassandraService.java?rev=908038&view=auto
==============================================================================
--- 
incubator/cassandra/trunk/src/java/org/apache/cassandra/service/EmbeddedCassandraService.java
 (added)
+++ 
incubator/cassandra/trunk/src/java/org/apache/cassandra/service/EmbeddedCassandraService.java
 Tue Feb  9 14:05:00 2010
@@ -0,0 +1,60 @@
+package org.apache.cassandra.service;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.thrift.CassandraDaemon;
+import org.apache.thrift.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An embedded, in-memory cassandra storage service that listens
+ * on the thrift interface as configured in storage-conf.xml
+ * This kind of service is useful when running unit tests of
+ * services using cassandra for example.
+ *
+ * See {...@link EmbeddedCassandraServiceTest} for usage.
+ * <p>
+ * This is the implementation of 
https://issues.apache.org/jira/browse/CASSANDRA-740
+ * <p>
+ * How to use:
+ * In the client code create a new thread and spawn it with its {...@link 
Thread#start()} method.
+ * Example:
+ * <pre>
+ *      // Tell cassandra where the configuration files are.
+        System.setProperty("storage-config", "conf");
+
+        cassandra = new EmbeddedCassandraService();
+        cassandra.init();
+
+        // spawn cassandra in a new thread
+        Thread t = new Thread(cassandra);
+        t.setDaemon(true);
+        t.start();
+
+ * </pre>
+ * @author Ran Tavory ([email protected])
+ *
+ */
+public class EmbeddedCassandraService implements Runnable
+{
+
+    CassandraDaemon cassandraDaemon;
+
+    public void init() throws TTransportException, IOException
+    {
+        cassandraDaemon = new CassandraDaemon();
+        cassandraDaemon.init(null);
+    }
+
+    public void run()
+    {
+        cassandraDaemon.start();
+    }
+}

Added: 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/service/EmbeddedCassandraServiceTest.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/unit/org/apache/cassandra/service/EmbeddedCassandraServiceTest.java?rev=908038&view=auto
==============================================================================
--- 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/service/EmbeddedCassandraServiceTest.java
 (added)
+++ 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/service/EmbeddedCassandraServiceTest.java
 Tue Feb  9 14:05:00 2010
@@ -0,0 +1,120 @@
+/*
+* 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.cassandra.service;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.cassandra.thrift.Cassandra;
+import org.apache.cassandra.thrift.ColumnOrSuperColumn;
+import org.apache.cassandra.thrift.ColumnPath;
+import org.apache.cassandra.thrift.ConsistencyLevel;
+import org.apache.cassandra.thrift.InvalidRequestException;
+import org.apache.cassandra.thrift.NotFoundException;
+import org.apache.cassandra.thrift.TimedOutException;
+import org.apache.cassandra.thrift.UnavailableException;
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.transport.TSocket;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Example how to use an embedded cassandra service.
+ *
+ * Tests connect to localhost:9160 when the embedded server is running.
+ *
+ * @author Ran Tavory ([email protected])
+ *
+ */
+public class EmbeddedCassandraServiceTest
+{
+
+    private static EmbeddedCassandraService cassandra;
+
+    /**
+     * Set embedded cassandra up and spawn it in a new thread.
+     *
+     * @throws TTransportException
+     * @throws IOException
+     * @throws InterruptedException
+     */
+    @BeforeClass
+    public static void setup() throws TTransportException, IOException, 
InterruptedException
+    {
+
+        // Tell cassandra where the configuration files are.
+        // Use the test configuration file.
+        System.setProperty("storage-config", "test/conf");
+
+        cassandra = new EmbeddedCassandraService();
+        cassandra.init();
+
+        // spawn cassandra in a new thread
+        Thread t = new Thread(cassandra);
+        t.setDaemon(true);
+        t.start();
+    }
+
+    @Test
+    public void testEmbeddedCassandraService() throws 
UnsupportedEncodingException, InvalidRequestException,
+            UnavailableException, TimedOutException, TException, 
NotFoundException
+    {
+        Cassandra.Client client = getClient();
+
+        String key_user_id = "1";
+
+        long timestamp = System.currentTimeMillis();
+        ColumnPath cp = new ColumnPath("Standard1");
+        cp.setColumn("name".getBytes("utf-8"));
+
+        // insert
+        client.insert("Keyspace1", key_user_id, cp, "Ran".getBytes("UTF-8"),
+                timestamp, ConsistencyLevel.ONE);
+
+        // read
+        ColumnOrSuperColumn got = client.get("Keyspace1", key_user_id, cp,
+                ConsistencyLevel.ONE);
+
+        // assert
+        assertNotNull("Got a null ColumnOrSuperColumn", got);
+        assertEquals("Ran", new String(got.getColumn().getValue(), "utf-8"));
+    }
+
+    /**
+     * Gets a connection to the localhost client
+     *
+     * @return
+     * @throws TTransportException
+     */
+    private Cassandra.Client getClient() throws TTransportException
+    {
+        TTransport tr = new TSocket("localhost", 9170);
+        TProtocol proto = new TBinaryProtocol(tr);
+        Cassandra.Client client = new Cassandra.Client(proto);
+        tr.open();
+        return client;
+    }
+}


Reply via email to