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