xref: /aoo4110/main/ucb/qa/complex/ucb/UCB.java (revision b1cdbd2c)
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 
24 package complex.ucb;
25 
26 /**
27  * @author ab106281
28  *
29  * To change the template for this generated type comment go to
30  * Window>Preferences>Java>Code Generation>Code and Comments
31  */
32 
33 import java.util.List;
34 import java.util.Vector;
35 
36 import com.sun.star.beans.Property;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sdbc.XResultSet;
39 import com.sun.star.sdbc.XRow;
40 // import com.sun.star.uno.XComponentContext;
41 import com.sun.star.ucb.*;
42 // import com.sun.star.bridge.XUnoUrlResolver;
43 import com.sun.star.uno.UnoRuntime;
44 // import com.sun.star.uno.XComponentContext;
45 // import com.sun.star.lang.XMultiComponentFactory;
46 // import com.sun.star.beans.XPropertySet;
47 
48 import org.junit.After;
49 import org.junit.AfterClass;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53 import org.openoffice.test.OfficeConnection;
54 import static org.junit.Assert.*;
55 
56 /**
57  * @author rpiterman
58  * This class is used to copy the content of a folder to
59  * another folder.
60  * There is an incosistency with argument order.
61  * It should be always: dir,filename.
62  */
63 public class UCB  {
64 	private Object ucb;
65 
66 //	public String[] getTestMethodNames() {
67 //		return new String[] {"checkWrongFtpConnection"};
68 //	}
69 
init(XMultiServiceFactory xmsf)70 	public void init(XMultiServiceFactory xmsf) throws Exception {
71 		String[] keys = new String[2];
72 		keys[0] = "Local";
73 		keys[1] = "Office";
74 		ucb =
75 			xmsf.createInstanceWithArguments(
76 				"com.sun.star.ucb.UniversalContentBroker",
77 				keys);
78 	}
79 
delete(String filename)80 	public void delete(String filename) throws Exception {
81 		executeCommand(getContent(filename), "delete", Boolean.TRUE);
82 	}
83 
84 	/**
85 	 * target name can be "", in which case the name stays lige the source name
86 
87          * @param xContent
88          * @param aCommandName
89          * @param aArgument
90          * @return
91          * @throws com.sun.star.ucb.CommandAbortedException
92          * @throws com.sun.star.uno.Exception
93 	 */
94 
executeCommand( Object xContent, String aCommandName, Object aArgument)95 	public Object executeCommand(
96 		Object xContent,
97 		String aCommandName,
98 		Object aArgument)
99 		throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
100 		XCommandProcessor xCmdProcessor =
101 			UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
102 		Command aCommand = new Command();
103 		aCommand.Name = aCommandName;
104 		aCommand.Handle = -1; // not available
105 		aCommand.Argument = aArgument;
106 		return xCmdProcessor.execute(aCommand, 0, null);
107 	}
108 
listFiles(String path, Verifier verifier)109 	private List listFiles(String path, Verifier verifier) throws Exception {
110 		Object xContent = getContent(path);
111 
112 		OpenCommandArgument2 aArg = new OpenCommandArgument2();
113 		aArg.Mode = OpenMode.ALL;
114 		aArg.Priority = 32768;
115 
116 		// Fill info for the properties wanted.
117 		aArg.Properties = new Property[] { new Property()};
118 
119 		aArg.Properties[0].Name = "Title";
120 		aArg.Properties[0].Handle = -1;
121 
122 		XDynamicResultSet xSet;
123 
124 		xSet =
125 			UnoRuntime.queryInterface(XDynamicResultSet.class, executeCommand(xContent, "open", aArg));
126 
127 		XResultSet xResultSet = xSet.getStaticResultSet();
128 
129 		List files = new Vector();
130 
131 		if (xResultSet.first())
132                 {
133 			// obtain XContentAccess interface for child content access and XRow for properties
134 			XContentAccess xContentAccess =	UnoRuntime.queryInterface(XContentAccess.class, xResultSet);
135 			XRow xRow = UnoRuntime.queryInterface(XRow.class, xResultSet);
136 			do
137                         {
138 				// Obtain URL of child.
139 				String aId = xContentAccess.queryContentIdentifierString();
140 				// First column: Title (column numbers are 1-based!)
141 				String aTitle = xRow.getString(1);
142 				if (aTitle.length() == 0 && xRow.wasNull())
143                                 {
144                                     //ignore
145                                 }
146 				else
147                                 {
148                                     files.add(aTitle);
149                                 }
150 			} while (xResultSet.next()); // next child
151 		}
152 
153 		if (verifier != null)
154                 {
155                     for (int i = 0; i < files.size(); i++)
156                     {
157                         if (!verifier.verify(files.get(i)))
158                         {
159                             files.remove(i--);
160                         }
161                     }
162                 }
163 		return files;
164 	}
165 
getContentProperty( Object content, String propName, Class type)166 	public Object getContentProperty(
167 		Object content,
168 		String propName,
169 		Class type)
170 		throws Exception {
171 		Property[] pv = new Property[1];
172 		pv[0] = new Property();
173 		pv[0].Name = propName;
174 		pv[0].Handle = -1;
175 
176 		Object row = executeCommand(content, "getPropertyValues", pv);
177 		XRow xrow = UnoRuntime.queryInterface(XRow.class, row);
178 		if (type.equals(String.class))
179                 {
180                     return xrow.getString(1);
181                 }
182 		else if (type.equals(Boolean.class))
183                 {
184                     return xrow.getBoolean(1) ? Boolean.TRUE : Boolean.FALSE;
185                 }
186 		else if (type.equals(Integer.class))
187                 {
188                     return new Integer(xrow.getInt(1));
189                 }
190 		else if (type.equals(Short.class))
191                 {
192                     return new Short(xrow.getShort(1));
193                 }
194 		else
195                 {
196                     return null;
197                 }
198 
199 	}
200 
getContent(String path)201 	public Object getContent(String path) throws Exception
202         {
203 		XContentIdentifier id =	(UnoRuntime.queryInterface(XContentIdentifierFactory.class, ucb)).createContentIdentifier(path);
204 		return (UnoRuntime.queryInterface(XContentProvider.class, ucb)).queryContent(id);
205 	}
206 
207 	public static interface Verifier {
verify(Object object)208 		public boolean verify(Object object);
209 	}
210 
checkWrongFtpConnection()211 	@Test public void checkWrongFtpConnection() {
212 		//localhost  ;Lo-1.Germany.sun.com; 10.16.65.155
213 		try {
214 			XMultiServiceFactory xLocMSF = getMSF();
215 			String acountUrl = "ftp://noname:nopasswd@nohost";
216 			System.out.println(acountUrl);
217 			init(xLocMSF);
218 			Object content = getContent(acountUrl);
219 
220 			OpenCommandArgument2 aArg = new OpenCommandArgument2();
221 			aArg.Mode = OpenMode.ALL; // FOLDER, DOCUMENTS -> simple filter
222 			aArg.Priority = 32768; // Ignored by most implementations
223 
224 			System.out.println("now executing open");
225 			executeCommand(content, "open", aArg);
226 			fail("Expected 'IllegalArgumentException' was not thrown.");
227 		} catch (com.sun.star.lang.IllegalArgumentException ex) {
228 			//TODO error message;
229 			System.out.println("Correct exception thrown: " + ex.getClass().toString());
230 		} catch(com.sun.star.ucb.InteractiveNetworkException ex) {
231 			System.out.println("This Exception is correctly thrown when no Proxy in StarOffice is used.");
232 			System.out.println("To reproduce the bug behaviour, use a Proxy and try again.");
233 		} catch (Exception ex) {
234 			ex.printStackTrace();
235 			String exceptionName = ex.toString();
236 			System.out.println("ExName: '"+exceptionName+"'");
237 			fail("Wrong exception thrown: " + exceptionName);
238 		}
239 //		System.exit(0);
240 	}
241 
242 
243 
getMSF()244          private XMultiServiceFactory getMSF()
245     {
246         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
247         return xMSF1;
248     }
249 
250     // setup and close connections
setUpConnection()251     @BeforeClass public static void setUpConnection() throws Exception {
252         System.out.println("setUpConnection()");
253         connection.setUp();
254     }
255 
tearDownConnection()256     @AfterClass public static void tearDownConnection()
257         throws InterruptedException, com.sun.star.uno.Exception
258     {
259         System.out.println("tearDownConnection()");
260         connection.tearDown();
261     }
262 
263     private static final OfficeConnection connection = new OfficeConnection();
264 
265 }
266