blob: 78ddb7552a3aba742684369d40e95a1e7650796e [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
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
Jake Farrell89414582011-11-10 00:53:17 +000020require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
Kevin Clark95833c52008-06-18 01:04:34 +000021
Kevin Clark356f8612008-06-18 01:08:05 +000022class ThriftExceptionSpec < Spec::ExampleGroup
Kevin Clark0ff9e8c2008-06-18 01:05:03 +000023 include Thrift
24
Kevin Clark61387bf2008-06-18 01:04:48 +000025 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
Kevin Clark95833c52008-06-18 01:04:34 +000030 end
Kevin Clark95833c52008-06-18 01:04:34 +000031
Kevin Clark61387bf2008-06-18 01:04:48 +000032 describe ApplicationException do
33 it "should inherit from Thrift::Exception" do
34 ApplicationException.superclass.should == Exception
35 end
Kevin Clark95833c52008-06-18 01:04:34 +000036
Kevin Clark61387bf2008-06-18 01:04:48 +000037 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
Kevin Clark95833c52008-06-18 01:04:34 +0000133 end
Kevin Clarkdfaada42008-06-18 01:06:01 +0000134
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
Kevin Clark95833c52008-06-18 01:04:34 +0000142end