Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PackUriHelperTests.cs « System.IO.Packaging « Test « WindowsBase « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 04494fff118f41c19a2e83aa45c106f739e3f78c (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// PackUriHelperTests.cs created with MonoDevelop
// User: alan at 13:39 28/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using System.IO.Packaging;
using NUnit.Framework;

namespace System.IO.Packaging.Tests {
	
    [TestFixture]
    [Category ("NotWorking")]
    [Ignore ("This depends on a fix to System.Uri to support the UriParser API")]
    public class PackUriHelperTests {
        static void Main (string [] args)
        {
            PackUriHelperTests t = new PackUriHelperTests ();
            t.ResolvePartUri2 ();
        }


        Uri a;
        Uri b;
        Uri part1 = new Uri ("/file1", UriKind.Relative);
        Uri part2 = new Uri ("/file2", UriKind.Relative);
        Uri main = new Uri ("/main.html", UriKind.Relative);

		[SetUpAttribute]
		public void Setup()
		{
			a = PackUriHelper.Create (new Uri ("http://www.test.com/pack1.pkg"));
			b = PackUriHelper.Create (new Uri ("http://www.test.com/pack2.pkg"));
			Console.WriteLine ("A is: {0}", a);
			Console.WriteLine("B is: {0}", b);
		}
		
        [Test]
        public void ComparePackUriTest ()
        {
            Assert.AreEqual (0, PackUriHelper.ComparePackUri (null, null), "#1");
            Assert.IsTrue (PackUriHelper.ComparePackUri (a, null) > 0, "#2");
            Assert.AreEqual (0, PackUriHelper.ComparePackUri (a, a), "#3");
            Assert.IsTrue (PackUriHelper.ComparePackUri (a, b) < 0, "#4");
        }

        [Test]
        [ExpectedException(typeof(UriFormatException))]
        public void CompareInvalidTest ()
        {
            Uri a = new Uri ("pack://url1");
            PackUriHelper.ComparePackUri (a, a);
        }

        [Test]
        [ExpectedException (typeof (ArgumentException))]
        public void NonPackUriCompareTest ()
        {
            PackUriHelper.ComparePackUri (new Uri ("http://wtest.com"), a);
        }

        [Test]
        [ExpectedException (typeof (ArgumentException))]
        public void NonPackUriCompareRelativeTest ()
        {
            PackUriHelper.ComparePackUri (new Uri ("wtest.com", UriKind.Relative), a);
        }

        [Test]
        [ExpectedException (typeof (ArgumentException))]
        public void InvalidPartUriCompareTest ()
        {
            PackUriHelper.ComparePartUri (a, b);
        }

        [Test]
        public void PartUriCompareTest ()
        {
            Assert.AreEqual (0, PackUriHelper.ComparePartUri (null, null), "#1");
            Assert.IsTrue (PackUriHelper.ComparePartUri (part1, null) > 0, "#2");
            Assert.IsTrue (PackUriHelper.ComparePartUri (part1, part2) < 0, "#3");
        }

        [Test]
        public void CreateTest ()
        {
            Assert.AreEqual ("pack://http:,,www.test.com,pack.pkg/",
                             PackUriHelper.Create (new Uri ("http://www.test.com/pack.pkg")).ToString (), "#1");
            Assert.AreEqual ("pack://http:,,www.test.com,pack.pkg/",
                             PackUriHelper.Create (new Uri ("http://www.test.com/pack.pkg"), null, null).ToString (), "#2");
            Assert.AreEqual ("pack://http:,,www.test.com,pack.pkg/main.html#frag",
                             PackUriHelper.Create (new Uri ("http://www.test.com/pack.pkg"),
			                                       new Uri ("/main.html", UriKind.Relative), "#frag").ToString (), "#3");
            Assert.AreEqual ("pack://http:,,www.test.com,pack.pkg/main.html#frag",
                             PackUriHelper.Create (new Uri ("http://www.test.com/pack.pkg"),
			                                       new Uri ("/main.html", UriKind.Relative), "#frag").ToString (), "#3");
        }

        [Test]
        [ExpectedException (typeof (ArgumentException))]
        public void CreateInvalidTest ()
        {
            PackUriHelper.Create (new Uri ("http://www.test.com/pack.pkg"), new Uri ("/main.html", UriKind.Relative), "notfrag");
        }

        [Test]
        [ExpectedException (typeof (ArgumentException))]
        public void CreateInvalidTest2 ()
        {
            PackUriHelper.Create (new Uri ("http://www.test.com/pack.pkg"), new Uri ("/main.html", UriKind.Relative), "");
        }

        [Test]
        [ExpectedException (typeof (ArgumentException))]
        public void CreateInvalidTest3 ()
        {
            PackUriHelper.Create (new Uri ("http://www.test.com/pack.pkg"), new Uri ("/main.html", UriKind.Relative), "");
        }

        [Test]
        public void CreateInvalidTest4 ()
        {
            PackUriHelper.Create (new Uri ("http://www.test.com/pack.pkg"), new Uri ("/main.html", UriKind.Relative));
        }

        [Test]
        public void CreatePartUri ()
        {
            Assert.IsFalse (PackUriHelper.CreatePartUri (part1).IsAbsoluteUri, "#1");
            Assert.AreEqual (new Uri (part1.ToString (), UriKind.Relative), PackUriHelper.CreatePartUri (part1), "#2");
        }

        [Test]
        [ExpectedException (typeof (ArgumentNullException))]
        public void CreatePartUri2 ()
        {
            PackUriHelper.CreatePartUri (null);
        }

        [Test]
        public void GetNormalizedPartUriTest ()
        {
            Uri uri = new Uri ("/test.com".ToUpperInvariant (), UriKind.Relative);
            Assert.IsTrue (uri == PackUriHelper.GetNormalizedPartUri (uri));
        }
        [Test]
        public void GetNormalisedPartUritest4 ()
        {
            PackUriHelper.GetNormalizedPartUri (part1);
        }

        [Test]
        [ExpectedException (typeof (ArgumentNullException))]
        public void GetNormalizedPartUriTest2 ()
        {
            PackUriHelper.GetNormalizedPartUri (null);
        }

        [Test]
        [ExpectedException (typeof (UriFormatException))]
        public void GetNormalizedPartUriTest3 ()
        {
            Assert.AreEqual (new Uri (a.ToString ().ToUpperInvariant (), UriKind.Relative), PackUriHelper.GetNormalizedPartUri (a));
        }

        [Test]
        public void GetPackageUriTest ()
        {
            Assert.AreEqual (a, PackUriHelper.GetPackageUri (PackUriHelper.Create (a, new Uri ("/test.html", UriKind.Relative))));
        }

        [Test]
        [ExpectedException (typeof (ArgumentNullException))]
        public void GetPackageUri2 ()
        {
            PackUriHelper.GetPackageUri (null);
        }

        [Test]
        [ExpectedException (typeof (ArgumentException))]
        public void GetPackageUri3 ()
        {
            PackUriHelper.GetPackageUri (part1);
        }

        [Test]
        public void GetPartUriTest ()
        {
            Assert.IsNull (PackUriHelper.GetPartUri (a), "#1");
            Assert.AreEqual (main, PackUriHelper.GetPartUri (PackUriHelper.Create (a, main)), "#2");
        }

        [Test]
        [ExpectedException (typeof (ArgumentNullException))]
        public void GetPartUriTest2 ()
        {
            PackUriHelper.GetPartUri (null);
        }

        [Test]
        [ExpectedException (typeof (ArgumentException))]
        public void GetPartUriTest3 ()
        {
            PackUriHelper.GetPartUri (part1);
        }

        [Test]
        public void GetRelationshipPartUriTest ()
        {
            Assert.AreEqual ("/_rels/file1.rels", PackUriHelper.GetRelationshipPartUri (part1).ToString());
        }

        [Test]
        [ExpectedException (typeof (ArgumentNullException))]
        public void GetRelationshipPartUriTest2 ()
        {
            PackUriHelper.GetRelationshipPartUri (null);
        }

        [Test]
        public void GetRelativeUriTest ()
        {
            Uri src = new Uri ("/1/2/3/file.jpg", UriKind.Relative);
            Uri dest = new Uri ("/file2.png", UriKind.Relative);
            Assert.AreEqual (new Uri ("../../../file2.png", UriKind.Relative), PackUriHelper.GetRelativeUri (src, dest), "#1");

            dest = new Uri ("/1/2", UriKind.Relative);
            Assert.AreEqual (new Uri ("../../2", UriKind.Relative), PackUriHelper.GetRelativeUri (src, dest), "#2");

            dest = new Uri ("/1/2/", UriKind.Relative);
            Assert.AreEqual (new Uri ("", UriKind.Relative), PackUriHelper.GetRelativeUri (src, src), "#4");

            // See: http://msdn.microsoft.com/en-us/library/system.io.packaging.packurihelper.getrelativeuri.aspx

            src = new Uri("/mydoc/markup/page.xml", UriKind.Relative);
            dest = new Uri("/mydoc/markup/picture.jpg", UriKind.Relative);
            Assert.AreEqual (new Uri ("picture.jpg", UriKind.Relative), PackUriHelper.GetRelativeUri (src, dest), "#5");

            src = new Uri("/mydoc/markup/page.xml", UriKind.Relative);
            dest = new Uri("/mydoc/picture.jpg", UriKind.Relative);
            Assert.AreEqual (new Uri ("../picture.jpg", UriKind.Relative), PackUriHelper.GetRelativeUri (src, dest), "#6");

            src = new Uri("/mydoc/markup/page.xml", UriKind.Relative);
            dest = new Uri("/mydoc/images/picture.jpg", UriKind.Relative);
            Assert.AreEqual (new Uri ("../images/picture.jpg", UriKind.Relative), PackUriHelper.GetRelativeUri (src, dest), "#7");

        }

        [Test]
        [ExpectedException (typeof(ArgumentException))]
        public void GetRelativeUriTest2 ()
        {
            Uri src = new Uri ("/1/2/3/file.jpg", UriKind.Relative);
            Uri dest = new Uri ("/1/2/", UriKind.Relative);
            Assert.AreEqual (new Uri ("../file2.png", UriKind.Relative), PackUriHelper.GetRelativeUri (src, dest), "#3");
        }

        [Test]
        public void IsRelationshipPartUriTest ()
        {
            Assert.IsFalse (PackUriHelper.IsRelationshipPartUri (new Uri ("/_rels/Whatever", UriKind.Relative)));
            Assert.IsTrue (PackUriHelper.IsRelationshipPartUri (new Uri ("/_rels/Whatever.rels", UriKind.Relative)));
        }

        [Test]
        public void IsRelationshipPartUriTest2 ()
        {
            Uri uri = new Uri ("/test/uri", UriKind.Relative);
            PackUriHelper.IsRelationshipPartUri (uri);
        }

        [Test]
        [ExpectedException(typeof(UriFormatException))]
        public void ResolvePartUri ()
        {
            Uri src = new Uri ("/1/2/3/4", UriKind.Relative);
            Uri dest = new Uri ("/MyFile", UriKind.Relative);

            // Can't be empty url
            Assert.AreEqual (new Uri (""), PackUriHelper.ResolvePartUri (src, dest), "#1");
        }


        [Test]
        //[ExpectedException (typeof (UriFormatException))]
        public void ResolvePartUri2 ()
        {
            Uri src = new Uri ("/1/2/3/4", UriKind.Relative);
            Uri dest = new Uri ("/1/2/MyFile", UriKind.Relative);

            // Can't be empty url
            Assert.AreEqual (new Uri ("/1/2/MyFile", UriKind.Relative), PackUriHelper.ResolvePartUri (src, dest), "#1");
            Assert.AreEqual (new Uri ("/1/2/3/4", UriKind.Relative), PackUriHelper.ResolvePartUri (dest, src), "#1");
        }

        [Test]
        [ExpectedException (typeof (ArgumentException))]
        public void ResolvePartUri3 ()
        {
            Uri src = new Uri ("/1/2/3/4", UriKind.Relative);
            Uri dest = new Uri ("http://www.example.com", UriKind.Absolute);
            PackUriHelper.ResolvePartUri (src, dest);
        }

        [Test]
        public void ResolvePartUri4 ()
        {
            Uri src = new Uri ("/", UriKind.Relative);
            Uri dest = new Uri ("word/document.xml", UriKind.Relative);

            Uri result = PackUriHelper.ResolvePartUri (src, dest);
            Assert.IsFalse(result.IsAbsoluteUri, "#1");
            Assert.AreEqual ("/word/document.xml", result.ToString(), "#2");
        }
    }
}