forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExistenceCacheTest.cs
More file actions
155 lines (132 loc) · 5.17 KB
/
Copy pathFileExistenceCacheTest.cs
File metadata and controls
155 lines (132 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using System.Threading;
using System.Web.Hosting;
using System.Web.WebPages.TestUtils;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.WebPages.Test
{
public class FileExistenceCacheTest
{
[Fact]
public void ConstructorTest()
{
var ms = 1000;
FileExistenceCache cache;
var vpp = new Mock<VirtualPathProvider>().Object;
cache = new FileExistenceCache(vpp);
Assert.Equal(vpp, cache.VirtualPathProvider);
Assert.Equal(ms, cache.MilliSecondsBeforeReset);
ms = 9999;
cache = new FileExistenceCache(vpp, ms);
Assert.Equal(vpp, cache.VirtualPathProvider);
Assert.Equal(ms, cache.MilliSecondsBeforeReset);
}
// Not a valid production scenario -- no HostingEnvironment
[Fact]
public void ConstructorTestWithNull()
{
// Arrange & Act
FileExistenceCache cache = new FileExistenceCache(() => null);
// Assert
Assert.Null(cache.VirtualPathProvider);
}
[Fact]
public void ConstructorTest_VPPRegistrationChanging()
{
// Arrange
Mock<VirtualPathProvider> mockProvider = new Mock<VirtualPathProvider>(MockBehavior.Strict);
VirtualPathProvider provider = null;
// Act
FileExistenceCache cache = new FileExistenceCache(() => provider);
// The moral equivalent of HostingEnvironment.RegisterVirtualPathProvider(mockProvider.Object)
provider = mockProvider.Object;
// Assert
Assert.Equal(provider, cache.VirtualPathProvider);
mockProvider.Verify();
}
[Fact]
public void FileExistsTest_VPPRegistrationChanging()
{
// Arrange
string path = "~/Index.cshtml";
Mock<VirtualPathProvider> mockProvider = new Mock<VirtualPathProvider>(MockBehavior.Strict);
mockProvider.Setup(c => c.FileExists(It.IsAny<string>())).Returns<string>(p => p.Equals(path)).Verifiable();
VirtualPathProvider provider = null;
// Act
FileExistenceCache cache = new FileExistenceCache(() => provider);
// The moral equivalent of HostingEnvironment.RegisterVirtualPathProvider(mockProvider.Object)
provider = mockProvider.Object;
bool createExists = cache.FileExists("~/Create.cshtml");
bool indexExists = cache.FileExists(path);
// Assert
Assert.False(createExists);
Assert.True(indexExists);
mockProvider.Verify();
mockProvider.Verify(vpp => vpp.FileExists(It.IsAny<string>()), Times.Exactly(2));
}
[Fact]
public void TimeExceededFalseTest()
{
var ms = 100000;
var cache = new FileExistenceCache(GetVpp(), ms);
Assert.False(cache.TimeExceeded);
}
[Fact]
public void TimeExceededTrueTest()
{
var ms = 5;
var cache = new FileExistenceCache(GetVpp(), ms);
Thread.Sleep(300);
Assert.True(cache.TimeExceeded);
}
[Fact]
public void ResetTest()
{
var cache = new FileExistenceCache(GetVpp());
var cacheInternal = cache.CacheInternal;
cache.Reset();
Assert.NotSame(cacheInternal, cache.CacheInternal);
}
[Fact]
public void FileExistsTest()
{
var path = "~/index.cshtml";
var cache = new FileExistenceCache(GetVpp(path));
Assert.True(cache.FileExists(path));
Assert.False(cache.FileExists("~/test.cshtml"));
}
[Fact]
public void FileExistsVppLaterTest()
{
var path = "~/index.cshtml";
var cache = new FileExistenceCache(GetVpp(path));
Assert.True(cache.FileExists(path));
Assert.False(cache.FileExists("~/test.cshtml"));
}
[Fact]
public void FileExistsTimeExceededTest()
{
AppDomainUtils.RunInSeparateAppDomain(() =>
{
var path = "~/index.cshtml";
Utils.SetupVirtualPathInAppDomain(path, "");
var cache = new FileExistenceCache(GetVpp(path));
var cacheInternal = cache.CacheInternal;
cache.MilliSecondsBeforeReset = 5;
Thread.Sleep(300);
Assert.True(cache.FileExists(path));
Assert.False(cache.FileExists("~/test.cshtml"));
Assert.NotEqual(cacheInternal, cache.CacheInternal);
});
}
private static VirtualPathProvider GetVpp(params string[] files)
{
var vpp = new Mock<VirtualPathProvider>();
vpp.Setup(c => c.FileExists(It.IsAny<string>())).Returns<string>(p => files.Contains(p, StringComparer.OrdinalIgnoreCase));
return vpp.Object;
}
}
}