001: package xdoclet.modules.ojb.tests;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.HashMap;
019: import java.util.Iterator;
020:
021: import xdoclet.junit.*;
022:
023: /**
024: * Base class for tests for the Xdoclet OJB module.
025: *
026: * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
027: */
028: public class OjbTestBase extends XDocletTestBase {
029: protected static final String OJB_DEST_FILE = "repository_user.xml";
030: protected static final String TORQUE_DEST_FILE = "project_schema.xml";
031: protected static final String PROPERTY_CHECKS = "checks";
032: protected static final String PROPERTY_DATABASENAME = "databaseName";
033: protected static final String PROPERTY_GENERATE_FOREIGNKEYS = "generateForeignkeys";
034:
035: public OjbTestBase(String name) {
036: super (name);
037: }
038:
039: protected String runOjbXDoclet(String destFile) {
040: return runOjbXDoclet(destFile, null, null);
041: }
042:
043: protected String runOjbXDoclet(String destFile, HashMap taskProps,
044: HashMap subTaskProps) {
045: clearTaskProperties();
046: clearSubTaskProperties();
047: setTaskName("xdoclet.modules.ojb.OjbDocletTask");
048: setSubTaskName("xdoclet.modules.ojb.OjbSubTask");
049: setProperties(taskProps, subTaskProps);
050: setDestFile(destFile);
051:
052: return runXDoclet();
053: }
054:
055: protected String runTorqueXDoclet(String destFile, String dbName) {
056: HashMap props = new HashMap();
057:
058: props.put(PROPERTY_DATABASENAME, dbName);
059:
060: return runTorqueXDoclet(destFile, null, props);
061: }
062:
063: protected String runTorqueXDoclet(String destFile,
064: HashMap taskProps, HashMap subTaskProps) {
065: clearTaskProperties();
066: clearSubTaskProperties();
067: setTaskName("xdoclet.modules.ojb.OjbDocletTask");
068: setSubTaskName("xdoclet.modules.ojb.TorqueSubTask");
069: setProperties(taskProps, subTaskProps);
070: setDestFile(destFile);
071:
072: return runXDoclet();
073: }
074:
075: private void setProperties(HashMap taskProps, HashMap subTaskProps) {
076: String key;
077:
078: if (taskProps != null) {
079: for (Iterator it = taskProps.keySet().iterator(); it
080: .hasNext();) {
081: key = (String) it.next();
082: setTaskProperty(key, taskProps.get(key));
083: }
084: }
085: if (subTaskProps != null) {
086: for (Iterator it = subTaskProps.keySet().iterator(); it
087: .hasNext();) {
088: key = (String) it.next();
089: setSubTaskProperty(key, subTaskProps.get(key));
090: }
091: }
092: }
093:
094: /**
095: * Helper method that compresses whitespaces, i.e. replaces all whitespaces with spaces and reduces
096: * space blocks to single spaces.
097: *
098: * @param text The input text
099: * @return The text with whitespace compressed
100: */
101: protected String compressWhitespaces(String text) {
102: if (text == null) {
103: return null;
104: }
105:
106: StringBuffer result = new StringBuffer();
107: char c;
108: boolean wasWS = false;
109:
110: for (int idx = 0; idx < text.length(); idx++) {
111: c = text.charAt(idx);
112: if (!Character.isWhitespace(c)) {
113: if (wasWS && (result.length() > 0)) {
114: result.append(' ');
115: }
116: result.append(c);
117: wasWS = false;
118: } else {
119: wasWS = true;
120: }
121: }
122:
123: return result.toString();
124: }
125:
126: protected void assertEqualsOjbDescriptorFile(String expected,
127: String value) {
128: assertEquals(
129: "<!-- file containing the repository descriptions for user-defined types --> "
130: + "<!-- Generated by the xdoclet-ojb module --> "
131: + compressWhitespaces(expected),
132: compressWhitespaces(value));
133: }
134:
135: protected void assertEqualsTorqueSchemaFile(String expected,
136: String value) {
137: assertEquals(
138: "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\" ?> "
139: + "<!DOCTYPE database SYSTEM \"http://jakarta.apache.org/turbine/dtd/database.dtd\"> "
140: + "<!-- Generated by the xdoclet-ojb module --> "
141: + compressWhitespaces(expected),
142: compressWhitespaces(value));
143: }
144: }
|