New test.
[mono.git] / mcs / class / System / Test / System / GenericUriParserTest.cs
1 //
2 // UriParserTest.cs - Unit tests for System.UriParser
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using NUnit.Framework;
32
33 using System;
34
35 namespace MonoTests.System {
36
37         public class UnitTestGenericUriParser: GenericUriParser {
38
39                 static bool registered;
40
41                 public UnitTestGenericUriParser ()
42                         : base (GenericUriParserOptions.Default)
43                 {
44                 }
45
46                 public static bool Registered {
47                         get {
48                                 return registered;
49                         }
50                 }
51
52                 protected override string GetComponents (Uri uri, UriComponents components, UriFormat format)
53                 {
54                         return base.GetComponents (uri, components, format);
55                 }
56
57                 protected override void InitializeAndValidate (Uri uri, out UriFormatException parsingError)
58                 {
59                         base.InitializeAndValidate (uri, out parsingError);
60                 }
61
62                 protected override bool IsBaseOf (Uri baseUri, Uri relativeUri)
63                 {
64                         return base.IsBaseOf (baseUri, relativeUri);
65                 }
66
67                 protected override bool IsWellFormedOriginalString (Uri uri)
68                 {
69                         return base.IsWellFormedOriginalString (uri);
70                 }
71
72                 protected override UriParser OnNewUri ()
73                 {
74                         return base.OnNewUri ();
75                 }
76
77                 protected override void OnRegister (string schemeName, int defaultPort)
78                 {
79                         registered = true;
80                         // try to mess up registration
81                         base.OnRegister (schemeName, 4040);
82                         base.OnRegister ("s" + schemeName, 4444);
83                 }
84
85                 protected override string Resolve (Uri baseUri, Uri relativeUri, out UriFormatException parsingError)
86                 {
87                         return base.Resolve (baseUri, relativeUri, out parsingError);
88                 }
89         }
90
91         [TestFixture]
92         public class GenericUriParserTest {
93
94                 private UnitTestGenericUriParser parser;
95
96                 [TestFixtureSetUp]
97                 public void FixtureSetUp ()
98                 {
99                         parser = new UnitTestGenericUriParser ();
100                         // unit tests are being reused in CAS tests
101                         if (!UriParser.IsKnownScheme ("generic"))
102                                 UriParser.Register (parser, "generic", 1);
103
104                         Assert.IsTrue (UnitTestGenericUriParser.Registered, "Registered");
105                         // our parser code was called
106                 }
107
108                 [Test]
109                 public void Generic ()
110                 {
111                         Uri uri = new Uri ("generic://www.mono-project.com/");
112                         Assert.AreEqual (1, uri.Port, "Port");
113                 }
114
115                 [Test]
116                 [Category ("NotWorking")]
117                 public void Generic_Methods ()
118                 {
119                         Uri uri = new Uri ("generic://www.mono-project.com/");
120                         Assert.AreEqual (String.Empty, uri.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped), "GetComponents");
121                         Assert.IsTrue (uri.IsBaseOf (uri), "IsBaseOf");
122                         Assert.IsTrue (uri.IsWellFormedOriginalString (), "IsWellFormedOriginalString");
123                 }
124
125                 [Test]
126                 public void SecureGeneric ()
127                 {
128                         Uri uri = new Uri ("sgenericx://www.mono-project.com/");
129                         Assert.AreEqual (-1, uri.Port, "Port");
130                         // OnRegister cannot be used to change the registering informations
131                 }
132
133                 [Test]
134                 public void AllOptions ()
135                 {
136                         for (int i = 0; i < 512; i++) {
137                                 GenericUriParserOptions gupo = (GenericUriParserOptions) i;
138                                 Assert.IsNotNull (new GenericUriParser (gupo), gupo.ToString ());
139                         }
140                 }
141
142                 [Test]
143                 public void InvalidOptions ()
144                 {
145                         Assert.IsNotNull (new GenericUriParser ((GenericUriParserOptions) 512), "512");
146                         Assert.IsNotNull (new GenericUriParser ((GenericUriParserOptions) Int32.MinValue), "Int32.MinValue");
147                         // there are no check for invalid values
148                 }
149         }
150 }
151
152 #endif