PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/test/System.Web.WebPages.Deployment.Test/PreApplicationStartCodeTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 510 lines | 382 code | 81 blank | 47 comment | 4 complexity | ebdac3f39c7b76395f1aef5d6ec44302 MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.Collections.Specialized;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Web.WebPages.TestUtils;
  8. using Xunit;
  9. using Assert = Microsoft.TestCommon.AssertEx;
  10. namespace System.Web.WebPages.Deployment.Test
  11. {
  12. public class PreApplicationStartCodeTest
  13. {
  14. private const string DeploymentVersionFile = "System.Web.WebPages.Deployment";
  15. private static readonly Version MaxVersion = new Version(2, 0, 0, 0);
  16. [Fact]
  17. public void PreApplicationStartCodeDoesNothingIfWebPagesIsExplicitlyDisabled()
  18. {
  19. // Arrange
  20. Version loadedVersion = null;
  21. bool registeredForChangeNotification = false;
  22. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("1", "2");
  23. var fileSystem = new TestFileSystem();
  24. var buildManager = new TestBuildManager();
  25. var nameValueCollection = GetAppSettings(enabled: false, webPagesVersion: null);
  26. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  27. Action registerForChange = () => { registeredForChangeNotification = true; };
  28. // Act
  29. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", "bin", nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, null);
  30. // Assert
  31. Assert.False(loaded);
  32. Assert.Null(loadedVersion);
  33. Assert.False(registeredForChangeNotification);
  34. Assert.Equal(0, buildManager.Stream.Length);
  35. }
  36. [Fact]
  37. public void PreApplicationStartCodeUsesVersionSpecifiedInConfigIfWebPagesIsImplicitlyEnabled()
  38. {
  39. // Arrange
  40. Version loadedVersion = null;
  41. bool registeredForChangeNotification = false;
  42. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("1.12.123.1234", "2.0.0.0");
  43. Version webPagesVersion = new Version("1.12.123.1234");
  44. var fileSystem = new TestFileSystem();
  45. fileSystem.AddFile("Default.cshtml");
  46. var buildManager = new TestBuildManager();
  47. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: webPagesVersion);
  48. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  49. Action registerForChange = () => { registeredForChangeNotification = true; };
  50. // Act
  51. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", "bin", nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, getAssemblyNameThunk: null);
  52. // Assert
  53. Assert.True(loaded);
  54. Assert.Equal(webPagesVersion, loadedVersion);
  55. Assert.False(registeredForChangeNotification);
  56. VerifyVersionFile(buildManager, webPagesVersion);
  57. }
  58. [Fact]
  59. public void PreApplicationStartCodeDoesNotLoadCurrentWebPagesIfOnlyVersionIsListedInConfigAndNoFilesAreFoundInSiteRoot()
  60. {
  61. // Arrange
  62. Version loadedVersion = null;
  63. bool registeredForChangeNotification = false;
  64. Version webPagesVersion = AssemblyUtils.ThisAssemblyName.Version;
  65. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("2.0.0.0");
  66. var fileSystem = new TestFileSystem();
  67. var buildManager = new TestBuildManager();
  68. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: webPagesVersion);
  69. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  70. Action registerForChange = () => { registeredForChangeNotification = true; };
  71. // Arrange
  72. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", "bin", nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, null);
  73. // Assert
  74. Assert.False(loaded);
  75. Assert.Null(loadedVersion);
  76. Assert.True(registeredForChangeNotification);
  77. Assert.Equal(0, buildManager.Stream.Length);
  78. }
  79. [Fact]
  80. public void PreApplicationStartCodeRegistersForChangeNotificationIfNotExplicitlyDisabledAndNoFilesFoundInSiteRoot()
  81. {
  82. // Arrange
  83. Version loadedVersion = null;
  84. bool registeredForChangeNotification = false;
  85. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("2.0.0.0");
  86. var fileSystem = new TestFileSystem();
  87. var buildManager = new TestBuildManager();
  88. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: null);
  89. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  90. Action registerForChange = () => { registeredForChangeNotification = true; };
  91. // Act
  92. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", "bin", nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, null);
  93. // Assert
  94. Assert.False(loaded);
  95. Assert.Null(loadedVersion);
  96. Assert.True(registeredForChangeNotification);
  97. Assert.Equal(0, buildManager.Stream.Length);
  98. }
  99. [Fact]
  100. public void PreApplicationStartCodeDoesNothingIfV1IsAvailableInBinAndSiteIsExplicitlyEnabled()
  101. {
  102. // Arrange
  103. Version loadedVersion = null;
  104. bool registeredForChangeNotification = false;
  105. var v1Version = new Version("1.0.0.0");
  106. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("1.0.0.0", "2.0.0.0");
  107. var binDirectory = DeploymentUtil.GetBinDirectory();
  108. var fileSystem = new TestFileSystem();
  109. fileSystem.AddFile(Path.Combine(binDirectory, "System.Web.WebPages.Deployment.dll"));
  110. var buildManager = new TestBuildManager();
  111. var nameValueCollection = GetAppSettings(enabled: true, webPagesVersion: null);
  112. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  113. Action registerForChange = () => { registeredForChangeNotification = true; };
  114. Func<string, AssemblyName> getAssembyName = _ => new AssemblyName("System.Web.WebPages.Deployment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
  115. // Act
  116. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", binDirectory, nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, getAssembyName);
  117. // Assert
  118. Assert.False(loaded);
  119. Assert.Null(loadedVersion);
  120. Assert.False(registeredForChangeNotification);
  121. Assert.Equal(0, buildManager.Stream.Length);
  122. }
  123. [Fact]
  124. public void PreApplicationStartCodeDoesNothingIfV1IsAvailableInBinAndFileExistsInRootOfWebSite()
  125. {
  126. // Arrange
  127. Version loadedVersion = null;
  128. bool registeredForChangeNotification = false;
  129. var v1Version = new Version("1.0.0.0");
  130. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("1.0.0.0", "2.0.0.0");
  131. var binDirectory = DeploymentUtil.GetBinDirectory();
  132. var fileSystem = new TestFileSystem();
  133. var buildManager = new TestBuildManager();
  134. fileSystem.AddFile("Default.cshtml");
  135. fileSystem.AddFile(Path.Combine(binDirectory, "System.Web.WebPages.Deployment.dll"));
  136. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: null);
  137. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  138. Action registerForChange = () => { registeredForChangeNotification = true; };
  139. Func<string, AssemblyName> getAssembyName = _ => new AssemblyName("System.Web.WebPages.Deployment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
  140. // Act
  141. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", binDirectory, nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, getAssembyName);
  142. // Assert
  143. Assert.False(loaded);
  144. Assert.Null(loadedVersion);
  145. Assert.False(registeredForChangeNotification);
  146. Assert.Equal(0, buildManager.Stream.Length);
  147. }
  148. [Fact]
  149. public void PreApplicationStartCodeDoesNothingIfItIsAvailableInBinAndFileExistsInRootOfWebSite()
  150. {
  151. // Arrange
  152. Version loadedVersion = null;
  153. bool registeredForChangeNotification = false;
  154. var webPagesVersion = AssemblyUtils.ThisAssemblyName.Version;
  155. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies(AssemblyUtils.ThisAssemblyName.Version.ToString());
  156. var fileSystem = new TestFileSystem();
  157. var binDirectory = DeploymentUtil.GetBinDirectory();
  158. var buildManager = new TestBuildManager();
  159. fileSystem.AddFile("Default.vbhtml");
  160. fileSystem.AddFile(Path.Combine(binDirectory, "System.Web.WebPages.Deployment.dll"));
  161. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: null);
  162. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  163. Action registerForChange = () => { registeredForChangeNotification = true; };
  164. Func<string, AssemblyName> getAssembyName = _ => new AssemblyName("System.Web.WebPages.Deployment, Version=" + AssemblyUtils.ThisAssemblyName.Version.ToString() + ", Culture=neutral, PublicKeyToken=31bf3856ad364e35");
  165. // Act
  166. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", binDirectory, nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, getAssembyName);
  167. // Assert
  168. Assert.False(loaded);
  169. Assert.Null(loadedVersion);
  170. Assert.False(registeredForChangeNotification);
  171. Assert.Equal(0, buildManager.Stream.Length);
  172. }
  173. [Fact]
  174. public void PreApplicationStartCodeLoadsMaxVersionIfNoVersionIsSpecifiedAndCurrentAssemblyIsTheMaximumVersionAvailable()
  175. {
  176. // Arrange
  177. Version loadedVersion = null;
  178. bool registeredForChangeNotification = false;
  179. var webPagesVersion = AssemblyUtils.ThisAssemblyName.Version;
  180. var v1Version = new Version("1.0.0.0");
  181. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("1.0.0.0", "2.0.0.0");
  182. // Note: For this test to work with future versions we would need to create corresponding embedded resources with that version in it.
  183. var fileSystem = new TestFileSystem();
  184. var buildManager = new TestBuildManager();
  185. fileSystem.AddFile("Index.cshtml");
  186. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: null);
  187. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  188. Action registerForChange = () => { registeredForChangeNotification = true; };
  189. // Act
  190. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", "bin", nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, null);
  191. // Assert
  192. Assert.True(loaded);
  193. Assert.Equal(MaxVersion, loadedVersion);
  194. Assert.False(registeredForChangeNotification);
  195. VerifyVersionFile(buildManager, MaxVersion);
  196. }
  197. [Fact]
  198. public void PreApplicationStartCodeDoesNotLoadIfAHigherVersionIsAvailableInBin()
  199. {
  200. // Arrange
  201. Version loadedVersion = null;
  202. bool registeredForChangeNotification = false;
  203. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("2.0.0.0", "8.0.0.0");
  204. var binDirectory = DeploymentUtil.GetBinDirectory();
  205. var fileSystem = new TestFileSystem();
  206. fileSystem.AddFile("Index.cshtml");
  207. fileSystem.AddFile(Path.Combine(binDirectory, "System.Web.WebPages.Deployment.dll"));
  208. var buildManager = new TestBuildManager();
  209. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: null);
  210. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  211. Action registerForChange = () => { registeredForChangeNotification = true; };
  212. Func<string, AssemblyName> getAssembyName = _ => new AssemblyName("System.Web.WebPages.Deployment, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
  213. // Act
  214. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", binDirectory, nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, getAssembyName);
  215. // Assert
  216. Assert.False(loaded);
  217. Assert.Null(loadedVersion);
  218. Assert.False(registeredForChangeNotification);
  219. Assert.Equal(0, buildManager.Stream.Length);
  220. }
  221. [Fact]
  222. public void PreApplicationStartCodeDoesNotLoadIfAHigherVersionIsAvailableInGac()
  223. {
  224. // Arrange
  225. Version loadedVersion = null;
  226. bool registeredForChangeNotification = false;
  227. // Hopefully we'd have figured out a better way to load Plan9 by v8.
  228. var webPagesVersion = new Version("8.0.0.0");
  229. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("1.0.0.0", "2.0.0.0", "8.0.0.0");
  230. var fileSystem = new TestFileSystem();
  231. fileSystem.AddFile("Index.cshtml");
  232. var buildManager = new TestBuildManager();
  233. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: null);
  234. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  235. Action registerForChange = () => { registeredForChangeNotification = true; };
  236. // Act
  237. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", "bin", nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, null);
  238. // Assert
  239. Assert.False(loaded);
  240. Assert.Null(loadedVersion);
  241. Assert.False(registeredForChangeNotification);
  242. Assert.Equal(0, buildManager.Stream.Length);
  243. }
  244. [Fact]
  245. public void PreApplicationStartCodeForcesRecompileIfPreviousVersionIsNotTheSameAsCurrentVersion()
  246. {
  247. // Arrange
  248. Version loadedVersion = null;
  249. bool registeredForChangeNotification = false;
  250. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("2.0.0.0");
  251. var fileSystem = new TestFileSystem();
  252. fileSystem.AddFile("Index.cshtml");
  253. var buildManager = new TestBuildManager();
  254. var content = "1.0.0.0" + Environment.NewLine;
  255. buildManager.Stream = new MemoryStream(Encoding.Default.GetBytes(content));
  256. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: new Version("2.0.0.0"));
  257. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  258. Action registerForChange = () => { registeredForChangeNotification = true; };
  259. // Act
  260. var ex = Assert.Throws<HttpCompileException>(() =>
  261. PreApplicationStartCode.StartCore(fileSystem, "", @"site\bin", nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, null)
  262. );
  263. // Assert
  264. Assert.Equal("Changes were detected in the Web Pages runtime version that require your application to be recompiled. Refresh your browser window to continue.", ex.Message);
  265. Assert.Equal(ex.Data["WebPages.VersionChange"], true);
  266. Assert.False(registeredForChangeNotification);
  267. VerifyVersionFile(buildManager, new Version("2.0.0.0"));
  268. Assert.True(fileSystem.FileExists(@"site\bin\WebPagesRecompilation.deleteme"));
  269. }
  270. [Fact]
  271. public void PreApplicationStartCodeDoesNotForceRecompileIfNewVersionIsV1AndCurrentAssemblyIsNotMaxVersion()
  272. {
  273. // Arrange
  274. Version loadedVersion = null;
  275. bool registeredForChangeNotification = false;
  276. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("2.0.0.0", "5.0.0.0");
  277. var fileSystem = new TestFileSystem();
  278. fileSystem.AddFile("Index.cshtml");
  279. var buildManager = new TestBuildManager();
  280. var content = AssemblyUtils.ThisAssemblyName.Version + Environment.NewLine;
  281. buildManager.Stream = new MemoryStream(Encoding.Default.GetBytes(content));
  282. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: new Version("1.0.0"));
  283. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  284. Action registerForChange = () => { registeredForChangeNotification = true; };
  285. // Act
  286. bool loaded = PreApplicationStartCode.StartCore(fileSystem, "", @"site\bin", nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, null);
  287. // Assert
  288. Assert.False(loaded);
  289. Assert.False(registeredForChangeNotification);
  290. VerifyVersionFile(buildManager, AssemblyUtils.ThisAssemblyName.Version);
  291. Assert.False(fileSystem.FileExists(@"site\bin\WebPagesRecompilation.deleteme"));
  292. }
  293. [Fact]
  294. public void PreApplicationStartCodeThrowsIfWebPagesIsInBinAndDifferentVersionIsSpecifiedInConfig()
  295. {
  296. // Arrange
  297. Version loadedVersion = null;
  298. bool registeredForChangeNotification = false;
  299. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("1.0.0.0", "2.0.0.0");
  300. var binDirectory = DeploymentUtil.GetBinDirectory();
  301. var fileSystem = new TestFileSystem();
  302. fileSystem.AddFile("Index.cshtml");
  303. fileSystem.AddFile(Path.Combine(binDirectory, "System.Web.WebPages.Deployment.dll"));
  304. var buildManager = new TestBuildManager();
  305. var content = AssemblyUtils.ThisAssemblyName.Version + Environment.NewLine;
  306. buildManager.Stream = new MemoryStream(Encoding.Default.GetBytes(content));
  307. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: new Version("2.0.0"));
  308. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  309. Action registerForChange = () => { registeredForChangeNotification = true; };
  310. Func<string, AssemblyName> getAssembyName = _ => new AssemblyName("System.Web.WebPages.Deployment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
  311. // Act and Assert
  312. Assert.Throws<InvalidOperationException>(() =>
  313. PreApplicationStartCode.StartCore(fileSystem, "", binDirectory, nameValueCollection, loadedAssemblies, buildManager, loadWebPages, registerForChange, getAssembyName),
  314. @"Conflicting versions of ASP.NET Web Pages detected: specified version is ""2.0.0.0"", but the version in bin is ""1.0.0.0"". To continue, remove files from the application's bin directory or remove the version specification in web.config."
  315. );
  316. Assert.False(registeredForChangeNotification);
  317. Assert.Null(loadedVersion);
  318. }
  319. [Fact]
  320. public void PreApplicationStartCodeThrowsIfVersionIsSpecifiedInConfigAndDifferentVersionExistsInBin()
  321. {
  322. // Arrange
  323. Version loadedVersion = null;
  324. var binDirectory = DeploymentUtil.GetBinDirectory();
  325. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("1.0.0.0", AssemblyUtils.ThisAssemblyName.Version.ToString());
  326. var fileSystem = new TestFileSystem();
  327. fileSystem.AddFile("Index.cshtml");
  328. fileSystem.AddFile(Path.Combine(binDirectory, "System.Web.WebPages.Deployment.dll"));
  329. var buildManager = new TestBuildManager();
  330. var content = AssemblyUtils.ThisAssemblyName.Version + Environment.NewLine;
  331. buildManager.Stream = new MemoryStream(Encoding.Default.GetBytes(content));
  332. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: new Version("1.0.0"));
  333. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  334. Action registerForChange = () => { };
  335. Func<string, AssemblyName> getAssembyName = _ => new AssemblyName("System.Web.WebPages.Deployment, Version=" + AssemblyUtils.ThisAssemblyName.Version + ", Culture=neutral, PublicKeyToken=31bf3856ad364e35");
  336. // Act and Assert
  337. Assert.Throws<InvalidOperationException>(() =>
  338. PreApplicationStartCode.StartCore(fileSystem, "", binDirectory, nameValueCollection, loadedAssemblies, buildManager: buildManager, loadWebPages: loadWebPages, registerForChangeNotification: registerForChange, getAssemblyNameThunk: getAssembyName),
  339. String.Format(@"Conflicting versions of ASP.NET Web Pages detected: specified version is ""1.0.0.0"", but the version in bin is ""{0}"". To continue, remove files from the application's bin directory or remove the version specification in web.config.",
  340. AssemblyUtils.ThisAssemblyName.Version));
  341. }
  342. [Fact]
  343. public void PreApplicationStartCodeThrowsIfVersionSpecifiedInConfigIsNotAvailable()
  344. {
  345. // Arrange
  346. Version loadedVersion = null;
  347. var binDirectory = DeploymentUtil.GetBinDirectory();
  348. IEnumerable<AssemblyName> loadedAssemblies = GetAssemblies("1.0.0.0", AssemblyUtils.ThisAssemblyName.Version.ToString());
  349. var fileSystem = new TestFileSystem();
  350. fileSystem.AddFile("Index.cshtml");
  351. var buildManager = new TestBuildManager();
  352. var content = AssemblyUtils.ThisAssemblyName.Version + Environment.NewLine;
  353. buildManager.Stream = new MemoryStream(Encoding.Default.GetBytes(content));
  354. var nameValueCollection = GetAppSettings(enabled: null, webPagesVersion: new Version("1.5"));
  355. Action<Version> loadWebPages = (version) => { loadedVersion = version; };
  356. Action registerForChange = () => { };
  357. // Act and Assert
  358. Assert.Throws<InvalidOperationException>(() =>
  359. PreApplicationStartCode.StartCore(fileSystem, "", binDirectory, nameValueCollection, loadedAssemblies, buildManager: buildManager, loadWebPages: loadWebPages, registerForChangeNotification: registerForChange, getAssemblyNameThunk: null),
  360. String.Format("Specified Web Pages version \"1.5.0.0\" could not be found. Update your web.config to specify a different version. Current version: \"{0}\".",
  361. AssemblyUtils.ThisAssemblyName.Version));
  362. }
  363. [Fact]
  364. public void TestPreAppStartClass()
  365. {
  366. PreAppStartTestHelper.TestPreAppStartClass(typeof(PreApplicationStartCode));
  367. }
  368. private static NameValueCollection GetAppSettings(bool? enabled, Version webPagesVersion)
  369. {
  370. var nameValueCollection = new NameValueCollection();
  371. if (enabled.HasValue)
  372. {
  373. nameValueCollection["webpages:enabled"] = enabled.Value ? "true" : "false";
  374. }
  375. if (webPagesVersion != null)
  376. {
  377. nameValueCollection["webpages:version"] = webPagesVersion.ToString();
  378. }
  379. return nameValueCollection;
  380. }
  381. private static void VerifyVersionFile(TestBuildManager buildManager, Version webPagesVersion)
  382. {
  383. var content = Encoding.UTF8.GetString(buildManager.Stream.ToArray());
  384. Version version = Version.Parse(content);
  385. Assert.Equal(webPagesVersion, version);
  386. }
  387. private class TestBuildManager : IBuildManager
  388. {
  389. private MemoryStream _memoryStream = new MemoryStream();
  390. public MemoryStream Stream
  391. {
  392. get { return _memoryStream; }
  393. set { _memoryStream = value; }
  394. }
  395. public Stream CreateCachedFile(string fileName)
  396. {
  397. Assert.Equal(DeploymentVersionFile, fileName);
  398. CopyMemoryStream();
  399. return _memoryStream;
  400. }
  401. public Stream ReadCachedFile(string fileName)
  402. {
  403. Assert.Equal(DeploymentVersionFile, fileName);
  404. CopyMemoryStream();
  405. return _memoryStream;
  406. }
  407. /// <summary>
  408. /// Need to do this because the MemoryStream is read and written to in consecutive calls which causes it to be closed / non-expandable.
  409. /// </summary>
  410. private void CopyMemoryStream()
  411. {
  412. var content = _memoryStream.ToArray();
  413. if (content.Length > 0)
  414. {
  415. _memoryStream = new MemoryStream(_memoryStream.ToArray());
  416. }
  417. else
  418. {
  419. _memoryStream = new MemoryStream();
  420. }
  421. }
  422. }
  423. private static IEnumerable<AssemblyName> GetAssemblies(params string[] versions)
  424. {
  425. return from version in versions
  426. select new AssemblyName("System.Web.WebPages.Deployment, Version=" + version + ", Culture=neutral, PublicKeyToken=31bf3856ad364e35");
  427. }
  428. }
  429. }