Pārlūkot izejas kodu

2010-05-07 Marek Habersack <[email protected]>

	* BuildManager.cs: implemented 4.0 methods {Create,Read}CacheFile.
	Fixed signature of the GetVirtualPathDependencies method.

2010-05-07  Marek Habersack  <[email protected]>

	* BuildManagerCacheFiles.cs: added - tests for
	BuildManager.{Create,Read}CacheFile

svn path=/trunk/mcs/; revision=156941
Marek Habersack 15 gadi atpakaļ
vecāks
revīzija
2d0ca9037e

+ 29 - 4
mcs/class/System.Web/System.Web.Compilation/BuildManager.cs

@@ -475,7 +475,28 @@ namespace System.Web.Compilation
 			codeDomProviders.Add (type, ret);
 			return ret;
 		}
-		
+#if NET_4_0
+		public static Stream CreateCachedFile (string fileName)
+		{
+			if (fileName != null && (fileName == String.Empty || fileName.IndexOf (Path.DirectorySeparatorChar) != -1))
+				throw new ArgumentException ("Value does not fall within the expected range.");
+
+			string path = Path.Combine (HttpRuntime.CodegenDir, fileName);
+			return new FileStream (path, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
+		}
+
+		public static Stream ReadCachedFile (string fileName)
+		{
+			if (fileName != null && (fileName == String.Empty || fileName.IndexOf (Path.DirectorySeparatorChar) != -1))
+				throw new ArgumentException ("Value does not fall within the expected range.");
+
+			string path = Path.Combine (HttpRuntime.CodegenDir, fileName);
+			if (!File.Exists (path))
+				return null;
+			
+			return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.None);
+		}
+#endif
 		public static object CreateInstanceFromVirtualPath (string virtualPath, Type requiredBaseType)
 		{
 			return CreateInstanceFromVirtualPath (GetAbsoluteVirtualPath (virtualPath), requiredBaseType);
@@ -924,12 +945,12 @@ namespace System.Web.Compilation
 			return ret;
 		}
 
-		public static IDictionary <string, bool> GetVirtualPathDependencies (string virtualPath)
+		public static ICollection GetVirtualPathDependencies (string virtualPath)
 		{
 			return GetVirtualPathDependencies (virtualPath, null);
 		}
 
-		internal static IDictionary <string, bool> GetVirtualPathDependencies (string virtualPath, BuildProvider bprovider)
+		internal static ICollection GetVirtualPathDependencies (string virtualPath, BuildProvider bprovider)
 		{
 			BuildProvider provider = bprovider;
 			if (provider == null) {
@@ -942,7 +963,11 @@ namespace System.Web.Compilation
 			if (provider == null)
 				return null;
 			
-			return provider.ExtractDependencies ();
+			IDictionary <string, bool> deps =  provider.ExtractDependencies ();
+			if (deps == null)
+				return null;
+
+			return (ICollection)deps.Keys;
 		}
 
 		internal static bool HasCachedItemNoLock (string vp, out bool entryExists)

+ 5 - 0
mcs/class/System.Web/System.Web.Compilation/ChangeLog

@@ -1,3 +1,8 @@
+2010-05-07  Marek Habersack  <[email protected]>
+
+	* BuildManager.cs: implemented 4.0 methods {Create,Read}CacheFile.
+	Fixed signature of the GetVirtualPathDependencies method.
+
 2010-04-29  Marek Habersack  <[email protected]>
 
 	* AppResourcesAssemblyBuilder.cs: when running on windows,

+ 2 - 0
mcs/class/System.Web/System.Web_standalone_test.dll.sources

@@ -5,3 +5,5 @@ Test/standalone-tests/RequestValidator.cs
 Test/standalone-tests/RequestValidatorTestGenerated.cs
 Test/standalone-tests/SiteMapDuplicateEntries_Bug570194.cs
 Test/standalone-tests/Unhandled_Exception_Global_Asax.cs
+Test/standalone-tests/BuildManagerCacheFiles.cs
+

+ 158 - 0
mcs/class/System.Web/Test/standalone-tests/BuildManagerCacheFiles.cs

@@ -0,0 +1,158 @@
+//
+// Authors:
+//   Marek Habersack ([email protected])
+//
+// (C) 2010 Novell, Inc http://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.Generic;
+using System.Configuration;
+using System.Configuration.Provider;
+using System.IO;
+using System.Web;
+using System.Web.Hosting;
+
+using StandAloneRunnerSupport;
+using StandAloneTests;
+
+using NUnit.Framework;
+
+namespace StandAloneTests.BuildManagerCacheFiles
+{
+	[TestCase ("BuildManagerCacheFiles 01", "Tests for BuildManager.{Create,Read}CacheFile")]
+	public sealed class BuildManagerCacheFiles_01 : ITestCase
+	{
+		static string[] expectedMessages = {
+			"create[1]: codeGen",
+			"create[1]: fileStream",
+			"create[1]: can read",
+			"create[1]: can write",
+			"create[1]: pathSubdirOfCodeGen",
+			"create[1]: our file name",
+			"read[1]: codeGen",
+			"read[1]: fileStream",
+			"read[1]: can read",
+			"read[1]: cannot write",
+			"read[1]: pathSubdirOfCodeGen",
+			"read[1]: our file name",
+			"read[1]: contents ok",
+			
+			"create[2]: codeGen",
+
+			// .NET exception:
+			//
+			// System.ArgumentException: Value does not fall within the expected range.
+			// at System.Web.Compilation.BuildManager.GetUserCacheFilePath(String fileName)
+			// at System.Web.Compilation.BuildManager.CreateCachedFile(String fileName)
+			// at _Default.RunTest(String fileName, String logTag, List`1 messages, Boolean noCreate) in c:\Users\grendel\Documents\Visual Studio 2010\Websites\BuildManager4.0\Default.aspx.cs:line 56)
+			"create[2]: error write (System.ArgumentException)",
+			"read[2]: codeGen",
+
+			// .NET exception:
+			//
+			// System.ArgumentException: Value does not fall within the expected range.
+			// at System.Web.Compilation.BuildManager.GetUserCacheFilePath(String fileName)
+			// at System.Web.Compilation.BuildManager.ReadCachedFile(String fileName)
+			// at _Default.RunTest(String fileName, String logTag, List`1 messages, Boolean noCreate) in c:\Users\grendel\Documents\Visual Studio 2010\Websites\BuildManager4.0\Default.aspx.cs:line 86)
+			"read[2]: error read (System.ArgumentException)",
+			
+			"read[3]: codeGen",
+			"read[3]: stream is null",
+
+			"create[4]: codeGen",
+
+			// .NET exception
+			// System.ArgumentNullException: Value cannot be null.
+			// Parameter name: path2
+			// at System.IO.Path.Combine(String path1, String path2)
+			// at System.Web.Compilation.BuildManager.GetUserCacheFilePath(String fileName)
+			// at System.Web.Compilation.BuildManager.CreateCachedFile(String fileName)
+			// at _Default.RunTest(String fileName, String logTag, List`1 messages, Boolean noCreate) in c:\Users\grendel\Documents\Visual Studio 2010\Websites\BuildManager4.0\Default.aspx.cs:line 61)
+			"create[4]: error write (System.ArgumentNullException)",
+			"read[4]: codeGen",
+
+			// .NET exception
+			// System.ArgumentNullException: Value cannot be null.
+			// Parameter name: path2
+			// at System.IO.Path.Combine(String path1, String path2)
+			// at System.Web.Compilation.BuildManager.GetUserCacheFilePath(String fileName)
+			// at System.Web.Compilation.BuildManager.ReadCachedFile(String fileName)
+			// at _Default.RunTest(String fileName, String logTag, List`1 messages, Boolean noCreate) in c:\Users\grendel\Documents\Visual Studio 2010\Websites\BuildManager4.0\Default.aspx.cs:line 91)
+			"read[4]: error read (System.ArgumentNullException)",
+			
+			"create[5]: codeGen",
+
+			// .NET exception
+			// System.ArgumentException: Value does not fall within the expected range.
+			// at System.Web.Compilation.BuildManager.GetUserCacheFilePath(String fileName)
+			// at System.Web.Compilation.BuildManager.CreateCachedFile(String fileName)
+			// at _Default.RunTest(String fileName, String logTag, List`1 messages, Boolean noCreate) in c:\Users\grendel\Documents\Visual Studio 2010\Websites\BuildManager4.0\Default.aspx.cs:line 61)
+			"create[5]: error write (System.ArgumentException)",
+			
+			"read[5]: codeGen",
+
+			// .NET exception
+			// System.ArgumentException: Value does not fall within the expected range.
+			// at System.Web.Compilation.BuildManager.GetUserCacheFilePath(String fileName)
+			// at System.Web.Compilation.BuildManager.ReadCachedFile(String fileName)
+			// at _Default.RunTest(String fileName, String logTag, List`1 messages, Boolean noCreate) in c:\Users\grendel\Documents\Visual Studio 2010\Websites\BuildManager4.0\Default.aspx.cs:line 91)
+			"read[5]: error read (System.ArgumentException)",
+		};
+		
+		public string PhysicalPath {
+			get {
+				return Path.Combine (
+					Consts.BasePhysicalDir,
+					"BuildManagerCacheFiles"
+				);
+			}
+		}
+		
+		public string VirtualPath  {
+			get { return "/"; }
+		}
+
+		public bool SetUp (List <TestRunItem> runItems)
+		{
+			runItems.Add (new TestRunItem ("/Default.aspx", Default_Aspx));
+			
+			return true;
+		}
+
+		void Default_Aspx (string result, TestRunItem runItem)
+		{
+			var messages = runItem.TestRunData as List <string>;
+
+			Assert.IsNotNull (messages, "#A1");
+
+			int len = messages.Count;
+			int i = 0;
+			for (; i < len; i++)
+				Assert.AreEqual (expectedMessages [i], messages [i], "#A2-" + i.ToString ());
+
+			if (i != len)
+				Assert.Fail ("Expected {0} messages, found {1}", i, len);
+		}
+	}
+}

+ 5 - 0
mcs/class/System.Web/Test/standalone-tests/ChangeLog

@@ -1,3 +1,8 @@
+2010-05-07  Marek Habersack  <[email protected]>
+
+	* BuildManagerCacheFiles.cs: added - tests for
+	BuildManager.{Create,Read}CacheFile
+
 2010-04-02  Marek Habersack  <[email protected]>
 
 	* Unhandled_Exception_Global_Asax.cs: added - tests for bug

+ 27 - 0
mcs/class/System.Web/Test/standalone/BuildManagerCacheFiles/Default.aspx

@@ -0,0 +1,27 @@
+<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head runat="server">
+    <title></title>
+</head>
+<body>
+    <form id="form1" runat="server">
+    <div>Create:</div>
+    <div>
+    CodeGenDir: <span runat="server" id="codeGenCreate" /><br />
+    File path: <span runat="server" id="filePathCreate" /><br />
+    </div>
+
+    <div>Read:</div>
+    <div>
+    CodeGenDir: <span runat="server" id="codeGenRead" /><br />
+    File path: <span runat="server" id="filePathRead" /><br />
+    </div>
+
+    <div>Log:</div>
+    <pre runat="server" id="log" />
+    </form>
+</body>
+</html>

+ 123 - 0
mcs/class/System.Web/Test/standalone/BuildManagerCacheFiles/Default.aspx.cs

@@ -0,0 +1,123 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using System.Web;
+using System.Web.Compilation;
+using System.Web.UI;
+using System.Web.UI.WebControls;
+
+public partial class _Default : System.Web.UI.Page
+{
+	protected void Page_Load (object sender, EventArgs e)
+	{
+		List<string> messages = new List<string> ();
+		AppDomain ad = AppDomain.CurrentDomain;
+
+		try {
+			RunTest ("myFile.cache", "1", messages);
+
+			// Subdirs not allowed
+			RunTest ("subdir/myFile.cache", "2", messages);
+
+			// File doesn't exist
+			RunTest ("myAnotherFile.cache", "3", messages, true);
+
+			//
+			RunTest (null, "4", messages);
+
+			RunTest (String.Empty, "5", messages);
+
+			var sb = new StringBuilder ();
+			foreach (string s in messages)
+				sb.AppendLine (s);
+
+			log.InnerText = sb.ToString ();
+		} finally {
+			ad.SetData ("TestRunData", messages);
+		}
+	}
+
+	void Log (List<string> messages, string format, params object [] parms)
+	{
+		if (parms == null || parms.Length == 0)
+			messages.Add (format);
+		else
+			messages.Add (String.Format (format, parms));
+	}
+
+	void RunTest (string fileName, string logTag, List <string> messages, bool noCreate = false)
+	{
+		string codeGenDir = null;
+		if (!noCreate) {
+			try {
+				codeGenCreate.InnerText = codeGenDir = HttpRuntime.CodegenDir;
+				Log (messages, "create[{0}]: codeGen", logTag);
+			} catch (Exception ex) {
+				Log (messages, "create[{0}]: error codeGen ({1})", logTag, ex.GetType ());
+			}
+
+			try {
+				using (FileStream st = BuildManager.CreateCachedFile (fileName) as FileStream) {
+					if (st != null) {
+						string path = st.Name;
+						Log (messages, "create[{0}]: fileStream", logTag);
+						filePathCreate.InnerText = path;
+
+						Log (messages, "create[{0}]: can{1} read", logTag, st.CanRead ? String.Empty : "not");
+						Log (messages, "create[{0}]: can{1} write", logTag, st.CanWrite ? String.Empty : "not");
+
+						if (codeGenDir != null && path.StartsWith (codeGenDir))
+							Log (messages, "create[{0}]: pathSubdirOfCodeGen", logTag);
+
+						if (Path.GetFileName (path) == fileName)
+							Log (messages, "create[{0}]: our file name", logTag);
+						using (var sw = new StreamWriter (st)) {
+							sw.Write ("test");
+						}
+					} else
+						Log (messages, "create[{0}]: stream is null", logTag);
+				}
+			} catch (Exception ex) {
+				Log (messages, "create[{0}]: error write ({1})", logTag, ex.GetType ());
+			}
+		}
+
+		try {
+			codeGenRead.InnerText = codeGenDir = HttpRuntime.CodegenDir;
+			Log (messages, "read[{0}]: codeGen", logTag);
+		} catch (Exception ex) {
+			Log (messages, "read[{0}]: error codeGen ({1})", logTag, ex.GetType ());
+		}
+
+		try {
+			using (FileStream st = BuildManager.ReadCachedFile (fileName) as FileStream) {
+				if (st != null) {
+					string path = st.Name;
+					Log (messages, "read[{0}]: fileStream", logTag);
+					filePathRead.InnerText = path;
+
+					Log (messages, "read[{0}]: can{1} read", logTag, st.CanRead ? String.Empty : "not");
+					Log (messages, "read[{0}]: can{1} write", logTag, st.CanWrite ? String.Empty : "not");
+
+					if (codeGenDir != null && path.StartsWith (codeGenDir))
+						Log (messages, "read[{0}]: pathSubdirOfCodeGen", logTag);
+
+					if (Path.GetFileName (path) == fileName)
+						Log (messages, "read[{0}]: our file name", logTag);
+
+					string contents;
+					using (var sr = new StreamReader (st)) {
+						contents = sr.ReadToEnd ();
+					}
+
+					if (contents != null && contents == "test")
+						Log (messages, "read[{0}]: contents ok", logTag);
+				} else
+					Log (messages, "read[{0}]: stream is null", logTag);
+			}
+		} catch (Exception ex) {
+			Log (messages, "read[{0}]: error read ({1})", logTag, ex.GetType ());
+		}
+	}
+}

+ 10 - 0
mcs/class/System.Web/Test/standalone/BuildManagerCacheFiles/web.config

@@ -0,0 +1,10 @@
+<?xml version="1.0"?>
+<!--
+  For more information on how to configure your ASP.NET application, please visit
+  http://go.microsoft.com/fwlink/?LinkId=169433
+  -->
+<configuration>
+	<system.web>
+		<compilation debug="true" targetFramework="4.0"/>
+	</system.web>
+</configuration>