blob: bbcf5e8fca0314034a7092a26dca16d81a323857 [file] [log] [blame]
Kevin Clark95833c52008-06-18 01:04:34 +00001require File.dirname(__FILE__) + '/spec_helper'
2
Kevin Clark356f8612008-06-18 01:08:05 +00003class ThriftExceptionSpec < Spec::ExampleGroup
Kevin Clark0ff9e8c2008-06-18 01:05:03 +00004 include Thrift
5
Kevin Clark61387bf2008-06-18 01:04:48 +00006 describe Exception do
7 it "should have an accessible message" do
8 e = Exception.new("test message")
9 e.message.should == "test message"
10 end
Kevin Clark95833c52008-06-18 01:04:34 +000011 end
Kevin Clark95833c52008-06-18 01:04:34 +000012
Kevin Clark61387bf2008-06-18 01:04:48 +000013 describe ApplicationException do
14 it "should inherit from Thrift::Exception" do
15 ApplicationException.superclass.should == Exception
16 end
Kevin Clark95833c52008-06-18 01:04:34 +000017
Kevin Clark61387bf2008-06-18 01:04:48 +000018 it "should have an accessible type and message" do
19 e = ApplicationException.new
20 e.type.should == ApplicationException::UNKNOWN
21 e.message.should be_nil
22 e = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, "test message")
23 e.type.should == ApplicationException::UNKNOWN_METHOD
24 e.message.should == "test message"
25 end
26
27 it "should read a struct off of a protocol" do
28 prot = mock("MockProtocol")
29 prot.should_receive(:read_struct_begin).ordered
30 prot.should_receive(:read_field_begin).exactly(3).times.and_return(
31 ["message", Types::STRING, 1],
32 ["type", Types::I32, 2],
33 [nil, Types::STOP, 0]
34 )
35 prot.should_receive(:read_string).ordered.and_return "test message"
36 prot.should_receive(:read_i32).ordered.and_return ApplicationException::BAD_SEQUENCE_ID
37 prot.should_receive(:read_field_end).exactly(2).times
38 prot.should_receive(:read_struct_end).ordered
39
40 e = ApplicationException.new
41 e.read(prot)
42 e.message.should == "test message"
43 e.type.should == ApplicationException::BAD_SEQUENCE_ID
44 end
45
46 it "should skip bad fields when reading a struct" do
47 prot = mock("MockProtocol")
48 prot.should_receive(:read_struct_begin).ordered
49 prot.should_receive(:read_field_begin).exactly(5).times.and_return(
50 ["type", Types::I32, 2],
51 ["type", Types::STRING, 2],
52 ["message", Types::MAP, 1],
53 ["message", Types::STRING, 3],
54 [nil, Types::STOP, 0]
55 )
56 prot.should_receive(:read_i32).and_return ApplicationException::INVALID_MESSAGE_TYPE
57 prot.should_receive(:skip).with(Types::STRING).twice
58 prot.should_receive(:skip).with(Types::MAP)
59 prot.should_receive(:read_field_end).exactly(4).times
60 prot.should_receive(:read_struct_end).ordered
61
62 e = ApplicationException.new
63 e.read(prot)
64 e.message.should be_nil
65 e.type.should == ApplicationException::INVALID_MESSAGE_TYPE
66 end
67
68 it "should write a Thrift::ApplicationException struct to the oprot" do
69 prot = mock("MockProtocol")
70 prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
71 prot.should_receive(:write_field_begin).with("message", Types::STRING, 1).ordered
72 prot.should_receive(:write_string).with("test message").ordered
73 prot.should_receive(:write_field_begin).with("type", Types::I32, 2).ordered
74 prot.should_receive(:write_i32).with(ApplicationException::UNKNOWN_METHOD).ordered
75 prot.should_receive(:write_field_end).twice
76 prot.should_receive(:write_field_stop).ordered
77 prot.should_receive(:write_struct_end).ordered
78
79 e = ApplicationException.new(ApplicationException::UNKNOWN_METHOD, "test message")
80 e.write(prot)
81 end
82
83 it "should skip nil fields when writing to the oprot" do
84 prot = mock("MockProtocol")
85 prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
86 prot.should_receive(:write_field_begin).with("message", Types::STRING, 1).ordered
87 prot.should_receive(:write_string).with("test message").ordered
88 prot.should_receive(:write_field_end).ordered
89 prot.should_receive(:write_field_stop).ordered
90 prot.should_receive(:write_struct_end).ordered
91
92 e = ApplicationException.new(nil, "test message")
93 e.write(prot)
94
95 prot = mock("MockProtocol")
96 prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
97 prot.should_receive(:write_field_begin).with("type", Types::I32, 2).ordered
98 prot.should_receive(:write_i32).with(ApplicationException::BAD_SEQUENCE_ID).ordered
99 prot.should_receive(:write_field_end).ordered
100 prot.should_receive(:write_field_stop).ordered
101 prot.should_receive(:write_struct_end).ordered
102
103 e = ApplicationException.new(ApplicationException::BAD_SEQUENCE_ID)
104 e.write(prot)
105
106 prot = mock("MockProtocol")
107 prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
108 prot.should_receive(:write_field_stop).ordered
109 prot.should_receive(:write_struct_end).ordered
110
111 e = ApplicationException.new(nil)
112 e.write(prot)
113 end
Kevin Clark95833c52008-06-18 01:04:34 +0000114 end
Kevin Clarkdfaada42008-06-18 01:06:01 +0000115
116 describe ProtocolException do
117 it "should have an accessible type" do
118 prot = ProtocolException.new(ProtocolException::SIZE_LIMIT, "message")
119 prot.type.should == ProtocolException::SIZE_LIMIT
120 prot.message.should == "message"
121 end
122 end
Kevin Clark95833c52008-06-18 01:04:34 +0000123end