在J2EE的软件架构中,通常会按程序的职责抽象划分展示层,业务层,持久层,每层都的Java类都组织到例如名为web,service,dao包下。按照设计原则,层与层之间只能单向依赖,且不能跨层以来,例如只能web->service->dao,->表示依赖方向,箭头指向被依赖一方。Architecture Rules是一个基于JDepend封装而成小工具,支持通过XML或者变成配置检查规则,检查项目代码的依赖情况。Architecture Rules相对于JDepend的好处是提供了灵活的可配置的检查规则。 XML配置规则,不用太多解释也能明白。
代码复制到剪贴板
1. <architecture>
2.
3. <configuration>
4.
5. <sources no-packages="exception">
6. <source not-found="ignore">core"target"classes</source>
7. <source not-found="ignore">web"target"classes</source>
8. <source not-found="exception">ws"target"classes</source>
9. </sources>
10.
11. <cyclicalDependency test="true"></cyclicalDependency>
12.
13. </configuration>
14.
15. <rules>
16.
17. <rule id="dao">
18. <comment>
19.
20. </comment>
21. <packages>
22. <package>com.company.app.core.dao</package>
23. <package>com.company.app.core.dao.jdbc</package>
24. </packages>
25. <violations>
26. <violation>com.co.app.core.services</violation>
27. <violation>com.co.app.web</violation>
28. <violation>com.co.app.web.spring</violation>
29. </violations>
30. </rule>
31.
32. <rule id="web">
33. <comment>
34.
35. </comment>
36. <packages>
37. <package>com.co.app.web</package>
38. <package>com.co.app.web.spring</package>
39. <package>com.co.app.web.decorators</package>
40. </packages>
41. <violations>
42. <violation>com.co.app.core.dao</violation>
43. <violation>com.co.app.core.dao.jdbc</violation>
44. <violation>com.co.app.core.dao.ldap</violation>
45. </violations>
46. </rule>
47.
48. </rules>
49.
50. </architecture>
测试并运行。
代码复制到剪贴板
1. public class SimpleArchitectureTest
2. extends AbstractArchitectureRulesConfigurationTest {
3.
4. /**
5. * @see AbstractArchitectureRulesConfigurationTest
6. */
7. public String getConfigurationFileName() {
8.
9. /**
10. * Provide the name of the rules configuration file. File file is
11. * loaded from the classpath.
12. */
13. return "architecture-rules.xml";
14. }
15.
16. /**
17. * @see AbstractArchitectureRulesConfigurationTest#testArchitecture()
18. */
19. public void testArchitecture() {
20.
21. /**
22. * Run the test via doTest(). If any rules are broken, or if
23. * the configuration can not be loaded properly, then the appropriate
24. * Exception will be thrown.
25. */
26. assertTrue(doTests());
27. }
28. }
编程配置和测试运行。
代码复制到剪贴板
1. public class SimpleProgrammaticArchitectureTest
2. extends AbstractArchitectureRulesConfigurationTest {
3.
4. /**
5. * Sets up the fixture, for example, open a network connection. This method
6. * is called before a test is executed.
7. */
8. protected void setUp() throws Exception {
9.
10. super.setUp();
11.
12. /* get the configuration reference */
13. final Configuration configuration = getConfiguration();
14.
15. /* add sources */
16. configuration.addSource(
17. new SourceDirectory("target""test-classes", true));
18.
19. /* set options */
20. configuration.setDoCyclicDependencyTest(false);
21. configuration.setThrowExceptionWhenNoPackages(true);
22.
23. /* add Rules */
24. final Rule daoRule = new Rule("dao");
25. daoRule.setComment("dao may not access presentation.");
26. daoRule.addPackage("test.com.seventytwomiles.dao.hibernate");
27. daoRule.addViolation("test.com.seventytwomiles.web.spring");
28.
29. configuration.addRule(daoRule);
30. }
31.
32. /**
33. * @see AbstractArchitectureRulesConfigurationTest#testArchitecture()
34. */
35. public void testArchitecture() {
36.
37. /**
38. * Run the test via doTest(). If any rules are broken, or if
39. * the configuration can not be loaded properly, then the appropriate
40. * Exception will be thrown.
41. */
42. assertTrue(doTests());
43. }
44. }
Architecture Rules同时对Ant和Maven支持,可以方便集成到项目的编译和测试环境中。
更多信息:
简约之美,JQuery之进度条插件
让开发文档自动化
本文作者 javaread.com
本文作者:javaread.com