|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Text; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Apache.NMS; |
| 7 | +using FakeItEasy; |
| 8 | +using NUnit.Framework; |
| 9 | + |
| 10 | +namespace Spring.Messaging.Nms.Connections |
| 11 | +{ |
| 12 | + [TestFixture] |
| 13 | + public class UserCredentialsConnectionFactoryAdapterTests |
| 14 | + { |
| 15 | + |
| 16 | + [Test] |
| 17 | + public void CreateConnectionWithoutCredentialsWhenTheyAreNotSet() |
| 18 | + { |
| 19 | + var underlyingConnectionFactory = A.Fake<IConnectionFactory>(); |
| 20 | + var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory); |
| 21 | + |
| 22 | + connectionFactory.CreateConnection(); |
| 23 | + |
| 24 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappenedOnceExactly(); |
| 25 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection(A<string>._, A<string>._)).MustHaveHappened(0, Times.Exactly); |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + [Test] |
| 30 | + public void CreateConnectionWithoutCredentialsWhenLoginIsNotSet() |
| 31 | + { |
| 32 | + var underlyingConnectionFactory = A.Fake<IConnectionFactory>(); |
| 33 | + var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory); |
| 34 | + connectionFactory.Password = "Secret"; |
| 35 | + connectionFactory.CreateConnection(); |
| 36 | + |
| 37 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappenedOnceExactly(); |
| 38 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection(A<string>._, A<string>._)).MustHaveHappened(0, Times.Exactly); |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public void CreateConnectionWithCredentialsWhenTheyAreSet() |
| 43 | + { |
| 44 | + var underlyingConnectionFactory = A.Fake<IConnectionFactory>(); |
| 45 | + var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory); |
| 46 | + connectionFactory.UserName = "SampleUser"; |
| 47 | + connectionFactory.Password = "Secret"; |
| 48 | + connectionFactory.CreateConnection(); |
| 49 | + |
| 50 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly); |
| 51 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Secret")).MustHaveHappenedOnceExactly(); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + [Test] |
| 56 | + public void SetConnectionCredentialsOnlyForCurrentThread() |
| 57 | + { |
| 58 | + var underlyingConnectionFactory = A.Fake<IConnectionFactory>(); |
| 59 | + var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory); |
| 60 | + |
| 61 | + // Call CreateConnection on thread that also called SetCredentialsForCurrentThread |
| 62 | + MultithreadingTestHelper.RunOnSeparateThread(() => |
| 63 | + { |
| 64 | + connectionFactory.SetCredentialsForCurrentThread("SampleUser", "Password"); |
| 65 | + connectionFactory.CreateConnection(); |
| 66 | + |
| 67 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly); |
| 68 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Password")).MustHaveHappenedOnceExactly(); |
| 69 | + }).Wait(); |
| 70 | + |
| 71 | + // Call CreateConnection on thread that didn't callSetCredentialsForCurrentThread |
| 72 | + MultithreadingTestHelper.RunOnSeparateThread(() => |
| 73 | + { |
| 74 | + connectionFactory.CreateConnection(); |
| 75 | + |
| 76 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappenedOnceExactly(); |
| 77 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection(A<string>._, A<string>._)).MustHaveHappenedOnceExactly(); |
| 78 | + }).Wait(); |
| 79 | + |
| 80 | + |
| 81 | + // Call CreateConnection on the main thread |
| 82 | + connectionFactory.CreateConnection(); |
| 83 | + |
| 84 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(2, Times.Exactly); |
| 85 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection(A<string>._, A<string>._)).MustHaveHappenedOnceExactly(); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public void InheritCredentialsIfTheyAreNotSetForSpecificThread() |
| 90 | + { |
| 91 | + var underlyingConnectionFactory = A.Fake<IConnectionFactory>(); |
| 92 | + var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory); |
| 93 | + |
| 94 | + |
| 95 | + connectionFactory.UserName = "SampleUser"; |
| 96 | + connectionFactory.Password = "Secret"; |
| 97 | + |
| 98 | + // Call CreateConnection on thread that also called SetCredentialsForCurrentThread |
| 99 | + MultithreadingTestHelper.RunOnSeparateThread(() => |
| 100 | + { |
| 101 | + connectionFactory.CreateConnection(); |
| 102 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly); |
| 103 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Secret")).MustHaveHappenedOnceExactly(); |
| 104 | + |
| 105 | + |
| 106 | + connectionFactory.SetCredentialsForCurrentThread("ThreadSampleUser", "ThreadPassword"); |
| 107 | + connectionFactory.CreateConnection(); |
| 108 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly); |
| 109 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection("ThreadSampleUser", "ThreadPassword")).MustHaveHappenedOnceExactly(); |
| 110 | + |
| 111 | + connectionFactory.RemoveCredentialsFromCurrentThread(); |
| 112 | + connectionFactory.CreateConnection(); |
| 113 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly); |
| 114 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Secret")).MustHaveHappened(2, Times.Exactly); |
| 115 | + }).Wait(); |
| 116 | + |
| 117 | + connectionFactory.CreateConnection(); |
| 118 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly); |
| 119 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Secret")).MustHaveHappened(3, Times.Exactly); |
| 120 | + } |
| 121 | + |
| 122 | + [Test] |
| 123 | + public void SetDifferentCredentialsOnDifferentThreads() |
| 124 | + { |
| 125 | + var underlyingConnectionFactory = A.Fake<IConnectionFactory>(); |
| 126 | + var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory); |
| 127 | + |
| 128 | + connectionFactory.UserName = "SampleUser"; |
| 129 | + connectionFactory.Password = "Secret"; |
| 130 | + |
| 131 | + var barrier = new Barrier(2); |
| 132 | + |
| 133 | + // Call CreateConnection on thread that also called SetCredentialsForCurrentThread |
| 134 | + var thread1 = MultithreadingTestHelper.RunOnSeparateThread(() => |
| 135 | + { |
| 136 | + connectionFactory.SetCredentialsForCurrentThread("Thread1SampleUser", "Password"); |
| 137 | + barrier.SignalAndWait(); |
| 138 | + |
| 139 | + connectionFactory.CreateConnection(); |
| 140 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly); |
| 141 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection("Thread1SampleUser", "Password")).MustHaveHappenedOnceExactly(); |
| 142 | + }); |
| 143 | + |
| 144 | + // Call CreateConnection on thread that didn't callSetCredentialsForCurrentThread |
| 145 | + var thread2 = MultithreadingTestHelper.RunOnSeparateThread(() => |
| 146 | + { |
| 147 | + connectionFactory.SetCredentialsForCurrentThread("Thread2SampleUser", "Password"); |
| 148 | + barrier.SignalAndWait(); |
| 149 | + |
| 150 | + connectionFactory.CreateConnection(); |
| 151 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly); |
| 152 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection("Thread2SampleUser", "Password")).MustHaveHappenedOnceExactly(); |
| 153 | + }); |
| 154 | + |
| 155 | + |
| 156 | + connectionFactory.CreateConnection(); |
| 157 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly); |
| 158 | + A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Secret")).MustHaveHappenedOnceExactly(); |
| 159 | + |
| 160 | + thread1.Wait(); |
| 161 | + thread2.Wait(); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments