You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ap...@apache.org on 2009/06/29 04:49:26 UTC

svn commit: r789195 [8/11] - in /hadoop/hbase/trunk_on_hadoop-0.18.3/contrib: ./ stargate/ stargate/conf/ stargate/lib/ stargate/src/ stargate/src/java/ stargate/src/java/org/ stargate/src/java/org/apache/ stargate/src/java/org/apache/hadoop/ stargate/...

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/provider/producer/ProtobufMessageBodyProducer.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/provider/producer/ProtobufMessageBodyProducer.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/provider/producer/ProtobufMessageBodyProducer.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/provider/producer/ProtobufMessageBodyProducer.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate.provider.producer;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.hadoop.hbase.stargate.Constants;
+import org.apache.hadoop.hbase.stargate.model.IProtobufWrapper;
+
+@Provider
+@Produces(Constants.MIMETYPE_PROTOBUF)
+public class ProtobufMessageBodyProducer implements MessageBodyWriter<IProtobufWrapper> {
+
+	@Override
+	public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+        return IProtobufWrapper.class.isAssignableFrom(type);
+    }
+
+    private Map<Object, byte[]> buffer = new WeakHashMap<Object, byte[]>();
+
+    @Override
+    public long getSize(IProtobufWrapper m, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try {
+        	baos.write(m.createProtobufOutput());
+        } catch (IOException e) {
+            return -1;
+        }
+        byte[] bytes = baos.toByteArray();
+        buffer.put(m, bytes);
+        return bytes.length;
+    }
+
+    public void writeTo(IProtobufWrapper m, Class<?> type, Type genericType, Annotation[] annotations, 
+                MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
+                OutputStream entityStream) throws IOException, WebApplicationException {
+        entityStream.write(buffer.remove(m));
+    }
+
+
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/MiniClusterTestCase.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/MiniClusterTestCase.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/MiniClusterTestCase.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/MiniClusterTestCase.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,239 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.LocalHBaseCluster;
+import org.apache.hadoop.hbase.MiniHBaseCluster;
+import org.apache.hadoop.hbase.MiniZooKeeperCluster;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.util.FSUtils;
+import org.apache.hadoop.hdfs.MiniDFSCluster;
+import org.apache.hadoop.util.StringUtils;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.servlet.Context;
+import org.mortbay.jetty.servlet.ServletHolder;
+
+import com.sun.jersey.spi.container.servlet.ServletContainer;
+
+import junit.framework.TestCase;
+
+public class MiniClusterTestCase extends TestCase {
+  private static final Log LOG =
+    LogFactory.getLog(MiniClusterTestCase.class);
+
+  public static final String MIMETYPE_BINARY = "application/octet-stream";
+  public static final String MIMETYPE_JSON = "application/json";
+  public static final String MIMETYPE_PLAIN = "text/plain";
+  public static final String MIMETYPE_PROTOBUF = "application/x-protobuf";
+  public static final String MIMETYPE_XML = "text/xml";
+
+  // use a nonstandard port
+  public static final int DEFAULT_TEST_PORT = 38080;
+
+  protected static HBaseConfiguration conf = new HBaseConfiguration();
+  protected static MiniZooKeeperCluster zooKeeperCluster;
+  protected static MiniHBaseCluster hbaseCluster;
+  protected static MiniDFSCluster dfsCluster;
+  protected static File testDir;
+  protected static int testServletPort;
+  protected static Server server;
+
+  public static boolean isMiniClusterRunning() {
+    return server != null;
+  }
+
+  private static void startDFS() throws Exception {
+    if (dfsCluster != null) {
+      LOG.error("MiniDFSCluster already running");
+      return;
+    }
+    Path path = new Path(
+      conf.get("test.build.data",
+        System.getProperty("test.build.data", "build/test/data")));
+    FileSystem testFS = FileSystem.get(conf);
+    if (testFS.exists(path)) {
+      testFS.delete(path, true);
+    }
+    testDir = new File(path.toString());
+    dfsCluster = new MiniDFSCluster(conf, 2, true, (String[])null);
+    FileSystem filesystem = dfsCluster.getFileSystem();
+    conf.set("fs.default.name", filesystem.getUri().toString());      
+    Path parentdir = filesystem.getHomeDirectory();
+    conf.set(HConstants.HBASE_DIR, parentdir.toString());
+    filesystem.mkdirs(parentdir);
+    FSUtils.setVersion(filesystem, parentdir);
+    LOG.info("started MiniDFSCluster in " + testDir.toString());
+  }
+
+  private static void stopDFS() {
+    if (dfsCluster != null) try {
+      dfsCluster.shutdown();
+      dfsCluster = null;
+    } catch (Exception e) {
+      LOG.warn(StringUtils.stringifyException(e));
+    }
+  }
+
+  private static void startZooKeeper() throws Exception {
+    if (zooKeeperCluster != null) {
+      LOG.error("ZooKeeper already running");
+      return;
+    }
+    zooKeeperCluster = new MiniZooKeeperCluster();
+    zooKeeperCluster.startup(testDir);
+    LOG.info("started " + zooKeeperCluster.getClass().getName());
+  }
+
+  private static void stopZooKeeper() {
+    if (zooKeeperCluster != null) try {
+      zooKeeperCluster.shutdown();
+      zooKeeperCluster = null;
+    } catch (Exception e) {
+      LOG.warn(StringUtils.stringifyException(e));
+    }
+  }
+  
+  private static void startHBase() throws Exception {
+    if (hbaseCluster != null) {
+      LOG.error("MiniHBaseCluster already running");
+      return;
+    }
+    hbaseCluster = new MiniHBaseCluster(conf, 1);
+    // opening the META table ensures that cluster is running
+    new HTable(conf, HConstants.META_TABLE_NAME);
+    LOG.info("started MiniHBaseCluster");
+  }
+  
+  private static void stopHBase() {
+    if (hbaseCluster != null) try {
+      for (LocalHBaseCluster.RegionServerThread regionThread:
+        hbaseCluster.getRegionThreads()) {
+        regionThread.getRegionServer().abort();
+      }
+      hbaseCluster.shutdown();
+      hbaseCluster = null;
+    } catch (Exception e) {
+      LOG.warn(StringUtils.stringifyException(e));
+    }
+  }
+
+  private static void startServletContainer() throws Exception {
+    if (server != null) {
+      LOG.error("ServletContainer already running");
+      return;
+    }
+
+    // set up the Jersey servlet container for Jetty
+    ServletHolder sh = new ServletHolder(ServletContainer.class);
+    sh.setInitParameter(
+      "com.sun.jersey.config.property.resourceConfigClass",
+      ResourceConfig.class.getCanonicalName());
+    sh.setInitParameter("com.sun.jersey.config.property.packages",
+      "jetty");
+
+    LOG.info("configured " + ServletContainer.class.getName());
+    
+    // set up Jetty and run the embedded server
+    testServletPort = conf.getInt("test.stargate.port", DEFAULT_TEST_PORT);
+    server = new Server(testServletPort);
+    server.setSendServerVersion(false);
+    server.setSendDateHeader(false);
+      // set up context
+    Context context = new Context(server, "/", Context.SESSIONS);
+    context.addServlet(sh, "/*");
+      // start the server
+    server.start();
+
+    LOG.info("started " + server.getClass().getName() + " on port " + 
+      testServletPort);
+  }
+
+  private static void stopServletContainer() {
+    if (server != null) try {
+      server.stop();
+      server = null;
+    } catch (Exception e) {
+      LOG.warn(StringUtils.stringifyException(e));
+    }
+  }
+
+  public static void startMiniCluster() throws Exception {
+    try {
+      startDFS();
+      startZooKeeper();
+      startHBase();
+      startServletContainer();
+    } catch (Exception e) {
+      stopServletContainer();
+      stopHBase();
+      stopZooKeeper();
+      stopDFS();
+      throw e;
+    }
+  }
+
+  public static void stopMiniCluster() {
+    stopServletContainer();
+    stopHBase();
+    stopZooKeeper();
+    stopDFS();
+  }
+
+  class MiniClusterShutdownThread extends Thread {
+    public void run() {
+      stopMiniCluster();
+      Path path = new Path(
+        conf.get("test.build.data",
+          System.getProperty("test.build.data", "build/test/data")));
+      try {
+        FileSystem.get(conf).delete(path, true);
+      } catch (IOException e) {
+        LOG.error(StringUtils.stringifyException(e));
+      }
+    }
+  }
+
+  @Override
+  protected void setUp() throws Exception {
+    // start the mini cluster if it is not running yet
+    if (!isMiniClusterRunning()) {
+      startMiniCluster();
+      Runtime.getRuntime().addShutdownHook(new MiniClusterShutdownThread());
+    }
+
+    // tell HttpClient to dump request and response headers into the test
+    // log at DEBUG level
+    Logger.getLogger("httpclient.wire.header").setLevel(Level.DEBUG);
+
+    super.setUp();
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/Test00MiniCluster.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/Test00MiniCluster.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/Test00MiniCluster.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/Test00MiniCluster.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate;
+
+import java.io.IOException;
+
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.client.HTable;
+
+public class Test00MiniCluster extends MiniClusterTestCase {
+  public void testDFSMiniCluster() {
+    assertNotNull(dfsCluster);
+  }
+
+  public void testZooKeeperMiniCluster() {
+    assertNotNull(zooKeeperCluster);
+  }
+
+  public void testHBaseMiniCluster() throws IOException {
+    assertNotNull(hbaseCluster);
+    assertNotNull(new HTable(conf, HConstants.META_TABLE_NAME));
+  }
+
+  public void testStargateServlet() throws IOException {
+    assertNotNull(server);
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestRowResource.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestRowResource.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestRowResource.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestRowResource.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,366 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+
+import org.apache.commons.httpclient.Header;
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.hbase.stargate.client.Client;
+import org.apache.hadoop.hbase.stargate.client.Cluster;
+import org.apache.hadoop.hbase.stargate.client.Response;
+import org.apache.hadoop.hbase.stargate.model.CellModel;
+import org.apache.hadoop.hbase.stargate.model.CellSetModel;
+import org.apache.hadoop.hbase.stargate.model.RowModel;
+import org.apache.hadoop.hbase.util.Bytes;
+
+public class TestRowResource extends MiniClusterTestCase {
+  private static final String TABLE = "TestRowResource";
+  private static final String COLUMN_1 = "a:";
+  private static final String COLUMN_2 = "b:";
+  private static final String ROW_1 = "testrow1";
+  private static final String VALUE_1 = "testvalue1";
+  private static final String ROW_2 = "testrow2";
+  private static final String VALUE_2 = "testvalue2";
+  private static final String ROW_3 = "testrow3";
+  private static final String VALUE_3 = "testvalue3";
+  private static final String ROW_4 = "testrow4";
+  private static final String VALUE_4 = "testvalue4";
+
+  private Client client;
+  private JAXBContext context;
+  private Marshaller marshaller;
+  private Unmarshaller unmarshaller;
+  private HBaseAdmin admin;
+
+  public TestRowResource() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(
+        CellModel.class,
+        CellSetModel.class,
+        RowModel.class);
+    marshaller = context.createMarshaller();
+    unmarshaller = context.createUnmarshaller();
+  }
+
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+    client = new Client(new Cluster().add("localhost", testServletPort));
+    admin = new HBaseAdmin(conf);
+    if (admin.tableExists(TABLE)) {
+      return;
+    }
+    HTableDescriptor htd = new HTableDescriptor(TABLE);
+    htd.addFamily(new HColumnDescriptor(COLUMN_1));
+    htd.addFamily(new HColumnDescriptor(COLUMN_2));
+    admin.createTable(htd);
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    client.shutdown();
+    super.tearDown();
+  }
+
+  private Response deleteRow(String table, String row) throws IOException {
+    StringBuilder path = new StringBuilder();
+    path.append('/');
+    path.append(table);
+    path.append('/');
+    path.append(row);
+    Response response = client.delete(path.toString());
+    Thread.yield();
+    return response;
+  }
+
+  private Response deleteValue(String table, String row, String column)
+      throws IOException {
+    StringBuilder path = new StringBuilder();
+    path.append('/');
+    path.append(table);
+    path.append('/');
+    path.append(row);
+    path.append('/');
+    path.append(column);
+    Response response = client.delete(path.toString());
+    Thread.yield();
+    return response;
+  }
+
+  private Response getValueXML(String table, String row, String column)
+      throws IOException {
+    StringBuilder path = new StringBuilder();
+    path.append('/');
+    path.append(table);
+    path.append('/');
+    path.append(row);
+    path.append('/');
+    path.append(column);
+    Response response = client.get(path.toString(), MIMETYPE_XML);
+    return response;
+  }
+
+  private Response getValuePB(String table, String row, String column) 
+      throws IOException {
+    StringBuilder path = new StringBuilder();
+    path.append('/');
+    path.append(table);
+    path.append('/');
+    path.append(row);
+    path.append('/');
+    path.append(column);
+    Response response = client.get(path.toString(), MIMETYPE_PROTOBUF); 
+    return response;
+  }
+
+  private Response putValueXML(String table, String row, String column,
+      String value) throws IOException, JAXBException {
+    StringBuilder path = new StringBuilder();
+    path.append('/');
+    path.append(table);
+    path.append('/');
+    path.append(row);
+    path.append('/');
+    path.append(column);
+    RowModel rowModel = new RowModel(row);
+    rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(value)));
+    CellSetModel cellSetModel = new CellSetModel();
+    cellSetModel.addRow(rowModel);
+    StringWriter writer = new StringWriter();
+    marshaller.marshal(cellSetModel, writer);
+    Response response = client.put(path.toString(), MIMETYPE_XML,
+      Bytes.toBytes(writer.toString()));
+    Thread.yield();
+    return response;
+  }
+
+  private void checkValueXML(String table, String row, String column, 
+      String value) throws IOException, JAXBException {
+    Response response = getValueXML(table, row, column);
+    assertEquals(response.getCode(), 200);
+    CellSetModel cellSet = (CellSetModel)
+      unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
+    RowModel rowModel = cellSet.getRows().get(0);
+    CellModel cell = rowModel.getCells().get(0);
+    assertEquals(Bytes.toString(cell.getColumn()), column);
+    assertEquals(Bytes.toString(cell.getValue()), value);
+  }
+
+  private Response putValuePB(String table, String row, String column,
+      String value) throws IOException {
+    StringBuilder path = new StringBuilder();
+    path.append('/');
+    path.append(table);
+    path.append('/');
+    path.append(row);
+    path.append('/');
+    path.append(column);
+    RowModel rowModel = new RowModel(row);
+    rowModel.addCell(new CellModel(Bytes.toBytes(column), Bytes.toBytes(value)));
+    CellSetModel cellSetModel = new CellSetModel();
+    cellSetModel.addRow(rowModel);
+    Response response = client.put(path.toString(), MIMETYPE_PROTOBUF,
+      cellSetModel.createProtobufOutput());
+    Thread.yield();
+    return response;
+  }
+
+  private void checkValuePB(String table, String row, String column, 
+      String value) throws IOException {
+    Response response = getValuePB(table, row, column);
+    assertEquals(response.getCode(), 200);
+    CellSetModel cellSet = new CellSetModel();
+    cellSet.getObjectFromMessage(response.getBody());
+    RowModel rowModel = cellSet.getRows().get(0);
+    CellModel cell = rowModel.getCells().get(0);
+    assertEquals(Bytes.toString(cell.getColumn()), column);
+    assertEquals(Bytes.toString(cell.getValue()), value);
+  }
+
+  public void testDelete() throws IOException, JAXBException {
+    Response response;
+    
+    response = putValueXML(TABLE, ROW_1, COLUMN_1, VALUE_1);
+    assertEquals(response.getCode(), 200);
+    response = putValueXML(TABLE, ROW_1, COLUMN_2, VALUE_2);
+    assertEquals(response.getCode(), 200);
+    checkValueXML(TABLE, ROW_1, COLUMN_1, VALUE_1);
+    checkValueXML(TABLE, ROW_1, COLUMN_2, VALUE_2);
+
+    response = deleteValue(TABLE, ROW_1, COLUMN_1);
+    assertEquals(response.getCode(), 200);
+    response = getValueXML(TABLE, ROW_1, COLUMN_1);
+    assertEquals(response.getCode(), 404);
+    checkValueXML(TABLE, ROW_1, COLUMN_2, VALUE_2);
+
+    response = deleteRow(TABLE, ROW_1);
+    assertEquals(response.getCode(), 200);    
+    response = getValueXML(TABLE, ROW_1, COLUMN_1);
+    assertEquals(response.getCode(), 404);
+    response = getValueXML(TABLE, ROW_1, COLUMN_2);
+    assertEquals(response.getCode(), 404);
+  }
+
+  public void testSingleCellGetPutXML() throws IOException, JAXBException {
+    Response response = getValueXML(TABLE, ROW_1, COLUMN_1);
+    assertEquals(response.getCode(), 404);
+
+    response = putValueXML(TABLE, ROW_1, COLUMN_1, VALUE_1);
+    assertEquals(response.getCode(), 200);
+    checkValueXML(TABLE, ROW_1, COLUMN_1, VALUE_1);
+    response = putValueXML(TABLE, ROW_1, COLUMN_1, VALUE_2);
+    assertEquals(response.getCode(), 200);
+    checkValueXML(TABLE, ROW_1, COLUMN_1, VALUE_2);
+
+    response = deleteRow(TABLE, ROW_1);
+    assertEquals(response.getCode(), 200);    
+  }
+
+  public void testSingleCellGetPutPB() throws IOException, JAXBException {
+    Response response = getValuePB(TABLE, ROW_1, COLUMN_1);
+    assertEquals(response.getCode(), 404);
+
+    response = putValuePB(TABLE, ROW_1, COLUMN_1, VALUE_1);
+    assertEquals(response.getCode(), 200);
+    checkValuePB(TABLE, ROW_1, COLUMN_1, VALUE_1);
+
+    response = putValuePB(TABLE, ROW_1, COLUMN_1, VALUE_1);
+    assertEquals(response.getCode(), 200);
+    checkValuePB(TABLE, ROW_1, COLUMN_1, VALUE_1);
+    response = putValueXML(TABLE, ROW_1, COLUMN_1, VALUE_2);
+    assertEquals(response.getCode(), 200);
+    checkValuePB(TABLE, ROW_1, COLUMN_1, VALUE_2);
+
+    response = deleteRow(TABLE, ROW_1);
+    assertEquals(response.getCode(), 200);    
+  }
+
+  public void testSingleCellGetPutBinary() throws IOException {
+    final String path = "/" + TABLE + "/" + ROW_3 + "/" + COLUMN_1;
+    final byte[] body = Bytes.toBytes(VALUE_3);
+    Response response = client.put(path, MIMETYPE_BINARY, body);
+    assertEquals(response.getCode(), 200);
+    Thread.yield();
+
+    response = client.get(path, MIMETYPE_BINARY);
+    assertEquals(response.getCode(), 200);
+    assertTrue(Bytes.equals(response.getBody(), body));
+    boolean foundTimestampHeader = false;
+    for (Header header: response.getHeaders()) {
+      if (header.getName().equals("X-Timestamp")) {
+        foundTimestampHeader = true;
+        break;
+      }
+    }
+    assertTrue(foundTimestampHeader);
+
+    response = deleteRow(TABLE, ROW_3);
+    assertEquals(response.getCode(), 200);
+  }
+
+  public void testSingleCellGetJSON() throws IOException, JAXBException {
+    final String path = "/" + TABLE + "/" + ROW_4 + "/" + COLUMN_1;
+    Response response = client.put(path, MIMETYPE_BINARY,
+      Bytes.toBytes(VALUE_4));
+    assertEquals(response.getCode(), 200);
+    Thread.yield();
+    response = client.get(path, MIMETYPE_JSON);
+    assertEquals(response.getCode(), 200);
+    response = deleteRow(TABLE, ROW_4);
+    assertEquals(response.getCode(), 200);
+  }
+
+  public void testMultiCellGetPutXML() throws IOException, JAXBException {
+    String path = "/" + TABLE + "/fakerow";  // deliberate nonexistent row
+
+    CellSetModel cellSetModel = new CellSetModel();
+    RowModel rowModel = new RowModel(ROW_1);
+    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_1), Bytes.toBytes(VALUE_1)));
+    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_2), Bytes.toBytes(VALUE_2)));
+    cellSetModel.addRow(rowModel);
+    rowModel = new RowModel(ROW_2);
+    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_1), Bytes.toBytes(VALUE_3)));
+    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_2), Bytes.toBytes(VALUE_4)));
+    cellSetModel.addRow(rowModel);
+    StringWriter writer = new StringWriter();
+    marshaller.marshal(cellSetModel, writer);
+    Response response = client.put(path, MIMETYPE_XML,
+      Bytes.toBytes(writer.toString()));
+    Thread.yield();
+
+    // make sure the fake row was not actually created
+    response = client.get(path);
+    assertEquals(response.getCode(), 404);
+
+    // check that all of the values were created
+    checkValueXML(TABLE, ROW_1, COLUMN_1, VALUE_1);
+    checkValueXML(TABLE, ROW_1, COLUMN_2, VALUE_2);
+    checkValueXML(TABLE, ROW_2, COLUMN_1, VALUE_3);
+    checkValueXML(TABLE, ROW_2, COLUMN_2, VALUE_4);
+
+    response = deleteRow(TABLE, ROW_1);
+    assertEquals(response.getCode(), 200);    
+    response = deleteRow(TABLE, ROW_2);
+    assertEquals(response.getCode(), 200);
+  }
+
+  public void testMultiCellGetPutPB() throws IOException {
+    String path = "/" + TABLE + "/fakerow";  // deliberate nonexistent row
+
+    CellSetModel cellSetModel = new CellSetModel();
+    RowModel rowModel = new RowModel(ROW_1);
+    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_1), Bytes.toBytes(VALUE_1)));
+    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_2), Bytes.toBytes(VALUE_2)));
+    cellSetModel.addRow(rowModel);
+    rowModel = new RowModel(ROW_2);
+    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_1), Bytes.toBytes(VALUE_3)));
+    rowModel.addCell(new CellModel(Bytes.toBytes(COLUMN_2), Bytes.toBytes(VALUE_4)));
+    cellSetModel.addRow(rowModel);
+    Response response = client.put(path, MIMETYPE_PROTOBUF,
+      cellSetModel.createProtobufOutput());
+    Thread.yield();
+
+    // make sure the fake row was not actually created
+    response = client.get(path);
+    assertEquals(response.getCode(), 404);
+
+    // check that all of the values were created
+    checkValuePB(TABLE, ROW_1, COLUMN_1, VALUE_1);
+    checkValuePB(TABLE, ROW_1, COLUMN_2, VALUE_2);
+    checkValuePB(TABLE, ROW_2, COLUMN_1, VALUE_3);
+    checkValuePB(TABLE, ROW_2, COLUMN_2, VALUE_4);
+
+    response = deleteRow(TABLE, ROW_1);
+    assertEquals(response.getCode(), 200);    
+    response = deleteRow(TABLE, ROW_2);
+    assertEquals(response.getCode(), 200);
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestScannerResource.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestScannerResource.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestScannerResource.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestScannerResource.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,269 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.Iterator;
+import java.util.Random;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+
+import org.apache.commons.httpclient.Header;
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.KeyValue;
+import org.apache.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.stargate.client.Client;
+import org.apache.hadoop.hbase.stargate.client.Cluster;
+import org.apache.hadoop.hbase.stargate.client.Response;
+import org.apache.hadoop.hbase.stargate.model.CellModel;
+import org.apache.hadoop.hbase.stargate.model.CellSetModel;
+import org.apache.hadoop.hbase.stargate.model.RowModel;
+import org.apache.hadoop.hbase.stargate.model.ScannerModel;
+import org.apache.hadoop.hbase.util.Bytes;
+
+public class TestScannerResource extends MiniClusterTestCase {
+  private static final String TABLE = "TestScannerResource";
+  private static final String COLUMN_1 = "a:";
+  private static final String COLUMN_2 = "b:";
+
+  private static int expectedRows1;
+  private static int expectedRows2;
+
+  private Client client;
+  private JAXBContext context;
+  private Marshaller marshaller;
+  private Unmarshaller unmarshaller;
+  private HBaseAdmin admin;
+
+  private int insertData(String tableName, String column, double prob)
+      throws IOException {
+    Random rng = new Random();
+    int count = 0;
+    HTable table = new HTable(conf, tableName);
+    byte[] k = new byte[3];
+    byte [][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(column));
+    for (byte b1 = 'a'; b1 < 'z'; b1++) {
+      for (byte b2 = 'a'; b2 < 'z'; b2++) {
+        for (byte b3 = 'a'; b3 < 'z'; b3++) {
+          if (rng.nextDouble() < prob) {
+            k[0] = b1;
+            k[1] = b2;
+            k[2] = b3;
+            Put put = new Put(k);
+            put.add(famAndQf[0], famAndQf[1], k);
+            table.put(put);
+            count++;
+          }
+        }
+      }
+    }
+    table.flushCommits();
+    return count;
+  }
+
+  public TestScannerResource() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(
+        CellModel.class,
+        CellSetModel.class,
+        RowModel.class,
+        ScannerModel.class);
+    marshaller = context.createMarshaller();
+    unmarshaller = context.createUnmarshaller();
+  }
+
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+    client = new Client(new Cluster().add("localhost", testServletPort));
+    admin = new HBaseAdmin(conf);
+    if (admin.tableExists(TABLE)) {
+      return;
+    }
+    HTableDescriptor htd = new HTableDescriptor(TABLE);
+    htd.addFamily(new HColumnDescriptor(COLUMN_1));
+    htd.addFamily(new HColumnDescriptor(COLUMN_2));
+    admin.createTable(htd);
+    expectedRows1 = insertData(TABLE, COLUMN_1, 1.0);
+    expectedRows2 = insertData(TABLE, COLUMN_2, 0.5);
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    client.shutdown();
+    super.tearDown();
+  }
+
+  private int countCellSet(CellSetModel model) {
+    int count = 0;
+    Iterator<RowModel> rows = model.getRows().iterator();
+    while (rows.hasNext()) {
+      RowModel row = rows.next();
+      Iterator<CellModel> cells = row.getCells().iterator();
+      while (cells.hasNext()) {
+        cells.next();
+        count++;
+      }
+    }
+    return count;
+  }
+
+  public void testSimpleScannerXML() throws IOException, JAXBException {
+    final int BATCH_SIZE = 5;
+    // new scanner
+    ScannerModel model = new ScannerModel();
+    model.setBatch(BATCH_SIZE);
+    model.addColumn(Bytes.toBytes(COLUMN_1));
+    StringWriter writer = new StringWriter();
+    marshaller.marshal(model, writer);
+    byte[] body = Bytes.toBytes(writer.toString());
+    Response response = client.put("/" + TABLE + "/scanner", MIMETYPE_XML,
+      body);
+    assertEquals(response.getCode(), 201);
+    String scannerURI = response.getLocation();
+    assertNotNull(scannerURI);
+
+    // get a cell set
+    response = client.get(scannerURI, MIMETYPE_XML);
+    assertEquals(response.getCode(), 200);
+    CellSetModel cellSet = (CellSetModel)
+      unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));
+    // confirm batch size conformance
+    assertEquals(countCellSet(cellSet), BATCH_SIZE);
+
+    // delete the scanner
+    response = client.delete(scannerURI);
+    assertEquals(response.getCode(), 200);
+  }
+
+  public void testSimpleScannerPB() throws IOException {
+    final int BATCH_SIZE = 10;
+    // new scanner
+    ScannerModel model = new ScannerModel();
+    model.setBatch(BATCH_SIZE);
+    model.addColumn(Bytes.toBytes(COLUMN_1));
+    Response response = client.put("/" + TABLE + "/scanner",
+      MIMETYPE_PROTOBUF, model.createProtobufOutput());
+    assertEquals(response.getCode(), 201);
+    String scannerURI = response.getLocation();
+    assertNotNull(scannerURI);
+
+    // get a cell set
+    response = client.get(scannerURI, MIMETYPE_PROTOBUF);
+    assertEquals(response.getCode(), 200);
+    CellSetModel cellSet = new CellSetModel();
+    cellSet.getObjectFromMessage(response.getBody());
+    // confirm batch size conformance
+    assertEquals(countCellSet(cellSet), BATCH_SIZE);
+
+    // delete the scanner
+    response = client.delete(scannerURI);
+    assertEquals(response.getCode(), 200);
+  }
+
+  public void testSimpleScannerBinary() throws IOException {
+    // new scanner
+    ScannerModel model = new ScannerModel();
+    model.setBatch(1);
+    model.addColumn(Bytes.toBytes(COLUMN_1));
+    Response response = client.put("/" + TABLE + "/scanner",
+      MIMETYPE_PROTOBUF, model.createProtobufOutput());
+    assertEquals(response.getCode(), 201);
+    String scannerURI = response.getLocation();
+    assertNotNull(scannerURI);
+
+    // get a cell
+    response = client.get(scannerURI, MIMETYPE_BINARY);
+    assertEquals(response.getCode(), 200);
+    // verify that data was returned
+    assertTrue(response.getBody().length > 0);
+    // verify that the expected X-headers are present
+    boolean foundRowHeader = false, foundColumnHeader = false,
+      foundTimestampHeader = false;
+    for (Header header: response.getHeaders()) {
+      if (header.getName().equals("X-Row")) {
+        foundRowHeader = true;
+      } else if (header.getName().equals("X-Column")) {
+        foundColumnHeader = true;
+      } else if (header.getName().equals("X-Timestamp")) {
+        foundTimestampHeader = true;
+      }
+    }
+    assertTrue(foundRowHeader);
+    assertTrue(foundColumnHeader);
+    assertTrue(foundTimestampHeader);
+
+    // delete the scanner
+    response = client.delete(scannerURI);
+    assertEquals(response.getCode(), 200);
+  }
+
+  private int fullTableScan(ScannerModel model) throws IOException {
+    model.setBatch(100);
+    Response response = client.put("/" + TABLE + "/scanner",
+        MIMETYPE_PROTOBUF, model.createProtobufOutput());
+    assertEquals(response.getCode(), 201);
+    String scannerURI = response.getLocation();
+    assertNotNull(scannerURI);
+    int count = 0;
+    while (true) {
+      response = client.get(scannerURI, MIMETYPE_PROTOBUF);
+      assertTrue(response.getCode() == 200 || response.getCode() == 204);
+      if (response.getCode() == 200) {
+        CellSetModel cellSet = new CellSetModel();
+        cellSet.getObjectFromMessage(response.getBody());
+        Iterator<RowModel> rows = cellSet.getRows().iterator();
+        while (rows.hasNext()) {
+          RowModel row = rows.next();
+          Iterator<CellModel> cells = row.getCells().iterator();
+          while (cells.hasNext()) {
+            cells.next();
+            count++;
+          }
+        }
+      } else {
+        break;
+      }
+    }
+    // delete the scanner
+    response = client.delete(scannerURI);
+    assertEquals(response.getCode(), 200);
+    return count;
+  }
+
+  public void testFullTableScan() throws IOException {
+    ScannerModel model = new ScannerModel();
+    model.addColumn(Bytes.toBytes(COLUMN_1));
+    assertEquals(fullTableScan(model), expectedRows1);
+
+    model = new ScannerModel();
+    model.addColumn(Bytes.toBytes(COLUMN_2));
+    assertEquals(fullTableScan(model), expectedRows2);
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestSchemaResource.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestSchemaResource.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestSchemaResource.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestSchemaResource.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import org.apache.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.hbase.stargate.client.Client;
+import org.apache.hadoop.hbase.stargate.client.Cluster;
+import org.apache.hadoop.hbase.stargate.client.Response;
+import org.apache.hadoop.hbase.stargate.model.ColumnSchemaModel;
+import org.apache.hadoop.hbase.stargate.model.TableSchemaModel;
+import org.apache.hadoop.hbase.stargate.model.TestTableSchemaModel;
+import org.apache.hadoop.hbase.util.Bytes;
+
+public class TestSchemaResource extends MiniClusterTestCase {
+  private Client client;
+  private JAXBContext context;
+  private HBaseAdmin admin;
+
+  private static String TABLE1 = "TestSchemaResource1";
+  private static String TABLE2 = "TestSchemaResource2";
+
+  public TestSchemaResource() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(
+        ColumnSchemaModel.class,
+        TableSchemaModel.class);
+  }
+
+  @Override
+  protected void setUp() throws Exception {
+    admin = new HBaseAdmin(conf);
+    client = new Client(new Cluster().add("localhost", testServletPort));
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    client.shutdown();
+    super.tearDown();
+  }
+
+  private byte[] toXML(TableSchemaModel model) throws JAXBException {
+    StringWriter writer = new StringWriter();
+    context.createMarshaller().marshal(model, writer);
+    return Bytes.toBytes(writer.toString());
+  }
+
+  private TableSchemaModel fromXML(byte[] content) throws JAXBException {
+    return (TableSchemaModel) context.createUnmarshaller()
+      .unmarshal(new ByteArrayInputStream(content));
+  }
+
+  public void testTableCreateAndDeleteXML() 
+      throws IOException, JAXBException {
+    String schemaPath = "/" + TABLE1 + "/schema";
+    TableSchemaModel model;
+    Response response;
+
+    assertFalse(admin.tableExists(TABLE1));
+
+    // create the table
+    model = TestTableSchemaModel.buildTestModel(TABLE1);
+    TestTableSchemaModel.checkModel(model, TABLE1);
+    response = client.put(schemaPath, MIMETYPE_XML, toXML(model));
+    assertEquals(response.getCode(), 201);
+
+    // make sure HBase concurs, and wait for the table to come online
+    admin.enableTable(TABLE1);
+
+    // retrieve the schema and validate it
+    response = client.get(schemaPath, MIMETYPE_XML);
+    assertEquals(response.getCode(), 200);
+    model = fromXML(response.getBody());
+    TestTableSchemaModel.checkModel(model, TABLE1);
+
+    // delete the table
+    client.delete(schemaPath);
+
+    // make sure HBase concurs
+    assertFalse(admin.tableExists(TABLE1));
+  }
+
+  public void testTableCreateAndDeletePB() throws IOException, JAXBException {
+    String schemaPath = "/" + TABLE2 + "/schema";
+    TableSchemaModel model;
+    Response response;
+
+    assertFalse(admin.tableExists(TABLE2));
+
+    // create the table
+    model = TestTableSchemaModel.buildTestModel(TABLE2);
+    TestTableSchemaModel.checkModel(model, TABLE2);
+    response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
+      model.createProtobufOutput());
+    assertEquals(response.getCode(), 201);
+
+    // make sure HBase concurs, and wait for the table to come online
+    admin.enableTable(TABLE2);
+
+    // retrieve the schema and validate it
+    response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF);
+    assertEquals(response.getCode(), 200);
+    model = new TableSchemaModel();
+    model.getObjectFromMessage(response.getBody());
+    TestTableSchemaModel.checkModel(model, TABLE2);
+
+    // delete the table
+    client.delete(schemaPath);
+
+    // make sure HBase concurs
+    assertFalse(admin.tableExists(TABLE2));
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestStatusResource.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestStatusResource.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestStatusResource.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestStatusResource.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import org.apache.hadoop.hbase.stargate.client.Client;
+import org.apache.hadoop.hbase.stargate.client.Cluster;
+import org.apache.hadoop.hbase.stargate.client.Response;
+import org.apache.hadoop.hbase.stargate.model.StorageClusterStatusModel;
+import org.apache.hadoop.hbase.util.Bytes;
+
+public class TestStatusResource extends MiniClusterTestCase {
+  private static final byte[] ROOT_REGION_NAME = Bytes.toBytes("-ROOT-,,0");
+  private static final byte[] META_REGION_NAME = Bytes.toBytes(".META.,,1");
+
+  private Client client;
+  private JAXBContext context;
+  
+  public TestStatusResource() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(
+        StorageClusterStatusModel.class);
+  }
+
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+    client = new Client(new Cluster().add("localhost", testServletPort));
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    client.shutdown();
+    super.tearDown();
+  }
+
+  private void validate(StorageClusterStatusModel model) {
+    assertNotNull(model);
+    assertTrue(model.getRegions() >= 2);
+    assertTrue(model.getRequests() >= 0);
+    // assumes minicluster with two regionservers
+    assertTrue(model.getAverageLoad() >= 1.0);
+    assertNotNull(model.getLiveNodes());
+    assertNotNull(model.getDeadNodes());
+    assertFalse(model.getLiveNodes().isEmpty());
+    boolean foundRoot = false, foundMeta = false;
+    for (StorageClusterStatusModel.Node node: model.getLiveNodes()) {
+      assertNotNull(node.getName());
+      assertTrue(node.getStartCode() > 0L);
+      assertTrue(node.getRequests() >= 0);
+      assertFalse(node.getRegions().isEmpty());
+      for (StorageClusterStatusModel.Node.Region region: node.getRegions()) {
+        if (Bytes.equals(region.getName(), ROOT_REGION_NAME)) {
+          foundRoot = true;
+        } else if (Bytes.equals(region.getName(), META_REGION_NAME)) {
+          foundMeta = true;
+        }
+      }
+    }
+    assertTrue(foundRoot);
+    assertTrue(foundMeta);
+  }
+
+  public void testGetClusterStatusXML() throws IOException, JAXBException {
+    Response response = client.get(Constants.PATH_STATUS_CLUSTER, MIMETYPE_XML);
+    assertEquals(response.getCode(), 200);
+    StorageClusterStatusModel model = (StorageClusterStatusModel)
+      context.createUnmarshaller().unmarshal(
+        new ByteArrayInputStream(response.getBody()));
+    validate(model);
+  }
+  
+  public void testGetClusterStatusPB() throws IOException {
+    Response response =
+      client.get(Constants.PATH_STATUS_CLUSTER, MIMETYPE_PROTOBUF);
+    assertEquals(response.getCode(), 200);
+    StorageClusterStatusModel model = new StorageClusterStatusModel();
+    model.getObjectFromMessage(response.getBody());
+    validate(model);
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestTableResource.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestTableResource.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestTableResource.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestTableResource.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,220 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HRegionInfo;
+import org.apache.hadoop.hbase.HServerAddress;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.KeyValue;
+import org.apache.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.stargate.client.Client;
+import org.apache.hadoop.hbase.stargate.client.Cluster;
+import org.apache.hadoop.hbase.stargate.client.Response;
+import org.apache.hadoop.hbase.stargate.model.TableModel;
+import org.apache.hadoop.hbase.stargate.model.TableInfoModel;
+import org.apache.hadoop.hbase.stargate.model.TableListModel;
+import org.apache.hadoop.hbase.stargate.model.TableRegionModel;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.util.StringUtils;
+
+public class TestTableResource extends MiniClusterTestCase {
+  private static final Log LOG = 
+    LogFactory.getLog(TestTableResource.class);
+
+  private static String TABLE = "TestTableResource";
+  private static String COLUMN = "test:";
+  private static Map<HRegionInfo,HServerAddress> regionMap;
+
+  private Client client;
+  private JAXBContext context;
+  private HBaseAdmin admin;
+
+  public TestTableResource() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(
+        TableModel.class,
+        TableInfoModel.class,
+        TableListModel.class,
+        TableRegionModel.class);
+  }
+
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+    client = new Client(new Cluster().add("localhost", testServletPort));
+    admin = new HBaseAdmin(conf);
+    if (admin.tableExists(TABLE)) {
+      return;
+    }
+    HTableDescriptor htd = new HTableDescriptor(TABLE);
+    htd.addFamily(new HColumnDescriptor(COLUMN));
+    admin.createTable(htd);
+    HTable table = new HTable(conf, TABLE);
+    byte[] k = new byte[3];
+    byte [][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(COLUMN));
+    for (byte b1 = 'a'; b1 < 'z'; b1++) {
+      for (byte b2 = 'a'; b2 < 'z'; b2++) {
+        for (byte b3 = 'a'; b3 < 'z'; b3++) {
+          k[0] = b1;
+          k[1] = b2;
+          k[2] = b3;
+          Put put = new Put(k);
+          put.add(famAndQf[0], famAndQf[1], k);
+          table.put(put);
+        }
+      }
+    }
+    table.flushCommits();
+    // get the initial layout (should just be one region)
+    Map<HRegionInfo,HServerAddress> m = table.getRegionsInfo();
+    assertEquals(m.size(), 1);
+    // tell the master to split the table
+    admin.split(TABLE);
+    // give some time for the split to happen
+    try {
+      Thread.sleep(15 * 1000);
+    } catch (InterruptedException e) {
+      LOG.warn(StringUtils.stringifyException(e));
+    }
+    // check again
+    m = table.getRegionsInfo();
+    // should have two regions now
+    assertEquals(m.size(), 2);
+    regionMap = m;
+    LOG.info("regions: " + regionMap);
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    client.shutdown();
+    super.tearDown();
+  }
+
+  private void checkTableList(TableListModel model) {
+    boolean found = false;
+    Iterator<TableModel> tables = model.getTables().iterator();
+    assertTrue(tables.hasNext());
+    while (tables.hasNext()) {
+      TableModel table = tables.next();
+      if (table.getName().equals(TABLE)) {
+        found = true;
+        break;
+      }
+    }
+    assertTrue(found);
+  }
+
+  public void testTableListText() throws IOException {
+    Response response = client.get("/", MIMETYPE_PLAIN);
+    assertEquals(response.getCode(), 200);
+  }
+
+  public void testTableListXML() throws IOException, JAXBException {
+    Response response = client.get("/", MIMETYPE_XML);
+    assertEquals(response.getCode(), 200);
+    TableListModel model = (TableListModel)
+      context.createUnmarshaller()
+        .unmarshal(new ByteArrayInputStream(response.getBody()));
+    checkTableList(model);
+  }
+
+  public void testTableListJSON() throws IOException {
+    Response response = client.get("/", MIMETYPE_JSON);
+    assertEquals(response.getCode(), 200);
+  }
+
+  public void testTableListPB() throws IOException, JAXBException {
+    Response response = client.get("/", MIMETYPE_PROTOBUF);
+    assertEquals(response.getCode(), 200);
+    TableListModel model = new TableListModel();
+    model.getObjectFromMessage(response.getBody());
+    checkTableList(model);
+  }
+
+  public void checkTableInfo(TableInfoModel model) {
+    assertEquals(model.getName(), TABLE);
+    Iterator<TableRegionModel> regions = model.getRegions().iterator();
+    assertTrue(regions.hasNext());
+    while (regions.hasNext()) {
+      TableRegionModel region = regions.next();
+      boolean found = false;
+      for (Map.Entry<HRegionInfo,HServerAddress> e: regionMap.entrySet()) {
+        HRegionInfo hri = e.getKey();
+        if (hri.getRegionNameAsString().equals(region.getName())) {
+          found = true;
+          byte[] startKey = hri.getStartKey();
+          byte[] endKey = hri.getEndKey();
+          InetSocketAddress sa = e.getValue().getInetSocketAddress();
+          String location = sa.getHostName() + ":" +
+            Integer.valueOf(sa.getPort());
+          assertEquals(hri.getRegionId(), region.getId());
+          assertTrue(Bytes.equals(startKey, region.getStartKey()));
+          assertTrue(Bytes.equals(endKey, region.getEndKey()));
+          assertEquals(location, region.getLocation());
+          break;
+        }
+      }
+      assertTrue(found);
+    }
+  }
+
+  public void testTableInfoText() throws IOException {
+    Response response = client.get("/" + TABLE + "/regions", MIMETYPE_PLAIN);
+    assertEquals(response.getCode(), 200);
+  }
+
+  public void testTableInfoXML() throws IOException, JAXBException {
+    Response response = client.get("/" + TABLE + "/regions", MIMETYPE_XML);
+    assertEquals(response.getCode(), 200);
+    TableInfoModel model = (TableInfoModel)
+      context.createUnmarshaller()
+        .unmarshal(new ByteArrayInputStream(response.getBody()));
+    checkTableInfo(model);
+  }
+
+  public void testTableInfoJSON() throws IOException {
+    Response response = client.get("/" + TABLE + "/regions", MIMETYPE_JSON);
+    assertEquals(response.getCode(), 200);
+  }
+
+  public void testTableInfoPB() throws IOException, JAXBException {
+    Response response = 
+      client.get("/" + TABLE + "/regions", MIMETYPE_PROTOBUF);
+    assertEquals(response.getCode(), 200);
+    TableInfoModel model = new TableInfoModel();
+    model.getObjectFromMessage(response.getBody());
+    checkTableInfo(model);
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestVersionResource.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestVersionResource.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestVersionResource.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/TestVersionResource.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,127 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hbase.stargate.client.Client;
+import org.apache.hadoop.hbase.stargate.client.Cluster;
+import org.apache.hadoop.hbase.stargate.client.Response;
+import org.apache.hadoop.hbase.stargate.model.StorageClusterVersionModel;
+import org.apache.hadoop.hbase.stargate.model.VersionModel;
+
+public class TestVersionResource extends MiniClusterTestCase {
+  private static final Log LOG =
+    LogFactory.getLog(TestVersionResource.class);
+
+  private Client client;
+  private JAXBContext context;
+
+  public TestVersionResource() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(
+        VersionModel.class,
+        StorageClusterVersionModel.class);
+  }
+
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+    client = new Client(new Cluster().add("localhost", testServletPort));
+  }
+
+  @Override
+  protected void tearDown() throws Exception {
+    client.shutdown();
+    super.tearDown();
+  }
+
+  private static void validate(VersionModel model) {
+    assertNotNull(model);
+    assertNotNull(model.getStargateVersion());
+    assertNotNull(model.getOsVersion());
+    assertNotNull(model.getJvmVersion());
+    assertNotNull(model.getServerVersion());
+    assertNotNull(model.getJerseyVersion());
+  }
+
+  public void testGetStargateVersionText() throws IOException {
+    Response response = client.get(Constants.PATH_VERSION, MIMETYPE_PLAIN);
+    assertTrue(response.getCode() == 200);
+  }
+
+  public void testGetStargateVersionXML() throws IOException, JAXBException {
+    Response response = client.get(Constants.PATH_VERSION, MIMETYPE_XML);
+    assertTrue(response.getCode() == 200);
+    VersionModel model = (VersionModel)
+      context.createUnmarshaller().unmarshal(
+        new ByteArrayInputStream(response.getBody()));
+    validate(model);
+    LOG.info("success retrieving Stargate version as XML");
+  }
+
+  public void testGetStargateVersionJSON() throws IOException {
+    Response response = client.get(Constants.PATH_VERSION, MIMETYPE_JSON);
+    assertTrue(response.getCode() == 200);
+  }
+
+  public void testGetStargateVersionPB() throws IOException {
+    Response response = client.get(Constants.PATH_VERSION, MIMETYPE_PROTOBUF);
+    assertTrue(response.getCode() == 200);
+    VersionModel model = new VersionModel();
+    model.getObjectFromMessage(response.getBody());
+    validate(model);
+    LOG.info("success retrieving Stargate version as protobuf");
+  }
+
+  public void testGetStorageClusterVersionText() throws IOException {
+    Response response =
+      client.get(Constants.PATH_VERSION_CLUSTER, MIMETYPE_PLAIN);
+    assertTrue(response.getCode() == 200);
+  }
+
+  public void testGetStorageClusterVersionXML() throws IOException,
+      JAXBException {
+    Response response =
+      client.get(Constants.PATH_VERSION_CLUSTER, MIMETYPE_XML);
+    assertTrue(response.getCode() == 200);
+    StorageClusterVersionModel clusterVersionModel = 
+      (StorageClusterVersionModel)
+        context.createUnmarshaller().unmarshal(
+          new ByteArrayInputStream(response.getBody()));
+    assertNotNull(clusterVersionModel);
+    assertNotNull(clusterVersionModel.getVersion());
+    LOG.info("success retrieving storage cluster version as XML");
+  }
+
+  public void testGetStorageClusterVersionJSON() throws IOException {
+    Response response =
+      client.get(Constants.PATH_VERSION_CLUSTER, MIMETYPE_JSON);
+    assertTrue(response.getCode() == 200);
+  }
+
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestCellModel.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestCellModel.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestCellModel.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestCellModel.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate.model;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import org.apache.hadoop.hbase.util.Base64;
+import org.apache.hadoop.hbase.util.Bytes;
+
+import junit.framework.TestCase;
+
+public class TestCellModel extends TestCase {
+
+  private static final long TIMESTAMP = 1245219839331L;
+  private static final byte[] COLUMN = Bytes.toBytes("testcolumn");
+  private static final byte[] VALUE = Bytes.toBytes("testvalue");
+
+  private static final String AS_XML =
+    "<Cell timestamp=\"1245219839331\"" +
+      " column=\"dGVzdGNvbHVtbg==\">" +
+      "dGVzdHZhbHVl</Cell>";
+
+  private static final String AS_PB = 
+    "Egp0ZXN0Y29sdW1uGOO6i+eeJCIJdGVzdHZhbHVl";
+
+  private JAXBContext context;
+
+  public TestCellModel() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(CellModel.class);
+  }
+
+  private CellModel buildTestModel() {
+    CellModel model = new CellModel();
+    model.setColumn(COLUMN);
+    model.setTimestamp(TIMESTAMP);
+    model.setValue(VALUE);
+    return model;
+  }
+
+  @SuppressWarnings("unused")
+  private String toXML(CellModel model) throws JAXBException {
+    StringWriter writer = new StringWriter();
+    context.createMarshaller().marshal(model, writer);
+    return writer.toString();
+  }
+
+  private CellModel fromXML(String xml) throws JAXBException {
+    return (CellModel)
+      context.createUnmarshaller().unmarshal(new StringReader(xml));
+  }
+
+  @SuppressWarnings("unused")
+  private byte[] toPB(CellModel model) {
+    return model.createProtobufOutput();
+  }
+
+  private CellModel fromPB(String pb) throws IOException {
+    return (CellModel) 
+      new CellModel().getObjectFromMessage(Base64.decode(AS_PB));
+  }
+
+  private void checkModel(CellModel model) {
+    assertTrue(Bytes.equals(model.getColumn(), COLUMN));
+    assertTrue(Bytes.equals(model.getValue(), VALUE));
+    assertTrue(model.hasUserTimestamp());
+    assertEquals(model.getTimestamp(), TIMESTAMP);
+  }
+
+  public void testBuildModel() throws Exception {
+    checkModel(buildTestModel());
+  }
+
+  public void testFromXML() throws Exception {
+    checkModel(fromXML(AS_XML));
+  }
+
+  public void testFromPB() throws Exception {
+    checkModel(fromPB(AS_PB));
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestCellSetModel.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestCellSetModel.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestCellSetModel.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestCellSetModel.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate.model;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Iterator;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import org.apache.hadoop.hbase.util.Base64;
+import org.apache.hadoop.hbase.util.Bytes;
+
+import junit.framework.TestCase;
+
+public class TestCellSetModel extends TestCase {
+
+  private static final byte[] ROW1 = Bytes.toBytes("testrow1");
+  private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1");
+  private static final byte[] VALUE1 = Bytes.toBytes("testvalue1");
+  private static final long TIMESTAMP1 = 1245219839331L;
+  private static final byte[] ROW2 = Bytes.toBytes("testrow1");
+  private static final byte[] COLUMN2 = Bytes.toBytes("testcolumn2");
+  private static final byte[] VALUE2 = Bytes.toBytes("testvalue2");
+  private static final long TIMESTAMP2 = 1245239813319L;
+  private static final byte[] COLUMN3 = Bytes.toBytes("testcolumn3");
+  private static final byte[] VALUE3 = Bytes.toBytes("testvalue3");
+  private static final long TIMESTAMP3 = 1245393318192L;
+
+  private static final String AS_XML =
+    "<CellSet>" + 
+      "<Row key=\"dGVzdHJvdzE=\">" + 
+        "<Cell timestamp=\"1245219839331\" column=\"dGVzdGNvbHVtbjE=\">" + 
+          "dGVzdHZhbHVlMQ==</Cell>" + 
+        "</Row>" + 
+      "<Row key=\"dGVzdHJvdzE=\">" + 
+        "<Cell timestamp=\"1245239813319\" column=\"dGVzdGNvbHVtbjI=\">" +
+          "dGVzdHZhbHVlMg==</Cell>" + 
+        "<Cell timestamp=\"1245393318192\" column=\"dGVzdGNvbHVtbjM=\">" + 
+          "dGVzdHZhbHVlMw==</Cell>" + 
+        "</Row>" +
+      "</CellSet>";
+
+  private static final String AS_PB = 
+    "CiwKCHRlc3Ryb3cxEiASC3Rlc3Rjb2x1bW4xGOO6i+eeJCIKdGVzdHZhbHVlMQpOCgh0ZXN0cm93" +
+    "MRIgEgt0ZXN0Y29sdW1uMhjHyc7wniQiCnRlc3R2YWx1ZTISIBILdGVzdGNvbHVtbjMYsOLnuZ8k" +
+    "Igp0ZXN0dmFsdWUz";
+
+  private JAXBContext context;
+
+  public TestCellSetModel() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(
+        CellModel.class,
+        CellSetModel.class,
+        RowModel.class);
+  }
+
+  private CellSetModel buildTestModel() {
+    CellSetModel model = new CellSetModel();
+    RowModel row;
+    row = new RowModel();
+    row.setKey(ROW1);
+    row.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
+    model.addRow(row);
+    row = new RowModel();
+    row.setKey(ROW2);
+    row.addCell(new CellModel(COLUMN2, TIMESTAMP2, VALUE2));
+    row.addCell(new CellModel(COLUMN3, TIMESTAMP3, VALUE3));
+    model.addRow(row);
+    return model;
+  }
+
+  @SuppressWarnings("unused")
+  private String toXML(CellSetModel model) throws JAXBException {
+    StringWriter writer = new StringWriter();
+    context.createMarshaller().marshal(model, writer);
+    return writer.toString();
+  }
+
+  private CellSetModel fromXML(String xml) throws JAXBException {
+    return (CellSetModel)
+      context.createUnmarshaller().unmarshal(new StringReader(xml));
+  }
+
+  @SuppressWarnings("unused")
+  private byte[] toPB(CellSetModel model) {
+    return model.createProtobufOutput();
+  }
+
+  private CellSetModel fromPB(String pb) throws IOException {
+    return (CellSetModel) 
+      new CellSetModel().getObjectFromMessage(Base64.decode(AS_PB));
+  }
+
+  private void checkModel(CellSetModel model) {
+    Iterator<RowModel> rows = model.getRows().iterator();
+    RowModel row = rows.next();
+    assertTrue(Bytes.equals(ROW1, row.getKey()));
+    Iterator<CellModel> cells = row.getCells().iterator();
+    CellModel cell = cells.next();
+    assertTrue(Bytes.equals(COLUMN1, cell.getColumn()));
+    assertTrue(Bytes.equals(VALUE1, cell.getValue()));
+    assertTrue(cell.hasUserTimestamp());
+    assertEquals(cell.getTimestamp(), TIMESTAMP1);
+    assertFalse(cells.hasNext());
+    row = rows.next();
+    assertTrue(Bytes.equals(ROW2, row.getKey()));
+    cells = row.getCells().iterator();
+    cell = cells.next();
+    assertTrue(Bytes.equals(COLUMN2, cell.getColumn()));
+    assertTrue(Bytes.equals(VALUE2, cell.getValue()));
+    assertTrue(cell.hasUserTimestamp());
+    assertEquals(cell.getTimestamp(), TIMESTAMP2);
+    cell = cells.next();
+    assertTrue(Bytes.equals(COLUMN3, cell.getColumn()));
+    assertTrue(Bytes.equals(VALUE3, cell.getValue()));
+    assertTrue(cell.hasUserTimestamp());
+    assertEquals(cell.getTimestamp(), TIMESTAMP3);
+    assertFalse(cells.hasNext());
+  }
+
+  public void testBuildModel() throws Exception {
+    checkModel(buildTestModel());
+  }
+
+  public void testFromXML() throws Exception {
+    checkModel(fromXML(AS_XML));
+  }
+
+  public void testFromPB() throws Exception {
+    checkModel(fromPB(AS_PB));
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestColumnSchemaModel.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestColumnSchemaModel.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestColumnSchemaModel.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestColumnSchemaModel.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate.model;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import junit.framework.TestCase;
+
+public class TestColumnSchemaModel extends TestCase {
+
+  protected static final String COLUMN_NAME = "testcolumn";
+  protected static final boolean BLOCKCACHE = true;
+  protected static final int BLOCKSIZE = 16384;
+  protected static final boolean BLOOMFILTER = false;
+  protected static final String COMPRESSION = "GZ";
+  protected static final boolean IN_MEMORY = false;
+  protected static final int TTL = 86400;
+  protected static final int VERSIONS = 1;
+
+  protected static final String AS_XML =
+    "<ColumnSchema name=\"testcolumn\"" +
+      " BLOCKSIZE=\"16384\"" +
+      " BLOOMFILTER=\"false\"" +
+      " BLOCKCACHE=\"true\"" +
+      " COMPRESSION=\"GZ\"" +
+      " VERSIONS=\"1\"" +
+      " TTL=\"86400\"" +
+      " IN_MEMORY=\"false\"/>";
+
+  private JAXBContext context;
+
+  public TestColumnSchemaModel() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(ColumnSchemaModel.class);
+  }
+
+  protected static ColumnSchemaModel buildTestModel() {
+    ColumnSchemaModel model = new ColumnSchemaModel();
+    model.setName(COLUMN_NAME);
+    model.__setBlockcache(BLOCKCACHE);
+    model.__setBlocksize(BLOCKSIZE);
+    model.__setBloomfilter(BLOOMFILTER);
+    model.__setCompression(COMPRESSION);
+    model.__setInMemory(IN_MEMORY);
+    model.__setTTL(TTL);
+    model.__setVersions(VERSIONS);
+    return model;
+  }
+
+  @SuppressWarnings("unused")
+  private String toXML(ColumnSchemaModel model) throws JAXBException {
+    StringWriter writer = new StringWriter();
+    context.createMarshaller().marshal(model, writer);
+    return writer.toString();
+  }
+
+  private ColumnSchemaModel fromXML(String xml) throws JAXBException {
+    return (ColumnSchemaModel)
+      context.createUnmarshaller().unmarshal(new StringReader(xml));
+  }
+
+  protected static void checkModel(ColumnSchemaModel model) {
+    assertEquals(model.getName(), COLUMN_NAME);
+    assertEquals(model.__getBlockcache(), BLOCKCACHE);
+    assertEquals(model.__getBlocksize(), BLOCKSIZE);
+    assertEquals(model.__getBloomfilter(), BLOOMFILTER);
+    assertTrue(model.__getCompression().equalsIgnoreCase(COMPRESSION));
+    assertEquals(model.__getInMemory(), IN_MEMORY);
+    assertEquals(model.__getTTL(), TTL);
+    assertEquals(model.__getVersions(), VERSIONS);
+  }
+
+  public void testBuildModel() throws Exception {
+    checkModel(buildTestModel());
+  }
+
+  public void testFromXML() throws Exception {
+    checkModel(fromXML(AS_XML));
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestRowModel.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestRowModel.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestRowModel.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestRowModel.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate.model;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Iterator;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import org.apache.hadoop.hbase.util.Bytes;
+
+import junit.framework.TestCase;
+
+public class TestRowModel extends TestCase {
+
+  private static final byte[] ROW1 = Bytes.toBytes("testrow1");
+  private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1");
+  private static final byte[] VALUE1 = Bytes.toBytes("testvalue1");
+  private static final long TIMESTAMP1 = 1245219839331L;
+
+  private static final String AS_XML =
+    "<Row key=\"dGVzdHJvdzE=\">" + 
+      "<Cell timestamp=\"1245219839331\" column=\"dGVzdGNvbHVtbjE=\">" + 
+        "dGVzdHZhbHVlMQ==</Cell>" + 
+      "</Row>";
+
+  private JAXBContext context;
+
+  public TestRowModel() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(
+        CellModel.class,
+        RowModel.class);
+  }
+
+  private RowModel buildTestModel() {
+    RowModel model = new RowModel();
+    model.setKey(ROW1);
+    model.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
+    return model;
+  }
+
+  @SuppressWarnings("unused")
+  private String toXML(RowModel model) throws JAXBException {
+    StringWriter writer = new StringWriter();
+    context.createMarshaller().marshal(model, writer);
+    return writer.toString();
+  }
+
+  private RowModel fromXML(String xml) throws JAXBException {
+    return (RowModel)
+      context.createUnmarshaller().unmarshal(new StringReader(xml));
+  }
+
+  private void checkModel(RowModel model) {
+    assertTrue(Bytes.equals(ROW1, model.getKey()));
+    Iterator<CellModel> cells = model.getCells().iterator();
+    CellModel cell = cells.next();
+    assertTrue(Bytes.equals(COLUMN1, cell.getColumn()));
+    assertTrue(Bytes.equals(VALUE1, cell.getValue()));
+    assertTrue(cell.hasUserTimestamp());
+    assertEquals(cell.getTimestamp(), TIMESTAMP1);
+    assertFalse(cells.hasNext());
+  }
+
+  public void testBuildModel() throws Exception {
+    checkModel(buildTestModel());
+  }
+
+  public void testFromXML() throws Exception {
+    checkModel(fromXML(AS_XML));
+  }
+}

Added: hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestScannerModel.java
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestScannerModel.java?rev=789195&view=auto
==============================================================================
--- hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestScannerModel.java (added)
+++ hadoop/hbase/trunk_on_hadoop-0.18.3/contrib/stargate/src/test/org/apache/hadoop/hbase/stargate/model/TestScannerModel.java Mon Jun 29 02:49:21 2009
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * 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.hadoop.hbase.stargate.model;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import org.apache.hadoop.hbase.util.Base64;
+import org.apache.hadoop.hbase.util.Bytes;
+
+import junit.framework.TestCase;
+
+public class TestScannerModel extends TestCase {
+  private static final byte[] START_ROW = Bytes.toBytes("abracadabra");
+  private static final byte[] END_ROW = Bytes.toBytes("zzyzx");
+  private static final byte[] COLUMN1 = Bytes.toBytes("column1");
+  private static final byte[] COLUMN2 = Bytes.toBytes("column2:foo");
+  private static final long START_TIME = 1245219839331L;
+  private static final long END_TIME = 1245393318192L;
+  private static final int BATCH = 100;
+
+  private static final String AS_XML =
+    "<Scanner startTime=\"1245219839331\"" +
+      " startRow=\"YWJyYWNhZGFicmE=\"" + 
+      " endTime=\"1245393318192\"" +
+      " endRow=\"enp5eng=\"" +
+      " column=\"Y29sdW1uMQ== Y29sdW1uMjpmb28=\"" +
+      " batch=\"100\"/>";
+
+  private static final String AS_PB = 
+    "CgthYnJhY2FkYWJyYRIFenp5engaB2NvbHVtbjEaC2NvbHVtbjI6Zm9vIGQo47qL554kMLDi57mf" +
+    "JA==";
+
+  private JAXBContext context;
+
+  public TestScannerModel() throws JAXBException {
+    super();
+    context = JAXBContext.newInstance(ScannerModel.class);
+  }
+
+  private ScannerModel buildTestModel() {
+    ScannerModel model = new ScannerModel();
+    model.setStartRow(START_ROW);
+    model.setEndRow(END_ROW);
+    model.addColumn(COLUMN1);
+    model.addColumn(COLUMN2);
+    model.setStartTime(START_TIME);
+    model.setEndTime(END_TIME);
+    model.setBatch(BATCH);
+    return model;
+  }
+
+  @SuppressWarnings("unused")
+  private String toXML(ScannerModel model) throws JAXBException {
+    StringWriter writer = new StringWriter();
+    context.createMarshaller().marshal(model, writer);
+    return writer.toString();
+  }
+
+  private ScannerModel fromXML(String xml) throws JAXBException {
+    return (ScannerModel)
+      context.createUnmarshaller().unmarshal(new StringReader(xml));
+  }
+
+  @SuppressWarnings("unused")
+  private byte[] toPB(ScannerModel model) {
+    return model.createProtobufOutput();
+  }
+
+  private ScannerModel fromPB(String pb) throws IOException {
+    return (ScannerModel) 
+      new ScannerModel().getObjectFromMessage(Base64.decode(AS_PB));
+  }
+
+  private void checkModel(ScannerModel model) {
+    assertTrue(Bytes.equals(model.getStartRow(), START_ROW));
+    assertTrue(Bytes.equals(model.getEndRow(), END_ROW));
+    boolean foundCol1 = false, foundCol2 = false;
+    for (byte[] column: model.getColumns()) {
+      if (Bytes.equals(column, COLUMN1)) {
+        foundCol1 = true;
+      } else if (Bytes.equals(column, COLUMN2)) {
+        foundCol2 = true;
+      }
+    }
+    assertTrue(foundCol1);
+    assertTrue(foundCol2);
+    assertEquals(model.getStartTime(), START_TIME);
+    assertEquals(model.getEndTime(), END_TIME);
+    assertEquals(model.getBatch(), BATCH);
+  }
+
+  public void testBuildModel() throws Exception {
+    checkModel(buildTestModel());
+  }
+
+  public void testFromXML() throws Exception {
+    checkModel(fromXML(AS_XML));
+  }
+
+  public void testFromPB() throws Exception {
+    checkModel(fromPB(AS_PB));
+  }
+}