lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject [Lucene.Net] svn commit: r1099613 - in /incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core: Lucene.Net.Test.csproj PartiallyTrustedAppDomain.cs TestSupportClass.cs
Date Wed, 04 May 2011 21:34:41 GMT
Author: digy
Date: Wed May  4 21:34:41 2011
New Revision: 1099613

URL: http://svn.apache.org/viewvc?rev=1099613&view=rev
Log:
[LUCENENET-412] Reorganized Medium Trust test cases.

Modified:
    incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Lucene.Net.Test.csproj
    incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/PartiallyTrustedAppDomain.cs
    incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/TestSupportClass.cs

Modified: incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Lucene.Net.Test.csproj
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Lucene.Net.Test.csproj?rev=1099613&r1=1099612&r2=1099613&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Lucene.Net.Test.csproj (original)
+++ incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/Lucene.Net.Test.csproj Wed May
 4 21:34:41 2011
@@ -96,6 +96,7 @@
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\..\lib\ICSharpCode\SharpZipLib\0.85\ICSharpCode.SharpZipLib.dll</HintPath>
     </Reference>
+    <Reference Include="Microsoft.CSharp" />
     <Reference Include="nunit.framework, Version=2.5.9.10348, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77,
processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\..\lib\NUnit.org\NUnit\2.5.9\bin\net-2.0\framework\nunit.framework.dll</HintPath>

Modified: incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/PartiallyTrustedAppDomain.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/PartiallyTrustedAppDomain.cs?rev=1099613&r1=1099612&r2=1099613&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/PartiallyTrustedAppDomain.cs
(original)
+++ incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/PartiallyTrustedAppDomain.cs
Wed May  4 21:34:41 2011
@@ -18,6 +18,7 @@
 
 using System;
 using System.IO;
+using System.Dynamic;
 using System.Security;
 using System.Security.Permissions;
 using System.Security.Policy;
@@ -29,45 +30,59 @@ using System.Text;
 
 namespace Lucene.Net.Test
 {
-    public class PartiallyTrustedAppDomain
+    public class PartiallyTrustedAppDomain<T> : DynamicObject, IDisposable where T
: MarshalByRefObject
     {
-        public static string TEMPDIR = @"c:\temp\testindex";
-        /// <summary>
-        /// 
-        /// </summary>
-        /// <param name="clazz">class to create</param>
-        /// <param name="methodName">method to invoke</param>
-        /// <param name="constructorArgs">constructor's parameters</param>
-        /// <param name="methodArgs">method's parameters</param>
-        public static object Run(Type clazz, string methodName, object[] constructorArgs,
object[] methodArgs)
+        string      _TempDir;
+        AppDomain   _AppDomain;
+        Type        _ClassType;
+        object      _RealObject;
+
+        public PartiallyTrustedAppDomain()
         {
-            AppDomain appDomain = null;
-            try
-            {
-                AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory
};
+            Init(null);
+        }
+
+        public PartiallyTrustedAppDomain(params object[] constructorArgs)
+        {
+            Init(constructorArgs);
+        }
+
+
+        void Init(object[] constructorArgs)
+        {
+            _TempDir = System.Environment.GetEnvironmentVariable("TEMP");
+
+            _ClassType = typeof(T);
+
+            AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory
};
+
+            PermissionSet permissions = new PermissionSet(null);
+            permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
+            permissions.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess));
+            permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.AllAccess,
TempDir));
+
+            _AppDomain = AppDomain.CreateDomain("PartiallyTrustedAppDomain", null, setup,
permissions);
 
-                PermissionSet permissions = new PermissionSet(null);
-                permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
-                permissions.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess));
-                permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.AllAccess,
TEMPDIR));
-
-                appDomain = AppDomain.CreateDomain("PartiallyTrustedAppDomain", null, setup,
permissions);
-
-                object obj = appDomain.CreateInstanceAndUnwrap(
-                    clazz.Assembly.FullName,
-                    clazz.FullName,
+            _RealObject = _AppDomain.CreateInstanceAndUnwrap(
+                    _ClassType.Assembly.FullName,
+                    _ClassType.FullName,
                     false,
                     BindingFlags.CreateInstance,
                     null,
                     constructorArgs,
                     System.Globalization.CultureInfo.CurrentCulture,
                     null);
+        }
 
-                object ret = clazz.InvokeMember(
+        object Run(string methodName, object[] constructorArgs, object[] methodArgs)
+        {
+            try
+            {
+                object ret = _ClassType.InvokeMember(
                     methodName,
                     BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public
| BindingFlags.NonPublic,
                     null,
-                    obj,
+                    _RealObject,
                     methodArgs);
 
                 return ret;
@@ -76,15 +91,31 @@ namespace Lucene.Net.Test
             {
                 throw tiex.InnerException;
             }
-            catch (Exception ex)
+        }
+
+        public string TempDir
+        {
+            get { return _TempDir; }
+        }
+
+        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out
object result)
+        {
+            try
             {
-                throw ex;
+                result = Run(binder.Name, null, args);
+                return true;
             }
-            finally
+            catch (MissingMethodException)
             {
-                if(appDomain!=null) AppDomain.Unload(appDomain);
+                result = null;
+                return false;
             }
         }
+
+        public void Dispose()
+        {
+            if (_AppDomain != null) AppDomain.Unload(_AppDomain);
+        }
     }
 }
 

Modified: incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/TestSupportClass.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/TestSupportClass.cs?rev=1099613&r1=1099612&r2=1099613&view=diff
==============================================================================
--- incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/TestSupportClass.cs (original)
+++ incubator/lucene.net/branches/Lucene.Net_2_9_4g/test/core/TestSupportClass.cs Wed May
 4 21:34:41 2011
@@ -17,6 +17,7 @@
 
 using System;
 using System.Collections;
+using System.Dynamic;
 using System.Threading;
 
 using Lucene.Net.Index;
@@ -1207,39 +1208,59 @@ namespace Lucene.Net._SupportClass
     }
 
     [TestFixture]
-    [Serializable]
     public class TestMediumTrust 
     {
+        dynamic _PartiallyTrustedClass;
+
+        public TestMediumTrust()
+        {
+            _PartiallyTrustedClass = new Lucene.Net.Test.PartiallyTrustedAppDomain<TestMethodsContainer>();

+        }
+        
+        [TestFixtureTearDown]
+        public void TestFixtureTearDown()
+        {
+            _PartiallyTrustedClass.Dispose();
+        }
+
+
         [Test]
         public void TestIndexAndSearch()
         {
-            Lucene.Net.Test.PartiallyTrustedAppDomain.Run(typeof(TestMethodContainer), "TestIndexAndSearch",
null, null);
+            string tempIndexDir = System.IO.Path.Combine(_PartiallyTrustedClass.TempDir,
"testindex");
+            try
+            {
+                _PartiallyTrustedClass.TestIndexAndSearch(tempIndexDir);
+            }
+            finally
+            {
+                if(System.IO.Directory.Exists(tempIndexDir))
+                    System.IO.Directory.Delete(tempIndexDir,true);
+            }
         }
 
         [Test]
         public void Test_Index_Term()
         {
-
-            Lucene.Net.Test.PartiallyTrustedAppDomain.Run(typeof(TestMethodContainer), "Test_Index_Term",
null, null);
+            _PartiallyTrustedClass.Test_Index_Term();
         }
 
         [Test]
         public void Test_Search_NumericRangeQuery()
         {
-            Lucene.Net.Test.PartiallyTrustedAppDomain.Run(typeof(TestMethodContainer), "Test_Search_NumericRangeQuery",
null, null);
+            _PartiallyTrustedClass.Test_Search_NumericRangeQuery();
         }
         
         [Test]
         public void Test_Search_SortField()
         {
-            //invoke any method
-            Lucene.Net.Test.PartiallyTrustedAppDomain.Run(typeof(TestMethodContainer), "Test_Search_SortField",
null, null);
+            _PartiallyTrustedClass.Test_Search_SortField();
         }
 
         [Test]
         public void Test_AlreadyClosedException()
         {
-            Lucene.Net.Test.PartiallyTrustedAppDomain.Run(typeof(TestMethodContainer), "Test_AlreadyClosedException",
null, null);
+            _PartiallyTrustedClass.Test_AlreadyClosedException();
         }
 
         [Test]
@@ -1247,7 +1268,7 @@ namespace Lucene.Net._SupportClass
         {
             try
             {
-                Lucene.Net.Test.PartiallyTrustedAppDomain.Run(typeof(TestMethodContainer),
"Test_AlreadyClosedException_Serialization", null, null);
+                _PartiallyTrustedClass.Test_AlreadyClosedException_Serialization();
             }
             catch (System.Security.SecurityException)
             {
@@ -1258,8 +1279,7 @@ namespace Lucene.Net._SupportClass
         [Test]
         public void Test_Util_Parameter()
         {
-            //invoke any method
-            Lucene.Net.Test.PartiallyTrustedAppDomain.Run(typeof(TestMethodContainer), "Test_Util_Parameter",
null, null);
+            _PartiallyTrustedClass.Test_Util_Parameter();
         }
         
         [Test]
@@ -1267,16 +1287,7 @@ namespace Lucene.Net._SupportClass
         {
             try
             {
-                Lucene.Net.Test.PartiallyTrustedAppDomain.Run(typeof(DummyClass), "DummyMethod",
null, null);
-                Assert.Fail("This call must fail-1");
-            }
-            catch(TypeLoadException)
-            {
-            }
-
-            try
-            {
-                Lucene.Net.Test.PartiallyTrustedAppDomain.Run(typeof(TestMethodContainer),
"MethodToFail", null, null);
+                _PartiallyTrustedClass.MethodToFail();
                 Assert.Fail("This call must fail-2");
             }
             catch (System.Security.SecurityException)
@@ -1285,24 +1296,11 @@ namespace Lucene.Net._SupportClass
         }
 
 
-        public class DummyClass: System.Runtime.Serialization.IObjectReference
+        public class TestMethodsContainer : MarshalByRefObject
         {
-            public void DummyMethod()
+            void TestIndexAndSearch(string tempDir)
             {
-            }
-
-            public object GetRealObject(System.Runtime.Serialization.StreamingContext context)
-            {
-                return this;
-            }
-        }
-
-
-        public class TestMethodContainer : MarshalByRefObject
-        {
-            void TestIndexAndSearch()
-            {
-                Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(Lucene.Net.Test.PartiallyTrustedAppDomain.TEMPDIR));
+                Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(tempDir));
 
                 Lucene.Net.Index.IndexWriter w = new Lucene.Net.Index.IndexWriter(dir, new
Lucene.Net.Analysis.Standard.StandardAnalyzer(), true);
                 Lucene.Net.Documents.Field f1 = new Lucene.Net.Documents.Field("field1",
"dark side of the moon", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED);
@@ -1376,13 +1374,11 @@ namespace Lucene.Net._SupportClass
             [Serializable]
             public class PARAM : Lucene.Net.Util.Parameter
             {
-                public PARAM(string field)
-                    : base(field)
+                public PARAM(string field) : base(field)
                 {
                 }
             }
-
-
+            
             string MethodToFail()
             {
                 return System.Environment.GetEnvironmentVariable("TEMP");



Mime
View raw message