This repository was archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPropertyBagTests.cs
134 lines (122 loc) · 4.42 KB
/
PropertyBagTests.cs
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
/*
* ====================
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of the Common Development
* and Distribution License("CDDL") (the "License"). You may not use this file
* except in compliance with the License.
*
* You can obtain a copy of the License at
* http://opensource.org/licenses/cddl1.php
* See the License for the specific language governing permissions and limitations
* under the License.
*
* When distributing the Covered Code, include this CDDL Header Notice in each file
* and include the License file at http://opensource.org/licenses/cddl1.php.
* If applicable, add the following below this CDDL Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
* ====================
* Portions Copyrighted 2014 ForgeRock AS.
*/
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Org.IdentityConnectors.Test.Common;
using Org.IdentityConnectors.Common;
namespace FrameworkTests
{
[TestFixture]
public class PropertyBagTests
{
private PropertyBag bag = null;
[SetUp]
public void SetUp()
{
bag = CreateBag();
}
[Test]
public void TestGetProperty()
{
Assert.AreEqual("value1", bag.GetProperty<string>("key1"));
Assert.IsNull(bag.GetProperty<string>("key2"));
Assert.AreEqual((int?)1, bag.GetProperty<int?>("key3"));
Assert.AreEqual((long?)1, bag.GetProperty<long?>("key5"));
// try not existing
try
{
bag.GetProperty<string>("key4");
Assert.Fail("Get Property must fail for unexisting property");
}
catch (ArgumentException)
{
}
// Try cast
try
{
bag.GetProperty<long?>("key3");
Assert.Fail("Get Property with incompatible type must fail on InvalidCastException");
}
catch (InvalidCastException)
{
}
}
[Test]
public void TestGetPropertyWithDef()
{
Assert.AreEqual("value1", bag.GetProperty<string>("key1", "def"));
Assert.IsNull(bag.GetProperty<string>("key2", "def"));
Assert.AreEqual("def", bag.GetProperty<string>("key4", "def"));
Assert.IsNull(bag.GetProperty<string>("key4", null));
}
[Test]
public void TestGetStringProperty()
{
Assert.AreEqual("value1", bag.GetStringProperty("key1"));
Assert.IsNull(bag.GetStringProperty("key2"));
// Try cast
try
{
bag.GetStringProperty("key3");
Assert.Fail("Get Property with incompatible type must fail on InvalidCastException");
}
catch (InvalidCastException)
{
}
// try not existing
try
{
bag.GetStringProperty("key4");
Assert.Fail("Get String Property must fail for unexisting property");
}
catch (ArgumentException)
{
}
}
[Test]
public void TestToDictionary()
{
var properties = bag.ToDictionary();
Assert.That(CollectionUtil.DictionariesEqual(properties, CreateDictionary()),
"ToDictionary must return the same properties as it was created with" );
}
private static PropertyBag CreateBag()
{
var properties = CreateDictionary();
return new PropertyBag(properties);
}
private static Dictionary<string, object> CreateDictionary()
{
var properties = new Dictionary<string, object>
{
{"key1", "value1"},
{"key2", null},
{"key3", (int?) 1},
{"key5", (long?) 1}
};
return properties;
}
}
}