package be.devijver.examples.groovy.statemachine import com.seventytwomiles.architecturerules.configuration.Configuration import com.seventytwomiles.architecturerules.domain.SourceDirectory import com.seventytwomiles.architecturerules.domain.Rule import com.seventytwomiles.architecturerules.services.CyclicRedundancyServiceImpl import com.seventytwomiles.architecturerules.services.RulesServiceImpl import com.seventytwomiles.architecturerules.exceptions.DependencyConstraintException class StateMachineTest extends GroovyTestCase { void testSimpleStateMachine() { def stateMachine = new StateMachine() MachineExecution execution = stateMachine.define { defaultAction = { } html { action = { head() body() } to("body").once().required() to("head").once().required() } head { } body { } } execution.html() } void testSimpleStateMachine2() { def machine = new StateMachine() def execution = machine.define { html { head({ title().once() }).once() body({ p { span() to("div") } div { to("p") } }).once() } } execution.html { head { title() } body { p { span() div { p { div { p { span() } } } } } } } execution.validateTransformations() } void testWithXmlSlurper() { def stateMachine = new StateMachine() MachineExecution execution = stateMachine.define { defaultAction = { element -> element.children().each { "${it.name()}"(it) } } html { to("body").once() to("head").once() } head { to("title").once() } title { } body { } } def html = new XmlSlurper().parseText(""" """) execution."${html.name()}"(html) execution.validateTransformations() } void testWithXmlSlurper2() { def stateMachine = new StateMachine() MachineExecution execution = stateMachine.define { defaultAction = { element -> element.children().each { "${it.name()}"(it) } } html { to("body").once() to("head").once() } head { title().once() } body { p { span() to("div") } div { to("p") } } } def html = new XmlSlurper().parseText("""

""") execution."${html.name()}"(html) execution.validateTransformations() } void testSimpleBuilder() { def stateMachine = new StateMachine() String _jar String _classes String _ruleName String _rulesActionResult int propCount = 0 def machine = stateMachine.define { architecture { jar { action = { String jarName -> _jar = jarName } } classes { action = { String classesLocation -> _classes = classesLocation } } rules { action = { _rulesActionResult = "ABC" assert context instanceof HierarchicalMap context['test1'] = new Object() assert context.test1 } "*" { action = { String name -> if (_ruleName == null) _ruleName = name assert context instanceof HierarchicalMap assert context['test1'] } comment = { String comment -> assert context.test1 propCount++ } "package"({ action = { String name -> } }).once() violation({ action = { String name -> } }).once() } } } } def token = "xyz" def count = 0 def count2 = 0 machine.architecture { jar "spring.jar" classes "target/classes" rules { "beans-web" { assert token == "xyz" count++ comment = "Test comment 1" "package" "test1" violation "test2" "testtest" } "must-fail" { count2++ comment = "Test comment 2" "package" "test4" violation "test5" } } } machine.validateTransformations() assert _jar == "spring.jar" assert _classes == "target/classes" assert _ruleName == "beans-web" assert _rulesActionResult == "ABC" assert count == 1 assert count2 == 1 assert propCount == 2 } void testArchitectureRulesBuilder() { def stateMachine = new StateMachine() Configuration configuration = new Configuration() configuration.doCyclicDependencyTest = true configuration.throwExceptionWhenNoPackages = true def machineExecution = stateMachine.define { architecture { action = { context.configuration = configuration } jar { action = { String jarName -> assert configuration context.configuration.addSource new SourceDirectory(jarName, true) } } classes { action = { String location -> assert configuration context.configuration.addSource new SourceDirectory(location, true) } } rules({ "*" { action = { String name -> assert configuration context.rule = new Rule(name) return { Configuration _configuration, Rule _rule -> _configuration.addRule _rule }.curry(context.configuration, context.rule) } comment = { String comment -> assert context.rule context.rule.comment = comment } "package"({ action = { String _package -> assert context.rule context.rule.addPackage _package } }).once() violation({ action = { String violation -> assert context.rule context.rule.addViolation violation } }).once() } }).once() } } assert machineExecution.&architecture instanceof Closure machineExecution.architecture { jar "spring.jar" rules { "beans-web" { comment = "org.springframework.beans.factory cannot depend on org.springframework.web" 'package' "org.springframework.beans" violation "org.springframework.web" } "must-fail" { comment = "org.springframework.orm.hibernate3 cannot depend on org.springframework.core.io" 'package' "org.springframework.orm.hibernate3" violation "org.springframework.core.io" } } } machineExecution.validateTransformations() new CyclicRedundancyServiceImpl(configuration).performCyclicRedundancyCheck() try { new RulesServiceImpl(configuration).performRulesTest() fail "DependencyConstraintException expected" } catch (DependencyConstraintException e) { assert e.message.startsWith("rule must-fail failed:") } } }