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