merge backout. CLOSED TREE
[mozilla-central.git] / netwerk / test / PropertiesTest.cpp
blobb87e4e0c6f2e42de552b2fec9dad7e1da37f416e
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);
72 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
73 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
74 registrar->AutoRegister(nsnull);
76 nsIInputStream* in = nsnull;
78 nsCOMPtr<nsIIOService> service(do_GetService(kIOServiceCID, &ret));
79 if (NS_FAILED(ret)) return ret;
81 nsIChannel *channel = nsnull;
82 ret = service->NewChannel(NS_LITERAL_CSTRING(TEST_URL), nsnull, nsnull, &channel);
83 if (NS_FAILED(ret)) return ret;
85 ret = channel->Open(&in);
86 if (NS_FAILED(ret)) return ret;
88 nsIPersistentProperties* props;
89 ret = CallCreateInstance(kPersistentPropertiesCID, &props);
90 if (NS_FAILED(ret) || (!props)) {
91 printf("create nsIPersistentProperties failed\n");
92 return 1;
94 ret = props->Load(in);
95 if (NS_FAILED(ret)) {
96 printf("cannot load properties\n");
97 return 1;
99 int i = 1;
100 while (1) {
101 char name[16];
102 name[0] = 0;
103 sprintf(name, "%d", i);
104 nsAutoString v;
105 ret = props->GetStringProperty(nsDependentCString(name), v);
106 if (NS_FAILED(ret) || (!v.Length())) {
107 break;
109 printf("\"%d\"=\"%s\"\n", i, NS_ConvertUTF16toUTF8(v).get());
110 i++;
113 nsCOMPtr<nsISimpleEnumerator> propEnum;
114 ret = props->Enumerate(getter_AddRefs(propEnum));
116 if (NS_FAILED(ret)) {
117 printf("cannot enumerate properties\n");
118 return 1;
122 printf("\nKey\tValue\n");
123 printf( "---\t-----\n");
125 PRBool hasMore;
126 while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) &&
127 hasMore) {
128 nsCOMPtr<nsISupports> sup;
129 ret = propEnum->GetNext(getter_AddRefs(sup));
131 nsCOMPtr<nsIPropertyElement> propElem = do_QueryInterface(sup, &ret);
132 if (NS_FAILED(ret)) {
133 printf("failed to get current item\n");
134 return 1;
137 nsCAutoString key;
138 nsAutoString value;
140 ret = propElem->GetKey(key);
141 if (NS_FAILED(ret)) {
142 printf("failed to get current element's key\n");
143 return 1;
145 ret = propElem->GetValue(value);
146 if (NS_FAILED(ret)) {
147 printf("failed to get current element's value\n");
148 return 1;
151 printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get());
153 return 0;