Permalink
Browse files

Added tests for sharing policy activator

  • Loading branch information...
1 parent 3b1b456 commit f9fa54339017c8282e6cf015d82daf98387a7e3f @ahjohannessen ahjohannessen committed Dec 22, 2011
View
1 src/FubuMVC.Spark.Tests/FubuMVC.Spark.Tests.csproj
@@ -115,6 +115,7 @@
<Compile Include="SparkModel\Policies\ViewPathPolicyTester.cs" />
<Compile Include="SparkModel\Sharing\RecordingSharingRegistrationTester.cs" />
<Compile Include="SparkModel\Sharing\SharingGraphTester.cs" />
+ <Compile Include="SparkModel\Sharing\SharingPolicyActivatorTester.cs" />
<Compile Include="SparkModel\Sharing\SharingRegistrationDiagnosticsTester.cs" />
<Compile Include="SparkModel\Sharing\SharingsGraphActivatorTester.cs" />
<Compile Include="SparkModel\Sharing\SparkDslReaderTester.cs" />
View
101 src/FubuMVC.Spark.Tests/SparkModel/Sharing/SharingPolicyActivatorTester.cs
@@ -0,0 +1,101 @@
+using System.Collections.Generic;
+using Bottles;
+using Bottles.Diagnostics;
+using FubuMVC.Spark.SparkModel;
+using FubuMVC.Spark.SparkModel.Sharing;
+using FubuTestingSupport;
+using NUnit.Framework;
+using Rhino.Mocks;
+
+namespace FubuMVC.Spark.Tests.SparkModel.Sharing
+{
+ [TestFixture]
+ public class SharingPolicyActivatorTester
+ {
+ private SharingGraph _graph;
+ private List<ISharingPolicy> _policies;
+ private SharingPolicyActivator _activator;
+ private IPackageLog _packageLog;
+ private SharingLogsCache _sharingLogs;
+
+ [SetUp]
+ public void beforeEach()
+ {
+ _policies = new List<ISharingPolicy> {MockRepository.GenerateMock<ISharingPolicy>()};
+ _graph = new SharingGraph();
+ _packageLog = MockRepository.GenerateMock<IPackageLog>();
+ _sharingLogs = new SharingLogsCache();
+ _activator = new SharingPolicyActivator(_graph, _sharingLogs, _policies);
+ }
+
+ [Test]
+ public void apply_policies_logs_policy()
+ {
+ _activator.ApplyPolicies(_packageLog);
+ _packageLog.AssertWasCalled(x => x.Trace("Applying policy [{0}].", _policies[0].ToString()));
+ }
+
+ [Test]
+ public void apply_policies_sets_provenance()
+ {
+ _activator.ApplyPolicies(_packageLog);
+ _activator.Diagnostics.CurrentProvenance.ShouldEqual(_policies[0].ToString());
+ }
+
+ [Test]
+ public void apply_policies_uses_diagnostics()
+ {
+ _activator.ApplyPolicies(_packageLog);
+ _policies[0].AssertWasCalled(x => x.Apply(_packageLog, _activator.Diagnostics));
+ }
+
+ [Test]
+ public void register_as_global_logs_operation()
+ {
+ _activator.RegisterAppGlobal(_packageLog);
+ _packageLog.AssertWasCalled(x => x.Trace("Registering application as global sharing."));
+ }
+
+ [Test]
+ public void register_as_global_invokes_diagnostics()
+ {
+ var inner = MockRepository.GenerateMock<ISharingRegistration>();
+ var fake = new SharingRegistrationDiagnostics(inner, _sharingLogs);
+
+ _activator.Diagnostics = fake;
+ _activator.RegisterAppGlobal(_packageLog);
+
+ fake.CurrentProvenance.ShouldEqual(FubuSparkConstants.HostOrigin);
+ inner.AssertWasCalled(x => x.Global(FubuSparkConstants.HostOrigin));
+ }
+
+ [Test]
+ public void compile_dependencies_logs_all_provenances()
+ {
+ var packages = new List<IPackageInfo>
+ {
+ new PackageInfo("a"),
+ new PackageInfo("b")
+ };
+
+ _activator.CompileDependencies(packages, _packageLog);
+
+ _packageLog.AssertWasCalled(x => x.Trace("Compiling dependencies for [{0}]", "a, b, Host"));
+ }
+
+ [Test]
+ public void compile_dependencies_implicit_adds_host_as_provenance()
+ {
+ var packages = new List<IPackageInfo>
+ {
+ new PackageInfo("a"),
+ new PackageInfo("b")
+ };
+
+ _graph.Global("x");
+ _activator.CompileDependencies(packages, _packageLog);
+
+ _graph.SharingsFor(FubuSparkConstants.HostOrigin).ShouldContain("x");
+ }
+ }
+}
View
42 src/FubuMVC.Spark/SparkModel/Sharing/SharingPolicyActivator.cs
@@ -5,48 +5,58 @@
namespace FubuMVC.Spark.SparkModel.Sharing
{
- // UT
public class SharingPolicyActivator : IActivator
{
private readonly SharingGraph _graph;
private readonly SharingLogsCache _logs;
private readonly IEnumerable<ISharingPolicy> _policies;
- private readonly SharingRegistrationDiagnostics _diagnostics;
public SharingPolicyActivator(SharingGraph graph, SharingLogsCache logs, IEnumerable<ISharingPolicy> policies)
{
_graph = graph;
_logs = logs;
_policies = policies;
- _diagnostics = new SharingRegistrationDiagnostics(_graph, _logs);
+
+ Diagnostics = new SharingRegistrationDiagnostics(_graph, _logs);
}
public void Activate(IEnumerable<IPackageInfo> packages, IPackageLog log)
{
+ ApplyPolicies(log);
+ RegisterAppGlobal(log);
+ CompileDependencies(packages, log);
+ }
+
+ public void ApplyPolicies(IPackageLog log)
+ {
_policies.Each(p =>
{
var policyName = p.ToString();
-
- log.Trace("Applying policy [{0}].", policyName);
- _diagnostics.SetCurrentProvenance(policyName);
-
- p.Apply(log, _diagnostics);
- });
- log.Trace("Registering application as global sharing.");
+ log.Trace("Applying policy [{0}].", policyName);
+ Diagnostics.SetCurrentProvenance(policyName);
- _diagnostics.SetCurrentProvenance(FubuSparkConstants.HostOrigin);
- _diagnostics.Global(FubuSparkConstants.HostOrigin);
-
+ p.Apply(log, Diagnostics);
+ });
+ }
+
+ public void CompileDependencies(IEnumerable<IPackageInfo> packages, IPackageLog log)
+ {
var provenances = packages.Select(p => p.Name).Union(new[] { FubuSparkConstants.HostOrigin }).ToArray();
+
log.Trace("Compiling dependencies for [{0}]", provenances.Join(", "));
- _graph.CompileDependencies(provenances);
+ _graph.CompileDependencies(provenances);
}
- public SharingRegistrationDiagnostics Diagnostics
+ public void RegisterAppGlobal(IPackageLog log)
{
- get { return _diagnostics; }
+ log.Trace("Registering application as global sharing.");
+
+ Diagnostics.SetCurrentProvenance(FubuSparkConstants.HostOrigin);
+ Diagnostics.Global(FubuSparkConstants.HostOrigin);
}
+
+ public SharingRegistrationDiagnostics Diagnostics { get; set; }
}
}

0 comments on commit f9fa543

Please sign in to comment.