summaryrefslogtreecommitdiff
path: root/qpid/cpp/bindings/qpid/ruby/spec/qpid_messaging/connection_spec.rb
blob: 50038a0fc099513932f93ea8568fd7e3ea199015 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#
# 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 'spec_helper'

module Qpid

  module Messaging

    describe Connection do

      before(:each) do
        @session_impl    = double('Cqpid::Session')
        @connection_impl = double('Cqpid::Connection')

        @connection = Qpid::Messaging::Connection.new :impl => @connection_impl
      end

      it "accepts options on construction" do
        expect {
          connection = Qpid::Messaging::Connection.new :options => {:username => "foo"}

          connection.options.should include("username")
        }.to_not raise_error
      end

      it "returns the underlying implementation" do
        impl = @connection.connection_impl

        impl.should == @connection_impl
      end

      it "opens the connection" do
        @connection_impl.should_receive(:open)

        @connection.open
      end

      it "closes the connection" do
        @connection_impl.should_receive(:close)

        @connection.close
      end

      it "retrieves a session by name" do
        @connection_impl.should_receive(:getSession).
          with("farkle").
          and_return(@session_impl)

        session = @connection.session "farkle"

        session.session_impl.should == @session_impl
      end

      it "raises an error when a session name is invalid" do
        @connection_impl.should_receive(:getSession).
          with("farkle").
          and_raise(RuntimeError)

        expect {
          @connection.session "farkle"
        }.to raise_error
      end

      ####################################################################
      # test conditions for when a connection is not connected to a broker
      ####################################################################
      describe "when closed" do

        before(:each) do
          @connection_impl.should_receive(:isOpen).
            and_return(false)
        end

        it "returns false when not connected to a broker" do
          open = @connection.open?

          open.should == false
        end

        it "should raise an error when creating a session on a closed connection" do
          expect {
            @connection.create_session
          }.to raise_error(RuntimeError)
        end

        it "raises an error when creating a transactional session on a closed connection" do
          expect {
            @connection.create_session :transactional => true
          }.to raise_error(RuntimeError)
        end

        it "raises an error when creating a named session on a closed connection" do
          expect {
            @connection.create_session :name => "test", :transactional => true
          }.to raise_error(RuntimeError)
        end

        it "returns a null username when not connected" do
          username = @connection.authenticated_username

          username.should be_nil
        end

      end

      #########################################################
      # test conditions for when a connection must be connected
      #########################################################
      describe "when connected" do

        before(:each) do
          @connection_impl.should_receive(:isOpen).
            and_return(true)
        end

        it "returns true when connected to a broker" do
          open = @connection.open?

          open.should == true
        end

        it "creates a session" do
          @connection_impl.should_receive(:createSession).
            and_return(@session_impl)

          session = @connection.create_session

          session.session_impl.should == @session_impl
        end

        it "creates a named session with a name when provided" do
          @connection_impl.should_receive(:createSession).with("farkle").
            and_return(@session_impl)

          session = @connection.create_session :name => "farkle"

          session.session_impl.should == @session_impl
        end

        it "creates a transactional session when specified" do
          @connection_impl.should_receive(:createTransactionalSession).
            and_return(@session_impl)

          session = @connection.create_session :transactional => true

          session.session_impl.should == @session_impl
        end

        it "creates a named transactional session when specified" do
          @connection_impl.should_receive(:createTransactionalSession).
            with("farkle").
            and_return(@session_impl)

          session = @connection.create_session :transactional => true, :name => "farkle"

          session.session_impl.should == @session_impl
        end

        it "returns the authenticated username when connected" do
          @connection_impl.should_receive(:getAuthenticatedUsername).
            and_return("mcpierce")

          username = @connection.authenticated_username

          username.should == "mcpierce"
        end

      end

    end

  end

end