1 /***
2 * Copyright 2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * For more information visit
11 * http://72miles.com and
12 * http://architecturerules.googlecode.com/svn/docs/index.html
13 */
14
15 package com.seventytwomiles.architecturerules.services;
16
17
18 import com.seventytwomiles.architecturerules.configuration.Configuration;
19 import com.seventytwomiles.architecturerules.domain.JPackage;
20 import com.seventytwomiles.architecturerules.domain.Rule;
21 import com.seventytwomiles.architecturerules.exceptions.DependencyConstraintException;
22 import com.seventytwomiles.architecturerules.exceptions.NoPackagesFoundException;
23 import com.seventytwomiles.architecturerules.exceptions.SourceNotFoundException;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.util.Collection;
28
29
30
31 /***
32 * <p>Drives the tests by reading the configuration then asserting each defined
33 * <code>Rule</code>.</p>
34 *
35 * @author mikenereson
36 * @see AbstractArchitecturalRules
37 */
38 public class RulesServiceImpl extends AbstractArchitecturalRules
39 implements RulesService {
40
41
42 /***
43 * <p>Log to log with.</p>
44 *
45 * @parameter log Log
46 */
47 protected static final Log log = LogFactory.getLog(RulesServiceImpl.class);
48
49
50 /***
51 * <p>Instantiates a new <code>RulesService</code> which will begin reading
52 * all the configured sources</p>
53 *
54 * @param configuration Configuration
55 * @throws SourceNotFoundException when an required source directory does
56 * not exist and when <tt>exception</tt>=<tt>"true"</tt> in the source
57 * configuration
58 * @throws NoPackagesFoundException when none of the source directories
59 * exist and <tt>no-packages</tt>="<tt>ignore</tt>" in the sources
60 * configuration
61 */
62 public RulesServiceImpl(final Configuration configuration)
63 throws SourceNotFoundException, NoPackagesFoundException {
64
65 super(configuration);
66
67 log.info("instantiating new RulesService");
68 }
69
70
71 /***
72 * <p>Assert that no <code>Rule</code> in the given <code>Configuration</code>
73 * has been violated.</p>
74 *
75 * @return boolean <tt>true</tt> when tests pass
76 */
77 public boolean performRulesTest() {
78
79 log.info("perform rules test required");
80
81 final Collection<Rule> rules = configuration.getRules();
82
83 /***
84 * If logging is enabled, describe each rule that is going to be
85 * validated.
86 */
87 if (log.isDebugEnabled()) {
88
89 for (final Rule rule : rules)
90 log.debug(rule.getDescriptionOfRule());
91 }
92
93 for (final Rule rule : rules) {
94
95 log.info("checking rule " + rule.getId());
96
97 final String description = rule.describePackages();
98 log.debug("checking for dependency violations in " + description);
99
100 try {
101
102 final Collection<JPackage> packages = rule.getPackages();
103
104 for (final JPackage packageName : packages)
105 testLayeringValid(packageName, rule.getViolations());
106
107 } catch (final DependencyConstraintException e) {
108
109
110 throw new DependencyConstraintException(
111 "rule " + rule.getId() + " failed: " + e.getMessage());
112 }
113
114 log.debug("no dependency violations in " + rule.getPackages());
115 }
116
117 log.info("architecture rules service has completed its tests.");
118
119 return true;
120 }
121 }