Back to index

lightning-sunbird  0.9+nobinonly
PropertiesTest.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "TestCommon.h"
00040 #include "nsXPCOM.h"
00041 #include "nsString.h"
00042 #include "nsIEventQueueService.h"
00043 #include "nsIPersistentProperties2.h"
00044 #include "nsIServiceManager.h"
00045 #include "nsIComponentRegistrar.h"
00046 #include "nsIURL.h"
00047 #include "nsIIOService.h"
00048 #include "nsNetCID.h"
00049 #include "nsIChannel.h"
00050 #include "nsIComponentManager.h"
00051 #include "nsIEnumerator.h"
00052 #include <stdio.h>
00053 #include "nsReadableUtils.h"
00054 
00055 
00056 #define TEST_URL "resource:/res/test.properties"
00057 static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID);
00058 
00059 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
00060 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00061 
00062 /***************************************************************************/
00063 
00064 int
00065 main(int argc, char* argv[])
00066 {
00067   if (test_common_init(&argc, &argv) != 0)
00068     return -1;
00069 
00070   nsresult ret;
00071 
00072 
00073   nsCOMPtr<nsIServiceManager> servMan;
00074   NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00075   nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00076   NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00077   registrar->AutoRegister(nsnull);
00078 
00079   nsIInputStream* in = nsnull;
00080 
00081   nsCOMPtr<nsIIOService> service(do_GetService(kIOServiceCID, &ret));
00082   if (NS_FAILED(ret)) return ret;
00083 
00084   nsCOMPtr<nsIEventQueueService> eventQService = 
00085            do_GetService(kEventQueueServiceCID, &ret);
00086   if (NS_FAILED(ret)) return ret;
00087 
00088   nsIChannel *channel = nsnull;
00089   ret = service->NewChannel(NS_LITERAL_CSTRING(TEST_URL), nsnull, nsnull, &channel);
00090   if (NS_FAILED(ret)) return ret;
00091 
00092   nsIEventQueue *eventQ = nsnull;
00093   ret = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &eventQ);
00094   if (NS_FAILED(ret)) return ret;
00095 
00096   ret = channel->Open(&in);
00097   if (NS_FAILED(ret)) return ret;
00098 
00099   nsIPersistentProperties* props;
00100   ret = CallCreateInstance(kPersistentPropertiesCID, &props);
00101   if (NS_FAILED(ret) || (!props)) {
00102     printf("create nsIPersistentProperties failed\n");
00103     return 1;
00104   }
00105   ret = props->Load(in);
00106   if (NS_FAILED(ret)) {
00107     printf("cannot load properties\n");
00108     return 1;
00109   }
00110   int i = 1;
00111   while (1) {
00112     char name[16];
00113     name[0] = 0;
00114     sprintf(name, "%d", i);
00115     nsAutoString v;
00116     ret = props->GetStringProperty(nsDependentCString(name), v);
00117     if (NS_FAILED(ret) || (!v.Length())) {
00118       break;
00119     }
00120     char* value = ToNewCString(v);
00121     if (value) {
00122       printf("\"%d\"=\"%s\"\n", i, value);
00123       delete[] value;
00124     }
00125     else {
00126       printf("%d: ToNewCString failed\n", i);
00127     }
00128     i++;
00129   }
00130 
00131   nsCOMPtr<nsISimpleEnumerator> propEnum;
00132   ret = props->Enumerate(getter_AddRefs(propEnum));
00133 
00134   if (NS_FAILED(ret)) {
00135     printf("cannot enumerate properties\n");
00136     return 1;
00137   }
00138   
00139 
00140   printf("\nKey\tValue\n");
00141   printf(  "---\t-----\n");
00142   
00143   PRBool hasMore;
00144   while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) &&
00145          hasMore) {
00146     nsCOMPtr<nsISupports> sup;
00147     ret = propEnum->GetNext(getter_AddRefs(sup));
00148     
00149     nsCOMPtr<nsIPropertyElement> propElem = do_QueryInterface(sup, &ret);
00150          if (NS_FAILED(ret)) {
00151       printf("failed to get current item\n");
00152       return 1;
00153          }
00154 
00155     nsCAutoString key;
00156     nsAutoString value;
00157 
00158     ret = propElem->GetKey(key);
00159          if (NS_FAILED(ret)) {
00160                 printf("failed to get current element's key\n");
00161                 return 1;
00162          }
00163     ret = propElem->GetValue(value);
00164          if (NS_FAILED(ret)) {
00165                 printf("failed to get current element's value\n");
00166                 return 1;
00167          }
00168 
00169     printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get());
00170   }
00171   return 0;
00172 }