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