|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.jms.connection; |
| 18 | + |
| 19 | +import jakarta.jms.ConnectionFactory; |
| 20 | +import jakarta.jms.JMSContext; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | +import static org.mockito.BDDMockito.given; |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for {@link UserCredentialsConnectionFactoryAdapter}. |
| 31 | + * |
| 32 | + * @author Stephane Nicoll |
| 33 | + */ |
| 34 | +class UserCredentialsConnectionFactoryAdapterTests { |
| 35 | + |
| 36 | + private static final JMSContext MOCK_CONTEXT = mock(JMSContext.class); |
| 37 | + |
| 38 | + private final ConnectionFactory target; |
| 39 | + |
| 40 | + private final UserCredentialsConnectionFactoryAdapter adapter; |
| 41 | + |
| 42 | + UserCredentialsConnectionFactoryAdapterTests() { |
| 43 | + this.target = mock(ConnectionFactory.class); |
| 44 | + this.adapter = new UserCredentialsConnectionFactoryAdapter(); |
| 45 | + this.adapter.setTargetConnectionFactory(this.target); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void createContextWhenNoAuthentication() { |
| 50 | + given(this.target.createContext()).willReturn(MOCK_CONTEXT); |
| 51 | + assertThat(this.adapter.createContext()).isSameAs(MOCK_CONTEXT); |
| 52 | + verify(this.target).createContext(); |
| 53 | + verifyNoMoreInteractions(this.target); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void createContextWhenAuthentication() { |
| 58 | + this.adapter.setUsername("user"); |
| 59 | + this.adapter.setPassword("password"); |
| 60 | + given(this.target.createContext("user", "password")).willReturn(MOCK_CONTEXT); |
| 61 | + assertThat(this.adapter.createContext()).isSameAs(MOCK_CONTEXT); |
| 62 | + verify(this.target).createContext("user", "password"); |
| 63 | + verifyNoMoreInteractions(this.target); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void createContextWhenThreadLevelAuthentication() { |
| 68 | + this.adapter.setCredentialsForCurrentThread("user", "password"); |
| 69 | + given(this.target.createContext("user", "password")).willReturn(MOCK_CONTEXT); |
| 70 | + assertThat(this.adapter.createContext()).isSameAs(MOCK_CONTEXT); |
| 71 | + verify(this.target).createContext("user", "password"); |
| 72 | + verifyNoMoreInteractions(this.target); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void createContextWhenAuthenticationAndThreadLevelAuthentication() { |
| 77 | + this.adapter.setCredentialsForCurrentThread("specific", "secret"); |
| 78 | + this.adapter.setUsername("user"); |
| 79 | + this.adapter.setPassword("password"); |
| 80 | + given(this.target.createContext("specific", "secret")).willReturn(MOCK_CONTEXT); |
| 81 | + assertThat(this.adapter.createContext()).isSameAs(MOCK_CONTEXT); |
| 82 | + verify(this.target).createContext("specific", "secret"); |
| 83 | + verifyNoMoreInteractions(this.target); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void createContextWithSessionModeWhenNoAuthentication() { |
| 88 | + given(this.target.createContext(1)).willReturn(MOCK_CONTEXT); |
| 89 | + assertThat(this.adapter.createContext(1)).isSameAs(MOCK_CONTEXT); |
| 90 | + verify(this.target).createContext(1); |
| 91 | + verifyNoMoreInteractions(this.target); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void createContextWithSessionModeWhenAuthentication() { |
| 96 | + this.adapter.setUsername("user"); |
| 97 | + this.adapter.setPassword("password"); |
| 98 | + given(this.target.createContext("user", "password", 1)).willReturn(MOCK_CONTEXT); |
| 99 | + assertThat(this.adapter.createContext(1)).isSameAs(MOCK_CONTEXT); |
| 100 | + verify(this.target).createContext("user", "password", 1); |
| 101 | + verifyNoMoreInteractions(this.target); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void createContextWithSessionModeWhenThreadLevelAuthentication() { |
| 106 | + this.adapter.setCredentialsForCurrentThread("user", "password"); |
| 107 | + given(this.target.createContext("user", "password", 1)).willReturn(MOCK_CONTEXT); |
| 108 | + assertThat(this.adapter.createContext(1)).isSameAs(MOCK_CONTEXT); |
| 109 | + verify(this.target).createContext("user", "password", 1); |
| 110 | + verifyNoMoreInteractions(this.target); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void createContextWithSessionModeWhenAuthenticationAndThreadLevelAuthentication() { |
| 115 | + this.adapter.setCredentialsForCurrentThread("specific", "secret"); |
| 116 | + this.adapter.setUsername("user"); |
| 117 | + this.adapter.setPassword("password"); |
| 118 | + given(this.target.createContext("specific", "secret", 1)).willReturn(MOCK_CONTEXT); |
| 119 | + assertThat(this.adapter.createContext(1)).isSameAs(MOCK_CONTEXT); |
| 120 | + verify(this.target).createContext("specific", "secret", 1); |
| 121 | + verifyNoMoreInteractions(this.target); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void createContextWithUsernamePasswordIgnoresAuthentication() { |
| 126 | + this.adapter.setUsername("user"); |
| 127 | + this.adapter.setPassword("password"); |
| 128 | + given(this.target.createContext("specific", "secret")).willReturn(MOCK_CONTEXT); |
| 129 | + assertThat(this.adapter.createContext("specific", "secret")).isSameAs(MOCK_CONTEXT); |
| 130 | + verify(this.target).createContext("specific", "secret"); |
| 131 | + verifyNoMoreInteractions(this.target); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void createContextWithSessionModeAndUsernamePasswordIgnoresAuthentication() { |
| 136 | + this.adapter.setUsername("user"); |
| 137 | + this.adapter.setPassword("password"); |
| 138 | + given(this.target.createContext("specific", "secret", 1)).willReturn(MOCK_CONTEXT); |
| 139 | + assertThat(this.adapter.createContext("specific", "secret", 1)).isSameAs(MOCK_CONTEXT); |
| 140 | + verify(this.target).createContext("specific", "secret", 1); |
| 141 | + verifyNoMoreInteractions(this.target); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments