Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
 
21
package org.apache.thrift.test;
22
 
23
import java.util.Map;
24
import org.apache.thrift.TFieldRequirementType;
25
import org.apache.thrift.meta_data.FieldMetaData;
26
import org.apache.thrift.meta_data.ListMetaData;
27
import org.apache.thrift.meta_data.MapMetaData;
28
import org.apache.thrift.meta_data.SetMetaData;
29
import org.apache.thrift.meta_data.StructMetaData;
30
import org.apache.thrift.protocol.TType;
31
import org.apache.thrift.TFieldIdEnum;
32
import thrift.test.*;
33
 
34
public class MetaDataTest {
35
  public static void main(String[] args) throws Exception {
36
    Map<CrazyNesting._Fields, FieldMetaData> mdMap = CrazyNesting.metaDataMap;
37
 
38
    // Check for struct fields existence
39
    if (mdMap.size() != 3)
40
      throw new RuntimeException("metadata map contains wrong number of entries!");
41
    if (!mdMap.containsKey(CrazyNesting._Fields.SET_FIELD) || !mdMap.containsKey(CrazyNesting._Fields.LIST_FIELD) || !mdMap.containsKey(CrazyNesting._Fields.STRING_FIELD))
42
      throw new RuntimeException("metadata map doesn't contain entry for a struct field!");
43
 
44
    // Check for struct fields contents
45
    if (!mdMap.get(CrazyNesting._Fields.STRING_FIELD).fieldName.equals("string_field") ||
46
            !mdMap.get(CrazyNesting._Fields.LIST_FIELD).fieldName.equals("list_field") ||
47
            !mdMap.get(CrazyNesting._Fields.SET_FIELD).fieldName.equals("set_field"))
48
      throw new RuntimeException("metadata map contains a wrong fieldname");
49
    if (mdMap.get(CrazyNesting._Fields.STRING_FIELD).requirementType != TFieldRequirementType.DEFAULT ||
50
            mdMap.get(CrazyNesting._Fields.LIST_FIELD).requirementType != TFieldRequirementType.REQUIRED ||
51
            mdMap.get(CrazyNesting._Fields.SET_FIELD).requirementType != TFieldRequirementType.OPTIONAL)
52
      throw new RuntimeException("metadata map contains the wrong requirement type for a field");
53
    if (mdMap.get(CrazyNesting._Fields.STRING_FIELD).valueMetaData.type != TType.STRING ||
54
            mdMap.get(CrazyNesting._Fields.LIST_FIELD).valueMetaData.type != TType.LIST ||
55
            mdMap.get(CrazyNesting._Fields.SET_FIELD).valueMetaData.type != TType.SET)
56
      throw new RuntimeException("metadata map contains the wrong requirement type for a field");
57
 
58
    // Check nested structures
59
    if (!mdMap.get(CrazyNesting._Fields.LIST_FIELD).valueMetaData.isContainer())
60
      throw new RuntimeException("value metadata for a list is stored as non-container!");
61
    if (mdMap.get(CrazyNesting._Fields.LIST_FIELD).valueMetaData.isStruct())
62
      throw new RuntimeException("value metadata for a list is stored as a struct!");
63
    if (((MapMetaData)((ListMetaData)((SetMetaData)((MapMetaData)((MapMetaData)((ListMetaData)mdMap.get(CrazyNesting._Fields.LIST_FIELD).valueMetaData).elemMetaData).valueMetaData).valueMetaData).elemMetaData).elemMetaData).keyMetaData.type != TType.STRUCT)
64
      throw new RuntimeException("metadata map contains wrong type for a value in a deeply nested structure");
65
    if (((StructMetaData)((MapMetaData)((ListMetaData)((SetMetaData)((MapMetaData)((MapMetaData)((ListMetaData)mdMap.get(CrazyNesting._Fields.LIST_FIELD).valueMetaData).elemMetaData).valueMetaData).valueMetaData).elemMetaData).elemMetaData).keyMetaData).structClass != Insanity.class)
66
      throw new RuntimeException("metadata map contains wrong class for a struct in a deeply nested structure");
67
 
68
    // Check that FieldMetaData contains a map with metadata for all generated struct classes
69
    if (FieldMetaData.getStructMetaDataMap(CrazyNesting.class) == null ||
70
            FieldMetaData.getStructMetaDataMap(Insanity.class) == null ||
71
            FieldMetaData.getStructMetaDataMap(Xtruct.class) == null)
72
      throw new RuntimeException("global metadata map doesn't contain an entry for a known struct");
73
    if (FieldMetaData.getStructMetaDataMap(CrazyNesting.class) != CrazyNesting.metaDataMap ||
74
            FieldMetaData.getStructMetaDataMap(Insanity.class) != Insanity.metaDataMap)
75
      throw new RuntimeException("global metadata map contains wrong entry for a loaded struct");    
76
 
77
    for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> mdEntry : mdMap.entrySet()) {
78
      if (!CrazyNesting._Fields.findByName(mdEntry.getValue().fieldName).equals(mdEntry.getKey())) {
79
        throw new RuntimeException("Field name map contained invalid Name <-> ID mapping");
80
      }
81
    }
82
  }
83
}