2 // StronglyTypedResourceBuilderResxFileTests.cs - tests overloads of Create
3 // method that accept resx file names
6 // Gary Barnett (gary.barnett.mono@gmail.com)
8 // Copyright (C) Gary Barnett (2012)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
33 using System.Resources.Tools;
34 using Microsoft.CSharp;
37 using System.Resources;
40 namespace MonoTests.System.Resources.Tools {
42 public class StronglyTypedResourceBuilderResxFileTests {
43 CSharpCodeProvider provider = new CSharpCodeProvider ();
45 [Test, ExpectedException (typeof (ArgumentException))]
46 public void ResXFilenameEmpty ()
48 // in .NET framework throws exception
49 string [] unmatchables;
51 string resx = String.Empty;
53 StronglyTypedResourceBuilder.Create (resx,
56 "TestResourcesNameSpace",
61 /* FIXME: need platform dependant check, (file returns not found if invalid anyway)
62 [Test, ExpectedException (typeof (ArgumentException))]
63 public void ResXFilenameInvalid ()
65 // in .NET framework throws exception
66 string [] unmatchables;
68 string resx = @"C::::\\\\Hello/World";
70 StronglyTypedResourceBuilder.Create (resx,
73 "TestResourcesNameSpace",
79 [Test, ExpectedException (typeof (ArgumentNullException))]
80 public void ResXFilenameNull ()
82 //should throw exception
83 string [] unmatchables;
87 StronglyTypedResourceBuilder.Create (resx,
90 "TestResourcesNameSpace",
96 [Test, ExpectedException (typeof (FileNotFoundException))]
97 public void ResXFileNotFound ()
99 // not documented on msdn but throws FileNotFoundException
100 string [] unmatchables;
102 //get a valid new filename and then make it not exist
103 string resx = Path.GetTempFileName ();
106 StronglyTypedResourceBuilder.Create (resx,
109 "TestResourcesNameSpace",
116 public void ResXFileNotResx ()
118 //***should throw exception but Not using ExpectedException as i want to delete temp file***
119 string [] unmatchables;
120 bool exceptionRaised = false;
122 string resx = Path.GetTempFileName();
125 StronglyTypedResourceBuilder.Create (resx,
128 "TestResourcesNameSpace",
132 } catch (Exception ex) {
133 exceptionRaised = true;
134 Assert.IsInstanceOfType (typeof (ArgumentException), ex);
136 Assert.IsTrue (exceptionRaised);
142 public void ResxFileProcessed ()
144 // resources in resx should be present in codecompileunit with correct property type
145 string [] unmatchables;
148 Bitmap bmp = new Bitmap (100, 100);
149 MemoryStream wav = new MemoryStream (1000);
151 string resxFileName = Path.GetTempFileName();
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);
160 ccu = StronglyTypedResourceBuilder.Create (resxFileName,
163 "TestResourcesNameSpace",
168 CodeMemberProperty cmp;
169 cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("astring", ccu);
170 Assert.IsNotNull (cmp);
171 Assert.AreEqual ("System.String", cmp.Type.BaseType);
173 cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("wav", ccu);
174 Assert.IsNotNull (cmp);
175 Assert.AreEqual ("System.IO.UnmanagedMemoryStream", cmp.Type.BaseType);
177 cmp = StronglyTypedResourceBuilderCodeDomTest.Get<CodeMemberProperty> ("bmp", ccu);
178 Assert.IsNotNull (cmp);
179 Assert.AreEqual ("System.Drawing.Bitmap", cmp.Type.BaseType);
182 File.Delete (resxFileName);