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
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.
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 ***** */
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
;
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
60 private static File grePath
;
63 * @param args 0 - full path to XULRunner binary directory
65 public static void main(String
[] args
) {
68 } catch (IllegalArgumentException e
) {
72 Mozilla mozilla
= Mozilla
.getInstance();
73 mozilla
.initialize(grePath
);
76 nsIServiceManager servMgr
= null;
78 profile
= createTempProfileDir();
79 LocationProvider locProvider
= new LocationProvider(grePath
,
81 servMgr
= mozilla
.initXPCOM(grePath
, locProvider
);
82 } catch (IOException e
) {
89 } catch (Exception e
) {
94 System
.out
.println("Test Passed.");
97 mozilla
.shutdownXPCOM(servMgr
);
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
);
112 throw new RuntimeException("Failed to QI to nsIURL.");
115 nsIURI uri
= (nsIURI
) foo
.queryInterface(nsIURI
.NS_IURI_IID
);
117 throw new RuntimeException("Failed to QI to nsIURI.");
120 nsISupports supp
= (nsISupports
) foo
121 .queryInterface(nsISupports
.NS_ISUPPORTS_IID
);
123 throw new RuntimeException("Failed to QI to nsISupports.");
127 private static void checkArgs(String
[] args
) {
128 if (args
.length
!= 1) {
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");
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);
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-")) {
161 for (int i
= 0; i
< files
.length
; i
++) {
165 // Create temporary profile directory
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()) {
185 * Dummy class that implements nsIFileUrl. The inheritance chain for
186 * nsIFileURL is as follows:
188 * nsIFileURL -> nsIURL -> nsIURI -> nsISupports
191 * The only method that is implemented is <code>queryInterface</code>, which
192 * simply calls <code>XPCOM.queryInterface</code>.
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() {
207 public void setFile(nsIFile aFile
) {
210 public String
getFilePath() {
214 public void setFilePath(String aFilePath
) {
217 public String
getParam() {
221 public void setParam(String aParam
) {
224 public String
getQuery() {
228 public void setQuery(String aQuery
) {
231 public String
getRef() {
235 public void setRef(String aRef
) {
238 public String
getDirectory() {
242 public void setDirectory(String aDirectory
) {
245 public String
getFileName() {
249 public void setFileName(String aFileName
) {
252 public String
getFileBaseName() {
256 public void setFileBaseName(String aFileBaseName
) {
259 public String
getFileExtension() {
263 public void setFileExtension(String aFileExtension
) {
266 public String
getCommonBaseSpec(nsIURI aURIToCompare
) {
270 public String
getRelativeSpec(nsIURI aURIToCompare
) {
274 public String
getSpec() {
278 public void setSpec(String aSpec
) {
281 public String
getPrePath() {
285 public String
getScheme() {
289 public void setScheme(String aScheme
) {
292 public String
getUserPass() {
296 public void setUserPass(String aUserPass
) {
299 public String
getUsername() {
303 public void setUsername(String aUsername
) {
306 public String
getPassword() {
310 public void setPassword(String aPassword
) {
313 public String
getHostPort() {
317 public void setHostPort(String aHostPort
) {
320 public String
getHost() {
324 public void setHost(String aHost
) {
327 public int getPort() {
331 public void setPort(int aPort
) {
334 public String
getPath() {
338 public void setPath(String aPath
) {
341 public boolean _equals(nsIURI other
) {
345 public boolean schemeIs(String scheme
) {
349 public nsIURI
_clone() {
353 public String
resolve(String relativePath
) {
357 public String
getAsciiSpec() {
361 public String
getAsciiHost() {
365 public String
getOriginCharset() {