5
5
using Microsoft . Toolkit . Uwp . Helpers ;
6
6
using Microsoft . VisualStudio . TestTools . UnitTesting ;
7
7
using System ;
8
- using System . Text . Json ;
9
- using UnitTests . UI ;
8
+ using Newtonsoft . Json ;
9
+ using UnitTests . UWP . Helpers ;
10
+ using System . Diagnostics . Contracts ;
10
11
11
12
namespace UnitTests . Helpers
12
13
{
13
14
[ TestClass ]
14
15
public class Test_StorageHelper
15
16
{
16
- private LocalObjectStorageHelper storageHelper = new LocalObjectStorageHelper ( ) ;
17
+ private LocalObjectStorageHelper _localStorageHelperSystem = new LocalObjectStorageHelper ( new SystemSerializer ( ) ) ;
18
+ private LocalObjectStorageHelper _localStorageHelperJsonCompat = new LocalObjectStorageHelper ( new JsonObjectSerializer ( ) ) ;
19
+
20
+ private LocalObjectStorageHelper _localStorageHelperJsonNew = new LocalObjectStorageHelper ( new SystemTextJsonSerializer ( ) ) ;
21
+
22
+ /// <summary>
23
+ /// Checks that we're running 10.0.3 version of Newtonsoft.Json package which we used in 6.1.1.
24
+ /// </summary>
25
+ [ TestCategory ( "Helpers" ) ]
26
+ [ TestMethod ]
27
+ public void Test_StorageHelper_CheckNewtonsoftVersion ( )
28
+ {
29
+ var version = typeof ( JsonSerializer ) . Assembly . GetName ( ) . Version ;
30
+ Assert . AreEqual ( 10 , version . Major ) ;
31
+ Assert . AreEqual ( 0 , version . Minor ) ;
32
+ Assert . AreEqual ( 0 , version . Revision ) ; // Apparently the file revision was not updated for the updated package
33
+ }
17
34
18
35
[ TestCategory ( "Helpers" ) ]
19
36
[ TestMethod ]
@@ -23,38 +40,45 @@ public void Test_StorageHelper_LegacyIntTest()
23
40
24
41
int input = 42 ;
25
42
26
- // simulate previous version by generating json and manually inserting it as string
27
- string jsonInput = JsonSerializer . Serialize ( input ) ;
28
-
29
- storageHelper . Save < string > ( key , jsonInput ) ;
43
+ // Use our previous Json layer to store value
44
+ _localStorageHelperJsonCompat . Save ( key , input ) ;
30
45
31
- // now read it as int to valid that the change works
32
- int output = storageHelper . Read < int > ( key , 0 ) ;
46
+ // But try and read from our new system to see if it works
47
+ int output = _localStorageHelperSystem . Read ( key , 0 ) ;
33
48
34
49
Assert . AreEqual ( input , output ) ;
35
50
}
36
51
37
- [ Ignore ]
52
+ /// <summary>
53
+ /// If we try and deserialize a complex type with the <see cref="SystemSerializer"/>, we do a check ourselves and will throw our own exception.
54
+ /// </summary>
38
55
[ TestCategory ( "Helpers" ) ]
39
56
[ TestMethod ]
40
- public void Test_StorageHelper_LegacyDateTest ( )
57
+ [ ExpectedException ( typeof ( NotSupportedException ) ) ]
58
+ public void Test_StorageHelper_LegacyDateTestFailure ( )
41
59
{
42
60
string key = "ChristmasDay" ;
43
61
44
62
DateTime input = new DateTime ( 2017 , 12 , 25 ) ;
45
63
46
- // simulate previous version by generating json and manually inserting it as string
47
- string jsonInput = JsonSerializer . Serialize ( input ) ;
48
-
49
- storageHelper . Save < string > ( key , jsonInput ) ;
64
+ _localStorageHelperJsonCompat . Save ( key , input ) ;
50
65
51
66
// now read it as int to valid that the change works
52
- DateTime output = storageHelper . Read < DateTime > ( key , DateTime . Today ) ;
67
+ DateTime output = _localStorageHelperSystem . Read ( key , DateTime . Today ) ;
68
+ }
53
69
54
- Assert . AreEqual ( input , output ) ;
70
+ /// <summary>
71
+ /// The <see cref="SystemSerializer"/> doesn't support complex types, since it just passes through directly.
72
+ /// We'll get the argument exception from the <see cref="ApplicationDataContainer"/> API.
73
+ /// </summary>
74
+ [ TestCategory ( "Helpers" ) ]
75
+ [ TestMethod ]
76
+ [ ExpectedException ( typeof ( ArgumentException ) ) ]
77
+ public void Test_StorageHelper_DateTestFailure ( )
78
+ {
79
+ _localStorageHelperSystem . Save ( "Today" , DateTime . Today ) ;
55
80
}
56
81
57
- [ Ignore ]
58
82
[ TestCategory ( "Helpers" ) ]
59
83
[ TestMethod ]
60
84
public void Test_StorageHelper_LegacyInternalClassTest ( )
@@ -64,12 +88,10 @@ public void Test_StorageHelper_LegacyInternalClassTest()
64
88
UI . Person input = new UI . Person ( ) { Name = "Joe Bloggs" , Age = 42 } ;
65
89
66
90
// simulate previous version by generating json and manually inserting it as string
67
- string jsonInput = JsonSerializer . Serialize ( input ) ;
68
-
69
- storageHelper . Save < string > ( key , jsonInput ) ;
91
+ _localStorageHelperJsonCompat . Save ( key , input ) ;
70
92
71
93
// now read it as int to valid that the change works
72
- UI . Person output = storageHelper . Read < UI . Person > ( key , null ) ;
94
+ UI . Person output = _localStorageHelperJsonCompat . Read < UI . Person > ( key , null ) ;
73
95
74
96
Assert . IsNotNull ( output ) ;
75
97
Assert . AreEqual ( input . Name , output . Name ) ;
@@ -82,15 +104,14 @@ public void Test_StorageHelper_LegacyPublicClassTest()
82
104
{
83
105
string key = "Contact" ;
84
106
107
+ // Here's we're serializing a different class which has the same properties as our other class below.
85
108
UI . Person input = new UI . Person ( ) { Name = "Joe Bloggs" , Age = 42 } ;
86
109
87
110
// simulate previous version by generating json and manually inserting it as string
88
- string jsonInput = JsonSerializer . Serialize ( input ) ;
89
-
90
- storageHelper . Save ( key , jsonInput ) ;
111
+ _localStorageHelperJsonCompat . Save ( key , input ) ;
91
112
92
113
// now read it as int to valid that the change works
93
- Person output = storageHelper . Read < Person > ( key , null ) ;
114
+ Person output = _localStorageHelperJsonCompat . Read < Person > ( key , null ) ;
94
115
95
116
Assert . IsNotNull ( output ) ;
96
117
Assert . AreEqual ( input . Name , output . Name ) ;
@@ -105,10 +126,10 @@ public void Test_StorageHelper_IntTest()
105
126
106
127
int input = 42 ;
107
128
108
- storageHelper . Save < int > ( key , input ) ;
129
+ _localStorageHelperSystem . Save < int > ( key , input ) ;
109
130
110
131
// now read it as int to valid that the change works
111
- int output = storageHelper . Read < int > ( key , 0 ) ;
132
+ int output = _localStorageHelperSystem . Read < int > ( key , 0 ) ;
112
133
113
134
Assert . AreEqual ( input , output ) ;
114
135
}
@@ -121,10 +142,10 @@ public void Test_StorageHelper_NewDateTest()
121
142
122
143
DateTime input = new DateTime ( 2017 , 12 , 25 ) ;
123
144
124
- storageHelper . Save < DateTime > ( key , input ) ;
145
+ _localStorageHelperJsonNew . Save ( key , input ) ;
125
146
126
147
// now read it as int to valid that the change works
127
- DateTime output = storageHelper . Read < DateTime > ( key , DateTime . Today ) ;
148
+ DateTime output = _localStorageHelperJsonNew . Read ( key , DateTime . Today ) ;
128
149
129
150
Assert . AreEqual ( input , output ) ;
130
151
}
@@ -137,10 +158,10 @@ public void Test_StorageHelper_NewPersonTest()
137
158
138
159
Person input = new Person ( ) { Name = "Joe Bloggs" , Age = 42 } ;
139
160
140
- storageHelper . Save < Person > ( key , input ) ;
161
+ _localStorageHelperJsonNew . Save ( key , input ) ;
141
162
142
163
// now read it as int to valid that the change works
143
- Person output = storageHelper . Read < Person > ( key , null ) ;
164
+ Person output = _localStorageHelperJsonNew . Read < Person > ( key , null ) ;
144
165
145
166
Assert . IsNotNull ( output ) ;
146
167
Assert . AreEqual ( input . Name , output . Name ) ;
0 commit comments