我的蛋殼

          倡導自由、開放、分享的Java技術社區 http://www.javaread.com

           

          用Architecture Rules檢查代碼越級調用

          在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

          posted on 2008-07-09 14:17 javaread.com 閱讀(1094) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆檔案

          Java

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 永胜县| 杭锦后旗| 双辽市| 漳平市| 绵阳市| 海淀区| 杭锦后旗| 咸阳市| 祁阳县| 东阳市| 长顺县| 平定县| 运城市| 永和县| 芮城县| 孙吴县| 朝阳市| 高邮市| 化德县| 怀安县| 镇巴县| 德江县| 郸城县| 东方市| 仙游县| 广丰县| 澳门| 克拉玛依市| 利辛县| 梅河口市| 武安市| 华蓥市| 珲春市| 文山县| 延吉市| 西平县| 措美县| 察隅县| 班玛县| 神池县| 乌拉特后旗|