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.core.datadictionary;
017:
018: import java.sql.Connection;
019: import java.sql.ResultSet;
020: import java.sql.Statement;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.HashSet;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.TreeMap;
027:
028: import javax.sql.DataSource;
029:
030: import org.apache.commons.lang.StringUtils;
031: import org.apache.log4j.Logger;
032: import org.kuali.core.bo.DocumentType;
033: import org.kuali.core.service.BusinessObjectService;
034: import org.kuali.core.service.DataDictionaryService;
035: import org.kuali.kfs.context.KualiTestBase;
036: import org.kuali.kfs.context.SpringContext;
037: import org.kuali.test.ConfigureContext;
038: import org.kuali.test.suite.AnnotationTestSuite;
039: import org.kuali.test.suite.PreCommitSuite;
040:
041: @AnnotationTestSuite(PreCommitSuite.class)
042: @ConfigureContext
043: public class DataDictionaryConfigurationTest extends KualiTestBase {
044: private static final Logger LOG = Logger
045: .getLogger(DataDictionaryConfigurationTest.class);
046: private DataDictionary dataDictionary;
047: private Map<String, Exception> dataDictionaryLoadFailures;
048: private Map<String, String> dataDictionaryWarnings;
049:
050: public void testLoadDataDictionaryConfiguration() throws Exception {
051: loadDataDictionary();
052: StringBuffer failureMessage = new StringBuffer(
053: "Unable to load DataDictionaryEntrys for some file locations:");
054: for (String key : dataDictionaryLoadFailures.keySet()) {
055: failureMessage.append("\n\t").append("key: ").append(key)
056: .append(" at location: ").append(
057: dataDictionary.getFileLocationMap()
058: .get(key)).append(" error: ")
059: .append(
060: ((Exception) dataDictionaryLoadFailures
061: .get(key)).getMessage());
062: }
063: StringBuffer warningMessage = new StringBuffer(
064: "Loaded DataDictionaryEntrys for some file locations with warnings:");
065: for (String key : dataDictionaryWarnings.keySet()) {
066: warningMessage.append("\n\t").append("key: ").append(key)
067: .append(" at location: ").append(
068: dataDictionary.getFileLocationMap()
069: .get(key)).append(" warning: ")
070: .append(dataDictionaryWarnings.get(key));
071: }
072: if (dataDictionaryWarnings.size() > 0) {
073: System.err.print(warningMessage);
074: }
075: assertTrue(failureMessage.toString(),
076: dataDictionaryLoadFailures.isEmpty());
077: }
078:
079: public void testAllDataDictionaryDocumentTypesExistInDocumentTypeTable()
080: throws Exception {
081: loadDataDictionary();
082: List<String> documentTypeCodes = new ArrayList<String>();
083: for (DocumentType type : (Collection<DocumentType>) SpringContext
084: .getBean(BusinessObjectService.class).findAll(
085: DocumentType.class)) {
086: documentTypeCodes.add(type.getFinancialDocumentTypeCode());
087: }
088: // Using HashSet since duplicate objects would otherwise be returned
089: HashSet<DocumentEntry> documentEntries = new HashSet(
090: SpringContext.getBean(DataDictionaryService.class)
091: .getDataDictionary().getDocumentEntries()
092: .values());
093: List<String> ddEntriesWithMissingTypes = new ArrayList<String>();
094: for (DocumentEntry documentEntry : documentEntries) {
095: String code = documentEntry.getDocumentTypeCode();
096: if (!documentTypeCodes.contains(code)
097: && !"RUSR".equals(code) && !"PRPL".equals(code)) { //PRPL is added here because two doc types reference it. This should be fixed
098: ddEntriesWithMissingTypes.add(code + " ("
099: + documentEntry.getDocumentTypeName() + ")");
100: } else {
101: documentTypeCodes.remove(code);
102: }
103: }
104: if (documentTypeCodes.size() > 0) {
105: System.err.print("superfluousTypesDefinedInDatabase: "
106: + documentTypeCodes);
107: }
108:
109: assertEquals(
110: "dataDictionaryDocumentTypesNotDefinedInDatabase: "
111: + ddEntriesWithMissingTypes, 0,
112: ddEntriesWithMissingTypes.size());
113:
114: }
115:
116: public void testAllDataDicitionaryDocumentTypesExistInWorkflowDocumentTypeTable()
117: throws Exception {
118: loadDataDictionary();
119: List<String> workflowDocumentTypeNames = new ArrayList<String>();
120: DataSource mySource = SpringContext.getBean(DataSource.class);
121: Connection dbCon = null;
122: try {
123:
124: dbCon = mySource.getConnection();
125: Statement dbAsk = dbCon.createStatement();
126: ResultSet dbAnswer = dbAsk
127: .executeQuery("select DOC_TYP_NM from EN_DOC_TYP_T where DOC_TYP_CUR_IND = 1");
128: while (dbAnswer.next()) {
129: String docName = dbAnswer.getString(1);
130: if (StringUtils.isNotBlank(docName)) {
131: workflowDocumentTypeNames.add(docName);
132: }
133: }
134: } catch (Exception e) {
135: throw (e);
136: }
137: // Using HashSet since duplicate objects would otherwise be returned
138: HashSet<DocumentEntry> documentEntries = new HashSet(
139: SpringContext.getBean(DataDictionaryService.class)
140: .getDataDictionary().getDocumentEntries()
141: .values());
142: List<String> ddEntriesWithMissingTypes = new ArrayList<String>();
143: for (DocumentEntry documentEntry : documentEntries) {
144: String name = documentEntry.getDocumentTypeName();
145: if (!workflowDocumentTypeNames.contains(name)
146: && !"RiceUserMaintenanceDocument".equals(name)) {
147: ddEntriesWithMissingTypes.add(name);
148: } else {
149: workflowDocumentTypeNames.remove(name);
150: }
151: }
152:
153: if (workflowDocumentTypeNames.size() > 0) {
154: System.err
155: .print("superfluousTypesDefinedInWorkflowDatabase: "
156: + workflowDocumentTypeNames);
157: }
158: assertEquals("documentTypesNotDefinedInWorkflowDatabase: "
159: + ddEntriesWithMissingTypes, 0,
160: ddEntriesWithMissingTypes.size());
161: }
162:
163: private void loadDataDictionary() throws Exception {
164: for (String key : dataDictionary.getFileLocationMap().keySet()) {
165: try {
166: DataDictionaryEntry entry = dataDictionary
167: .getDictionaryObjectEntry(key);
168: if (entry == null) {
169: dataDictionaryWarnings.put(key,
170: "DataDictionaryEntry is null");
171: } else if ((entry instanceof BusinessObjectEntry)
172: && !((BusinessObjectEntry) entry)
173: .getBusinessObjectClass()
174: .getSimpleName().equals(key)) {
175: dataDictionaryWarnings
176: .put(
177: key,
178: "BusinessObjectEntry xml file name and business object class simple name are out of sync");
179: } else if ((entry instanceof MaintenanceDocumentEntry)
180: && (!(((MaintenanceDocumentEntry) entry)
181: .getBusinessObjectClass()
182: .getSimpleName() + "MaintenanceDocument")
183: .equals(key) || !((MaintenanceDocumentEntry) entry)
184: .getDocumentTypeName().equals(key))) {
185: dataDictionaryWarnings
186: .put(
187: key,
188: "MaintenanceDocumentEntry xml file name and business object class simple name or workflow document type name are out of sync");
189: } else if ((entry instanceof TransactionalDocumentEntry)
190: && (!((TransactionalDocumentEntry) entry)
191: .getDocumentClass().getSimpleName()
192: .equals(key) || !((TransactionalDocumentEntry) entry)
193: .getDocumentTypeName().equals(key))) {
194: dataDictionaryWarnings
195: .put(
196: key,
197: "TransactionalDocumentEntry xml file name and document class simple name or workflow document type name are out of sync");
198: }
199:
200: } catch (Exception e) {
201: dataDictionaryLoadFailures.put(key, e);
202: }
203: }
204: }
205:
206: protected void setUp() throws Exception {
207: super .setUp();
208: dataDictionary = SpringContext.getBean(
209: DataDictionaryService.class).getDataDictionary();
210: dataDictionaryLoadFailures = new TreeMap();
211: dataDictionaryWarnings = new TreeMap();
212: }
213: }
|