Bug 601299: Find RegExpStatics in cx->globalObject if necessary. (r=mrbkap)
[mozilla-central.git] / netwerk / test / PropertiesTest.cpp
blob29ede48fbce4f2a47c89a2f4e9509db0137e7229
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "TestCommon.h"
40 #include "nsXPCOM.h"
41 #include "nsStringAPI.h"
42 #include "nsIPersistentProperties2.h"
43 #include "nsIServiceManager.h"
44 #include "nsIComponentRegistrar.h"
45 #include "nsIURL.h"
46 #include "nsIIOService.h"
47 #include "nsNetCID.h"
48 #include "nsIChannel.h"
49 #include "nsIComponentManager.h"
50 #include "nsIEnumerator.h"
51 #include <stdio.h>
52 #include "nsComponentManagerUtils.h"
53 #include "nsServiceManagerUtils.h"
55 #define TEST_URL "resource:/res/test.properties"
56 static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID);
58 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
60 /***************************************************************************/
62 int
63 main(int argc, char* argv[])
65 if (test_common_init(&argc, &argv) != 0)
66 return -1;
68 nsresult ret;
70 nsCOMPtr<nsIServiceManager> servMan;
71 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
73 nsIInputStream* in = nsnull;
75 nsCOMPtr<nsIIOService> service(do_GetService(kIOServiceCID, &ret));
76 if (NS_FAILED(ret)) return ret;
78 nsIChannel *channel = nsnull;
79 ret = service->NewChannel(NS_LITERAL_CSTRING(TEST_URL), nsnull, nsnull, &channel);
80 if (NS_FAILED(ret)) return ret;
82 ret = channel->Open(&in);
83 if (NS_FAILED(ret)) return ret;
85 nsIPersistentProperties* props;
86 ret = CallCreateInstance(kPersistentPropertiesCID, &props);
87 if (NS_FAILED(ret) || (!props)) {
88 printf("create nsIPersistentProperties failed\n");
89 return 1;
91 ret = props->Load(in);
92 if (NS_FAILED(ret)) {
93 printf("cannot load properties\n");
94 return 1;
96 int i = 1;
97 while (1) {
98 char name[16];
99 name[0] = 0;
100 sprintf(name, "%d", i);
101 nsAutoString v;
102 ret = props->GetStringProperty(nsDependentCString(name), v);
103 if (NS_FAILED(ret) || (!v.Length())) {
104 break;
106 printf("\"%d\"=\"%s\"\n", i, NS_ConvertUTF16toUTF8(v).get());
107 i++;
110 nsCOMPtr<nsISimpleEnumerator> propEnum;
111 ret = props->Enumerate(getter_AddRefs(propEnum));
113 if (NS_FAILED(ret)) {
114 printf("cannot enumerate properties\n");
115 return 1;
119 printf("\nKey\tValue\n");
120 printf( "---\t-----\n");
122 PRBool hasMore;
123 while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) &&
124 hasMore) {
125 nsCOMPtr<nsISupports> sup;
126 ret = propEnum->GetNext(getter_AddRefs(sup));
128 nsCOMPtr<nsIPropertyElement> propElem = do_QueryInterface(sup, &ret);
129 if (NS_FAILED(ret)) {
130 printf("failed to get current item\n");
131 return 1;
134 nsCAutoString key;
135 nsAutoString value;
137 ret = propElem->GetKey(key);
138 if (NS_FAILED(ret)) {
139 printf("failed to get current element's key\n");
140 return 1;
142 ret = propElem->GetValue(value);
143 if (NS_FAILED(ret)) {
144 printf("failed to get current element's value\n");
145 return 1;
148 printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get());
150 return 0;