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