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;
16
17
18 import com.seventytwomiles.architecturerules.configuration.Configuration;
19 import com.seventytwomiles.architecturerules.configuration.ConfigurationFactory;
20 import com.seventytwomiles.architecturerules.configuration.UnmodifiableConfiguration;
21 import com.seventytwomiles.architecturerules.configuration.xml.DigesterConfigurationFactory;
22 import com.seventytwomiles.architecturerules.services.CyclicRedundancyService;
23 import com.seventytwomiles.architecturerules.services.CyclicRedundancyServiceImpl;
24 import com.seventytwomiles.architecturerules.services.RulesService;
25 import com.seventytwomiles.architecturerules.services.RulesServiceImpl;
26 import junit.framework.TestCase;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30
31 /***
32 * <p>Abstract class that the users of this library will extend in order to
33 * create a unit test that asserts architecture. </p> <p/> <p>Once extended,
34 * implement <tt>testArchitecture</tt> and call <tt>doTest()</tt>. Also override
35 * <tt>getConfigurationFileName()</tt> if you want to load an XML configuration
36 * file.</p> <p/> <p>If you want to define the configuration programmatically in
37 * addition to the xml configuration, or want to solely use programmatic
38 * configuration, you may call <tt>getConfiguration</tt> which will return
39 * <code>Configuration</code> that you may then add new <code>Rule</code>, or
40 * <code>SourceDirectory</code> to. </p>
41 *
42 * @author mikenereson
43 * @noinspection PointlessBooleanExpression
44 */
45 public abstract class AbstractArchitectureRulesConfigurationTest
46 extends TestCase {
47
48
49 protected static final Log log = LogFactory.getLog(
50 AbstractArchitectureRulesConfigurationTest.class);
51
52 /***
53 * <p>The <code>Configuration</code> containing the <code>SourceDirectory</code>
54 * and <code>Rules</code> to assert.</p>
55 *
56 * @parameter configuration Configuration
57 */
58 final private Configuration configuration = new Configuration();
59
60
61 /***
62 * Upon instantiation, instances a new Configuration by reading the XML
63 * configuration file named architecture-rules.xml (by default) or by
64 * reading the XMl file name by the <code>getConfigurationFileName</code>
65 * method.
66 */
67 protected AbstractArchitectureRulesConfigurationTest() {
68
69
70
71 final String configurationFileName = getConfigurationFileName();
72
73 final ConfigurationFactory configurationFactory;
74
75 if (configurationFileName != null && configurationFileName.length() > 0) {
76
77 configurationFactory
78 = new DigesterConfigurationFactory(configurationFileName);
79
80 configuration.getRules().addAll(configurationFactory.getRules());
81
82 configuration.getSources()
83 .addAll(configurationFactory.getSources());
84
85 configuration.setDoCyclicDependencyTest(
86 configurationFactory.doCyclicDependencyTest());
87 }
88 }
89
90
91
92 /***
93 * <p>Get the name of the xml configuration file that is located in the
94 * classpath.</p> <p/> <p>Recommend value, and the default value, is
95 * <samp>architecture-rules.xml</samp></p>
96 *
97 * @return String name of the xml file including <samp>.xml</samp>
98 * @see ConfigurationFactory#DEFAULT_CONFIGURATION_FILE_NAME
99 */
100 protected String getConfigurationFileName() {
101 return ConfigurationFactory.DEFAULT_CONFIGURATION_FILE_NAME;
102 }
103
104
105 /***
106 * Getter for property {@link #configuration}.
107 *
108 * @return Value for property <tt>configuration</tt>.
109 */
110 protected Configuration getConfiguration() {
111 return configuration;
112 }
113
114
115 /***
116 * <p>Runs tests that are configured either programmatically, or by the XML
117 * configuration file that is loaded.</p>
118 *
119 * @return boolean <tt>true</tt> when the tests all pass.
120 */
121 protected final boolean doTests() {
122
123 final RulesService rulesService = new RulesServiceImpl(
124 new UnmodifiableConfiguration(configuration));
125
126 final boolean rulesResults = rulesService.performRulesTest();
127
128 if (configuration.shouldDoCyclicDependencyTest()) {
129
130 final UnmodifiableConfiguration unmodifiableConfiguration
131 = new UnmodifiableConfiguration(configuration);
132
133 final CyclicRedundancyService redundancyService
134 = new CyclicRedundancyServiceImpl(
135 unmodifiableConfiguration);
136
137 redundancyService.performCyclicRedundancyCheck();
138 }
139
140 return rulesResults;
141 }
142
143
144 /***
145 * <p>Implement this method and call {@link #doTests}</p>
146 */
147 public abstract void testArchitecture();
148 }