Back to index

lightning-sunbird  0.9+nobinonly
TestProps.java
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Java XPCOM Bindings.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * IBM Corporation. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Javier Pedemonte (jhpedemonte@gmail.com)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 import org.mozilla.xpcom.*;
00039 import java.io.*;
00040 import java.util.*;
00041 
00042 
00049 public class TestProps {
00050   public static final String NS_PROPERTIES_CONTRACTID = "@mozilla.org/properties;1";
00051 
00052   public static void main(String [] args) throws Exception {
00053     System.loadLibrary("javaxpcom");
00054 
00055     String mozillaPath = System.getProperty("MOZILLA_FIVE_HOME");
00056     if (mozillaPath == null) {
00057       throw new RuntimeException("MOZILLA_FIVE_HOME system property not set.");
00058     }
00059 
00060     File localFile = new File(mozillaPath);
00061     XPCOM.initXPCOM(localFile, null);
00062     // XPCOM.initXPCOM() only initializes XPCOM.  If you want to initialize
00063     // Gecko, you would do the following instead:
00064     //    GeckoEmbed.initEmbedding(localFile, null);
00065 
00066     nsIComponentManager componentManager = XPCOM.getComponentManager();
00067     nsIProperties props = (nsIProperties)
00068       componentManager.createInstanceByContractID(NS_PROPERTIES_CONTRACTID, null,
00069                                                   nsIProperties.NS_IPROPERTIES_IID);
00070     if (props == null) {
00071       throw new RuntimeException("Failed to create nsIProperties.");
00072     }
00073 
00074     // create the nsISupports objects we will use
00075     nsILocalFile localFile1 = XPCOM.newLocalFile("/user/local/share", false);
00076     nsILocalFile localFile2 = XPCOM.newLocalFile("/home/foo", false);
00077     nsILocalFile localFile3 = XPCOM.newLocalFile("/home/foo/bar", false);
00078 
00079     // set the properties and associate with the created objects
00080     props.set("File One", localFile1);
00081     props.set("File Two", localFile2);
00082     props.set("File One Repeated", localFile1);
00083     props.set("File Three", localFile3);
00084 
00085     // test the "has" method
00086     boolean hasProp = props.has("File One");
00087     if (hasProp == false)
00088       throw new NoSuchElementException("Could not find property 'File One'.");
00089     hasProp = props.has("File One Repeated");
00090     if (hasProp == false)
00091       throw new NoSuchElementException("Could not find property 'File One Repeated'.");
00092     hasProp = props.has("Nonexistant Property");
00093     if (hasProp == true)
00094       throw new Exception("Found property that doesn't exist.");
00095 
00096     // test the "get" method
00097     nsILocalFile tempLocalFile = (nsILocalFile) props.get("File One Repeated",
00098                                                           nsILocalFile.NS_ILOCALFILE_IID);
00099     if (tempLocalFile == null)
00100       throw new NoSuchElementException("Property 'File One Repeated' not found.");
00101     if (tempLocalFile != localFile1)
00102       throw new Exception("Object returned by 'get' not the same as object passed to 'set'.");
00103 
00104     // test the "undefine" method
00105     hasProp = props.has("File Two");
00106     if (hasProp == false)
00107       throw new NoSuchElementException();
00108     props.undefine("File Two");
00109     hasProp = props.has("File Two");
00110     if (hasProp == true)
00111       throw new NoSuchElementException();
00112 
00113     // test the "getKeys" method
00114     long[] count = new long[1];
00115     String[] keys = props.getKeys(count);
00116     if (keys == null || keys.length != 3) {
00117       System.out.println("getKeys returned incorrect array.");
00118     }
00119     for (int i = 0; i < keys.length; i++) {
00120       System.out.println("key " + i + ": " + keys[i]);
00121     }
00122 
00123     XPCOM.shutdownXPCOM(null);
00124     //    GeckoEmbed.termEmbedding();
00125 
00126     System.out.println("Test Passed.");
00127   }
00128 }
00129