2007-07-24 12:28:13 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-11 07:07:37 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2007-07-24 12:28:13 +00:00
|
|
|
*
|
2010-02-12 15:01:35 +01:00
|
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
2007-07-24 12:28:13 +00:00
|
|
|
*
|
2008-04-11 07:07:37 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2007-07-24 12:28:13 +00:00
|
|
|
*
|
2008-04-11 07:07:37 +00:00
|
|
|
* This file is part of OpenOffice.org.
|
2007-07-24 12:28:13 +00:00
|
|
|
*
|
2008-04-11 07:07:37 +00:00
|
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
* only, as published by the Free Software Foundation.
|
2007-07-24 12:28:13 +00:00
|
|
|
*
|
2008-04-11 07:07:37 +00:00
|
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License version 3 for more details
|
|
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
2007-07-24 12:28:13 +00:00
|
|
|
*
|
2008-04-11 07:07:37 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
|
|
* <http://www.openoffice.org/license.html>
|
|
|
|
* for a copy of the LGPLv3 License.
|
2007-07-24 12:28:13 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
package complex.path_substitution;
|
|
|
|
|
|
|
|
import com.sun.star.lang.XMultiServiceFactory;
|
|
|
|
import com.sun.star.uno.UnoRuntime;
|
|
|
|
import com.sun.star.util.XStringSubstitution;
|
2010-06-28 12:54:49 +02:00
|
|
|
|
2007-07-24 12:28:13 +00:00
|
|
|
import java.util.Vector;
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
// ---------- junit imports -----------------
|
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.openoffice.test.OfficeConnection;
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
// ------------------------------------------
|
|
|
|
|
2007-07-24 12:28:13 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2010-06-28 12:54:49 +02:00
|
|
|
public class PathSubstitutionTest
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
|
|
|
|
private static XMultiServiceFactory xMSF;
|
|
|
|
// all substitution variables
|
|
|
|
private VariableContainer substVars = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A function to tell the framework, which test functions are available.
|
|
|
|
* Right now, it's only 'checkXStringSubstitution'.
|
|
|
|
* @return All test methods.
|
|
|
|
*/
|
2010-06-28 12:54:49 +02:00
|
|
|
// public String[] getTestMethodNames() {
|
|
|
|
// return new String[]{"checkXStringSubstitution"};
|
|
|
|
// }
|
2007-07-24 12:28:13 +00:00
|
|
|
/**
|
|
|
|
* Create an array with all substitution variables
|
|
|
|
*/
|
2010-06-28 12:54:49 +02:00
|
|
|
@Before private void initialize()
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
substVars = new VariableContainer();
|
|
|
|
substVars.add("$(prog)", false, true);
|
|
|
|
substVars.add("$(inst)", false, true);
|
|
|
|
substVars.add("$(user)", false, true);
|
|
|
|
substVars.add("$(work)", false, true);
|
|
|
|
substVars.add("$(home)", false, true);
|
|
|
|
substVars.add("$(temp)", false, true);
|
|
|
|
substVars.add("$(lang)", false, false);
|
|
|
|
substVars.add("$(langid)", false, false);
|
2010-06-28 12:54:49 +02:00
|
|
|
substVars.add("$(vlang)", false, false);
|
2007-07-24 12:28:13 +00:00
|
|
|
// path won't resubstitute
|
2010-06-28 12:54:49 +02:00
|
|
|
substVars.add("$(path)", false, false);
|
2007-07-24 12:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* One actual test: as the method 'getTestMethodNames()' tells.
|
|
|
|
*/
|
2010-06-28 12:54:49 +02:00
|
|
|
@Test public void checkXStringSubstitution()
|
2007-07-24 12:28:13 +00:00
|
|
|
{
|
2010-06-28 12:54:49 +02:00
|
|
|
xMSF = getMSF();
|
|
|
|
System.out.println("---- Testing the XStringSubstitution interface ----");
|
|
|
|
System.out.println("Create intance of test object.\n");
|
2007-07-24 12:28:13 +00:00
|
|
|
XStringSubstitution oObj = null;
|
2010-06-28 12:54:49 +02:00
|
|
|
try
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
Object x = xMSF.createInstance(
|
2010-06-28 12:54:49 +02:00
|
|
|
"com.sun.star.util.PathSubstitution");
|
|
|
|
oObj = UnoRuntime.queryInterface(XStringSubstitution.class, x);
|
|
|
|
if (oObj == null)
|
|
|
|
{
|
|
|
|
throw new com.sun.star.uno.Exception();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (com.sun.star.uno.Exception e)
|
|
|
|
{
|
|
|
|
System.out.println(e.getClass().getName());
|
|
|
|
System.out.println("Message: " + e.getMessage());
|
|
|
|
fail("Could not create an instance of the test object.");
|
2007-07-24 12:28:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
// initialize();
|
2007-07-24 12:28:13 +00:00
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
for (int i = 0; i < substVars.size(); i++)
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
String var = substVars.getVariable(i);
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println("Testing var '" + var + "'");
|
|
|
|
try
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
String substVal = oObj.getSubstituteVariableValue(var);
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println("\tvalue '" + substVal + "'");
|
|
|
|
substVars.putValue(i, substVal);
|
2007-07-24 12:28:13 +00:00
|
|
|
|
|
|
|
// simple check: let path in a string replace
|
|
|
|
String substString = var + "/additional/path";
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println("Substitute '" + substString + "'");
|
2007-07-24 12:28:13 +00:00
|
|
|
String newValue = oObj.substituteVariables(substString, true);
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println("Return value '" + newValue + "'");
|
2007-07-24 12:28:13 +00:00
|
|
|
// 2do: better check for correct substitution
|
2010-06-28 12:54:49 +02:00
|
|
|
assertTrue("Did not substitute '"
|
|
|
|
+ substString + "' to '" + newValue
|
2007-07-24 12:28:13 +00:00
|
|
|
+ "' correctly:", newValue.startsWith(substVal));
|
|
|
|
|
|
|
|
// simple check part two:
|
|
|
|
//make substitution backwards if possible
|
2010-06-28 12:54:49 +02:00
|
|
|
if (substVars.canReSubstitute(i))
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
substString = substVal + "/additional/path";
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println("Substitute backwards '" + substString + "'");
|
2007-07-24 12:28:13 +00:00
|
|
|
newValue = oObj.reSubstituteVariables(substString);
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println("Return value '" + newValue + "'");
|
2007-07-24 12:28:13 +00:00
|
|
|
// 2do: better check for correct substitution
|
2010-06-28 12:54:49 +02:00
|
|
|
assertTrue("Did not reSubstitute '"
|
2007-07-24 12:28:13 +00:00
|
|
|
+ substString + "' to '" + newValue
|
|
|
|
+ "' correctly:", checkResubstitute(newValue, var));
|
|
|
|
}
|
|
|
|
|
|
|
|
// simple check part three: look if replace
|
|
|
|
//in middle of text works
|
|
|
|
substString = "file:///starting/" + var + "/path";
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println("Substitute '" + substString + "'");
|
2007-07-24 12:28:13 +00:00
|
|
|
newValue = oObj.substituteVariables(substString, false);
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println("Return value '" + newValue + "'");
|
2007-07-24 12:28:13 +00:00
|
|
|
boolean erg = true;
|
2010-06-28 12:54:49 +02:00
|
|
|
if (substVars.onlySubstituteAtBegin(i))
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
// in this case it should not have worked
|
2010-06-28 12:54:49 +02:00
|
|
|
erg = newValue.indexOf(substVal) == -1;
|
|
|
|
}
|
2007-07-24 12:28:13 +00:00
|
|
|
else
|
2010-06-28 12:54:49 +02:00
|
|
|
{
|
|
|
|
erg = newValue.indexOf(substVal) != -1;
|
|
|
|
}
|
|
|
|
assertTrue("Did not substitute '"
|
2007-07-24 12:28:13 +00:00
|
|
|
+ substString + "' to '" + newValue
|
|
|
|
+ "' correctly:", erg);
|
|
|
|
|
|
|
|
}
|
2010-06-28 12:54:49 +02:00
|
|
|
catch (com.sun.star.uno.Exception e)
|
|
|
|
{
|
|
|
|
System.out.println(e.getClass().getName());
|
|
|
|
System.out.println("Message: " + e.getMessage());
|
|
|
|
fail("Could not create an instance of the test object.");
|
2007-07-24 12:28:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println("Finish testing '" + var + "'\n");
|
2007-07-24 12:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check of greedy resubstitution
|
|
|
|
String prog = "$(prog)";
|
|
|
|
String inst = "$(inst)";
|
|
|
|
String instPth = substVars.getValue(inst);
|
|
|
|
String progPth = substVars.getValue(prog);
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
if (progPth.startsWith(instPth) && instPth.startsWith(progPth))
|
|
|
|
{
|
|
|
|
System.out.println("Greedy ReSubstitute");
|
2007-07-24 12:28:13 +00:00
|
|
|
String substString = progPth + "/additional/path";
|
|
|
|
String newVal = oObj.reSubstituteVariables(substString);
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println("String '" + substString
|
|
|
|
+ "' should be resubstituted with");
|
|
|
|
System.out.println("Variable '" + prog + "' instead of Variable '"
|
|
|
|
+ inst + "'");
|
|
|
|
assertTrue("Did not reSubstitute '" + substString
|
2007-07-24 12:28:13 +00:00
|
|
|
+ "' to '" + newVal + "' correctly:",
|
|
|
|
newVal.startsWith(prog));
|
|
|
|
}
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
System.out.println(
|
2007-07-24 12:28:13 +00:00
|
|
|
"---- Finish testing the XStringSubstitution interface ----");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test the resubstitution
|
|
|
|
* @return true, if resubstitution is correct.
|
|
|
|
*/
|
2010-06-28 12:54:49 +02:00
|
|
|
private boolean checkResubstitute(String subst, String original)
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
// simple: subst starts with original
|
2010-06-28 12:54:49 +02:00
|
|
|
if (subst.startsWith(original))
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-06-28 12:54:49 +02:00
|
|
|
else
|
|
|
|
{ // hard: been resubstituted with a differernt variable.
|
|
|
|
for (int i = 0; i < substVars.size(); i++)
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
String var = substVars.getVariable(i);
|
2010-06-28 12:54:49 +02:00
|
|
|
if (subst.startsWith(var) && original.startsWith(original))
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-28 12:54:49 +02:00
|
|
|
|
2007-07-24 12:28:13 +00:00
|
|
|
/**
|
|
|
|
* Class for containing the substitution variables with their
|
|
|
|
* values and some information.
|
|
|
|
*/
|
2010-06-28 12:54:49 +02:00
|
|
|
private class VariableContainer
|
|
|
|
{
|
|
|
|
|
2007-07-24 12:28:13 +00:00
|
|
|
public Vector varName;
|
|
|
|
public Vector varValue;
|
|
|
|
public Vector substAtBegin;
|
|
|
|
public Vector resubst;
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
public VariableContainer()
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
varName = new Vector();
|
|
|
|
varValue = new Vector();
|
|
|
|
substAtBegin = new Vector();
|
|
|
|
resubst = new Vector();
|
|
|
|
}
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
public void add(String var)
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
varName.add(var);
|
|
|
|
substAtBegin.add(Boolean.TRUE);
|
|
|
|
resubst.add(Boolean.TRUE);
|
|
|
|
}
|
2010-06-28 12:54:49 +02:00
|
|
|
|
2007-07-24 12:28:13 +00:00
|
|
|
public void add(String var, boolean onlySubstAtBegin,
|
2010-06-28 12:54:49 +02:00
|
|
|
boolean canResubst)
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
varName.add(var);
|
|
|
|
this.substAtBegin.add(new Boolean(onlySubstAtBegin));
|
|
|
|
this.resubst.add(new Boolean(canResubst));
|
|
|
|
}
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
public void putValue(int i, String val)
|
|
|
|
{
|
2007-07-24 12:28:13 +00:00
|
|
|
varValue.add(i, val);
|
|
|
|
}
|
|
|
|
|
2010-06-28 12:54:49 +02:00
|
|
|
public int size()
|
|
|
|
{
|
|
|
|
return varName.size();
|
2007-07-24 12:28:13 +00:00
|
|
|
}
|
2010-06-28 12:54:49 +02:00
|
|
|
|
|
|
|
public String getVariable(int i)
|
|
|
|
{
|
|
|
|
return (String) varName.get(i);
|
2007-07-24 12:28:13 +00:00
|
|
|
}
|
2010-06-28 12:54:49 +02:00
|
|
|
|
|
|
|
public String getValue(int i)
|
|
|
|
{
|
|
|
|
return (String) varName.get(i);
|
2007-07-24 12:28:13 +00:00
|
|
|
}
|
2010-06-28 12:54:49 +02:00
|
|
|
|
|
|
|
public String getValue(String var)
|
|
|
|
{
|
|
|
|
return (String) varValue.get(varName.indexOf(var));
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean onlySubstituteAtBegin(int i)
|
|
|
|
{
|
|
|
|
return ((Boolean) substAtBegin.get(i)).booleanValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canReSubstitute(int i)
|
|
|
|
{
|
|
|
|
return ((Boolean) resubst.get(i)).booleanValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private XMultiServiceFactory getMSF()
|
|
|
|
{
|
|
|
|
final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
|
|
|
|
return xMSF1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup and close connections
|
|
|
|
@BeforeClass
|
|
|
|
public static void setUpConnection() throws Exception
|
|
|
|
{
|
|
|
|
System.out.println("setUpConnection()");
|
|
|
|
connection.setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
public static void tearDownConnection()
|
|
|
|
throws InterruptedException, com.sun.star.uno.Exception
|
|
|
|
{
|
|
|
|
System.out.println("tearDownConnection()");
|
|
|
|
connection.tearDown();
|
2007-07-24 12:28:13 +00:00
|
|
|
}
|
2010-06-28 12:54:49 +02:00
|
|
|
private static final OfficeConnection connection = new OfficeConnection();
|
2007-07-24 12:28:13 +00:00
|
|
|
}
|