[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Design / Test / System.Resources.Tools / StronglyTypedResourceBuilderResxFileTests.cs
1 //
2 // StronglyTypedResourceBuilderResxFileTests.cs - tests overloads of Create
3 // method that accept resx file names
4 // 
5 // Author:
6 //      Gary Barnett (gary.barnett.mono@gmail.com)
7 // 
8 // Copyright (C) Gary Barnett (2012)
9 //
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.Resources.Tools;
34 using Microsoft.CSharp;
35 using System.IO;
36 using System.CodeDom;
37 using System.Resources;
38 using System.Drawing;
39
40 namespace MonoTests.System.Resources.Tools {
41         [TestFixture]
42         public class StronglyTypedResourceBuilderResxFileTests  {       
43                 CSharpCodeProvider provider = new CSharpCodeProvider ();
44                 
45                 [Test, ExpectedException (typeof (ArgumentException))]
46                 public void ResXFilenameEmpty ()
47                 {
48                         // in .NET framework throws exception
49                         string [] unmatchables;
50
51                         string resx = String.Empty;
52                         
53                         StronglyTypedResourceBuilder.Create (resx,
54                                                                 "TestRes",
55                                                                 "TestNamespace",
56                                                                 "TestResourcesNameSpace",
57                                                                 provider,
58                                                                 true,
59                                                                 out unmatchables);
60                 }
61                 /* FIXME: need platform dependant check, (file returns not found if invalid anyway)
62                 [Test, ExpectedException (typeof (ArgumentException))]
63                 public void ResXFilenameInvalid ()
64                 {
65                         // in .NET framework throws exception
66                         string [] unmatchables;
67
68                         string resx = @"C::::\\\\Hello/World";
69                         
70                         StronglyTypedResourceBuilder.Create (resx,
71                                                                 "TestRes",
72                                                                 "TestNamespace",
73                                                                 "TestResourcesNameSpace",
74                                                                 provider,
75                                                                 true,
76                                                                 out unmatchables);
77                 }
78                 */
79                 [Test, ExpectedException (typeof (ArgumentNullException))]
80                 public void ResXFilenameNull ()
81                 {
82                         //should throw exception
83                         string [] unmatchables;
84
85                         string resx = null;
86                         
87                         StronglyTypedResourceBuilder.Create (resx,
88                                                                 "TestRes",
89                                                                 "TestNamespace",
90                                                                 "TestResourcesNameSpace",
91                                                                 provider,
92                                                                 true,
93                                                                 out unmatchables);
94                 }
95                 
96                 [Test, ExpectedException (typeof (FileNotFoundException))]
97                 public void ResXFileNotFound ()
98                 {
99                         // not documented on msdn but throws FileNotFoundException
100                         string [] unmatchables;
101
102                         //get a valid new filename and then make it not exist
103                         string resx = Path.GetTempFileName ();
104                         File.Delete (resx);
105                         
106                         StronglyTypedResourceBuilder.Create (resx,
107                                                                 "TestRes",
108                                                                 "TestNamespace",
109                                                                 "TestResourcesNameSpace",
110                                                                 provider,
111                                                                 true,
112                                                                 out unmatchables);
113                 }
114                 
115                 [Test]
116                 public void ResXFileNotResx ()
117                 {
118                         //***should throw exception but Not using ExpectedException as i want to delete temp file***
119                         string [] unmatchables;
120                         bool exceptionRaised = false;
121
122                         string resx = Path.GetTempFileName();
123                         
124                         try {
125                                 StronglyTypedResourceBuilder.Create (resx,
126                                                                         "TestRes",
127                                                                         "TestNamespace",
128                                                                         "TestResourcesNameSpace",
129                                                                         provider,
130                                                                         true,
131                                                                         out unmatchables);
132                         } catch (Exception ex) {
133                                 exceptionRaised = true;
134                                 Assert.IsInstanceOfType (typeof (ArgumentException), ex);
135                         } finally {
136                                 Assert.IsTrue (exceptionRaised);
137                                 File.Delete (resx);
138                         }
139                 }
140                 
141                 [Test]
142                 public void ResxFileProcessed ()
143                 {
144                         // resources in resx should be present in codecompileunit with correct property type
145                         string [] unmatchables;
146                         CodeCompileUnit ccu;
147                         
148                         Bitmap bmp = new Bitmap (100, 100);
149                         MemoryStream wav = new MemoryStream (1000);
150                         
151                         string resxFileName = Path.GetTempFileName();
152                         
153                         using (ResXResourceWriter writer = new ResXResourceWriter(resxFileName)) {
154                                 writer.AddResource ("astring", "myvalue"); // dont use key of "string" as its a keyword
155                                 writer.AddResource ("bmp", bmp);
156                                 writer.AddResource ("wav", wav);
157                                 writer.Generate ();
158                         }
159                         
160                         ccu = StronglyTypedResourceBuilder.Create (resxFileName,
161                                                                 "TestRes",
162                                                                 "TestNamespace",
163                                                                 "TestResourcesNameSpace",
164                                                                 provider,
165                                                                 true,
166                                                                 out unmatchables);
167                         
168                         CodeMemberProperty cmp;
169                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("astring", ccu);
170                         Assert.IsNotNull (cmp);
171                         Assert.AreEqual ("System.String", cmp.Type.BaseType);
172                         
173                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("wav", ccu);
174                         Assert.IsNotNull (cmp);
175                         Assert.AreEqual ("System.IO.UnmanagedMemoryStream", cmp.Type.BaseType);
176                         
177                         cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("bmp", ccu);
178                         Assert.IsNotNull (cmp);
179                         Assert.AreEqual ("System.Drawing.Bitmap", cmp.Type.BaseType);
180                         
181                         wav.Close ();
182                         File.Delete (resxFileName);
183                 }
184         }
185 }
186