merge the formfield patch from ooo-build
[ooovba.git] / framework / qa / complex / path_substitution / PathSubstitutionTest.java
blobfe6d51df3c6e9bfcd5c8278aedbe2a21f2caecf1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PathSubstitutionTest.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package complex.path_substitution;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.util.XStringSubstitution;
35 import complexlib.ComplexTestCase;
36 import java.util.Vector;
38 /**
41 public class PathSubstitutionTest extends ComplexTestCase {
43 private static XMultiServiceFactory xMSF;
45 // all substitution variables
46 private VariableContainer substVars = null;
48 /**
49 * A function to tell the framework, which test functions are available.
50 * Right now, it's only 'checkXStringSubstitution'.
51 * @return All test methods.
53 public String[] getTestMethodNames() {
54 return new String[]{"checkXStringSubstitution"};
57 /**
58 * Create an array with all substitution variables
60 private void initialize() {
61 substVars = new VariableContainer();
62 substVars.add("$(prog)", false, true);
63 substVars.add("$(inst)", false, true);
64 substVars.add("$(user)", false, true);
65 substVars.add("$(work)", false, true);
66 substVars.add("$(home)", false, true);
67 substVars.add("$(temp)", false, true);
68 substVars.add("$(lang)", false, false);
69 substVars.add("$(langid)", false, false);
70 substVars.add("$(vlang)", false,false);
71 // path won't resubstitute
72 substVars.add("$(path)", false,false);
75 /**
76 * One actual test: as the method 'getTestMethodNames()' tells.
78 public void checkXStringSubstitution()
80 xMSF = (XMultiServiceFactory)param.getMSF();
81 log.println("---- Testing the XStringSubstitution interface ----");
82 log.println("Create intance of test object.\n");
83 XStringSubstitution oObj = null;
84 try {
85 Object x = xMSF.createInstance(
86 "com.sun.star.util.PathSubstitution");
87 oObj = (XStringSubstitution)
88 UnoRuntime.queryInterface(XStringSubstitution.class, x);
89 if (oObj == null) throw new com.sun.star.uno.Exception();
91 catch(com.sun.star.uno.Exception e) {
92 log.println(e.getClass().getName());
93 log.println("Message: " + e.getMessage());
94 failed("Could not create an instance of the test object.");
95 return;
98 initialize();
100 for (int i=0; i<substVars.size(); i++) {
101 String var = substVars.getVariable(i);
102 log.println("Testing var '" + var + "'");
103 try {
104 String substVal = oObj.getSubstituteVariableValue(var);
105 log.println("\tvalue '" + substVal + "'");
106 substVars.putValue(i,substVal);
108 // simple check: let path in a string replace
109 String substString = var + "/additional/path";
111 log.println("Substitute '"+substString+"'");
112 String newValue = oObj.substituteVariables(substString, true);
113 log.println("Return value '"+newValue+"'");
114 // 2do: better check for correct substitution
115 assure("Did not substitute '"
116 + substString+"' to '" + newValue
117 + "' correctly:", newValue.startsWith(substVal));
119 // simple check part two:
120 //make substitution backwards if possible
121 if (substVars.canReSubstitute(i)) {
122 substString = substVal + "/additional/path";
124 log.println("Substitute backwards '"+substString+"'");
125 newValue = oObj.reSubstituteVariables(substString);
126 log.println("Return value '"+newValue+"'");
127 // 2do: better check for correct substitution
128 assure("Did not reSubstitute '"
129 + substString + "' to '" + newValue
130 + "' correctly:", checkResubstitute(newValue, var));
133 // simple check part three: look if replace
134 //in middle of text works
135 substString = "file:///starting/" + var + "/path";
137 log.println("Substitute '"+substString+"'");
138 newValue = oObj.substituteVariables(substString, false);
139 log.println("Return value '"+newValue+"'");
140 boolean erg = true;
141 if(substVars.onlySubstituteAtBegin(i))
142 // in this case it should not have worked
143 erg = newValue.indexOf(substVal)==-1;
144 else
145 erg = newValue.indexOf(substVal)!=-1;
147 assure("Did not substitute '"
148 + substString + "' to '" + newValue
149 + "' correctly:", erg);
152 catch(com.sun.star.uno.Exception e) {
153 log.println(e.getClass().getName());
154 log.println("Message: " + e.getMessage());
155 failed("Could not create an instance of the test object.");
156 return;
158 log.println("Finish testing '" + var + "'\n");
161 // check of greedy resubstitution
162 String prog = "$(prog)";
163 String inst = "$(inst)";
164 String instPth = substVars.getValue(inst);
165 String progPth = substVars.getValue(prog);
167 if (progPth.startsWith(instPth) && instPth.startsWith(progPth)) {
168 log.println("Greedy ReSubstitute");
169 String substString = progPth + "/additional/path";
170 String newVal = oObj.reSubstituteVariables(substString);
171 log.println("String '" + substString +
172 "' should be resubstituted with");
173 log.println("Variable '" + prog + "' instead of Variable '" +
174 inst + "'");
175 assure("Did not reSubstitute '" + substString
176 + "' to '" + newVal + "' correctly:",
177 newVal.startsWith(prog));
180 log.println(
181 "---- Finish testing the XStringSubstitution interface ----");
185 * test the resubstitution
186 * @return true, if resubstitution is correct.
188 private boolean checkResubstitute(String subst, String original) {
189 // simple: subst starts with original
190 if ( subst.startsWith(original) ) {
191 return true;
193 else { // hard: been resubstituted with a differernt variable.
194 for (int i=0; i<substVars.size(); i++) {
195 String var = substVars.getVariable(i);
196 if ( subst.startsWith(var) && original.startsWith(original)) {
197 return true;
201 return false;
204 * Class for containing the substitution variables with their
205 * values and some information.
207 private class VariableContainer {
208 public Vector varName;
209 public Vector varValue;
210 public Vector substAtBegin;
211 public Vector resubst;
213 public VariableContainer() {
214 varName = new Vector();
215 varValue = new Vector();
216 substAtBegin = new Vector();
217 resubst = new Vector();
220 public void add(String var) {
221 varName.add(var);
222 substAtBegin.add(Boolean.TRUE);
223 resubst.add(Boolean.TRUE);
225 public void add(String var, boolean onlySubstAtBegin,
226 boolean canResubst) {
227 varName.add(var);
228 this.substAtBegin.add(new Boolean(onlySubstAtBegin));
229 this.resubst.add(new Boolean(canResubst));
232 public void putValue(int i, String val) {
233 varValue.add(i, val);
236 public int size() { return varName.size(); }
237 public String getVariable(int i) { return (String)varName.get(i); }
238 public String getValue(int i) { return (String)varName.get(i); }
239 public String getValue(String var) {
240 return (String)varValue.get(varName.indexOf(var));
242 public boolean onlySubstituteAtBegin(int i) {
243 return ((Boolean)substAtBegin.get(i)).booleanValue();
245 public boolean canReSubstitute(int i) {
246 return ((Boolean)resubst.get(i)).booleanValue();