??ü??? JDepend

JDepend ????????????????Υ???????????????????????? ????????????????? Ant ?? Maven ????? ????????????? Java™API ?????????и????????????????? ???????????????????????汾????????????????????????У???????????????? ???Υ?????????????????????????????
????????Afferent coupling??????????Efferent coupling??

JDepend ?У???????????Щ????????????Щ????????????????????????????????????????????????????? Struts ????? Web ???? ?????????Щ?????и????????? ????????????????????????Щ???? ????????????????????????????????????????????????????????????????????????е???????????????

??????

??????ж??????????????????????о??????????????????????????????????????????????????м??? ???????????????????????????????????

???????????? 2 ?????????У? data????′????????????? 0??????ò???? ????? business ??????????????????????? data??????????? import?????????????????????????????????????????? JUnit??JDepend ?? API ??????????????У??????????Щ???ɡ?

????????????????? Ant ?? Maven?????????У????????????? JDepend API ?? JUnit ?????????????????仯?? ?????????Щ?仯?????????????????????? ????????????????????

????????????? JUnit ???????? JDepend ?????????????嵥 2 ?????

?嵥 2. ?? JUnit ?????? JDepend

import junit.framework.TestCase;
import jdepend.framework.JavaPackage;
import jdepend.framework.JDepend;

public class ArchitecturalRulesTest extends TestCase {
  private static final String DIRECTORY_TO_ANALYZE =
      "C:/dev/project-sandbox/brewery/classes";
  private JDepend jdepend;
  private String dataLayer = "com.beer.business.data";
  private String businessLayer = "com.beer.business.service";
  private Collection dataLayerViolations = new ArrayList<String>();

  public ArchitecturalRulesTest(String name) {
    super(name);
  }

  protected void setUp()throws IOException {
    jdepend = new JDepend();
    jdepend.addDirectory(DIRECTORY_TO_ANALYZE);
    // Calling the businessLayer from the dataLayer is a violation
    dataLayerViolations.add(businessLayer);
  }


?嵥 2 ??????????????????????

    ??????? JDepend ??jdepend.framework.JavaPackage?? jdepend.framework.JDepend??

    ???????????λ???? DIRECTORY_TO_ANALYZE???????塣JDepend ??? ???? JDepend.addDirectory?????????ò????????? fixture ?????? setUp()????????

    ?????????? “Layer” String???塣

    dataLayerViolations Collection????? businessLayer String????????????????? ??????????????Υ????

???????????????????Ч???????? JDepend???????????????????????? ?????????趨?Щ???????????????????仯??

?嵥 3 ?е? testDataLayer()????????????????????? ?÷??????ж???????? dataLayer???κ?Υ?? —— ??? isLayeringValid()????????????? ?嵥 4?ж??壩???? false?????????? ????????????ζ??????????????Υ????

?嵥 3. ??? JDepend ??????Υ??

 public void testDataLayer() {
  if (!isLayeringValid(dataLayer?? dataLayerViolations)) {
    fail("Dependency Constraint failed in Data Layer");
  }
 }


?嵥 3 ?в????????????????????嵥 4 ?????

?嵥 4. ????????????????????

private boolean isLayeringValid(String layer?? Collection rules) {
??boolean rulesCorrect = true;
??Collection packages = jdepend.analyze();
??Iterator itor = packages.iterator();
??JavaPackage jPackage = null;
??String analyzedPackageName = null;
??while (itor.hasNext()) {
????jPackage = (JavaPackage) itor.next();
????analyzedPackageName = jPackage.getName();
????Iterator afferentItor = jPackage.getAfferents().iterator();
????String afferentPackageName = null;
????while (afferentItor.hasNext()) {
??????JavaPackage afferentPackage = (JavaPackage) afferentItor.next();
??????afferentPackageName = afferentPackage.getName();
????}
????rulesCorrect = isEfferentsValid(layer?? rules?? rulesCorrect?? jPackage?? analyzedPackageName);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error:??The previous line is longer than the max of 90 characters ---------|
??}
??return rulesCorrect;
}