summaryrefslogtreecommitdiff
path: root/lib/rb/spec/exception_spec.rb
blob: fc3213781dd50da6f05cc0a30aeb08e7331ecf88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

require File.dirname(__FILE__) + '/spec_helper'

class ThriftExceptionSpec < Spec::ExampleGroup
  include Thrift

  describe Exception do
    it "should have an accessible message" do
      e = Exception.new("test message")
      e.message.should == "test message"
    end
  end

  describe ApplicationException do
    it "should inherit from Thrift::Exception" do
      ApplicationException.superclass.should == Exception
    end

    it "should have an accessible type and message" do
      e = ApplicationException.new
      e.type.should == ApplicationException::UNKNOWN
      e.message.should be_nil
      e = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, "test message")
      e.type.should == ApplicationException::UNKNOWN_METHOD
      e.message.should == "test message"
    end

    it "should read a struct off of a protocol" do
      prot = mock("MockProtocol")
      prot.should_receive(:read_struct_begin).ordered
      prot.should_receive(:read_field_begin).exactly(3).times.and_return(
        ["message", Types::STRING, 1],
        ["type", Types::I32, 2],
        [nil, Types::STOP, 0]
      )
      prot.should_receive(:read_string).ordered.and_return "test message"
      prot.should_receive(:read_i32).ordered.and_return ApplicationException::BAD_SEQUENCE_ID
      prot.should_receive(:read_field_end).exactly(2).times
      prot.should_receive(:read_struct_end).ordered

      e = ApplicationException.new
      e.read(prot)
      e.message.should == "test message"
      e.type.should == ApplicationException::BAD_SEQUENCE_ID
    end

    it "should skip bad fields when reading a struct" do
      prot = mock("MockProtocol")
      prot.should_receive(:read_struct_begin).ordered
      prot.should_receive(:read_field_begin).exactly(5).times.and_return(
        ["type", Types::I32, 2],
        ["type", Types::STRING, 2],
        ["message", Types::MAP, 1],
        ["message", Types::STRING, 3],
        [nil, Types::STOP, 0]
      )
      prot.should_receive(:read_i32).and_return ApplicationException::INVALID_MESSAGE_TYPE
      prot.should_receive(:skip).with(Types::STRING).twice
      prot.should_receive(:skip).with(Types::MAP)
      prot.should_receive(:read_field_end).exactly(4).times
      prot.should_receive(:read_struct_end).ordered

      e = ApplicationException.new
      e.read(prot)
      e.message.should be_nil
      e.type.should == ApplicationException::INVALID_MESSAGE_TYPE
    end

    it "should write a Thrift::ApplicationException struct to the oprot" do
      prot = mock("MockProtocol")
      prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
      prot.should_receive(:write_field_begin).with("message", Types::STRING, 1).ordered
      prot.should_receive(:write_string).with("test message").ordered
      prot.should_receive(:write_field_begin).with("type", Types::I32, 2).ordered
      prot.should_receive(:write_i32).with(ApplicationException::UNKNOWN_METHOD).ordered
      prot.should_receive(:write_field_end).twice
      prot.should_receive(:write_field_stop).ordered
      prot.should_receive(:write_struct_end).ordered

      e = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, "test message")
      e.write(prot)
    end

    it "should skip nil fields when writing to the oprot" do
      prot = mock("MockProtocol")
      prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
      prot.should_receive(:write_field_begin).with("message", Types::STRING, 1).ordered
      prot.should_receive(:write_string).with("test message").ordered
      prot.should_receive(:write_field_end).ordered
      prot.should_receive(:write_field_stop).ordered
      prot.should_receive(:write_struct_end).ordered

      e = ApplicationException.new(nil, "test message")
      e.write(prot)

      prot = mock("MockProtocol")
      prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
      prot.should_receive(:write_field_begin).with("type", Types::I32, 2).ordered
      prot.should_receive(:write_i32).with(ApplicationException::BAD_SEQUENCE_ID).ordered
      prot.should_receive(:write_field_end).ordered
      prot.should_receive(:write_field_stop).ordered
      prot.should_receive(:write_struct_end).ordered

      e = ApplicationException.new(ApplicationException::BAD_SEQUENCE_ID)
      e.write(prot)

      prot = mock("MockProtocol")
      prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
      prot.should_receive(:write_field_stop).ordered
      prot.should_receive(:write_struct_end).ordered

      e = ApplicationException.new(nil)
      e.write(prot)
    end
  end

  describe ProtocolException do
    it "should have an accessible type" do
      prot = ProtocolException.new(ProtocolException::SIZE_LIMIT, "message")
      prot.type.should == ProtocolException::SIZE_LIMIT
      prot.message.should == "message"
    end
  end
end