Author: spouliot
Date: 2008-01-21 09:17:36 -0500 (Mon, 21 Jan 2008)
New Revision: 93388

Added:
   
trunk/cecil/gendarme/rules/Gendarme.Rules.Ui/Test/UseSTAThreadAttributeOnSWFEntryPointsTest.cs
Modified:
   trunk/cecil/gendarme/rules/Gendarme.Rules.Ui/Test/ChangeLog
Log:
2008-01-21  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * UseSTAThreadAttributeOnSWFEntryPointsTest.cs: New. Unit tests 
        from Daniel Abramov (with a bit more coverage by myself).



Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Ui/Test/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Ui/Test/ChangeLog 2008-01-21 
14:12:21 UTC (rev 93387)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Ui/Test/ChangeLog 2008-01-21 
14:17:36 UTC (rev 93388)
@@ -1,3 +1,8 @@
+2008-01-21  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * UseSTAThreadAttributeOnSWFEntryPointsTest.cs: New. Unit tests 
+       from Daniel Abramov (with a bit more coverage by myself).
+
 2007-10-07  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * Test.Rules.Ui.mdp: Update project file.

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.Ui/Test/UseSTAThreadAttributeOnSWFEntryPointsTest.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.Ui/Test/UseSTAThreadAttributeOnSWFEntryPointsTest.cs
      2008-01-21 14:12:21 UTC (rev 93387)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.Ui/Test/UseSTAThreadAttributeOnSWFEntryPointsTest.cs
      2008-01-21 14:17:36 UTC (rev 93388)
@@ -0,0 +1,171 @@
+// 
+// Unit tests for UseSTAThreadAttributeOnSWFEntryPointsTest
+//
+// Authors:
+//     Daniel Abramov <[EMAIL PROTECTED]>
+//     Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Daniel Abramov
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to 
deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using System.Collections;
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+
+using Gendarme.Framework;
+using Gendarme.Rules.UI;
+
+using NUnit.Framework;
+
+namespace Test.Rules.UI {
+       internal class CommonMainClass { }
+
+       internal class NoAttributesMain : CommonMainClass {
+               public static void Main ()
+               {
+               }
+       }
+
+       internal class STAThreadMain : CommonMainClass {
+               [STAThread]
+               public static void Main ()
+               {
+               }
+       }
+
+       internal class MTAThreadMain : CommonMainClass {
+               [MTAThread]
+               public static void Main ()
+               {
+               }
+       }
+
+       internal class BothSTAAndMTAThreadMain : CommonMainClass {
+               [STAThread]
+               [MTAThread]
+               public static void Main ()
+               {
+               }
+       }
+
+       [TestFixture]
+       public class UseSTAThreadAttributeOnSWFEntryPointsTest {
+
+               private UseSTAThreadAttributeOnSWFEntryPointsRule rule;
+               private AssemblyDefinition assembly;
+               private Runner runner;
+
+               [TestFixtureSetUp]
+               public void FixtureSetUp ()
+               {
+                       rule = new UseSTAThreadAttributeOnSWFEntryPointsRule ();
+                       string unit = 
System.Reflection.Assembly.GetExecutingAssembly ().Location;
+                       assembly = AssemblyFactory.GetAssembly (unit);
+                       runner = new MinimalRunner ();
+               }
+
+               private AssemblyDefinition GetAssemblyAndInject<TInjectedType> 
(bool SWF)
+                       where TInjectedType : CommonMainClass
+               {
+                       // return executable assembly with predefined entry 
point - Main () of TInjectedType
+                       string fullClassName = typeof (TInjectedType).FullName;
+                       AssemblyDefinition ass = AssemblyFactory.DefineAssembly 
(typeof (TInjectedType).Name + "Assembly", AssemblyKind.Console);
+                       if (SWF) {
+                               ass.Kind = AssemblyKind.Windows;
+                               AssemblyNameReference winFormsRef = new 
AssemblyNameReference ();
+                               winFormsRef.Name = "System.Windows.Forms";
+                               winFormsRef.PublicKeyToken = new byte [] { 
0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89 };
+                               winFormsRef.Version = new Version (2, 0, 0, 0);
+                               ass.MainModule.AssemblyReferences.Add 
(winFormsRef);
+                       }
+                       TypeDefinition mainClass = ass.MainModule.Inject 
(assembly.MainModule.Types [fullClassName]);
+                       ass.EntryPoint = GetMethod (mainClass, "Main");
+                       return ass;
+               }
+
+               private MethodDefinition GetMethod (TypeDefinition type, string 
name)
+               {
+                       foreach (MethodDefinition method in type.Methods) {
+                               if (method.Name == name)
+                                       return method;
+                       }
+                       return null;
+               }
+
+               [Test]
+               public void TestNoEntryPoint ()
+               {
+                       MessageCollection messages = rule.CheckAssembly 
(assembly, runner);
+                       Assert.IsNull (messages);
+               }
+
+               [Test]
+               public void TestNoTANonSWFAssembly ()
+               {
+                       MessageCollection messages = rule.CheckAssembly 
(GetAssemblyAndInject<NoAttributesMain> (false), runner);
+                       Assert.IsNull (messages);
+               }
+
+               [Test]
+               public void TestNoTASWFAssembly ()
+               {
+                       MessageCollection messages = rule.CheckAssembly 
(GetAssemblyAndInject<NoAttributesMain> (true), runner);
+                       Assert.IsNotNull (messages);
+               }
+
+               [Test]
+               public void TestSTANonSWFAssembly ()
+               {
+                       MessageCollection messages = rule.CheckAssembly 
(GetAssemblyAndInject<STAThreadMain> (false), runner);
+                       Assert.IsNull (messages);
+               }
+
+               [Test]
+               public void TestMTANonSWFAssembly ()
+               {
+                       MessageCollection messages = rule.CheckAssembly 
(GetAssemblyAndInject<MTAThreadMain> (false), runner);
+                       Assert.IsNull (messages);
+               }
+
+               [Test]
+               public void TestSTAThreadSWFAssembly ()
+               {
+                       MessageCollection messages = rule.CheckAssembly 
(GetAssemblyAndInject<STAThreadMain> (true), runner);
+                       Assert.IsNull (messages);
+               }
+
+               [Test]
+               public void TestMTAThreadSWFAssembly ()
+               {
+                       MessageCollection messages = rule.CheckAssembly 
(GetAssemblyAndInject<MTAThreadMain> (true), runner);
+                       Assert.IsNotNull (messages);
+               }
+
+               [Test]
+               public void TestSTAAndMTAThreadSWFAssembly ()
+               {
+                       MessageCollection messages = rule.CheckAssembly 
(GetAssemblyAndInject<BothSTAAndMTAThreadMain> (true), runner);
+                       Assert.IsNotNull (messages);
+               }
+       }
+}

_______________________________________________
Mono-patches maillist  -  Mono-patches@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to