/// <summary>
 ///     Verifies that the architecture meets the criteria of the archrule.
 /// </summary>
 /// <param name="architecture">The architecture to be tested</param>
 /// <param name="archRule">The rule to test the architecture with</param>
 public static void FulfilsRule(Architecture architecture, IArchRule archRule)
 {
     if (!architecture.FulfilsRule(archRule))
     {
         Assert.Fail(architecture.EvaluateRule(archRule).ToErrorMessage());
     }
 }
Exemple #2
0
 /// <summary>
 ///     Verifies that the architecture meets the criteria of the archrule.
 /// </summary>
 /// <param name="architecture">The architecture to be tested</param>
 /// <param name="archRule">The rule to test the architecture with</param>
 /// <exception cref="FailedArchRuleException">Thrown if the rule is violated</exception>
 public static void CheckRule(Architecture architecture, IArchRule archRule)
 {
     if (!archRule.HasNoViolations(architecture))
     {
         throw new FailedArchRuleException(architecture, archRule);
     }
 }
Exemple #3
0
        public void Cycles()
        {
            IArchRule rule = Slices().Matching("Module.(*)").Should().BeFreeOfCycles();

            Assert.False(rule.HasNoViolations(Architecture));
            //rule.Check(Architecture);
        }
Exemple #4
0
        public void StoreCurrentViolations(IArchRule rule, IEnumerable <StringIdentifier> violations)
        {
            var directory = Path.GetDirectoryName(_storagePath);

            if (directory == null)
            {
                throw new ArgumentException("Invalid Path");
            }

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            var violationElements =
                violations.Select(violation =>
                                  new XElement("Violation", violation.Identifier));
            var ruleElement = new XElement("FrozenRule", violationElements);

            ruleElement.SetAttributeValue("ArchRule", rule.Description);

            var storageDoc = LoadStorage();
            var storedRule = FindStoredRule(storageDoc, rule);

            storedRule?.Remove();
            storageDoc.Root.Add(ruleElement);

            using (var writer = XmlWriter.Create(DefaultStoragePath, WriterSettings))
            {
                storageDoc.WriteTo(writer);
            }
        }
Exemple #5
0
        public bool RuleAlreadyFrozen(IArchRule rule)
        {
            var storageDoc = LoadStorage();
            var storedRule = FindStoredRule(storageDoc, rule);

            return(storedRule != null);
        }
 /// <summary>
 ///     Verifies that the architecture meets the criteria of the archrule.
 /// </summary>
 /// <param name="architecture">The architecture to be tested</param>
 /// <param name="archRule">The rule to test the architecture with</param>
 /// <exception cref="FailedArchRuleException">Thrown if the rule is violated</exception>
 public static void ArchRule(Architecture architecture, IArchRule archRule)
 {
     if (!architecture.FulfilsRule(archRule))
     {
         throw new FailedArchRuleException(architecture, archRule);
     }
 }
Exemple #7
0
 public static void Setup(TestContext context)
 {
     _architecture         = new ArchLoader().LoadAssemblies(typeof(RuleEvaluationTests).Assembly).Build();
     _trueRule             = Classes().That().Are(typeof(RuleEvaluationTests)).Should().Exist();
     _falseRule            = Classes().That().Are(typeof(RuleEvaluationTests)).Should().NotExist();
     _expectedErrorMessage = _falseRule.Evaluate(_architecture).ToErrorMessage();
 }
        public void All_Interfaces_Are_In_Contracts_Namespace()
        {
            IArchRule interfacesShouldBeInContractsLayer =
                ArchRuleDefinition.Interfaces().That().Are(Interfaces).Should().Be(InterfaceLayer);

            Assert.IsTrue(interfacesShouldBeInContractsLayer.HasNoViolations(Architecture));
        }
        public void ClassesShouldAdhereToShoppingExampleConsideringOnlyDependenciesInDiagram()
        {
            var filename = "./Resources/shopping_example.puml";

            IArchRule adhereToPlantUmlDiagram = Types().Should().AdhereToPlantUmlDiagram(filename);

            adhereToPlantUmlDiagram.Check(Architecture);
        }
Exemple #10
0
        public void AttributeAccess()
        {
            IArchRule rule = Classes().That().DoNotHaveAnyAttributes(typeof(Display)).Should()
                             .NotDependOnAny(Classes().That().AreAssignableTo(typeof(ICanvas)));

            Assert.False(rule.HasNoViolations(Architecture));
            //rule.Check(Architecture);
        }
Exemple #11
0
        public void ClassNamespaceContainment()
        {
            IArchRule rule = Classes().That().HaveNameContaining("Canvas").Should()
                             .ResideInNamespace(typeof(ICanvas).Namespace);

            Assert.False(rule.HasNoViolations(Architecture));
            //rule.Check(Architecture);
        }
Exemple #12
0
        public void InheritanceNaming()
        {
            IArchRule rule = Classes().That().AreAssignableTo(typeof(ICar)).Should()
                             .HaveNameContaining("Car");

            Assert.False(rule.HasNoViolations(Architecture));
            //rule.Check(Architecture);
        }
Exemple #13
0
        public void ClassDependency()
        {
            IArchRule rule = Classes().That().AreAssignableTo(typeof(ICar)).Should()
                             .NotDependOnAny(Classes().That().AreAssignableTo(typeof(ICanvas)));

            Assert.False(rule.HasNoViolations(Architecture));
            //rule.Check(Architecture);
        }
Exemple #14
0
        public void NamespaceDependency()
        {
            IArchRule rule = Types().That().ResideInNamespace("Model").Should()
                             .NotDependOnAny(Types().That().ResideInNamespace("Controller"));

            Assert.False(rule.HasNoViolations(Architecture));
            //rule.Check(Architecture);
        }
        public void ExampleLayerShouldNotAccessForbiddenLayer()
        {
            //you can give your rules a custom reason, which is displayed when it fails (together with the types that failed the rule)
            IArchRule exampleLayerShouldNotAccessForbiddenLayer = Types().That().Are(ExampleLayer).Should()
                                                                  .NotDependOnAny(ForbiddenLayer).Because("it's forbidden");

            exampleLayerShouldNotAccessForbiddenLayer.Check(Architecture);
        }
Exemple #16
0
        public void CertificateManagement_BoundedContext_Shoud_Not_Depend_On_Any_Infrastructure_Layer()
        {
            IArchRule rule = Types()
                             .That().Are(CertificateManagementBoundedContext)
                             .Should().NotDependOnAny(AdapterLayer)
                             .Because("it's forbidden");

            rule.Check(Architecture);
        }
Exemple #17
0
        public void Meeting_BoundedContext_Shoud_Not_Depend_Of_Certificate_BoundedContext()
        {
            IArchRule rule = Types()
                             .That().Are(MeetingManagementBoundedContext)
                             .Should().NotDependOnAny(CertificateManagementBoundedContext)
                             .Because("it's forbidden");

            rule.Check(Architecture);
        }
Exemple #18
0
        public void All_Classes_Have_Correct_Namespace()
        {
            IArchRule classesHaveCorrectNamespace =
                ArchRuleDefinition.Classes().That().Are(Classes).Should().Be(Layer);
            IArchRule interfacesHaveCorrectNamespace =
                ArchRuleDefinition.Interfaces().That().Are(Interfaces).Should().Be(Layer);

            IArchRule combinedArchRule =
                classesHaveCorrectNamespace.And(interfacesHaveCorrectNamespace);

            Assert.IsTrue(combinedArchRule.HasNoViolations(Architecture));
        }
Exemple #19
0
        public IEnumerable <StringIdentifier> GetFrozenViolations(IArchRule rule)
        {
            var storageDoc = LoadStorage();
            var storedRule = FindStoredRule(storageDoc, rule);

            if (storedRule == null) //rule not stored
            {
                yield break;
            }

            foreach (var xElement in storedRule.Elements())
            {
                yield return(new StringIdentifier(xElement.Value));
            }
        }
Exemple #20
0
        public IEnumerable <StringIdentifier> GetFrozenViolations(IArchRule rule)
        {
            var storedRules   = LoadStorage();
            var matchingRules = storedRules.Where(r => r.ArchRuleDescription == rule.Description).ToList();

            if (!matchingRules.Any())
            {
                return(Enumerable.Empty <StringIdentifier>());
            }

            if (matchingRules.Count > 1)
            {
                throw new MultipleOccurrencesInSequenceException("Multiple stored rules with same description found.");
            }

            return(matchingRules.First().Violations.Select(violation => new StringIdentifier(violation)));
        }
Exemple #21
0
        public void Check_Infrastructure_Dependencies()
        {
            IArchRule infrastructureLayerShouldNotAccessControllerLayer =
                ArchRuleDefinition.Types().That().Are(InfrastructureLayer).Should().NotDependOnAny(ControllerLayer);

            IArchRule infrastructureLayerShouldAccessDomainLayer =
                ArchRuleDefinition.Types().That().Are(InfrastructureLayer).Should().DependOnAny(DomainLayer);

            IArchRule infrastructureLayerShouldNotAccessApplicationLayer =
                ArchRuleDefinition.Types().That().Are(InfrastructureLayer).Should().NotDependOnAny(ApplicationLayer);

            IArchRule combinedArchRule =
                infrastructureLayerShouldNotAccessControllerLayer
                .And(infrastructureLayerShouldAccessDomainLayer)
                .And(infrastructureLayerShouldNotAccessApplicationLayer);

            Assert.IsTrue(combinedArchRule.HasNoViolations(Architecture));
        }
        public void TypesShouldBeInCorrectLayer()
        {
            //you can use the fluent API to write your own rules
            IArchRule exampleClassesShouldBeInExampleLayer =
                Classes().That().Are(ExampleClasses).Should().Be(ExampleLayer);
            IArchRule forbiddenInterfacesShouldBeInForbiddenLayer =
                Interfaces().That().Are(ForbiddenInterfaces).Should().Be(ForbiddenLayer);

            //check if your architecture fulfils your rules
            exampleClassesShouldBeInExampleLayer.Check(Architecture);
            forbiddenInterfacesShouldBeInForbiddenLayer.Check(Architecture);

            //you can also combine your rules
            IArchRule combinedArchRule =
                exampleClassesShouldBeInExampleLayer.And(forbiddenInterfacesShouldBeInForbiddenLayer);

            combinedArchRule.Check(Architecture);
        }
Exemple #23
0
        public void StoreCurrentViolations(IArchRule rule, IEnumerable <StringIdentifier> violations)
        {
            var violationIdentifiers = violations.Select(violation => violation.Identifier).ToList();
            var directory            = Path.GetDirectoryName(_storagePath);

            if (directory == null)
            {
                throw new ArgumentException("Invalid Path");
            }

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            var storedRules         = LoadStorage();
            var matchingRulesAmount = storedRules.Count(r => r.ArchRuleDescription == rule.Description);

            if (matchingRulesAmount > 1)
            {
                throw new MultipleOccurrencesInSequenceException(
                          "Multiple Rules with the same description were found in the given Json.");
            }

            if (matchingRulesAmount == 1)
            {
                storedRules.First(r => r.ArchRuleDescription == rule.Description).Violations = violationIdentifiers;
            }
            else
            {
                var frozenRule = new FrozenRule(rule.Description, violationIdentifiers);
                storedRules.Add(frozenRule);
            }

            var json = JsonConvert.SerializeObject(storedRules, Formatting.Indented);

            using (var w = new StreamWriter(_storagePath))
            {
                w.Write(json);
            }
        }
Exemple #24
0
 /// <summary>
 ///     Creates a new instance of the <see href="FailedArchRuleException" /> class.
 /// </summary>
 /// <param name="architecture">The architecture which was tested</param>
 /// <param name="archRule">The archrule that failed</param>
 public FailedArchRuleException(Architecture architecture, IArchRule archRule)
     : this(architecture.EvaluateRule(archRule))
 {
 }
Exemple #25
0
 public IEnumerable <EvaluationResult> EvaluateRule(IArchRule archRule)
 {
     return(archRule.Evaluate(this));
 }
Exemple #26
0
 public bool FulfilsRule(IArchRule archRule)
 {
     return(archRule.HasNoViolations(this));
 }
Exemple #27
0
 public IArchRule Or(IArchRule archRule)
 {
     return(new CombinedArchRule(_ruleCreator, LogicalConjunctionDefinition.Or, archRule));
 }
 public static FreezingArchRule Freeze(IArchRule rule)
 {
     return(new FreezingArchRule(rule, new JsonViolationStore()));
 }
 public static FreezingArchRule Freeze(IArchRule rule, string storagePath)
 {
     return(new FreezingArchRule(rule, new JsonViolationStore(storagePath)));
 }
 public static FreezingArchRule Freeze(IArchRule rule, IViolationStore violationStore)
 {
     return(new FreezingArchRule(rule, violationStore));
 }