Bug 496271, automation config for Tb2.0.0.22 build1, p=joduinn, r=me
[mozilla-1.9.git] / extensions / java / xpcom / tests / TestQI.java
blob97c500623ff0592c21de64fd2b1501455e906669
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Java XPCOM Bindings.
16 * The Initial Developer of the Original Code is IBM Corporation.
17 * Portions created by the Initial Developer are Copyright (C) 2007
18 * IBM Corporation. All Rights Reserved.
20 * Contributor(s):
21 * Javier Pedemonte (jhpedemonte@gmail.com)
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 import java.io.File;
38 import java.io.FileFilter;
39 import java.io.IOException;
41 import org.mozilla.xpcom.Mozilla;
42 import org.mozilla.interfaces.nsIFile;
43 import org.mozilla.interfaces.nsIFileURL;
44 import org.mozilla.interfaces.nsIServiceManager;
45 import org.mozilla.interfaces.nsISupports;
46 import org.mozilla.interfaces.nsIURI;
47 import org.mozilla.interfaces.nsIURL;
50 /**
51 * Simple test for the <code>XPCOM.queryInterface</code> method. It should be
52 * able to QI any interfaces implemented by the given object or its superclass
53 * (and so on up the inheritance chain).
55 * @see XPCOM#queryInterface
58 public class TestQI {
60 private static File grePath;
62 /**
63 * @param args 0 - full path to XULRunner binary directory
65 public static void main(String[] args) {
66 try {
67 checkArgs(args);
68 } catch (IllegalArgumentException e) {
69 System.exit(-1);
72 Mozilla mozilla = Mozilla.getInstance();
73 mozilla.initialize(grePath);
75 File profile = null;
76 nsIServiceManager servMgr = null;
77 try {
78 profile = createTempProfileDir();
79 LocationProvider locProvider = new LocationProvider(grePath,
80 profile);
81 servMgr = mozilla.initXPCOM(grePath, locProvider);
82 } catch (IOException e) {
83 e.printStackTrace();
84 System.exit(-1);
87 try {
88 runTest();
89 } catch (Exception e) {
90 e.printStackTrace();
91 System.exit(-1);
94 System.out.println("Test Passed.");
96 // cleanup
97 mozilla.shutdownXPCOM(servMgr);
98 deleteDir(profile);
101 private static void runTest() {
102 FooFile foo = new FooFile();
104 nsIFileURL fileURL = (nsIFileURL) foo
105 .queryInterface(nsIFileURL.NS_IFILEURL_IID);
106 if (fileURL == null) {
107 throw new RuntimeException("Failed to QI to nsIFileURL.");
110 nsIURL url = (nsIURL) foo.queryInterface(nsIURL.NS_IURL_IID);
111 if (url == null) {
112 throw new RuntimeException("Failed to QI to nsIURL.");
115 nsIURI uri = (nsIURI) foo.queryInterface(nsIURI.NS_IURI_IID);
116 if (uri == null) {
117 throw new RuntimeException("Failed to QI to nsIURI.");
120 nsISupports supp = (nsISupports) foo
121 .queryInterface(nsISupports.NS_ISUPPORTS_IID);
122 if (supp == null) {
123 throw new RuntimeException("Failed to QI to nsISupports.");
127 private static void checkArgs(String[] args) {
128 if (args.length != 1) {
129 printUsage();
130 throw new IllegalArgumentException();
133 grePath = new File(args[0]);
134 if (!grePath.exists() || !grePath.isDirectory()) {
135 System.err.println("ERROR: given path doesn't exist");
136 printUsage();
137 throw new IllegalArgumentException();
141 private static void printUsage() {
142 // TODO Auto-generated method stub
145 private static File createTempProfileDir() throws IOException {
146 // Get name of temporary profile directory
147 File profile = File.createTempFile("mozilla-test-", null);
148 profile.delete();
150 // On some operating systems (particularly Windows), the previous
151 // temporary profile may not have been deleted. Delete them now.
152 File[] files = profile.getParentFile()
153 .listFiles(new FileFilter() {
154 public boolean accept(File file) {
155 if (file.getName().startsWith("mozilla-test-")) {
156 return true;
158 return false;
161 for (int i = 0; i < files.length; i++) {
162 deleteDir(files[i]);
165 // Create temporary profile directory
166 profile.mkdir();
168 return profile;
171 private static void deleteDir(File dir) {
172 File[] files = dir.listFiles();
173 for (int i = 0; i < files.length; i++) {
174 if (files[i].isDirectory()) {
175 deleteDir(files[i]);
177 files[i].delete();
179 dir.delete();
185 * Dummy class that implements nsIFileUrl. The inheritance chain for
186 * nsIFileURL is as follows:
187 * <pre>
188 * nsIFileURL -> nsIURL -> nsIURI -> nsISupports
189 * </pre>
190 * <p>
191 * The only method that is implemented is <code>queryInterface</code>, which
192 * simply calls <code>XPCOM.queryInterface</code>.
193 * </p>
195 * @see XPCOM#queryInterface
197 class FooFile implements nsIFileURL {
199 public nsISupports queryInterface(String aIID) {
200 return Mozilla.queryInterface(this, aIID);
203 public nsIFile getFile() {
204 return null;
207 public void setFile(nsIFile aFile) {
210 public String getFilePath() {
211 return null;
214 public void setFilePath(String aFilePath) {
217 public String getParam() {
218 return null;
221 public void setParam(String aParam) {
224 public String getQuery() {
225 return null;
228 public void setQuery(String aQuery) {
231 public String getRef() {
232 return null;
235 public void setRef(String aRef) {
238 public String getDirectory() {
239 return null;
242 public void setDirectory(String aDirectory) {
245 public String getFileName() {
246 return null;
249 public void setFileName(String aFileName) {
252 public String getFileBaseName() {
253 return null;
256 public void setFileBaseName(String aFileBaseName) {
259 public String getFileExtension() {
260 return null;
263 public void setFileExtension(String aFileExtension) {
266 public String getCommonBaseSpec(nsIURI aURIToCompare) {
267 return null;
270 public String getRelativeSpec(nsIURI aURIToCompare) {
271 return null;
274 public String getSpec() {
275 return null;
278 public void setSpec(String aSpec) {
281 public String getPrePath() {
282 return null;
285 public String getScheme() {
286 return null;
289 public void setScheme(String aScheme) {
292 public String getUserPass() {
293 return null;
296 public void setUserPass(String aUserPass) {
299 public String getUsername() {
300 return null;
303 public void setUsername(String aUsername) {
306 public String getPassword() {
307 return null;
310 public void setPassword(String aPassword) {
313 public String getHostPort() {
314 return null;
317 public void setHostPort(String aHostPort) {
320 public String getHost() {
321 return null;
324 public void setHost(String aHost) {
327 public int getPort() {
328 return 0;
331 public void setPort(int aPort) {
334 public String getPath() {
335 return null;
338 public void setPath(String aPath) {
341 public boolean _equals(nsIURI other) {
342 return false;
345 public boolean schemeIs(String scheme) {
346 return false;
349 public nsIURI _clone() {
350 return null;
353 public String resolve(String relativePath) {
354 return null;
357 public String getAsciiSpec() {
358 return null;
361 public String getAsciiHost() {
362 return null;
365 public String getOriginCharset() {
366 return null;