| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
package complex.path_substitution; |
| 19 |
|
| 20 |
import static org.junit.Assert.assertTrue; |
| 21 |
import static org.junit.Assert.fail; |
| 22 |
|
| 23 |
import java.util.ArrayList; |
| 24 |
|
| 25 |
import org.junit.AfterClass; |
| 26 |
import org.junit.Before; |
| 27 |
import org.junit.BeforeClass; |
| 28 |
import org.junit.Test; |
| 29 |
import org.openoffice.test.OfficeConnection; |
| 30 |
|
| 31 |
import com.sun.star.lang.XMultiServiceFactory; |
| 32 |
import com.sun.star.uno.UnoRuntime; |
| 33 |
import com.sun.star.util.XStringSubstitution; |
| 34 |
|
| 35 |
/** |
| 36 |
* |
| 37 |
*/ |
| 38 |
public class PathSubstitutionTest |
| 39 |
{ |
| 40 |
|
| 41 |
// all substitution variables |
| 42 |
private VariableContainer substVars = null; |
| 43 |
|
| 44 |
/** |
| 45 |
* Create an array with all substitution variables |
| 46 |
*/ |
| 47 |
@Before public void initialize() |
| 48 |
{ |
| 49 |
substVars = new VariableContainer(); |
| 50 |
substVars.add("$(prog)", true, true); |
| 51 |
substVars.add("$(inst)", true, true); |
| 52 |
substVars.add("$(user)", true, true); |
| 53 |
substVars.add("$(work)", true, true); |
| 54 |
substVars.add("$(home)", true, true); |
| 55 |
substVars.add("$(temp)", true, true); |
| 56 |
substVars.add("$(lang)", false, false); |
| 57 |
substVars.add("$(username)", false, false); |
| 58 |
substVars.add("$(langid)", false, false); |
| 59 |
substVars.add("$(vlang)", false, false); |
| 60 |
// path won't resubstitute |
| 61 |
substVars.add("$(path)", true, false); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* One actual test: as the method 'getTestMethodNames()' tells. |
| 66 |
*/ |
| 67 |
@Test public void checkXStringSubstitution() |
| 68 |
{ |
| 69 |
XMultiServiceFactory xMSF = getMSF(); |
| 70 |
System.out.println("---- Testing the XStringSubstitution interface ----"); |
| 71 |
System.out.println("Create instance of test object.\n"); |
| 72 |
XStringSubstitution oObj = null; |
| 73 |
try |
| 74 |
{ |
| 75 |
Object x = xMSF.createInstance( |
| 76 |
"com.sun.star.util.PathSubstitution"); |
| 77 |
oObj = UnoRuntime.queryInterface(XStringSubstitution.class, x); |
| 78 |
if (oObj == null) |
| 79 |
{ |
| 80 |
throw new com.sun.star.uno.Exception(); |
| 81 |
} |
| 82 |
} |
| 83 |
catch (com.sun.star.uno.Exception e) |
| 84 |
{ |
| 85 |
System.out.println(e.getClass().getName()); |
| 86 |
System.out.println("Message: " + e.getMessage()); |
| 87 |
fail("Could not create an instance of the test object."); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
for (int i = 0; i < substVars.size(); i++) |
| 92 |
{ |
| 93 |
String var = substVars.getVariable(i); |
| 94 |
System.out.println("Testing var '" + var + "'"); |
| 95 |
try |
| 96 |
{ |
| 97 |
String substVal = oObj.getSubstituteVariableValue(var); |
| 98 |
System.out.println("\tvalue '" + substVal + "'"); |
| 99 |
substVars.putValue(i, substVal); |
| 100 |
|
| 101 |
// simple check: let path in a string replace |
| 102 |
String substString = var + "/additional/path"; |
| 103 |
|
| 104 |
System.out.println("Substitute '" + substString + "'"); |
| 105 |
String newValue = oObj.substituteVariables(substString, true); |
| 106 |
System.out.println("Return value '" + newValue + "'"); |
| 107 |
// 2do: better check for correct substitution |
| 108 |
assertTrue("Did not substitute '" |
| 109 |
+ substString + "' to '" + newValue |
| 110 |
+ "' correctly:", newValue.startsWith(substVal)); |
| 111 |
|
| 112 |
// simple check part two: |
| 113 |
//make substitution backwards if possible |
| 114 |
if (substVars.canReSubstitute(i)) |
| 115 |
{ |
| 116 |
substString = substVal + "/additional/path"; |
| 117 |
|
| 118 |
System.out.println("Substitute backwards '" + substString + "'"); |
| 119 |
newValue = oObj.reSubstituteVariables(substString); |
| 120 |
System.out.println("Return value '" + newValue + "'"); |
| 121 |
// 2do: better check for correct substitution |
| 122 |
assertTrue("Did not reSubstitute '" |
| 123 |
+ substString + "' to '" + newValue |
| 124 |
+ "' correctly:", checkResubstitute(newValue, var)); |
| 125 |
} |
| 126 |
|
| 127 |
// simple check part three: look if replace |
| 128 |
//in middle of text works |
| 129 |
substString = "file:///starting/" + var + "/path"; |
| 130 |
|
| 131 |
String sCanSubstAllPos; |
| 132 |
if (substVars.onlySubstituteAtBegin(i)) |
| 133 |
sCanSubstAllPos = "NO"; |
| 134 |
else |
| 135 |
sCanSubstAllPos = "YES"; |
| 136 |
System.out.println("Variable can substitute within string: "+sCanSubstAllPos); |
| 137 |
System.out.println("Substitute '" + substString + "'"); |
| 138 |
newValue = oObj.substituteVariables(substString, false); |
| 139 |
System.out.println("Return value '" + newValue + "'"); |
| 140 |
boolean erg = true; |
| 141 |
if (substVars.onlySubstituteAtBegin(i)) |
| 142 |
{ |
| 143 |
// in this case it should not have worked |
| 144 |
erg = newValue.indexOf(substVal) == -1; |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
erg = newValue.indexOf(substVal) != -1; |
| 149 |
} |
| 150 |
assertTrue("Did not substitute '" |
| 151 |
+ substString + "' to '" + newValue |
| 152 |
+ "' correctly:", erg); |
| 153 |
|
| 154 |
} |
| 155 |
catch (com.sun.star.uno.Exception e) |
| 156 |
{ |
| 157 |
System.out.println(e.getClass().getName()); |
| 158 |
System.out.println("Message: " + e.getMessage()); |
| 159 |
fail("Could not create an instance of the test object."); |
| 160 |
return; |
| 161 |
} |
| 162 |
System.out.println("Finish testing '" + var + "'\n"); |
| 163 |
} |
| 164 |
|
| 165 |
// check of greedy resubstitution |
| 166 |
String prog = "$(prog)"; |
| 167 |
String inst = "$(inst)"; |
| 168 |
String instPth = substVars.getValue(inst); |
| 169 |
String progPth = substVars.getValue(prog); |
| 170 |
|
| 171 |
if (progPth.startsWith(instPth) && instPth.startsWith(progPth)) |
| 172 |
{ |
| 173 |
System.out.println("Greedy ReSubstitute"); |
| 174 |
String substString = progPth + "/additional/path"; |
| 175 |
String newVal = oObj.reSubstituteVariables(substString); |
| 176 |
System.out.println("String '" + substString |
| 177 |
+ "' should be resubstituted with"); |
| 178 |
System.out.println("Variable '" + prog + "' instead of Variable '" |
| 179 |
+ inst + "'"); |
| 180 |
assertTrue("Did not reSubstitute '" + substString |
| 181 |
+ "' to '" + newVal + "' correctly:", |
| 182 |
newVal.startsWith(prog)); |
| 183 |
} |
| 184 |
|
| 185 |
System.out.println( |
| 186 |
"---- Finish testing the XStringSubstitution interface ----"); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* test the resubstitution |
| 191 |
* @return true, if resubstitution is correct. |
| 192 |
*/ |
| 193 |
private boolean checkResubstitute(String subst, String original) |
| 194 |
{ |
| 195 |
// simple: subst starts with original |
| 196 |
if (subst.startsWith(original)) |
| 197 |
{ |
| 198 |
return true; |
| 199 |
} |
| 200 |
else |
| 201 |
{ // hard: been resubstituted with a different variable. |
| 202 |
for (int i = 0; i < substVars.size(); i++) |
| 203 |
{ |
| 204 |
String var = substVars.getVariable(i); |
| 205 |
if (subst.startsWith(var) && original.startsWith(original)) |
| 206 |
{ |
| 207 |
return true; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Class for containing the substitution variables with their |
| 216 |
* values and some information. |
| 217 |
*/ |
| 218 |
private static class VariableContainer |
| 219 |
{ |
| 220 |
|
| 221 |
public ArrayList<String> varName; |
| 222 |
public ArrayList<String> varValue; |
| 223 |
public ArrayList<Boolean> substAtBegin; |
| 224 |
public ArrayList<Boolean> resubst; |
| 225 |
|
| 226 |
public VariableContainer() |
| 227 |
{ |
| 228 |
varName = new ArrayList<String>(); |
| 229 |
varValue = new ArrayList<String>(); |
| 230 |
substAtBegin = new ArrayList<Boolean>(); |
| 231 |
resubst = new ArrayList<Boolean>(); |
| 232 |
} |
| 233 |
|
| 234 |
public void add(String var, boolean onlySubstAtBegin, |
| 235 |
boolean canResubst) |
| 236 |
{ |
| 237 |
varName.add(var); |
| 238 |
this.substAtBegin.add(Boolean.valueOf(onlySubstAtBegin)); |
| 239 |
this.resubst.add(Boolean.valueOf(canResubst)); |
| 240 |
} |
| 241 |
|
| 242 |
public void putValue(int i, String val) |
| 243 |
{ |
| 244 |
varValue.add(i, val); |
| 245 |
} |
| 246 |
|
| 247 |
public int size() |
| 248 |
{ |
| 249 |
return varName.size(); |
| 250 |
} |
| 251 |
|
| 252 |
public String getVariable(int i) |
| 253 |
{ |
| 254 |
return varName.get(i); |
| 255 |
} |
| 256 |
|
| 257 |
public String getValue(String var) |
| 258 |
{ |
| 259 |
return varValue.get(varName.indexOf(var)); |
| 260 |
} |
| 261 |
|
| 262 |
public boolean onlySubstituteAtBegin(int i) |
| 263 |
{ |
| 264 |
return substAtBegin.get(i).booleanValue(); |
| 265 |
} |
| 266 |
|
| 267 |
public boolean canReSubstitute(int i) |
| 268 |
{ |
| 269 |
return resubst.get(i).booleanValue(); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
private XMultiServiceFactory getMSF() |
| 274 |
{ |
| 275 |
return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); |
| 276 |
} |
| 277 |
|
| 278 |
// setup and close connections |
| 279 |
@BeforeClass |
| 280 |
public static void setUpConnection() throws Exception |
| 281 |
{ |
| 282 |
System.out.println("setUpConnection()"); |
| 283 |
connection.setUp(); |
| 284 |
} |
| 285 |
|
| 286 |
@AfterClass |
| 287 |
public static void tearDownConnection() |
| 288 |
throws InterruptedException, com.sun.star.uno.Exception |
| 289 |
{ |
| 290 |
System.out.println("tearDownConnection()"); |
| 291 |
connection.tearDown(); |
| 292 |
} |
| 293 |
private static final OfficeConnection connection = new OfficeConnection(); |
| 294 |
} |