View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.IOException;
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.SmallTests;
30  import org.apache.hadoop.hbase.util.Base64;
31  
32  import junit.framework.TestCase;
33  import org.junit.experimental.categories.Category;
34  
35  @Category(SmallTests.class)
36  public class TestVersionModel extends TestCase {
37    private static final String REST_VERSION = "0.0.1";
38    private static final String OS_VERSION = 
39      "Linux 2.6.18-128.1.6.el5.centos.plusxen amd64";
40    private static final String JVM_VERSION =
41      "Sun Microsystems Inc. 1.6.0_13-11.3-b02";
42    private static final String JETTY_VERSION = "6.1.14";
43    private static final String JERSEY_VERSION = "1.1.0-ea";
44    
45    private static final String AS_XML =
46      "<Version REST=\"" + REST_VERSION + "\"" +
47      " OS=\"" + OS_VERSION + "\"" +
48      " JVM=\"" + JVM_VERSION + "\"" +
49      " Server=\"" + JETTY_VERSION + "\"" +
50      " Jersey=\"" + JERSEY_VERSION + "\"/>";
51  
52    private static final String AS_PB = 
53      "CgUwLjAuMRInU3VuIE1pY3Jvc3lzdGVtcyBJbmMuIDEuNi4wXzEzLTExLjMtYjAyGi1MaW51eCAy" +
54      "LjYuMTgtMTI4LjEuNi5lbDUuY2VudG9zLnBsdXN4ZW4gYW1kNjQiBjYuMS4xNCoIMS4xLjAtZWE=";
55  
56    private JAXBContext context;
57  
58    public TestVersionModel() throws JAXBException {
59      super();
60      context = JAXBContext.newInstance(VersionModel.class);
61    }
62  
63    private VersionModel buildTestModel() {
64      VersionModel model = new VersionModel();
65      model.setRESTVersion(REST_VERSION);
66      model.setOSVersion(OS_VERSION);
67      model.setJVMVersion(JVM_VERSION);
68      model.setServerVersion(JETTY_VERSION);
69      model.setJerseyVersion(JERSEY_VERSION);
70      return model;
71    }
72  
73    @SuppressWarnings("unused")
74    private String toXML(VersionModel model) throws JAXBException {
75      StringWriter writer = new StringWriter();
76      context.createMarshaller().marshal(model, writer);
77      return writer.toString();
78    }
79  
80    private VersionModel fromXML(String xml) throws JAXBException {
81      return (VersionModel)
82        context.createUnmarshaller().unmarshal(new StringReader(xml));
83    }
84  
85    @SuppressWarnings("unused")
86    private byte[] toPB(VersionModel model) {
87      return model.createProtobufOutput();
88    }
89  
90    private VersionModel fromPB(String pb) throws IOException {
91      return (VersionModel) 
92        new VersionModel().getObjectFromMessage(Base64.decode(AS_PB));
93    }
94  
95    private void checkModel(VersionModel model) {
96      assertEquals(model.getRESTVersion(), REST_VERSION);
97      assertEquals(model.getOSVersion(), OS_VERSION);
98      assertEquals(model.getJVMVersion(), JVM_VERSION);
99      assertEquals(model.getServerVersion(), JETTY_VERSION);
100     assertEquals(model.getJerseyVersion(), JERSEY_VERSION);
101   }
102 
103   public void testBuildModel() throws Exception {
104     checkModel(buildTestModel());
105   }
106 
107   public void testFromXML() throws Exception {
108     checkModel(fromXML(AS_XML));
109   }
110 
111   public void testFromPB() throws Exception {
112     checkModel(fromPB(AS_PB));
113   }
114 
115 }
116