Skip to content

Commit 02c4323

Browse files
committed
Make UrlInfo immutable
1 parent 1a983cf commit 02c4323

File tree

2 files changed

+62
-50
lines changed

2 files changed

+62
-50
lines changed

Orm/Xtensive.Orm.Tests.Core/UrlInfoTest.cs

+43-26
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,53 @@
66

77
using NUnit.Framework;
88

9-
namespace Xtensive.Orm.Tests.Core
9+
namespace Xtensive.Orm.Tests.Core;
10+
11+
[TestFixture]
12+
public class UrlInfoTest
1013
{
11-
[TestFixture]
12-
public class UrlInfoTest
14+
[Test]
15+
public void CombinedTest()
1316
{
14-
[Test]
15-
public void CombinedTest()
16-
{
17-
UrlInfo a1 = UrlInfo.Parse("tcp://user:password@someHost:1000/someUrl/someUrl?someParameter=someValue&someParameter2=someValue2");
18-
UrlInfo a2 = UrlInfo.Parse("tcp://user:password@someHost:1000/someUrl/someUrl?someParameter=someValue&someParameter2=someValue2");
19-
UrlInfo aX = UrlInfo.Parse("tcp://user:password@someHost:1000/someUrl/someUrl?someParameter2=someValue2&someParameter=someValue");
20-
UrlInfo b = UrlInfo.Parse("tcp://user:password@someHost:1000/someUrl/someUrl");
17+
UrlInfo a1 = UrlInfo.Parse("tcp://user:password@someHost:1000/someUrl/someUrl?someParameter=someValue&someParameter2=someValue2");
18+
UrlInfo a2 = UrlInfo.Parse("tcp://user:password@someHost:1000/someUrl/someUrl?someParameter=someValue&someParameter2=someValue2");
19+
UrlInfo aX = UrlInfo.Parse("tcp://user:password@someHost:1000/someUrl/someUrl?someParameter2=someValue2&someParameter=someValue");
20+
UrlInfo b = UrlInfo.Parse("tcp://user:password@someHost:1000/someUrl/someUrl");
21+
22+
Assert.IsTrue(a1.GetHashCode()==a2.GetHashCode());
23+
Assert.IsTrue(a1.GetHashCode()!=b.GetHashCode());
2124

22-
Assert.IsTrue(a1.GetHashCode()==a2.GetHashCode());
23-
Assert.IsTrue(a1.GetHashCode()!=b.GetHashCode());
25+
Assert.IsTrue(a1.Equals(a2));
26+
Assert.IsFalse(a1.Equals(b));
27+
}
2428

25-
Assert.IsTrue(a1.Equals(a2));
26-
Assert.IsFalse(a1.Equals(b));
27-
}
29+
[Test]
30+
public void WithTest()
31+
{
32+
UrlInfo a1 = UrlInfo.Parse("tcp://user:password@someHost:1000/someUrl/someUrl?p3=v3&p4=v4&p1=v1&p2=v2");
33+
var a2 = a1 with {
34+
Port = 2000,
35+
Password = "xxx",
36+
Protocol = "unkProto",
37+
Resource = "other/resource",
38+
Params = new Dictionary<string, string> { { "a", "b" } }
39+
};
40+
Assert.AreEqual("unkProto://user:xxx@someHost:2000/other/resource?a=b", a2.ToString());
41+
Assert.IsTrue(a2.Equals(a2));
42+
Assert.IsFalse(a1.Equals(a2));
43+
var a3 = UrlInfo.Parse("unkProto://user:xxx@someHost:2000/other/resource?a=b");
44+
Assert.IsTrue(a2 == a3);
45+
}
2846

29-
[Test]
30-
public void WithTest()
31-
{
32-
UrlInfo a1 = UrlInfo.Parse("tcp://user:password@someHost:1000/someUrl/someUrl?p3=v3&p4=v4&p1=v1&p2=v2");
33-
var a2 = a1 with { Port = 2000 };
34-
Assert.AreEqual("tcp://user:password@someHost:2000/someUrl/someUrl?p1=v1&p2=v2&p3=v3&p4=v4", a2.ToString());
35-
Assert.IsTrue(a2.Equals(a2));
36-
Assert.IsFalse(a1.Equals(a2));
37-
var a3 = UrlInfo.Parse("tcp://user:password@someHost:2000/someUrl/someUrl?p1=v1&p2=v2&p3=v3&p4=v4");
38-
Assert.IsTrue(a2 == a3);
39-
}
47+
[Test]
48+
public void TestUrlProps()
49+
{
50+
var url = UrlInfo.Parse("sqlserver://int:[email protected]:51571/db");
51+
Assert.AreEqual("sqlserver", url.Protocol);
52+
Assert.AreEqual("int", url.User);
53+
Assert.AreEqual("xxx", url.Password);
54+
Assert.AreEqual("127.0.0.1", url.Host);
55+
Assert.AreEqual(51571, url.Port);
56+
Assert.AreEqual("db", url.Resource);
4057
}
4158
}

Orm/Xtensive.Orm/Orm/UrlInfo.cs

+19-24
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public string Url
112112
}
113113
return field;
114114
}
115-
private set;
115+
private init;
116116
}
117117

118118
/// <summary>
@@ -122,7 +122,7 @@ public string Url
122122
public string Protocol
123123
{
124124
[DebuggerStepThrough] get => field;
125-
set {
125+
init {
126126
field = value;
127127
Url = null;
128128
}
@@ -135,7 +135,7 @@ public string Protocol
135135
public bool Secure
136136
{
137137
[DebuggerStepThrough] get => field;
138-
set {
138+
init {
139139
field = value;
140140
Url = null;
141141
}
@@ -148,7 +148,7 @@ public bool Secure
148148
public string Host
149149
{
150150
[DebuggerStepThrough] get;
151-
set {
151+
init {
152152
field = value;
153153
Url = null;
154154
}
@@ -161,7 +161,7 @@ public string Host
161161
public int Port
162162
{
163163
[DebuggerStepThrough] get;
164-
set {
164+
init {
165165
field = value;
166166
Url = null;
167167
}
@@ -174,7 +174,7 @@ public int Port
174174
public string Resource
175175
{
176176
[DebuggerStepThrough] get;
177-
set {
177+
init {
178178
field = value;
179179
Url = null;
180180
}
@@ -187,7 +187,7 @@ public string Resource
187187
public string User
188188
{
189189
[DebuggerStepThrough] get => field;
190-
set {
190+
init {
191191
field = value;
192192
Url = null;
193193
}
@@ -200,7 +200,7 @@ public string User
200200
public string Password
201201
{
202202
[DebuggerStepThrough] get => field;
203-
set {
203+
init {
204204
field = value;
205205
Url = null;
206206
}
@@ -219,7 +219,7 @@ public IReadOnlyDictionary<string, string> Params
219219
{
220220
[DebuggerStepThrough]
221221
get => field;
222-
set {
222+
init {
223223
field = value;
224224
Url = null;
225225
}
@@ -237,13 +237,6 @@ public IReadOnlyDictionary<string, string> Params
237237
/// </remarks>
238238
/// <exception cref="ArgumentException">Specified <paramref name="url"/> is invalid (cannot be parsed).</exception>
239239
public static UrlInfo Parse(string url)
240-
{
241-
var result = new UrlInfo();
242-
Parse(url, result);
243-
return result;
244-
}
245-
246-
private static void Parse(string url, UrlInfo info)
247240
{
248241
try {
249242
string tUrl = url;
@@ -273,14 +266,16 @@ private static void Parse(string url, UrlInfo info)
273266
}
274267
}
275268

276-
info.User = UrlDecode(result.Result("${username}"));
277-
info.Password = UrlDecode(result.Result("${password}"));
278-
info.Resource = UrlDecode(result.Result("${resource}"));
279-
info.Host = UrlDecode(result.Result("${host}"));
280-
info.Protocol = UrlDecode(result.Result("${proto}"));
281-
info.Secure = !string.IsNullOrEmpty(result.Result("${secure}"));
282-
info.Port = @port;
283-
info.Params = parameters;
269+
return new UrlInfo {
270+
User = UrlDecode(result.Result("${username}")),
271+
Password = UrlDecode(result.Result("${password}")),
272+
Resource = UrlDecode(result.Result("${resource}")),
273+
Host = UrlDecode(result.Result("${host}")),
274+
Protocol = UrlDecode(result.Result("${proto}")),
275+
Secure = !string.IsNullOrEmpty(result.Result("${secure}")),
276+
Port = @port,
277+
Params = parameters
278+
};
284279
}
285280
catch (Exception e) when (!(e is ArgumentException or InvalidOperationException)) {
286281
throw Exceptions.InvalidUrl(url, "url");

0 commit comments

Comments
 (0)