HBASE-9435 Fix jersey serialization/deserialization of json objects

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1520179 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2013-09-05 00:05:26 +00:00
parent f515772c1f
commit f8252c4bb1
23 changed files with 486 additions and 551 deletions

View File

@ -124,7 +124,7 @@ public class RemoteHTable implements HTableInterface {
} }
} }
} }
if (startTime != 0 && endTime != Long.MAX_VALUE) { if (startTime >= 0 && endTime != Long.MAX_VALUE) {
sb.append('/'); sb.append('/');
sb.append(startTime); sb.append(startTime);
if (startTime != endTime) { if (startTime != endTime) {

View File

@ -22,6 +22,8 @@ package org.apache.hadoop.hbase.rest.model;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue; import javax.xml.bind.annotation.XmlValue;
@ -33,6 +35,7 @@ import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell; import org.apache.hadoop.hbase.rest.protobuf.generated.CellMessage.Cell;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import org.codehaus.jackson.annotate.JsonProperty;
/** /**
* Representation of a cell. A cell is a single value associated a column and * Representation of a cell. A cell is a single value associated a column and
@ -54,12 +57,21 @@ import com.google.protobuf.ByteString;
* </pre> * </pre>
*/ */
@XmlRootElement(name="Cell") @XmlRootElement(name="Cell")
@XmlAccessorType(XmlAccessType.FIELD)
@InterfaceAudience.Private @InterfaceAudience.Private
public class CellModel implements ProtobufMessageHandler, Serializable { public class CellModel implements ProtobufMessageHandler, Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private long timestamp = HConstants.LATEST_TIMESTAMP; @JsonProperty("column")
@XmlAttribute
private byte[] column; private byte[] column;
@JsonProperty("timestamp")
@XmlAttribute
private long timestamp = HConstants.LATEST_TIMESTAMP;
@JsonProperty("$")
@XmlValue
private byte[] value; private byte[] value;
/** /**
@ -123,7 +135,6 @@ public class CellModel implements ProtobufMessageHandler, Serializable {
/** /**
* @return the column * @return the column
*/ */
@XmlAttribute
public byte[] getColumn() { public byte[] getColumn() {
return column; return column;
} }
@ -146,7 +157,6 @@ public class CellModel implements ProtobufMessageHandler, Serializable {
/** /**
* @return the timestamp * @return the timestamp
*/ */
@XmlAttribute
public long getTimestamp() { public long getTimestamp() {
return timestamp; return timestamp;
} }
@ -161,7 +171,6 @@ public class CellModel implements ProtobufMessageHandler, Serializable {
/** /**
* @return the value * @return the value
*/ */
@XmlValue
public byte[] getValue() { public byte[] getValue() {
return value; return value;
} }

View File

@ -24,6 +24,8 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
@ -69,11 +71,13 @@ import com.google.protobuf.ByteString;
* </pre> * </pre>
*/ */
@XmlRootElement(name="CellSet") @XmlRootElement(name="CellSet")
@XmlAccessorType(XmlAccessType.FIELD)
@InterfaceAudience.Private @InterfaceAudience.Private
public class CellSetModel implements Serializable, ProtobufMessageHandler { public class CellSetModel implements Serializable, ProtobufMessageHandler {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@XmlElement(name="Row")
private List<RowModel> rows; private List<RowModel> rows;
/** /**
@ -102,7 +106,6 @@ public class CellSetModel implements Serializable, ProtobufMessageHandler {
/** /**
* @return the rows * @return the rows
*/ */
@XmlElement(name="Row")
public List<RowModel> getRows() { public List<RowModel> getRows() {
return rows; return rows;
} }

View File

@ -31,6 +31,8 @@ import javax.xml.namespace.QName;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HConstants;
import org.codehaus.jackson.annotate.JsonAnyGetter;
import org.codehaus.jackson.annotate.JsonAnySetter;
/** /**
* Representation of a column family schema. * Representation of a column family schema.
@ -67,6 +69,7 @@ public class ColumnSchemaModel implements Serializable {
* @param name the attribute name * @param name the attribute name
* @param value the attribute value * @param value the attribute value
*/ */
@JsonAnySetter
public void addAttribute(String name, Object value) { public void addAttribute(String name, Object value) {
attrs.put(new QName(name), value); attrs.put(new QName(name), value);
} }
@ -92,6 +95,7 @@ public class ColumnSchemaModel implements Serializable {
* @return the map for holding unspecified (user) attributes * @return the map for holding unspecified (user) attributes
*/ */
@XmlAnyAttribute @XmlAnyAttribute
@JsonAnyGetter
public Map<QName,Object> getAny() { public Map<QName,Object> getAny() {
return attrs; return attrs;
} }

View File

@ -24,12 +24,15 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.hbase.rest.ProtobufMessageHandler; import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
import org.codehaus.jackson.annotate.JsonProperty;
/** /**
* Representation of a row. A row is a related set of cells, grouped by common * Representation of a row. A row is a related set of cells, grouped by common
@ -47,13 +50,20 @@ import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
* </pre> * </pre>
*/ */
@XmlRootElement(name="Row") @XmlRootElement(name="Row")
@XmlAccessorType(XmlAccessType.FIELD)
@InterfaceAudience.Private @InterfaceAudience.Private
public class RowModel implements ProtobufMessageHandler, Serializable { public class RowModel implements ProtobufMessageHandler, Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@JsonProperty("key")
@XmlAttribute
private byte[] key; private byte[] key;
@JsonProperty("Cell")
@XmlElement(name="Cell")
private List<CellModel> cells = new ArrayList<CellModel>(); private List<CellModel> cells = new ArrayList<CellModel>();
/** /**
* Default constructor * Default constructor
*/ */
@ -106,7 +116,6 @@ public class RowModel implements ProtobufMessageHandler, Serializable {
/** /**
* @return the row key * @return the row key
*/ */
@XmlAttribute
public byte[] getKey() { public byte[] getKey() {
return key; return key;
} }
@ -121,7 +130,6 @@ public class RowModel implements ProtobufMessageHandler, Serializable {
/** /**
* @return the cells * @return the cells
*/ */
@XmlElement(name="Cell")
public List<CellModel> getCells() { public List<CellModel> getCells() {
return cells; return cells;
} }

View File

@ -19,6 +19,8 @@
package org.apache.hadoop.hbase.rest.model; package org.apache.hadoop.hbase.rest.model;
import org.codehaus.jackson.annotate.JsonValue;
import java.io.Serializable; import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
@ -60,8 +62,17 @@ public class StorageClusterVersionModel implements Serializable {
/* (non-Javadoc) /* (non-Javadoc)
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
@JsonValue
@Override @Override
public String toString() { public String toString() {
return version; return version;
} }
//needed for jackson deserialization
private static StorageClusterVersionModel valueOf(String value) {
StorageClusterVersionModel versionModel
= new StorageClusterVersionModel();
versionModel.setVersion(value);
return versionModel;
}
} }

View File

@ -43,6 +43,9 @@ import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
import org.apache.hadoop.hbase.rest.protobuf.generated.ColumnSchemaMessage.ColumnSchema; import org.apache.hadoop.hbase.rest.protobuf.generated.ColumnSchemaMessage.ColumnSchema;
import org.apache.hadoop.hbase.rest.protobuf.generated.TableSchemaMessage.TableSchema; import org.apache.hadoop.hbase.rest.protobuf.generated.TableSchemaMessage.TableSchema;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import org.codehaus.jackson.annotate.JsonAnyGetter;
import org.codehaus.jackson.annotate.JsonAnySetter;
import org.codehaus.jackson.annotate.JsonIgnore;
/** /**
* A representation of HBase table descriptors. * A representation of HBase table descriptors.
@ -107,6 +110,7 @@ public class TableSchemaModel implements Serializable, ProtobufMessageHandler {
* @param name attribute name * @param name attribute name
* @param value attribute value * @param value attribute value
*/ */
@JsonAnySetter
public void addAttribute(String name, Object value) { public void addAttribute(String name, Object value) {
attrs.put(new QName(name), value); attrs.put(new QName(name), value);
} }
@ -151,6 +155,7 @@ public class TableSchemaModel implements Serializable, ProtobufMessageHandler {
* @return the map for holding unspecified (user) attributes * @return the map for holding unspecified (user) attributes
*/ */
@XmlAnyAttribute @XmlAnyAttribute
@JsonAnyGetter
public Map<QName,Object> getAny() { public Map<QName,Object> getAny() {
return attrs; return attrs;
} }
@ -337,6 +342,7 @@ public class TableSchemaModel implements Serializable, ProtobufMessageHandler {
/** /**
* @return a table descriptor * @return a table descriptor
*/ */
@JsonIgnore
public HTableDescriptor getTableDescriptor() { public HTableDescriptor getTableDescriptor() {
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(getName())); HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(getName()));
for (Map.Entry<QName, Object> e: getAny().entrySet()) { for (Map.Entry<QName, Object> e: getAny().entrySet()) {

View File

@ -0,0 +1,31 @@
/**
* 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.rest.provider;
import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
import javax.ws.rs.ext.Provider;
//create a class in the defined resource package name
//so it gets activated
//Use jackson to take care of json
//since it has better support for object
//deserializaiton and less clunky to deal with
@Provider
public class JacksonProvider extends JacksonJaxbJsonProvider {
}

View File

@ -56,6 +56,7 @@ public class TestSchemaResource {
private static Client client; private static Client client;
private static JAXBContext context; private static JAXBContext context;
private static Configuration conf; private static Configuration conf;
private static TestTableSchemaModel testTableSchemaModel;
@BeforeClass @BeforeClass
public static void setUpBeforeClass() throws Exception { public static void setUpBeforeClass() throws Exception {
@ -64,6 +65,7 @@ public class TestSchemaResource {
REST_TEST_UTIL.startServletContainer(conf); REST_TEST_UTIL.startServletContainer(conf);
client = new Client(new Cluster().add("localhost", client = new Client(new Cluster().add("localhost",
REST_TEST_UTIL.getServletPort())); REST_TEST_UTIL.getServletPort()));
testTableSchemaModel = new TestTableSchemaModel();
context = JAXBContext.newInstance( context = JAXBContext.newInstance(
ColumnSchemaModel.class, ColumnSchemaModel.class,
TableSchemaModel.class); TableSchemaModel.class);
@ -97,8 +99,8 @@ public class TestSchemaResource {
assertFalse(admin.tableExists(TABLE1)); assertFalse(admin.tableExists(TABLE1));
// create the table // create the table
model = TestTableSchemaModel.buildTestModel(TABLE1); model = testTableSchemaModel.buildTestModel(TABLE1);
TestTableSchemaModel.checkModel(model, TABLE1); testTableSchemaModel.checkModel(model, TABLE1);
response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model)); response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
assertEquals(response.getCode(), 201); assertEquals(response.getCode(), 201);
@ -112,7 +114,14 @@ public class TestSchemaResource {
assertEquals(response.getCode(), 200); assertEquals(response.getCode(), 200);
assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
model = fromXML(response.getBody()); model = fromXML(response.getBody());
TestTableSchemaModel.checkModel(model, TABLE1); testTableSchemaModel.checkModel(model, TABLE1);
// with json retrieve the schema and validate it
response = client.get(schemaPath, Constants.MIMETYPE_JSON);
assertEquals(response.getCode(), 200);
assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
model = testTableSchemaModel.fromJSON(Bytes.toString(response.getBody()));
testTableSchemaModel.checkModel(model, TABLE1);
// delete the table // delete the table
client.delete(schemaPath); client.delete(schemaPath);
@ -134,8 +143,8 @@ public class TestSchemaResource {
assertFalse(admin.tableExists(TABLE2)); assertFalse(admin.tableExists(TABLE2));
// create the table // create the table
model = TestTableSchemaModel.buildTestModel(TABLE2); model = testTableSchemaModel.buildTestModel(TABLE2);
TestTableSchemaModel.checkModel(model, TABLE2); testTableSchemaModel.checkModel(model, TABLE2);
response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF, response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF,
model.createProtobufOutput()); model.createProtobufOutput());
assertEquals(response.getCode(), 201); assertEquals(response.getCode(), 201);
@ -152,7 +161,7 @@ public class TestSchemaResource {
assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type")); assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
model = new TableSchemaModel(); model = new TableSchemaModel();
model.getObjectFromMessage(response.getBody()); model.getObjectFromMessage(response.getBody());
TestTableSchemaModel.checkModel(model, TABLE2); testTableSchemaModel.checkModel(model, TABLE2);
// retrieve the schema and validate it with alternate pbuf type // retrieve the schema and validate it with alternate pbuf type
response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF_IETF); response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF_IETF);
@ -160,7 +169,7 @@ public class TestSchemaResource {
assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type")); assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
model = new TableSchemaModel(); model = new TableSchemaModel();
model.getObjectFromMessage(response.getBody()); model.getObjectFromMessage(response.getBody());
TestTableSchemaModel.checkModel(model, TABLE2); testTableSchemaModel.checkModel(model, TABLE2);
// delete the table // delete the table
client.delete(schemaPath); client.delete(schemaPath);

View File

@ -53,7 +53,7 @@ public class TestStatusResource {
private static void validate(StorageClusterStatusModel model) { private static void validate(StorageClusterStatusModel model) {
assertNotNull(model); assertNotNull(model);
assertTrue(model.getRegions() >= 1); assertTrue(model.getRegions() + ">= 1", model.getRegions() >= 1);
assertTrue(model.getRequests() >= 0); assertTrue(model.getRequests() >= 0);
assertTrue(model.getAverageLoad() >= 0.0); assertTrue(model.getAverageLoad() >= 0.0);
assertNotNull(model.getLiveNodes()); assertNotNull(model.getLiveNodes());

View File

@ -26,6 +26,7 @@ import java.io.StringWriter;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import com.sun.jersey.api.json.JSONJAXBContext;
import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.SmallTests;
import org.apache.hadoop.hbase.util.Base64; import org.apache.hadoop.hbase.util.Base64;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
@ -34,28 +35,25 @@ import junit.framework.TestCase;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestCellModel extends TestCase { public class TestCellModel extends TestModelBase<CellModel> {
private static final long TIMESTAMP = 1245219839331L; private static final long TIMESTAMP = 1245219839331L;
private static final byte[] COLUMN = Bytes.toBytes("testcolumn"); private static final byte[] COLUMN = Bytes.toBytes("testcolumn");
private static final byte[] VALUE = Bytes.toBytes("testvalue"); private static final byte[] VALUE = Bytes.toBytes("testvalue");
private static final String AS_XML = public TestCellModel() throws Exception {
"<Cell timestamp=\"1245219839331\"" + super(CellModel.class);
" column=\"dGVzdGNvbHVtbg==\">" + AS_XML =
"dGVzdHZhbHVl</Cell>"; "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Cell " +
"column=\"dGVzdGNvbHVtbg==\" timestamp=\"1245219839331\">dGVzdHZhbHVl</Cell>";
AS_PB =
"Egp0ZXN0Y29sdW1uGOO6i+eeJCIJdGVzdHZhbHVl";
private static final String AS_PB = AS_JSON =
"Egp0ZXN0Y29sdW1uGOO6i+eeJCIJdGVzdHZhbHVl"; "{\"column\":\"dGVzdGNvbHVtbg==\",\"timestamp\":1245219839331,\"$\":\"dGVzdHZhbHVl\"}";
private JAXBContext context;
public TestCellModel() throws JAXBException {
super();
context = JAXBContext.newInstance(CellModel.class);
} }
private CellModel buildTestModel() { protected CellModel buildTestModel() {
CellModel model = new CellModel(); CellModel model = new CellModel();
model.setColumn(COLUMN); model.setColumn(COLUMN);
model.setTimestamp(TIMESTAMP); model.setTimestamp(TIMESTAMP);
@ -63,29 +61,7 @@ public class TestCellModel extends TestCase {
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(CellModel model) {
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.getColumn(), COLUMN));
assertTrue(Bytes.equals(model.getValue(), VALUE)); assertTrue(Bytes.equals(model.getValue(), VALUE));
assertTrue(model.hasUserTimestamp()); assertTrue(model.hasUserTimestamp());

View File

@ -35,7 +35,7 @@ import junit.framework.TestCase;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestCellSetModel extends TestCase { public class TestCellSetModel extends TestModelBase<CellSetModel> {
private static final byte[] ROW1 = Bytes.toBytes("testrow1"); private static final byte[] ROW1 = Bytes.toBytes("testrow1");
private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1"); private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1");
@ -49,36 +49,46 @@ public class TestCellSetModel extends TestCase {
private static final byte[] VALUE3 = Bytes.toBytes("testvalue3"); private static final byte[] VALUE3 = Bytes.toBytes("testvalue3");
private static final long TIMESTAMP3 = 1245393318192L; private static final long TIMESTAMP3 = 1245393318192L;
private static final String AS_XML = public TestCellSetModel() throws Exception {
"<CellSet>" + super(CellSetModel.class);
"<Row key=\"dGVzdHJvdzE=\">" + AS_XML =
"<Cell timestamp=\"1245219839331\" column=\"dGVzdGNvbHVtbjE=\">" + "<CellSet>" +
"dGVzdHZhbHVlMQ==</Cell>" + "<Row key=\"dGVzdHJvdzE=\">" +
"</Row>" + "<Cell timestamp=\"1245219839331\" column=\"dGVzdGNvbHVtbjE=\">" +
"<Row key=\"dGVzdHJvdzE=\">" + "dGVzdHZhbHVlMQ==</Cell>" +
"<Cell timestamp=\"1245239813319\" column=\"dGVzdGNvbHVtbjI=\">" + "</Row>" +
"dGVzdHZhbHVlMg==</Cell>" + "<Row key=\"dGVzdHJvdzE=\">" +
"<Cell timestamp=\"1245393318192\" column=\"dGVzdGNvbHVtbjM=\">" + "<Cell timestamp=\"1245239813319\" column=\"dGVzdGNvbHVtbjI=\">" +
"dGVzdHZhbHVlMw==</Cell>" + "dGVzdHZhbHVlMg==</Cell>" +
"</Row>" + "<Cell timestamp=\"1245393318192\" column=\"dGVzdGNvbHVtbjM=\">" +
"</CellSet>"; "dGVzdHZhbHVlMw==</Cell>" +
"</Row>" +
"</CellSet>";
private static final String AS_PB = AS_PB =
"CiwKCHRlc3Ryb3cxEiASC3Rlc3Rjb2x1bW4xGOO6i+eeJCIKdGVzdHZhbHVlMQpOCgh0ZXN0cm93" + "CiwKCHRlc3Ryb3cxEiASC3Rlc3Rjb2x1bW4xGOO6i+eeJCIKdGVzdHZhbHVlMQpOCgh0ZXN0cm93" +
"MRIgEgt0ZXN0Y29sdW1uMhjHyc7wniQiCnRlc3R2YWx1ZTISIBILdGVzdGNvbHVtbjMYsOLnuZ8k" + "MRIgEgt0ZXN0Y29sdW1uMhjHyc7wniQiCnRlc3R2YWx1ZTISIBILdGVzdGNvbHVtbjMYsOLnuZ8k" +
"Igp0ZXN0dmFsdWUz"; "Igp0ZXN0dmFsdWUz";
private JAXBContext context; AS_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><CellSet>" +
"<Row key=\"dGVzdHJvdzE=\"><Cell column=\"dGVzdGNvbHVtbjE=\" timestamp=\"1245219839331\">" +
"dGVzdHZhbHVlMQ==</Cell></Row><Row key=\"dGVzdHJvdzE=\">" +
"<Cell column=\"dGVzdGNvbHVtbjI=\" timestamp=\"1245239813319\">" +
"dGVzdHZhbHVlMg==</Cell>" +
"<Cell column=\"dGVzdGNvbHVtbjM=\" timestamp=\"1245393318192\">dGVzdHZhbHVlMw==</Cell>" +
"</Row></CellSet>";
public TestCellSetModel() throws JAXBException { AS_JSON =
super(); "{\"Row\":[{\"key\":\"dGVzdHJvdzE=\"," +
context = JAXBContext.newInstance( "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjE=\",\"timestamp\":1245219839331," +
CellModel.class, "\"$\":\"dGVzdHZhbHVlMQ==\"}]},{\"key\":\"dGVzdHJvdzE=\"," +
CellSetModel.class, "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjI=\",\"timestamp\":1245239813319," +
RowModel.class); "\"$\":\"dGVzdHZhbHVlMg==\"},{\"column\":\"dGVzdGNvbHVtbjM=\"," +
"\"timestamp\":1245393318192,\"$\":\"dGVzdHZhbHVlMw==\"}]}]}";
} }
private CellSetModel buildTestModel() { protected CellSetModel buildTestModel() {
CellSetModel model = new CellSetModel(); CellSetModel model = new CellSetModel();
RowModel row; RowModel row;
row = new RowModel(); row = new RowModel();
@ -93,29 +103,7 @@ public class TestCellSetModel extends TestCase {
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(CellSetModel model) {
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(); Iterator<RowModel> rows = model.getRows().iterator();
RowModel row = rows.next(); RowModel row = rows.next();
assertTrue(Bytes.equals(ROW1, row.getKey())); assertTrue(Bytes.equals(ROW1, row.getKey()));

View File

@ -27,10 +27,12 @@ import javax.xml.bind.JAXBException;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.SmallTests;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestColumnSchemaModel extends TestCase { public class TestColumnSchemaModel extends TestModelBase<ColumnSchemaModel> {
protected static final String COLUMN_NAME = "testcolumn"; protected static final String COLUMN_NAME = "testcolumn";
protected static final boolean BLOCKCACHE = true; protected static final boolean BLOCKCACHE = true;
@ -41,24 +43,20 @@ public class TestColumnSchemaModel extends TestCase {
protected static final int TTL = 86400; protected static final int TTL = 86400;
protected static final int VERSIONS = 1; protected static final int VERSIONS = 1;
protected static final String AS_XML = public TestColumnSchemaModel() throws Exception {
"<ColumnSchema name=\"testcolumn\"" + super(ColumnSchemaModel.class);
" BLOCKSIZE=\"16384\"" + AS_XML =
" BLOOMFILTER=\"NONE\"" + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ColumnSchema " +
" BLOCKCACHE=\"true\"" + "name=\"testcolumn\" BLOCKSIZE=\"16384\" BLOOMFILTER=\"NONE\" BLOCKCACHE=\"true\" " +
" COMPRESSION=\"GZ\"" + "COMPRESSION=\"GZ\" VERSIONS=\"1\" TTL=\"86400\" IN_MEMORY=\"false\"/>";
" VERSIONS=\"1\"" +
" TTL=\"86400\"" +
" IN_MEMORY=\"false\"/>";
private JAXBContext context; AS_JSON =
"{\"name\":\"testcolumn\",\"BLOCKSIZE\":\"16384\",\"BLOOMFILTER\":\"NONE\"," +
public TestColumnSchemaModel() throws JAXBException { "\"BLOCKCACHE\":\"true\",\"COMPRESSION\":\"GZ\",\"VERSIONS\":\"1\"," +
super(); "\"TTL\":\"86400\",\"IN_MEMORY\":\"false\"}";
context = JAXBContext.newInstance(ColumnSchemaModel.class);
} }
protected static ColumnSchemaModel buildTestModel() { protected ColumnSchemaModel buildTestModel() {
ColumnSchemaModel model = new ColumnSchemaModel(); ColumnSchemaModel model = new ColumnSchemaModel();
model.setName(COLUMN_NAME); model.setName(COLUMN_NAME);
model.__setBlockcache(BLOCKCACHE); model.__setBlockcache(BLOCKCACHE);
@ -71,19 +69,7 @@ public class TestColumnSchemaModel extends TestCase {
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(ColumnSchemaModel model) {
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.getName(), COLUMN_NAME);
assertEquals(model.__getBlockcache(), BLOCKCACHE); assertEquals(model.__getBlockcache(), BLOCKCACHE);
assertEquals(model.__getBlocksize(), BLOCKSIZE); assertEquals(model.__getBlocksize(), BLOCKSIZE);
@ -94,13 +80,7 @@ public class TestColumnSchemaModel extends TestCase {
assertEquals(model.__getVersions(), VERSIONS); assertEquals(model.__getVersions(), VERSIONS);
} }
public void testBuildModel() throws Exception { public void testFromPB() throws Exception {
checkModel(buildTestModel());
} }
public void testFromXML() throws Exception {
checkModel(fromXML(AS_XML));
}
} }

View File

@ -0,0 +1,143 @@
/*
* Copyright 2010 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.rest.model;
import com.sun.jersey.api.json.JSONJAXBContext;
import junit.framework.TestCase;
import org.apache.hadoop.hbase.SmallTests;
import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
import org.apache.hadoop.hbase.rest.provider.JAXBContextResolver;
import org.apache.hadoop.hbase.util.Base64;
import org.apache.hadoop.hbase.util.Bytes;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
import org.codehaus.jackson.map.AnnotationIntrospector;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector;
import org.codehaus.jackson.node.ObjectNode;
import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;
import org.junit.experimental.categories.Category;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
@Category(SmallTests.class)
public abstract class TestModelBase<T> extends TestCase {
protected String AS_XML;
protected String AS_PB;
protected String AS_JSON;
protected JAXBContext context;
protected Class<?> clazz;
protected ObjectMapper mapper;
protected TestModelBase(Class<?> clazz) throws Exception {
super();
this.clazz = clazz;
context = new JAXBContextResolver().getContext(clazz);
mapper = new JacksonJaxbJsonProvider().locateMapper(clazz,
MediaType.APPLICATION_JSON_TYPE);
}
protected abstract T buildTestModel();
@SuppressWarnings("unused")
protected String toXML(T model) throws JAXBException {
StringWriter writer = new StringWriter();
context.createMarshaller().marshal(model, writer);
return writer.toString();
}
protected String toJSON(T model) throws JAXBException, IOException {
StringWriter writer = new StringWriter();
mapper.writeValue(writer, model);
// original marshaller, uncomment this and comment mapper to verify backward compatibility
// ((JSONJAXBContext)context).createJSONMarshaller().marshallToJSON(model, writer);
return writer.toString();
}
public T fromJSON(String json) throws JAXBException, IOException {
return (T)
mapper.readValue(json, clazz);
}
public T fromXML(String xml) throws JAXBException {
return (T)
context.createUnmarshaller().unmarshal(new StringReader(xml));
}
@SuppressWarnings("unused")
protected byte[] toPB(ProtobufMessageHandler model) {
return model.createProtobufOutput();
}
protected T fromPB(String pb) throws
Exception {
return (T)clazz.getMethod("getObjectFromMessage", byte[].class).invoke(
clazz.newInstance(),
Base64.decode(AS_PB));
}
protected abstract void checkModel(T model);
public void testBuildModel() throws Exception {
checkModel(buildTestModel());
}
public void testFromPB() throws Exception {
checkModel(fromPB(AS_PB));
}
public void testFromXML() throws Exception {
checkModel(fromXML(AS_XML));
}
public void testToXML() throws Exception {
assertEquals(AS_XML, toXML(buildTestModel()));
}
public void testToJSON() throws Exception {
try {
ObjectNode expObj = mapper.readValue(AS_JSON, ObjectNode.class);
ObjectNode actObj = mapper.readValue(toJSON(buildTestModel()), ObjectNode.class);
assertEquals(expObj, actObj);
} catch(Exception e) {
assertEquals(AS_JSON, toJSON(buildTestModel()));
}
}
public void testFromJSON() throws Exception {
checkModel(fromJSON(AS_JSON));
}
}

View File

@ -33,48 +33,34 @@ import junit.framework.TestCase;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestRowModel extends TestCase { public class TestRowModel extends TestModelBase<RowModel> {
private static final byte[] ROW1 = Bytes.toBytes("testrow1"); private static final byte[] ROW1 = Bytes.toBytes("testrow1");
private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1"); private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1");
private static final byte[] VALUE1 = Bytes.toBytes("testvalue1"); private static final byte[] VALUE1 = Bytes.toBytes("testvalue1");
private static final long TIMESTAMP1 = 1245219839331L; 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; private JAXBContext context;
public TestRowModel() throws JAXBException { public TestRowModel() throws Exception {
super(); super(RowModel.class);
context = JAXBContext.newInstance( AS_XML =
CellModel.class, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Row key=\"dGVzdHJvdzE=\">" +
RowModel.class); "<Cell column=\"dGVzdGNvbHVtbjE=\" timestamp=\"1245219839331\">dGVzdHZhbHVlMQ==</Cell></Row>";
AS_JSON =
"{\"key\":\"dGVzdHJvdzE=\",\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjE=\"," +
"\"timestamp\":1245219839331,\"$\":\"dGVzdHZhbHVlMQ==\"}]}";
} }
private RowModel buildTestModel() { protected RowModel buildTestModel() {
RowModel model = new RowModel(); RowModel model = new RowModel();
model.setKey(ROW1); model.setKey(ROW1);
model.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1)); model.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(RowModel model) {
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())); assertTrue(Bytes.equals(ROW1, model.getKey()));
Iterator<CellModel> cells = model.getCells().iterator(); Iterator<CellModel> cells = model.getCells().iterator();
CellModel cell = cells.next(); CellModel cell = cells.next();
@ -85,13 +71,9 @@ public class TestRowModel extends TestCase {
assertFalse(cells.hasNext()); assertFalse(cells.hasNext());
} }
public void testBuildModel() throws Exception { @Override
checkModel(buildTestModel()); public void testFromPB() throws Exception {
//do nothing row model has no PB
} }
public void testFromXML() throws Exception {
checkModel(fromXML(AS_XML));
}
} }

View File

@ -34,7 +34,7 @@ import junit.framework.TestCase;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestScannerModel extends TestCase { public class TestScannerModel extends TestModelBase<ScannerModel> {
private static final byte[] START_ROW = Bytes.toBytes("abracadabra"); private static final byte[] START_ROW = Bytes.toBytes("abracadabra");
private static final byte[] END_ROW = Bytes.toBytes("zzyzx"); private static final byte[] END_ROW = Bytes.toBytes("zzyzx");
private static final byte[] COLUMN1 = Bytes.toBytes("column1"); private static final byte[] COLUMN1 = Bytes.toBytes("column1");
@ -44,29 +44,25 @@ public class TestScannerModel extends TestCase {
private static final int CACHING = 1000; private static final int CACHING = 1000;
private static final int BATCH = 100; private static final int BATCH = 100;
private static final String AS_XML = public TestScannerModel() throws Exception {
"<Scanner startTime=\"1245219839331\"" + super(ScannerModel.class);
" startRow=\"YWJyYWNhZGFicmE=\"" + AS_XML =
" endTime=\"1245393318192\"" + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
" endRow=\"enp5eng=\"" + "<Scanner batch=\"100\" caching=\"1000\" endRow=\"enp5eng=\" endTime=\"1245393318192\" " +
" batch=\"100\"" + "maxVersions=\"2147483647\" startRow=\"YWJyYWNhZGFicmE=\" startTime=\"1245219839331\">" +
" caching=\"1000\">" + "<column>Y29sdW1uMQ==</column><column>Y29sdW1uMjpmb28=</column></Scanner>";
"<column>Y29sdW1uMQ==</column>" +
"<column>Y29sdW1uMjpmb28=</column>" +
"</Scanner>";
private static final String AS_PB = AS_JSON =
"CgthYnJhY2FkYWJyYRIFenp5engaB2NvbHVtbjEaC2NvbHVtbjI6Zm9" + "{\"batch\":100,\"caching\":1000,\"endRow\":\"enp5eng=\",\"endTime\":1245393318192,"+
"vIGQo47qL554kMLDi57mfJDj/////B0joBw=="; "\"maxVersions\":2147483647,\"startRow\":\"YWJyYWNhZGFicmE=\",\"startTime\":1245219839331,"+
"\"column\":[\"Y29sdW1uMQ==\",\"Y29sdW1uMjpmb28=\"]}";
private JAXBContext context; AS_PB =
"CgthYnJhY2FkYWJyYRIFenp5engaB2NvbHVtbjEaC2NvbHVtbjI6Zm9vIGQo47qL554kMLDi57mf" +
public TestScannerModel() throws JAXBException { "JDj/////B0joBw==";
super();
context = JAXBContext.newInstance(ScannerModel.class);
} }
private ScannerModel buildTestModel() { protected ScannerModel buildTestModel() {
ScannerModel model = new ScannerModel(); ScannerModel model = new ScannerModel();
model.setStartRow(START_ROW); model.setStartRow(START_ROW);
model.setEndRow(END_ROW); model.setEndRow(END_ROW);
@ -79,29 +75,7 @@ public class TestScannerModel extends TestCase {
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(ScannerModel model) {
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.getStartRow(), START_ROW));
assertTrue(Bytes.equals(model.getEndRow(), END_ROW)); assertTrue(Bytes.equals(model.getEndRow(), END_ROW));
boolean foundCol1 = false, foundCol2 = false; boolean foundCol1 = false, foundCol2 = false;
@ -120,16 +94,5 @@ public class TestScannerModel extends TestCase {
assertEquals(model.getCaching(), CACHING); assertEquals(model.getCaching(), CACHING);
} }
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));
}
} }

View File

@ -19,60 +19,62 @@
package org.apache.hadoop.hbase.rest.model; package org.apache.hadoop.hbase.rest.model;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Iterator; import java.util.Iterator;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.SmallTests;
import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.util.Base64; import org.apache.hadoop.hbase.util.Base64;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import junit.framework.TestCase;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestStorageClusterStatusModel extends TestCase { public class TestStorageClusterStatusModel extends TestModelBase<StorageClusterStatusModel> {
private static final String AS_XML = public TestStorageClusterStatusModel() throws Exception {
"<ClusterStatus requests=\"0\" regions=\"2\" averageLoad=\"1.0\">" + super(StorageClusterStatusModel.class);
"<DeadNodes/>" +
"<LiveNodes><Node startCode=\"1245219839331\" requests=\"0\"" +
" name=\"test1\" maxHeapSizeMB=\"1024\" heapSizeMB=\"128\">" +
"<Region stores=\"1\" storefiles=\"1\" storefileSizeMB=\"0\"" +
" storefileIndexSizeMB=\"0\" name=\"aGJhc2U6cm9vdCwsMA==\"" +
" memstoreSizeMB=\"0\" readRequestsCount=\"1\"" +
" writeRequestsCount=\"2\" rootIndexSizeKB=\"1\"" +
" totalStaticIndexSizeKB=\"1\" totalStaticBloomSizeKB=\"1\"" +
" totalCompactingKVs=\"1\" currentCompactedKVs=\"1\"/></Node>" +
"<Node startCode=\"1245239331198\" requests=\"0\" name=\"test2\"" +
" maxHeapSizeMB=\"1024\" heapSizeMB=\"512\">" +
"<Region stores=\"1\" storefiles=\"1\" storefileSizeMB=\"0\"" +
" storefileIndexSizeMB=\"0\" name=\"aGJhc2U6bWV0YSwsMTI0NjAwMDA0MzcyNA==\"" +
" memstoreSizeMB=\"0\" readRequestsCount=\"1\"" +
" writeRequestsCount=\"2\" rootIndexSizeKB=\"1\"" +
" totalStaticIndexSizeKB=\"1\" totalStaticBloomSizeKB=\"1\"" +
" totalCompactingKVs=\"1\" currentCompactedKVs=\"1\"/></Node>"+
"</LiveNodes></ClusterStatus>";
private static final String AS_PB = AS_XML =
"Cj8KBXRlc3QxEOO6i+eeJBgAIIABKIAIMicKDWhiYXNlOnJvb3QsLDAQARgBIAAoADAAOAFAAkgB"+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"UAFYAWABaAEKSwoFdGVzdDIQ/pKx8J4kGAAggAQogAgyMwoZaGJhc2U6bWV0YSwsMTI0NjAwMDA0"+ "<ClusterStatus averageLoad=\"1.0\" regions=\"2\" requests=\"0\">" +
"<DeadNodes/><LiveNodes>" +
"<Node heapSizeMB=\"128\" maxHeapSizeMB=\"1024\" name=\"test1\" requests=\"0\" startCode=\"1245219839331\">" +
"<Region currentCompactedKVs=\"1\" memstoreSizeMB=\"0\" name=\"aGJhc2U6cm9vdCwsMA==\" readRequestsCount=\"1\" " +
"rootIndexSizeKB=\"1\" storefileIndexSizeMB=\"0\" storefileSizeMB=\"0\" storefiles=\"1\" stores=\"1\" " +
"totalCompactingKVs=\"1\" totalStaticBloomSizeKB=\"1\" totalStaticIndexSizeKB=\"1\" writeRequestsCount=\"2\"/>" +
"</Node>" +
"<Node heapSizeMB=\"512\" maxHeapSizeMB=\"1024\" name=\"test2\" requests=\"0\" startCode=\"1245239331198\">" +
"<Region currentCompactedKVs=\"1\" memstoreSizeMB=\"0\" name=\"aGJhc2U6bWV0YSwsMTI0NjAwMDA0MzcyNA==\" " +
"readRequestsCount=\"1\" rootIndexSizeKB=\"1\" storefileIndexSizeMB=\"0\" storefileSizeMB=\"0\" " +
"storefiles=\"1\" stores=\"1\" totalCompactingKVs=\"1\" totalStaticBloomSizeKB=\"1\" " +
"totalStaticIndexSizeKB=\"1\" writeRequestsCount=\"2\"/></Node></LiveNodes></ClusterStatus>";
AS_PB =
"Cj8KBXRlc3QxEOO6i+eeJBgAIIABKIAIMicKDWhiYXNlOnJvb3QsLDAQARgBIAAoADAAOAFAAkgB" +
"UAFYAWABaAEKSwoFdGVzdDIQ/pKx8J4kGAAggAQogAgyMwoZaGJhc2U6bWV0YSwsMTI0NjAwMDA0" +
"MzcyNBABGAEgACgAMAA4AUACSAFQAVgBYAFoARgCIAApAAAAAAAA8D8="; "MzcyNBABGAEgACgAMAA4AUACSAFQAVgBYAFoARgCIAApAAAAAAAA8D8=";
private JAXBContext context;
public TestStorageClusterStatusModel() throws JAXBException { //Using jackson will break json backward compatibilty for this representation
super(); //but the original one was broken as it would only print one Node element
context = JAXBContext.newInstance(StorageClusterStatusModel.class); //so the format itself was broken
AS_JSON =
"{\"regions\":2,\"requests\":0,\"averageLoad\":1.0,\"LiveNodes\":[{\"name\":\"test1\"," +
"\"Region\":[{\"name\":\"aGJhc2U6cm9vdCwsMA==\",\"stores\":1,\"storefiles\":1," +
"\"storefileSizeMB\":0,\"memstoreSizeMB\":0,\"storefileIndexSizeMB\":0," +
"\"readRequestsCount\":1,\"writeRequestsCount\":2,\"rootIndexSizeKB\":1," +
"\"totalStaticIndexSizeKB\":1,\"totalStaticBloomSizeKB\":1,\"totalCompactingKVs\":1," +
"\"currentCompactedKVs\":1}],\"requests\":0,\"startCode\":1245219839331," +
"\"heapSizeMB\":128,\"maxHeapSizeMB\":1024},{\"name\":\"test2\"," +
"\"Region\":[{\"name\":\"aGJhc2U6bWV0YSwsMTI0NjAwMDA0MzcyNA==\",\"stores\":1," +
"\"storefiles\":1,\"storefileSizeMB\":0,\"memstoreSizeMB\":0,\"storefileIndexSizeMB\":0," +
"\"readRequestsCount\":1,\"writeRequestsCount\":2,\"rootIndexSizeKB\":1," +
"\"totalStaticIndexSizeKB\":1,\"totalStaticBloomSizeKB\":1,\"totalCompactingKVs\":1," +
"\"currentCompactedKVs\":1}],\"requests\":0,\"startCode\":1245239331198," +
"\"heapSizeMB\":512,\"maxHeapSizeMB\":1024}],\"DeadNodes\":[]}";
} }
private StorageClusterStatusModel buildTestModel() { protected StorageClusterStatusModel buildTestModel() {
StorageClusterStatusModel model = new StorageClusterStatusModel(); StorageClusterStatusModel model = new StorageClusterStatusModel();
model.setRegions(2); model.setRegions(2);
model.setRequests(0); model.setRequests(0);
@ -85,29 +87,7 @@ public class TestStorageClusterStatusModel extends TestCase {
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(StorageClusterStatusModel model) {
private String toXML(StorageClusterStatusModel model) throws JAXBException {
StringWriter writer = new StringWriter();
context.createMarshaller().marshal(model, writer);
return writer.toString();
}
private StorageClusterStatusModel fromXML(String xml) throws JAXBException {
return (StorageClusterStatusModel)
context.createUnmarshaller().unmarshal(new StringReader(xml));
}
@SuppressWarnings("unused")
private byte[] toPB(StorageClusterStatusModel model) {
return model.createProtobufOutput();
}
private StorageClusterStatusModel fromPB(String pb) throws IOException {
return (StorageClusterStatusModel)
new StorageClusterStatusModel().getObjectFromMessage(Base64.decode(AS_PB));
}
private void checkModel(StorageClusterStatusModel model) {
assertEquals(model.getRegions(), 2); assertEquals(model.getRegions(), 2);
assertEquals(model.getRequests(), 0); assertEquals(model.getRequests(), 0);
assertEquals(model.getAverageLoad(), 1.0); assertEquals(model.getAverageLoad(), 1.0);
@ -161,18 +141,5 @@ public class TestStorageClusterStatusModel extends TestCase {
assertFalse(regions.hasNext()); assertFalse(regions.hasNext());
assertFalse(nodes.hasNext()); assertFalse(nodes.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));
}
} }

View File

@ -30,48 +30,31 @@ import org.apache.hadoop.hbase.SmallTests;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestStorageClusterVersionModel extends TestCase { public class TestStorageClusterVersionModel extends TestModelBase<StorageClusterVersionModel> {
private static final String VERSION = "0.0.1-testing"; private static final String VERSION = "0.0.1-testing";
private static final String AS_XML = public TestStorageClusterVersionModel() throws Exception {
"<ClusterVersion>" + VERSION + "</ClusterVersion>"; super(StorageClusterVersionModel.class);
AS_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+
"<ClusterVersion>" + VERSION + "</ClusterVersion>";
private JAXBContext context; AS_JSON = "\"0.0.1-testing\"";
public TestStorageClusterVersionModel() throws JAXBException {
super();
context = JAXBContext.newInstance(StorageClusterVersionModel.class);
} }
private StorageClusterVersionModel buildTestModel() { protected StorageClusterVersionModel buildTestModel() {
StorageClusterVersionModel model = new StorageClusterVersionModel(); StorageClusterVersionModel model = new StorageClusterVersionModel();
model.setVersion(VERSION); model.setVersion(VERSION);
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(StorageClusterVersionModel model) {
private String toXML(StorageClusterVersionModel model) throws JAXBException {
StringWriter writer = new StringWriter();
context.createMarshaller().marshal(model, writer);
return writer.toString();
}
private StorageClusterVersionModel fromXML(String xml) throws JAXBException {
return (StorageClusterVersionModel)
context.createUnmarshaller().unmarshal(new StringReader(xml));
}
private void checkModel(StorageClusterVersionModel model) {
assertEquals(model.getVersion(), VERSION); assertEquals(model.getVersion(), VERSION);
} }
public void testBuildModel() throws Exception { @Override
checkModel(buildTestModel()); public void testFromPB() throws Exception {
//ignore test no pb
} }
public void testFromXML() throws Exception {
checkModel(fromXML(AS_XML));
}
} }

View File

@ -35,65 +35,41 @@ import junit.framework.TestCase;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestTableInfoModel extends TestCase { public class TestTableInfoModel extends TestModelBase<TableInfoModel> {
private static final String TABLE = "testtable"; private static final String TABLE = "testtable";
private static final byte[] START_KEY = Bytes.toBytes("abracadbra"); private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
private static final byte[] END_KEY = Bytes.toBytes("zzyzx"); private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
private static final long ID = 8731042424L; private static final long ID = 8731042424L;
private static final String LOCATION = "testhost:9876"; private static final String LOCATION = "testhost:9876";
private static final String AS_XML =
"<TableInfo name=\"testtable\">" +
"<Region location=\"testhost:9876\"" +
" endKey=\"enp5eng=\"" +
" startKey=\"YWJyYWNhZGJyYQ==\"" +
" id=\"8731042424\"" +
" name=\"testtable,abracadbra,8731042424\"/>" +
"</TableInfo>";
private static final String AS_PB = public TestTableInfoModel() throws Exception {
"Cgl0ZXN0dGFibGUSSQofdGVzdHRhYmxlLGFicmFjYWRicmEsODczMTA0MjQyNBIKYWJyYWNhZGJy" + super(TableInfoModel.class);
"YRoFenp5engg+MSkwyAqDXRlc3Rob3N0Ojk4NzY="; AS_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><TableInfo " +
"name=\"testtable\"><Region endKey=\"enp5eng=\" id=\"8731042424\" " +
"location=\"testhost:9876\" " +
"name=\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\" " +
"startKey=\"YWJyYWNhZGJyYQ==\"/></TableInfo>";
private JAXBContext context; AS_PB =
"Cgl0ZXN0dGFibGUSSQofdGVzdHRhYmxlLGFicmFjYWRicmEsODczMTA0MjQyNBIKYWJyYWNhZGJy" +
"YRoFenp5engg+MSkwyAqDXRlc3Rob3N0Ojk4NzY=";
public TestTableInfoModel() throws JAXBException { AS_JSON =
super(); "{\"name\":\"testtable\",\"Region\":[{\"endKey\":\"enp5eng=\",\"id\":8731042424," +
context = JAXBContext.newInstance( "\"location\":\"testhost:9876\",\"" +
TableInfoModel.class, "name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\"" +
TableRegionModel.class); "startKey\":\"YWJyYWNhZGJyYQ==\"}]}";
} }
private TableInfoModel buildTestModel() { protected TableInfoModel buildTestModel() {
TableInfoModel model = new TableInfoModel(); TableInfoModel model = new TableInfoModel();
model.setName(TABLE); model.setName(TABLE);
model.add(new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION)); model.add(new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION));
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(TableInfoModel model) {
private String toXML(TableInfoModel model) throws JAXBException {
StringWriter writer = new StringWriter();
context.createMarshaller().marshal(model, writer);
return writer.toString();
}
private TableInfoModel fromXML(String xml) throws JAXBException {
return (TableInfoModel)
context.createUnmarshaller().unmarshal(new StringReader(xml));
}
@SuppressWarnings("unused")
private byte[] toPB(TableInfoModel model) {
return model.createProtobufOutput();
}
private TableInfoModel fromPB(String pb) throws IOException {
return (TableInfoModel)
new TableInfoModel().getObjectFromMessage(Base64.decode(AS_PB));
}
private void checkModel(TableInfoModel model) {
assertEquals(model.getName(), TABLE); assertEquals(model.getName(), TABLE);
Iterator<TableRegionModel> regions = model.getRegions().iterator(); Iterator<TableRegionModel> regions = model.getRegions().iterator();
TableRegionModel region = regions.next(); TableRegionModel region = regions.next();

View File

@ -34,27 +34,24 @@ import junit.framework.TestCase;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestTableListModel extends TestCase { public class TestTableListModel extends TestModelBase<TableListModel> {
private static final String TABLE1 = "table1"; private static final String TABLE1 = "table1";
private static final String TABLE2 = "table2"; private static final String TABLE2 = "table2";
private static final String TABLE3 = "table3"; private static final String TABLE3 = "table3";
private static final String AS_XML =
"<TableList><table name=\"table1\"/><table name=\"table2\"/>" +
"<table name=\"table3\"/></TableList>";
private static final String AS_PB = "CgZ0YWJsZTEKBnRhYmxlMgoGdGFibGUz"; public TestTableListModel() throws Exception {
super(TableListModel.class);
AS_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><TableList><table " +
"name=\"table1\"/><table name=\"table2\"/><table name=\"table3\"/></TableList>";
private JAXBContext context; AS_PB = "CgZ0YWJsZTEKBnRhYmxlMgoGdGFibGUz";
public TestTableListModel() throws JAXBException { AS_JSON =
super(); "{\"table\":[{\"name\":\"table1\"},{\"name\":\"table2\"},{\"name\":\"table3\"}]}";
context = JAXBContext.newInstance(
TableListModel.class,
TableModel.class);
} }
private TableListModel buildTestModel() { protected TableListModel buildTestModel() {
TableListModel model = new TableListModel(); TableListModel model = new TableListModel();
model.add(new TableModel(TABLE1)); model.add(new TableModel(TABLE1));
model.add(new TableModel(TABLE2)); model.add(new TableModel(TABLE2));
@ -62,29 +59,7 @@ public class TestTableListModel extends TestCase {
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(TableListModel model) {
private String toXML(TableListModel model) throws JAXBException {
StringWriter writer = new StringWriter();
context.createMarshaller().marshal(model, writer);
return writer.toString();
}
private TableListModel fromXML(String xml) throws JAXBException {
return (TableListModel)
context.createUnmarshaller().unmarshal(new StringReader(xml));
}
@SuppressWarnings("unused")
private byte[] toPB(TableListModel model) {
return model.createProtobufOutput();
}
private TableListModel fromPB(String pb) throws IOException {
return (TableListModel)
new TableListModel().getObjectFromMessage(Base64.decode(AS_PB));
}
private void checkModel(TableListModel model) {
Iterator<TableModel> tables = model.getTables().iterator(); Iterator<TableModel> tables = model.getTables().iterator();
TableModel table = tables.next(); TableModel table = tables.next();
assertEquals(table.getName(), TABLE1); assertEquals(table.getName(), TABLE1);
@ -94,18 +69,5 @@ public class TestTableListModel extends TestCase {
assertEquals(table.getName(), TABLE3); assertEquals(table.getName(), TABLE3);
assertFalse(tables.hasNext()); assertFalse(tables.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));
}
} }

View File

@ -32,46 +32,35 @@ import junit.framework.TestCase;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestTableRegionModel extends TestCase { public class TestTableRegionModel extends TestModelBase<TableRegionModel> {
private static final String TABLE = "testtable"; private static final String TABLE = "testtable";
private static final byte[] START_KEY = Bytes.toBytes("abracadbra"); private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
private static final byte[] END_KEY = Bytes.toBytes("zzyzx"); private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
private static final long ID = 8731042424L; private static final long ID = 8731042424L;
private static final String LOCATION = "testhost:9876"; private static final String LOCATION = "testhost:9876";
private static final String AS_XML = public TestTableRegionModel() throws Exception {
"<Region location=\"testhost:9876\"" + super(TableRegionModel.class);
" endKey=\"enp5eng=\"" +
" startKey=\"YWJyYWNhZGJyYQ==\"" +
" id=\"8731042424\"" +
" name=\"testtable,abracadbra,8731042424\"/>";
private JAXBContext context; AS_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Region endKey=\"enp5eng=\" " +
"id=\"8731042424\" location=\"testhost:9876\" " +
"name=\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\" " +
"startKey=\"YWJyYWNhZGJyYQ==\"/>";
public TestTableRegionModel() throws JAXBException { AS_JSON =
super(); "{\"endKey\":\"enp5eng=\",\"id\":8731042424,\"location\":\"testhost:9876\"," +
context = JAXBContext.newInstance(TableRegionModel.class); "\"name\":\"testtable,abracadbra,8731042424.ad9860f031282c46ed431d7af8f94aca.\",\"" +
"startKey\":\"YWJyYWNhZGJyYQ==\"}";
} }
private TableRegionModel buildTestModel() { protected TableRegionModel buildTestModel() {
TableRegionModel model = TableRegionModel model =
new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION); new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION);
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(TableRegionModel model) {
private String toXML(TableRegionModel model) throws JAXBException {
StringWriter writer = new StringWriter();
context.createMarshaller().marshal(model, writer);
return writer.toString();
}
private TableRegionModel fromXML(String xml) throws JAXBException {
return (TableRegionModel)
context.createUnmarshaller().unmarshal(new StringReader(xml));
}
private void checkModel(TableRegionModel model) {
assertTrue(Bytes.equals(model.getStartKey(), START_KEY)); assertTrue(Bytes.equals(model.getStartKey(), START_KEY));
assertTrue(Bytes.equals(model.getEndKey(), END_KEY)); assertTrue(Bytes.equals(model.getEndKey(), END_KEY));
assertEquals(model.getId(), ID); assertEquals(model.getId(), ID);
@ -81,10 +70,6 @@ public class TestTableRegionModel extends TestCase {
".ad9860f031282c46ed431d7af8f94aca."); ".ad9860f031282c46ed431d7af8f94aca.");
} }
public void testBuildModel() throws Exception {
checkModel(buildTestModel());
}
public void testGetName() { public void testGetName() {
TableRegionModel model = buildTestModel(); TableRegionModel model = buildTestModel();
String modelName = model.getName(); String modelName = model.getName();
@ -100,9 +85,9 @@ public class TestTableRegionModel extends TestCase {
assertEquals(name, model.getName()); assertEquals(name, model.getName());
} }
public void testFromXML() throws Exception { @Override
checkModel(fromXML(AS_XML)); public void testFromPB() throws Exception {
//no pb ignore
} }
} }

View File

@ -30,81 +30,66 @@ import javax.xml.bind.JAXBException;
import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.SmallTests;
import org.apache.hadoop.hbase.util.Base64; import org.apache.hadoop.hbase.util.Base64;
import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestTableSchemaModel extends TestCase { public class TestTableSchemaModel extends TestModelBase<TableSchemaModel> {
public static final String TABLE_NAME = "testTable"; public static final String TABLE_NAME = "testTable";
private static final boolean IS_META = false; private static final boolean IS_META = false;
private static final boolean IS_ROOT = false; private static final boolean IS_ROOT = false;
private static final boolean READONLY = false; private static final boolean READONLY = false;
private static final String AS_XML = TestColumnSchemaModel testColumnSchemaModel;
"<TableSchema name=\"testTable\"" +
" IS_META=\"false\"" +
" IS_ROOT=\"false\"" +
" READONLY=\"false\">" +
TestColumnSchemaModel.AS_XML +
"</TableSchema>";
private static final String AS_PB =
"Cgl0ZXN0VGFibGUSEAoHSVNfTUVUQRIFZmFsc2USEAoHSVNfUk9PVBIFZmFsc2USEQoIUkVBRE9O" +
"TFkSBWZhbHNlGpcBCgp0ZXN0Y29sdW1uEhIKCUJMT0NLU0laRRIFMTYzODQSEwoLQkxPT01GSUxU" +
"RVISBE5PTkUSEgoKQkxPQ0tDQUNIRRIEdHJ1ZRIRCgtDT01QUkVTU0lPThICR1oSDQoIVkVSU0lP" +
"TlMSATESDAoDVFRMEgU4NjQwMBISCglJTl9NRU1PUlkSBWZhbHNlGICjBSABKgJHWigA";
private JAXBContext context; private JAXBContext context;
public TestTableSchemaModel() throws JAXBException { public TestTableSchemaModel() throws Exception {
super(); super(TableSchemaModel.class);
context = JAXBContext.newInstance( testColumnSchemaModel = new TestColumnSchemaModel();
ColumnSchemaModel.class,
TableSchemaModel.class); AS_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<TableSchema name=\"testTable\" IS_META=\"false\" IS_ROOT=\"false\" READONLY=\"false\">" +
"<ColumnSchema name=\"testcolumn\" BLOCKSIZE=\"16384\" BLOOMFILTER=\"NONE\" " +
"BLOCKCACHE=\"true\" COMPRESSION=\"GZ\" VERSIONS=\"1\" TTL=\"86400\" IN_MEMORY=\"false\"/>" +
"</TableSchema>";
AS_PB =
"Cgl0ZXN0VGFibGUSEAoHSVNfTUVUQRIFZmFsc2USEAoHSVNfUk9PVBIFZmFsc2USEQoIUkVBRE9O" +
"TFkSBWZhbHNlGpcBCgp0ZXN0Y29sdW1uEhIKCUJMT0NLU0laRRIFMTYzODQSEwoLQkxPT01GSUxU" +
"RVISBE5PTkUSEgoKQkxPQ0tDQUNIRRIEdHJ1ZRIRCgtDT01QUkVTU0lPThICR1oSDQoIVkVSU0lP" +
"TlMSATESDAoDVFRMEgU4NjQwMBISCglJTl9NRU1PUlkSBWZhbHNlGICjBSABKgJHWigA";
AS_JSON =
"{\"name\":\"testTable\",\"IS_META\":\"false\",\"IS_ROOT\":\"false\"," +
"\"READONLY\":\"false\",\"ColumnSchema\":[{\"name\":\"testcolumn\"," +
"\"BLOCKSIZE\":\"16384\",\"BLOOMFILTER\":\"NONE\",\"BLOCKCACHE\":\"true\"," +
"\"COMPRESSION\":\"GZ\",\"VERSIONS\":\"1\",\"TTL\":\"86400\",\"IN_MEMORY\":\"false\"}]}";
} }
public static TableSchemaModel buildTestModel() { protected TableSchemaModel buildTestModel() {
return buildTestModel(TABLE_NAME); return buildTestModel(TABLE_NAME);
} }
public static TableSchemaModel buildTestModel(String name) { public TableSchemaModel buildTestModel(String name) {
TableSchemaModel model = new TableSchemaModel(); TableSchemaModel model = new TableSchemaModel();
model.setName(name); model.setName(name);
model.__setIsMeta(IS_META); model.__setIsMeta(IS_META);
model.__setIsRoot(IS_ROOT); model.__setIsRoot(IS_ROOT);
model.__setReadOnly(READONLY); model.__setReadOnly(READONLY);
model.addColumnFamily(TestColumnSchemaModel.buildTestModel()); model.addColumnFamily(testColumnSchemaModel.buildTestModel());
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(TableSchemaModel model) {
private String toXML(TableSchemaModel model) throws JAXBException {
StringWriter writer = new StringWriter();
context.createMarshaller().marshal(model, writer);
return writer.toString();
}
private TableSchemaModel fromXML(String xml) throws JAXBException {
return (TableSchemaModel)
context.createUnmarshaller().unmarshal(new StringReader(xml));
}
@SuppressWarnings("unused")
private byte[] toPB(TableSchemaModel model) {
return model.createProtobufOutput();
}
private TableSchemaModel fromPB(String pb) throws IOException {
return (TableSchemaModel)
new TableSchemaModel().getObjectFromMessage(Base64.decode(AS_PB));
}
public static void checkModel(TableSchemaModel model) {
checkModel(model, TABLE_NAME); checkModel(model, TABLE_NAME);
} }
public static void checkModel(TableSchemaModel model, String tableName) { public void checkModel(TableSchemaModel model, String tableName) {
assertEquals(model.getName(), tableName); assertEquals(model.getName(), tableName);
assertEquals(model.__getIsMeta(), IS_META); assertEquals(model.__getIsMeta(), IS_META);
assertEquals(model.__getIsRoot(), IS_ROOT); assertEquals(model.__getIsRoot(), IS_ROOT);
@ -112,7 +97,7 @@ public class TestTableSchemaModel extends TestCase {
Iterator<ColumnSchemaModel> families = model.getColumns().iterator(); Iterator<ColumnSchemaModel> families = model.getColumns().iterator();
assertTrue(families.hasNext()); assertTrue(families.hasNext());
ColumnSchemaModel family = families.next(); ColumnSchemaModel family = families.next();
TestColumnSchemaModel.checkModel(family); testColumnSchemaModel.checkModel(family);
assertFalse(families.hasNext()); assertFalse(families.hasNext());
} }

View File

@ -33,7 +33,7 @@ import junit.framework.TestCase;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestVersionModel extends TestCase { public class TestVersionModel extends TestModelBase<VersionModel> {
private static final String REST_VERSION = "0.0.1"; private static final String REST_VERSION = "0.0.1";
private static final String OS_VERSION = private static final String OS_VERSION =
"Linux 2.6.18-128.1.6.el5.centos.plusxen amd64"; "Linux 2.6.18-128.1.6.el5.centos.plusxen amd64";
@ -42,25 +42,24 @@ public class TestVersionModel extends TestCase {
private static final String JETTY_VERSION = "6.1.14"; private static final String JETTY_VERSION = "6.1.14";
private static final String JERSEY_VERSION = "1.1.0-ea"; private static final String JERSEY_VERSION = "1.1.0-ea";
private static final String AS_XML = public TestVersionModel() throws Exception {
"<Version REST=\"" + REST_VERSION + "\"" + super(VersionModel.class);
" OS=\"" + OS_VERSION + "\"" + AS_XML =
" JVM=\"" + JVM_VERSION + "\"" + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Version JVM=\"Sun " +
" Server=\"" + JETTY_VERSION + "\"" + "Microsystems Inc. 1.6.0_13-11.3-b02\" Jersey=\"1.1.0-ea\" " +
" Jersey=\"" + JERSEY_VERSION + "\"/>"; "OS=\"Linux 2.6.18-128.1.6.el5.centos.plusxen amd64\" REST=\"0.0.1\" Server=\"6.1.14\"/>";
private static final String AS_PB = AS_PB =
"CgUwLjAuMRInU3VuIE1pY3Jvc3lzdGVtcyBJbmMuIDEuNi4wXzEzLTExLjMtYjAyGi1MaW51eCAy" + "CgUwLjAuMRInU3VuIE1pY3Jvc3lzdGVtcyBJbmMuIDEuNi4wXzEzLTExLjMtYjAyGi1MaW51eCAy" +
"LjYuMTgtMTI4LjEuNi5lbDUuY2VudG9zLnBsdXN4ZW4gYW1kNjQiBjYuMS4xNCoIMS4xLjAtZWE="; "LjYuMTgtMTI4LjEuNi5lbDUuY2VudG9zLnBsdXN4ZW4gYW1kNjQiBjYuMS4xNCoIMS4xLjAtZWE=";
private JAXBContext context; AS_JSON =
"{\"JVM\":\"Sun Microsystems Inc. 1.6.0_13-11.3-b02\",\"Jersey\":\"1.1.0-ea\"," +
public TestVersionModel() throws JAXBException { "\"OS\":\"Linux 2.6.18-128.1.6.el5.centos.plusxen amd64\",\"" +
super(); "REST\":\"0.0.1\",\"Server\":\"6.1.14\"}";
context = JAXBContext.newInstance(VersionModel.class);
} }
private VersionModel buildTestModel() { protected VersionModel buildTestModel() {
VersionModel model = new VersionModel(); VersionModel model = new VersionModel();
model.setRESTVersion(REST_VERSION); model.setRESTVersion(REST_VERSION);
model.setOSVersion(OS_VERSION); model.setOSVersion(OS_VERSION);
@ -70,47 +69,12 @@ public class TestVersionModel extends TestCase {
return model; return model;
} }
@SuppressWarnings("unused") protected void checkModel(VersionModel model) {
private String toXML(VersionModel model) throws JAXBException {
StringWriter writer = new StringWriter();
context.createMarshaller().marshal(model, writer);
return writer.toString();
}
private VersionModel fromXML(String xml) throws JAXBException {
return (VersionModel)
context.createUnmarshaller().unmarshal(new StringReader(xml));
}
@SuppressWarnings("unused")
private byte[] toPB(VersionModel model) {
return model.createProtobufOutput();
}
private VersionModel fromPB(String pb) throws IOException {
return (VersionModel)
new VersionModel().getObjectFromMessage(Base64.decode(AS_PB));
}
private void checkModel(VersionModel model) {
assertEquals(model.getRESTVersion(), REST_VERSION); assertEquals(model.getRESTVersion(), REST_VERSION);
assertEquals(model.getOSVersion(), OS_VERSION); assertEquals(model.getOSVersion(), OS_VERSION);
assertEquals(model.getJVMVersion(), JVM_VERSION); assertEquals(model.getJVMVersion(), JVM_VERSION);
assertEquals(model.getServerVersion(), JETTY_VERSION); assertEquals(model.getServerVersion(), JETTY_VERSION);
assertEquals(model.getJerseyVersion(), JERSEY_VERSION); assertEquals(model.getJerseyVersion(), JERSEY_VERSION);
} }
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));
}
} }