KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > tests > OjbTestBase


1 package xdoclet.modules.ojb.tests;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import xdoclet.junit.*;
22
23 /**
24  * Base class for tests for the Xdoclet OJB module.
25  *
26  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
27  */

28 public class OjbTestBase extends XDocletTestBase
29 {
30     protected static final String JavaDoc OJB_DEST_FILE = "repository_user.xml";
31     protected static final String JavaDoc TORQUE_DEST_FILE = "project_schema.xml";
32     protected static final String JavaDoc PROPERTY_CHECKS = "checks";
33     protected static final String JavaDoc PROPERTY_DATABASENAME = "databaseName";
34     protected static final String JavaDoc PROPERTY_GENERATE_FOREIGNKEYS = "generateForeignkeys";
35
36     public OjbTestBase(String JavaDoc name)
37     {
38         super(name);
39     }
40
41     protected String JavaDoc runOjbXDoclet(String JavaDoc destFile)
42     {
43         return runOjbXDoclet(destFile, null, null);
44     }
45
46     protected String JavaDoc runOjbXDoclet(String JavaDoc destFile, HashMap JavaDoc taskProps, HashMap JavaDoc subTaskProps)
47     {
48         clearTaskProperties();
49         clearSubTaskProperties();
50         setTaskName("xdoclet.modules.ojb.OjbDocletTask");
51         setSubTaskName("xdoclet.modules.ojb.OjbSubTask");
52         setProperties(taskProps, subTaskProps);
53         setDestFile(destFile);
54
55         return runXDoclet();
56     }
57     
58     protected String JavaDoc runTorqueXDoclet(String JavaDoc destFile, String JavaDoc dbName)
59     {
60         HashMap JavaDoc props = new HashMap JavaDoc();
61
62         props.put(PROPERTY_DATABASENAME, dbName);
63
64         return runTorqueXDoclet(destFile, null, props);
65     }
66
67     protected String JavaDoc runTorqueXDoclet(String JavaDoc destFile, HashMap JavaDoc taskProps, HashMap JavaDoc subTaskProps)
68     {
69         clearTaskProperties();
70         clearSubTaskProperties();
71         setTaskName("xdoclet.modules.ojb.OjbDocletTask");
72         setSubTaskName("xdoclet.modules.ojb.TorqueSubTask");
73         setProperties(taskProps, subTaskProps);
74         setDestFile(destFile);
75
76         return runXDoclet();
77     }
78     
79     private void setProperties(HashMap JavaDoc taskProps, HashMap JavaDoc subTaskProps)
80     {
81         String JavaDoc key;
82
83         if (taskProps != null)
84         {
85             for (Iterator JavaDoc it = taskProps.keySet().iterator(); it.hasNext();)
86             {
87                 key = (String JavaDoc)it.next();
88                 setTaskProperty(key, taskProps.get(key));
89             }
90         }
91         if (subTaskProps != null)
92         {
93             for (Iterator JavaDoc it = subTaskProps.keySet().iterator(); it.hasNext();)
94             {
95                 key = (String JavaDoc)it.next();
96                 setSubTaskProperty(key, subTaskProps.get(key));
97             }
98         }
99     }
100     
101     /**
102      * Helper method that compresses whitespaces, i.e. replaces all whitespaces with spaces and reduces
103      * space blocks to single spaces.
104      *
105      * @param text The input text
106      * @return The text with whitespace compressed
107      */

108     protected String JavaDoc compressWhitespaces(String JavaDoc text)
109     {
110         if (text == null)
111         {
112             return null;
113         }
114
115         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
116         char c;
117         boolean wasWS = false;
118
119         for (int idx = 0; idx < text.length(); idx++)
120         {
121             c = text.charAt(idx);
122             if (!Character.isWhitespace(c))
123             {
124                 if (wasWS && (result.length() > 0))
125                 {
126                     result.append(' ');
127                 }
128                 result.append(c);
129                 wasWS = false;
130             }
131             else
132             {
133                 wasWS = true;
134             }
135         }
136
137         return result.toString();
138     }
139
140     protected void assertEqualsOjbDescriptorFile(String JavaDoc expected, String JavaDoc value)
141     {
142         assertEquals("<!-- file containing the repository descriptions for user-defined types --> "+
143                      "<!-- Generated by the xdoclet-ojb module --> "+
144                      compressWhitespaces(expected),
145                      compressWhitespaces(value));
146     }
147
148     protected void assertEqualsTorqueSchemaFile(String JavaDoc expected, String JavaDoc value)
149     {
150         assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\" ?> "+
151                      "<!DOCTYPE database SYSTEM \"http://jakarta.apache.org/turbine/dtd/database.dtd\"> "+
152                      "<!-- Generated by the xdoclet-ojb module --> "+
153                      compressWhitespaces(expected),
154                      compressWhitespaces(value));
155     }
156 }
157
Popular Tags