~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/netwerk/test/TestCacheService.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is TestCacheService.cpp, released February 7, 2001.
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape Communications
 
16
 * Corporation.  Portions created by Netscape are
 
17
 * Copyright (C) 2001 Netscape Communications Corporation.  All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s): 
 
21
 *    Gordon Sheridan, 7-February-2001
 
22
 */
 
23
 
 
24
 
 
25
#include "nspr.h"
 
26
#include "nscore.h"
 
27
#include "nsError.h"
 
28
#include "nsIServiceManager.h"
 
29
#include "nsNetCID.h"
 
30
#include "nsICache.h"
 
31
#include "nsICacheService.h"
 
32
#include "nsICacheSession.h"
 
33
#include "nsICacheEntryDescriptor.h"
 
34
#include "nsICacheListener.h"
 
35
#include "nsIDNSService.h"
 
36
#include "nsXPCOM.h"
 
37
#include "nsISupportsPrimitives.h"
 
38
#include "nsSupportsPrimitives.h"
 
39
#include "nsIEventQueueService.h"
 
40
 
 
41
 
 
42
static NS_DEFINE_CID(kEventQueueServiceCID,      NS_EVENTQUEUESERVICE_CID);
 
43
static NS_DEFINE_CID(kCacheServiceCID,           NS_CACHESERVICE_CID);
 
44
 
 
45
nsCOMPtr<nsIEventQueue>   gEventQ;
 
46
nsCOMPtr<nsICacheService> gCacheService;
 
47
nsCOMPtr<nsICacheSession> gSession;
 
48
 
 
49
class AsyncCacheRequest
 
50
{
 
51
public:
 
52
    AsyncCacheRequest(const char * key);
 
53
    ~AsyncCacheRequest();
 
54
 
 
55
    const char * mKey;
 
56
};
 
57
 
 
58
 
 
59
nsresult
 
60
MakeCacheSession(const char * clientID, nsICacheSession **session)
 
61
{
 
62
    nsresult rv;
 
63
 
 
64
    if (!gCacheService) {
 
65
        //    nsCOMPtr<nsICacheService> cacheService = 
 
66
        //             do_GetService(kCacheServiceCID, &rv);
 
67
        gCacheService = do_GetService(kCacheServiceCID, &rv);
 
68
        if (NS_FAILED(rv) || !gCacheService) {
 
69
            printf("do_GetService(kCacheServiceCID) failed : %x\n", rv);
 
70
            goto error_exit;
 
71
        }
 
72
    }
 
73
 
 
74
    rv = gCacheService->CreateSession(clientID,
 
75
                                     nsICache::STORE_IN_MEMORY,
 
76
                                     nsICache::NOT_STREAM_BASED,
 
77
                                     session);
 
78
    if (NS_FAILED(rv))
 
79
        printf("nsCacheService::CreateSession() failed : %x\n", rv);
 
80
 
 
81
 error_exit:
 
82
    return rv;
 
83
}
 
84
 
 
85
 
 
86
void
 
87
TestMemoryObjectCache()
 
88
{
 
89
    printf("\nTesting Memory Object Cache:\n");
 
90
    nsCOMPtr<nsICacheSession> session;
 
91
    nsresult rv = MakeCacheSession("testClientID", getter_AddRefs(session));
 
92
    if (NS_FAILED(rv))  return;
 
93
 
 
94
    nsCOMPtr<nsICacheEntryDescriptor> descriptor;
 
95
 
 
96
    // Test ACCESS_READ for non-existent entry
 
97
    printf("\nTest ACCESS_READ:\n");
 
98
    rv = session->OpenCacheEntry("non-existent entry",
 
99
                                 nsICache::ACCESS_READ,
 
100
                                 nsICache::BLOCKING,
 
101
                                 getter_AddRefs(descriptor));
 
102
    if (rv != NS_ERROR_CACHE_KEY_NOT_FOUND)
 
103
        printf("OpenCacheEntry(ACCESS_READ) returned: %x for non-existent entry\n", rv);
 
104
 
 
105
    // Test creating new entry
 
106
    printf("\nTest ACCESS_READ_WRITE:\n");
 
107
    rv = session->OpenCacheEntry("http://www.mozilla.org/somekey",
 
108
                                 nsICache::ACCESS_READ_WRITE,
 
109
                                 nsICache::BLOCKING,
 
110
                                 getter_AddRefs(descriptor));
 
111
    if (NS_FAILED(rv)) {
 
112
        printf("OpenCacheEntry(ACCESS_READ_WRITE) failed: %x\n", rv);
 
113
        goto error_exit;
 
114
    }
 
115
 
 
116
    nsCOMPtr<nsISupportsCString> foo =
 
117
        do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
 
118
 
 
119
    foo->SetData(NS_LITERAL_CSTRING("hello world"));
 
120
 
 
121
    rv = descriptor->SetCacheElement(foo);
 
122
    rv = descriptor->SetDataSize(11);
 
123
    rv = descriptor->SetMetaDataElement("itemOne", "metaData works");
 
124
    descriptor = nsnull;
 
125
 
 
126
    // Test refetching entry
 
127
 
 
128
    rv = session->OpenCacheEntry("http://www.mozilla.org/somekey",
 
129
                                 nsICache::ACCESS_READ_WRITE,
 
130
                                 nsICache::BLOCKING,
 
131
                                 getter_AddRefs(descriptor));
 
132
    if (NS_FAILED(rv)) {
 
133
        printf("OpenCacheEntry(ACCESS_READ_WRITE #2) failed: %x", rv);
 
134
        goto error_exit;
 
135
    }
 
136
 
 
137
    nsCOMPtr<nsISupportsCString> bar;
 
138
    descriptor->GetCacheElement(getter_AddRefs(bar));
 
139
    if (foo.get() != bar.get()) {
 
140
        printf("cache elements not the same\n");
 
141
    } else {
 
142
        printf("data matches...\n");
 
143
    }
 
144
 
 
145
    char * metaData;
 
146
    rv = descriptor->GetMetaDataElement("itemOne", &metaData);
 
147
    if (NS_SUCCEEDED(rv))   printf("metaData = %s\n", metaData);
 
148
    else printf("GetMetaDataElement failed : rv = %x\n", rv);
 
149
    descriptor = nsnull;
 
150
 
 
151
    // Test ACCESS_WRITE entry
 
152
    printf("\nTest ACCESS_WRITE:\n");
 
153
    rv = session->OpenCacheEntry("http://www.mozilla.org/somekey",
 
154
                                 nsICache::ACCESS_WRITE,
 
155
                                 nsICache::BLOCKING,
 
156
                                 getter_AddRefs(descriptor));
 
157
    if (NS_FAILED(rv)) {
 
158
        printf("OpenCacheEntry(ACCESS_WRITE) failed: %x", rv);
 
159
        goto error_exit;
 
160
    }
 
161
    rv = descriptor->GetCacheElement(getter_AddRefs(bar));
 
162
    if (bar)
 
163
        printf("FAILED: we didn't get new entry.\n");
 
164
    if (NS_FAILED(rv))
 
165
        printf("GetCacheElement failed : %x\n", rv);
 
166
 
 
167
    rv = descriptor->GetMetaDataElement("itemOne", &metaData);
 
168
    if (NS_SUCCEEDED(rv))
 
169
        if (metaData)  printf("metaData = %s\n", metaData);
 
170
        else           printf("metaData = nsnull\n");
 
171
    else printf("GetMetaDataElement failed : rv = %x\n", rv);
 
172
 
 
173
    printf("\n");
 
174
 error_exit:
 
175
 
 
176
    return;
 
177
}
 
178
 
 
179
 
 
180
int
 
181
main(int argc, char* argv[])
 
182
{
 
183
    nsresult rv = NS_OK;
 
184
 
 
185
    // Start up XPCOM
 
186
    rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
 
187
    if (NS_FAILED(rv)) return rv;
 
188
 
 
189
    /**
 
190
     * Create event queue for this thread
 
191
     */
 
192
    nsCOMPtr<nsIEventQueueService> eventQService = 
 
193
             do_GetService(kEventQueueServiceCID, &rv);
 
194
    if (NS_FAILED(rv)) goto error_exit;
 
195
 
 
196
    eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(gEventQ));
 
197
 
 
198
    /**
 
199
     * Test Cache
 
200
     */
 
201
    TestMemoryObjectCache();
 
202
 
 
203
 
 
204
 error_exit:
 
205
    if (gCacheService) {
 
206
        rv = gCacheService->Shutdown();
 
207
        if (NS_FAILED(rv))
 
208
            printf("nsCacheService::Shutdown() :    rv      = %x\n", rv);
 
209
        gCacheService = nsnull;
 
210
    }
 
211
 
 
212
    gEventQ = nsnull;
 
213
    eventQService = nsnull;
 
214
 
 
215
    NS_ShutdownXPCOM(nsnull);
 
216
 
 
217
    printf("XPCOM shut down.\n\n");
 
218
    return rv;
 
219
}
 
220
 
 
221
 
 
222