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
require File.dirname(__FILE__) + '/spec_helper'
21
 
22
class ThriftExceptionSpec < Spec::ExampleGroup
23
  include Thrift
24
 
25
  describe Exception do
26
    it "should have an accessible message" do
27
      e = Exception.new("test message")
28
      e.message.should == "test message"
29
    end
30
  end
31
 
32
  describe ApplicationException do
33
    it "should inherit from Thrift::Exception" do
34
      ApplicationException.superclass.should == Exception
35
    end
36
 
37
    it "should have an accessible type and message" do
38
      e = ApplicationException.new
39
      e.type.should == ApplicationException::UNKNOWN
40
      e.message.should be_nil
41
      e = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, "test message")
42
      e.type.should == ApplicationException::UNKNOWN_METHOD
43
      e.message.should == "test message"
44
    end
45
 
46
    it "should read a struct off of a protocol" do
47
      prot = mock("MockProtocol")
48
      prot.should_receive(:read_struct_begin).ordered
49
      prot.should_receive(:read_field_begin).exactly(3).times.and_return(
50
        ["message", Types::STRING, 1],
51
        ["type", Types::I32, 2],
52
        [nil, Types::STOP, 0]
53
      )
54
      prot.should_receive(:read_string).ordered.and_return "test message"
55
      prot.should_receive(:read_i32).ordered.and_return ApplicationException::BAD_SEQUENCE_ID
56
      prot.should_receive(:read_field_end).exactly(2).times
57
      prot.should_receive(:read_struct_end).ordered
58
 
59
      e = ApplicationException.new
60
      e.read(prot)
61
      e.message.should == "test message"
62
      e.type.should == ApplicationException::BAD_SEQUENCE_ID
63
    end
64
 
65
    it "should skip bad fields when reading a struct" do
66
      prot = mock("MockProtocol")
67
      prot.should_receive(:read_struct_begin).ordered
68
      prot.should_receive(:read_field_begin).exactly(5).times.and_return(
69
        ["type", Types::I32, 2],
70
        ["type", Types::STRING, 2],
71
        ["message", Types::MAP, 1],
72
        ["message", Types::STRING, 3],
73
        [nil, Types::STOP, 0]
74
      )
75
      prot.should_receive(:read_i32).and_return ApplicationException::INVALID_MESSAGE_TYPE
76
      prot.should_receive(:skip).with(Types::STRING).twice
77
      prot.should_receive(:skip).with(Types::MAP)
78
      prot.should_receive(:read_field_end).exactly(4).times
79
      prot.should_receive(:read_struct_end).ordered
80
 
81
      e = ApplicationException.new
82
      e.read(prot)
83
      e.message.should be_nil
84
      e.type.should == ApplicationException::INVALID_MESSAGE_TYPE
85
    end
86
 
87
    it "should write a Thrift::ApplicationException struct to the oprot" do
88
      prot = mock("MockProtocol")
89
      prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
90
      prot.should_receive(:write_field_begin).with("message", Types::STRING, 1).ordered
91
      prot.should_receive(:write_string).with("test message").ordered
92
      prot.should_receive(:write_field_begin).with("type", Types::I32, 2).ordered
93
      prot.should_receive(:write_i32).with(ApplicationException::UNKNOWN_METHOD).ordered
94
      prot.should_receive(:write_field_end).twice
95
      prot.should_receive(:write_field_stop).ordered
96
      prot.should_receive(:write_struct_end).ordered
97
 
98
      e = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, "test message")
99
      e.write(prot)
100
    end
101
 
102
    it "should skip nil fields when writing to the oprot" do
103
      prot = mock("MockProtocol")
104
      prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
105
      prot.should_receive(:write_field_begin).with("message", Types::STRING, 1).ordered
106
      prot.should_receive(:write_string).with("test message").ordered
107
      prot.should_receive(:write_field_end).ordered
108
      prot.should_receive(:write_field_stop).ordered
109
      prot.should_receive(:write_struct_end).ordered
110
 
111
      e = ApplicationException.new(nil, "test message")
112
      e.write(prot)
113
 
114
      prot = mock("MockProtocol")
115
      prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
116
      prot.should_receive(:write_field_begin).with("type", Types::I32, 2).ordered
117
      prot.should_receive(:write_i32).with(ApplicationException::BAD_SEQUENCE_ID).ordered
118
      prot.should_receive(:write_field_end).ordered
119
      prot.should_receive(:write_field_stop).ordered
120
      prot.should_receive(:write_struct_end).ordered
121
 
122
      e = ApplicationException.new(ApplicationException::BAD_SEQUENCE_ID)
123
      e.write(prot)
124
 
125
      prot = mock("MockProtocol")
126
      prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
127
      prot.should_receive(:write_field_stop).ordered
128
      prot.should_receive(:write_struct_end).ordered
129
 
130
      e = ApplicationException.new(nil)
131
      e.write(prot)
132
    end
133
  end
134
 
135
  describe ProtocolException do
136
    it "should have an accessible type" do
137
      prot = ProtocolException.new(ProtocolException::SIZE_LIMIT, "message")
138
      prot.type.should == ProtocolException::SIZE_LIMIT
139
      prot.message.should == "message"
140
    end
141
  end
142
end