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