001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.kfs.batch;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.List;
021:
022: import org.kuali.core.KualiModule;
023: import org.kuali.core.service.KualiModuleService;
024: import org.kuali.kfs.context.KualiTestBase;
025: import org.kuali.kfs.context.SpringContext;
026: import org.kuali.test.ConfigureContext;
027: import org.kuali.test.suite.AnnotationTestSuite;
028: import org.kuali.test.suite.PreCommitSuite;
029: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
030:
031: /**
032: * Tests the spring configuration for batch jobs.
033: */
034: @ConfigureContext
035: @AnnotationTestSuite(PreCommitSuite.class)
036: public class BatchConfigurationTest extends KualiTestBase {
037: private List<KualiModule> modules;
038: private List<String> kfsJobNames;
039: private Collection<JobDescriptor> jobDescriptors;
040: private List<String> kfsTriggerNames;
041: private Collection<TriggerDescriptor> triggerDescriptors;
042:
043: /**
044: * @see junit.framework.TestCase#setUp()
045: */
046: @Override
047: protected void setUp() throws Exception {
048: super .setUp();
049: modules = SpringContext.getBean(KualiModuleService.class)
050: .getInstalledModules();
051: kfsJobNames = (List<String>) BatchSpringContext
052: .getBatchComponents()
053: .get(JobDescriptor.class.getName());
054: jobDescriptors = SpringContext.getBeansOfType(
055: JobDescriptor.class).values();
056: kfsTriggerNames = (List<String>) BatchSpringContext
057: .getBatchComponents().get(
058: TriggerDescriptor.class.getName());
059: triggerDescriptors = SpringContext.getBeansOfType(
060: TriggerDescriptor.class).values();
061: }
062:
063: /**
064: * Verifies that registered job names correspond to JobDescriptor beans.
065: */
066: public final void testRegisteredJobsExist() throws Exception {
067: List<String> nonExistentJobNames = new ArrayList();
068: StringBuffer errorMessage = new StringBuffer(
069: "The following registered job names do not correspond to JobDescriptor beans:");
070: for (String jobName : kfsJobNames) {
071: try {
072: BatchSpringContext.getJobDescriptor(jobName);
073: } catch (NoSuchBeanDefinitionException e) {
074: nonExistentJobNames.add(jobName);
075: errorMessage.append("\n\tKFS: ").append(jobName);
076: }
077: }
078: for (KualiModule module : modules) {
079: for (String jobName : module.getJobNames()) {
080: try {
081: BatchSpringContext.getJobDescriptor(jobName);
082: } catch (NoSuchBeanDefinitionException e) {
083: nonExistentJobNames.add(jobName);
084: errorMessage.append("\n\t").append(
085: module.getModuleCode()).append(": ")
086: .append(jobName);
087: }
088: }
089: }
090: assertTrue(errorMessage.toString(), nonExistentJobNames
091: .isEmpty());
092: }
093:
094: /**
095: * Verifies that registered trigger names correspond to TriggerDescriptor beans.
096: */
097: public final void testRegisteredTriggersExist() throws Exception {
098: List<String> nonExistentTriggerNames = new ArrayList();
099: StringBuffer errorMessage = new StringBuffer(
100: "The following registered trigger names do not correspond to TriggerDescriptor beans:");
101: for (String triggerName : kfsTriggerNames) {
102: try {
103: BatchSpringContext.getTriggerDescriptor(triggerName);
104: } catch (NoSuchBeanDefinitionException e) {
105: nonExistentTriggerNames.add(triggerName);
106: errorMessage.append("\n\tKFS: ").append(triggerName);
107: }
108: }
109: for (KualiModule module : modules) {
110: for (String triggerName : module.getTriggerNames()) {
111: try {
112: BatchSpringContext
113: .getTriggerDescriptor(triggerName);
114: } catch (NoSuchBeanDefinitionException e) {
115: nonExistentTriggerNames.add(triggerName);
116: errorMessage.append("\n\t").append(
117: module.getModuleCode()).append(": ")
118: .append(triggerName);
119: }
120: }
121: }
122: assertTrue(errorMessage.toString(), nonExistentTriggerNames
123: .isEmpty());
124: }
125:
126: /**
127: * Verifies that beans of type JobDescriptor are registered with KFS as a whole or with an individual module.
128: */
129: public final void testJobsRegistered() throws Exception {
130: List<String> unregisteredJobNames = new ArrayList();
131: StringBuffer errorMessage = new StringBuffer(
132: "The following JobDescriptor beans are not registered with KFS as a whole or any module:");
133: for (JobDescriptor jobDescriptor : SpringContext
134: .getBeansOfType(JobDescriptor.class).values()) {
135: boolean isRegistered = kfsJobNames.contains(jobDescriptor
136: .getJobDetail().getName());
137: if (!isRegistered) {
138: for (KualiModule module : modules) {
139: if (module.getJobNames().contains(
140: jobDescriptor.getJobDetail().getName())) {
141: isRegistered = true;
142: break;
143: }
144: }
145: }
146: if (!isRegistered) {
147: unregisteredJobNames.add(jobDescriptor.getJobDetail()
148: .getName());
149: errorMessage.append("\n\t").append(
150: jobDescriptor.getJobDetail().getFullName());
151: }
152: }
153: assertTrue(errorMessage.toString(), unregisteredJobNames
154: .isEmpty());
155: }
156:
157: /**
158: * Verifies that beans of type TriggerDescriptor are registered with KFS as a whole or with an individual module.
159: */
160: public final void testTriggersRegistered() throws Exception {
161: List<String> unregisteredTriggerNames = new ArrayList();
162: StringBuffer errorMessage = new StringBuffer(
163: "The following TriggerDescriptor beans are not registered with KFS as a whole or any module:");
164: for (TriggerDescriptor triggerDescriptor : SpringContext
165: .getBeansOfType(TriggerDescriptor.class).values()) {
166: boolean isRegistered = kfsTriggerNames
167: .contains(triggerDescriptor.getTrigger().getName());
168: if (!isRegistered) {
169: for (KualiModule module : modules) {
170: if (module.getTriggerNames().contains(
171: triggerDescriptor.getTrigger().getName())) {
172: isRegistered = true;
173: break;
174: }
175: }
176: }
177: if (!isRegistered) {
178: unregisteredTriggerNames.add(triggerDescriptor
179: .getTrigger().getName());
180: errorMessage.append("\n\t").append(
181: triggerDescriptor.getTrigger().getFullName());
182: }
183: }
184: assertTrue(errorMessage.toString(), unregisteredTriggerNames
185: .isEmpty());
186: }
187:
188: /**
189: * Verifies that dependecies listed for a name defined JobDescriptor beans.
190: */
191: public final void testDependenciesExist() throws Exception {
192: List<String> nonExistentDependencies = new ArrayList();
193: StringBuffer errorMessage = new StringBuffer(
194: "The following dependencies do not refer to JobDescriptor beans:");
195: for (JobDescriptor jobDescriptor : SpringContext
196: .getBeansOfType(JobDescriptor.class).values()) {
197: for (String dependencyJobName : jobDescriptor
198: .getDependencies().keySet()) {
199: try {
200: BatchSpringContext
201: .getJobDescriptor(dependencyJobName);
202: } catch (NoSuchBeanDefinitionException e) {
203: nonExistentDependencies.add(dependencyJobName);
204: errorMessage.append("\n\t").append(
205: jobDescriptor.getJobDetail().getFullName())
206: .append("depends on: ").append(
207: dependencyJobName);
208: }
209: }
210: }
211: assertTrue(errorMessage.toString(), nonExistentDependencies
212: .isEmpty());
213: }
214: }
|