1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest.model;
22  
23  import java.io.StringReader;
24  import java.io.StringWriter;
25  
26  import javax.xml.bind.JAXBContext;
27  import javax.xml.bind.JAXBException;
28  
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  import junit.framework.TestCase;
32  
33  public class TestTableRegionModel extends TestCase {
34    private static final String TABLE = "testtable";
35    private static final byte[] START_KEY = Bytes.toBytes("abracadbra");
36    private static final byte[] END_KEY = Bytes.toBytes("zzyzx");
37    private static final long ID = 8731042424L;
38    private static final String LOCATION = "testhost:9876";
39  
40    private static final String AS_XML =
41      "<Region location=\"testhost:9876\"" +
42        " endKey=\"enp5eng=\"" +
43        " startKey=\"YWJyYWNhZGJyYQ==\"" +
44        " id=\"8731042424\"" +
45        " name=\"testtable,abracadbra,8731042424\"/>";
46  
47    private JAXBContext context;
48  
49    public TestTableRegionModel() throws JAXBException {
50      super();
51      context = JAXBContext.newInstance(TableRegionModel.class);
52    }
53  
54    private TableRegionModel buildTestModel() {
55      TableRegionModel model =
56        new TableRegionModel(TABLE, ID, START_KEY, END_KEY, LOCATION);
57      return model;
58    }
59  
60    @SuppressWarnings("unused")
61    private String toXML(TableRegionModel model) throws JAXBException {
62      StringWriter writer = new StringWriter();
63      context.createMarshaller().marshal(model, writer);
64      return writer.toString();
65    }
66  
67    private TableRegionModel fromXML(String xml) throws JAXBException {
68      return (TableRegionModel)
69        context.createUnmarshaller().unmarshal(new StringReader(xml));
70    }
71  
72    private void checkModel(TableRegionModel model) {
73      assertTrue(Bytes.equals(model.getStartKey(), START_KEY));
74      assertTrue(Bytes.equals(model.getEndKey(), END_KEY));
75      assertEquals(model.getId(), ID);
76      assertEquals(model.getLocation(), LOCATION);
77      assertEquals(model.getName(), 
78        TABLE + "," + Bytes.toString(START_KEY) + "," + Long.toString(ID));
79    }
80  
81    public void testBuildModel() throws Exception {
82      checkModel(buildTestModel());
83    }
84  
85    public void testFromXML() throws Exception {
86      checkModel(fromXML(AS_XML));
87    }
88  }