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 | 8941458 | 2011-11-10 00:53:17 +0000 | [diff] [blame] | 20 | require File.expand_path("#{File.dirname(__FILE__)}/spec_helper") |
Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 21 | |
Kevin Clark | 356f861 | 2008-06-18 01:08:05 +0000 | [diff] [blame] | 22 | class ThriftExceptionSpec < Spec::ExampleGroup |
Kevin Clark | 0ff9e8c | 2008-06-18 01:05:03 +0000 | [diff] [blame] | 23 | include Thrift |
| 24 | |
Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 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 |
Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 30 | end |
Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 31 | |
Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 32 | describe ApplicationException do |
| 33 | it "should inherit from Thrift::Exception" do |
| 34 | ApplicationException.superclass.should == Exception |
| 35 | end |
Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 36 | |
Kevin Clark | 61387bf | 2008-06-18 01:04:48 +0000 | [diff] [blame] | 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 |
Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 133 | end |
Kevin Clark | dfaada4 | 2008-06-18 01:06:01 +0000 | [diff] [blame] | 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 |
Kevin Clark | 95833c5 | 2008-06-18 01:04:34 +0000 | [diff] [blame] | 142 | end |