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.IOException;
24  import java.io.StringReader;
25  import java.io.StringWriter;
26  
27  import javax.xml.bind.JAXBContext;
28  import javax.xml.bind.JAXBException;
29  
30  import org.apache.hadoop.hbase.util.Base64;
31  
32  import junit.framework.TestCase;
33  
34  public class TestVersionModel extends TestCase {
35    private static final String REST_VERSION = "0.0.1";
36    private static final String OS_VERSION = 
37      "Linux 2.6.18-128.1.6.el5.centos.plusxen amd64";
38    private static final String JVM_VERSION =
39      "Sun Microsystems Inc. 1.6.0_13-11.3-b02";
40    private static final String JETTY_VERSION = "6.1.14";
41    private static final String JERSEY_VERSION = "1.1.0-ea";
42    
43    private static final String AS_XML =
44      "<Version REST=\"" + REST_VERSION + "\"" +
45      " OS=\"" + OS_VERSION + "\"" +
46      " JVM=\"" + JVM_VERSION + "\"" +
47      " Server=\"" + JETTY_VERSION + "\"" +
48      " Jersey=\"" + JERSEY_VERSION + "\"/>";
49  
50    private static final String AS_PB = 
51      "CgUwLjAuMRInU3VuIE1pY3Jvc3lzdGVtcyBJbmMuIDEuNi4wXzEzLTExLjMtYjAyGi1MaW51eCAy" +
52      "LjYuMTgtMTI4LjEuNi5lbDUuY2VudG9zLnBsdXN4ZW4gYW1kNjQiBjYuMS4xNCoIMS4xLjAtZWE=";
53  
54    private JAXBContext context;
55  
56    public TestVersionModel() throws JAXBException {
57      super();
58      context = JAXBContext.newInstance(VersionModel.class);
59    }
60  
61    private VersionModel buildTestModel() {
62      VersionModel model = new VersionModel();
63      model.setRESTVersion(REST_VERSION);
64      model.setOSVersion(OS_VERSION);
65      model.setJVMVersion(JVM_VERSION);
66      model.setServerVersion(JETTY_VERSION);
67      model.setJerseyVersion(JERSEY_VERSION);
68      return model;
69    }
70  
71    @SuppressWarnings("unused")
72    private String toXML(VersionModel model) throws JAXBException {
73      StringWriter writer = new StringWriter();
74      context.createMarshaller().marshal(model, writer);
75      return writer.toString();
76    }
77  
78    private VersionModel fromXML(String xml) throws JAXBException {
79      return (VersionModel)
80        context.createUnmarshaller().unmarshal(new StringReader(xml));
81    }
82  
83    @SuppressWarnings("unused")
84    private byte[] toPB(VersionModel model) {
85      return model.createProtobufOutput();
86    }
87  
88    private VersionModel fromPB(String pb) throws IOException {
89      return (VersionModel) 
90        new VersionModel().getObjectFromMessage(Base64.decode(AS_PB));
91    }
92  
93    private void checkModel(VersionModel model) {
94      assertEquals(model.getRESTVersion(), REST_VERSION);
95      assertEquals(model.getOSVersion(), OS_VERSION);
96      assertEquals(model.getJVMVersion(), JVM_VERSION);
97      assertEquals(model.getServerVersion(), JETTY_VERSION);
98      assertEquals(model.getJerseyVersion(), JERSEY_VERSION);
99    }
100 
101   public void testBuildModel() throws Exception {
102     checkModel(buildTestModel());
103   }
104 
105   public void testFromXML() throws Exception {
106     checkModel(fromXML(AS_XML));
107   }
108 
109   public void testFromPB() throws Exception {
110     checkModel(fromPB(AS_PB));
111   }
112 }