001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024: package com.sun.midp.jump.push.executive.persistence;
025:
026: import com.sun.jump.module.contentstore.InMemoryContentStore;
027: import junit.framework.*;
028: import java.io.IOException;
029: import java.util.HashMap;
030: import java.util.Map;
031:
032: public final class AppSuiteDataStoreTest extends TestCase {
033:
034: public AppSuiteDataStoreTest(String testName) {
035: super (testName);
036: }
037:
038: /** Dir for AppSuiteDataStore. */
039: private static final String DIR = "./dir";
040:
041: /**
042: * Data converter.
043: *
044: * <p>As plaing <code>String</code>s are used, it quite simple</p>
045: */
046: private static final AppSuiteDataStore.DataConverter DATA_CONVERTER = new AppSuiteDataStore.DataConverter() {
047: public String dataToString(final Object obj) {
048: return (String) obj;
049: }
050:
051: public Object stringToData(final String s) {
052: return s;
053: }
054: };
055:
056: /**
057: * Creates a handle for <code>JUMPStore</code>.
058: *
059: * @returns a handle
060: */
061: private static StoreOperationManager createStoreManager()
062: throws IOException {
063: return StoreUtils.createInMemoryManager(new String[] { DIR });
064: }
065:
066: private AppSuiteDataStore createAppSuiteDataStore(
067: final StoreOperationManager storeManager)
068: throws IOException {
069: return new AppSuiteDataStore(storeManager, DIR, DATA_CONVERTER);
070: }
071:
072: private void _testCtorThrowsIllegalArgumentException(
073: final StoreOperationManager storeManager, final String dir,
074: final AppSuiteDataStore.DataConverter dataConverter,
075: final String paramName) throws IOException {
076: try {
077: new AppSuiteDataStore(storeManager, dir, dataConverter);
078: fail("AppSuiteDataStore.<init> doesn't throw"
079: + " IllegalArgumentException for null " + paramName);
080: } catch (IllegalArgumentException _) {
081: }
082: }
083:
084: /**
085: * Tests that AppSuiteDataStore constructor throws an exception
086: * when fed with <code>null</code> as store handler.
087: */
088: public void testCtorNullStoreHandler() throws IOException {
089: _testCtorThrowsIllegalArgumentException(null, DIR,
090: DATA_CONVERTER, "store handle");
091: }
092:
093: /**
094: * Tests that AppSuiteDataStore constructor throws an exception
095: * when fed with <code>null</code> as store handler.
096: */
097: public void testCtorNullDir() throws IOException {
098: _testCtorThrowsIllegalArgumentException(createStoreManager(),
099: null, DATA_CONVERTER, "dir");
100: }
101:
102: /**
103: * Tests that AppSuiteDataStore constructor throws an exception
104: * when fed with <code>null</code> as store handler.
105: */
106: public void testCtorNullDataConverter() throws IOException {
107: _testCtorThrowsIllegalArgumentException(createStoreManager(),
108: DIR, null, "data converter");
109: }
110:
111: /**
112: * Tests that <code>updateSuiteData</code> throws
113: * <code>IllegalArgumentException</code> when fed with <code>null</code>.
114: */
115: public void testUpdateSuiteDataThrows() throws IOException {
116: try {
117: createAppSuiteDataStore(createStoreManager())
118: .updateSuiteData(0, null);
119: fail("should throw IllegalArgumentException");
120: } catch (IllegalArgumentException _) {
121: }
122: }
123:
124: /** Bundle of suite id and data. */
125: private static final class SuiteData {
126: private final int suiteId;
127: private final String data;
128:
129: SuiteData(final int suiteId, final String data) {
130: this .suiteId = suiteId;
131: this .data = data;
132: }
133:
134: void updateStore(final AppSuiteDataStore store)
135: throws IOException {
136: if (data != null) {
137: store.updateSuiteData(suiteId, data);
138: } else {
139: store.removeSuiteData(suiteId);
140: }
141: }
142:
143: static void updateStore(final AppSuiteDataStore store,
144: final SuiteData[] dataToUpdate) throws IOException {
145: for (int i = 0; i < dataToUpdate.length; i++) {
146: dataToUpdate[i].updateStore(store);
147: }
148: }
149: }
150:
151: private void checkTestData(final AppSuiteDataStore store,
152: final SuiteData[] expected) {
153: /*
154: * NB: it's ok for expected to have several records
155: * for the same suite: only the last one will be picked
156: * (following AppSuiteDataStore semantics)
157: */
158: final Map e = new HashMap();
159: for (int i = 0; i < expected.length; i++) {
160: final SuiteData d = expected[i];
161: e.put(new Integer(d.suiteId), d.data);
162: }
163:
164: final Map actual = new HashMap();
165:
166: store.listData(new AppSuiteDataStore.DataConsumer() {
167: public void consume(final int suiteId, final Object data) {
168: assertNull("reported twice: " + suiteId, actual.put(
169: new Integer(suiteId), data));
170: }
171: });
172:
173: assertEquals(e, actual);
174: }
175:
176: private void checkTestData(final SuiteData[] dataToUpdate,
177: final SuiteData[] expected) throws IOException {
178: final StoreOperationManager storeManager = createStoreManager();
179:
180: final AppSuiteDataStore store = createAppSuiteDataStore(storeManager);
181: SuiteData.updateStore(store, dataToUpdate);
182: checkTestData(store, expected);
183: // And reread it afresh (to emulate real persistence)
184: checkTestData(createAppSuiteDataStore(storeManager), expected);
185: }
186:
187: private void checkGetSuiteData(final AppSuiteDataStore store)
188: throws IOException {
189: store.listData(new AppSuiteDataStore.DataConsumer() {
190: public void consume(final int suiteId, final Object data) {
191: assertEquals(data, store.getSuiteData(suiteId));
192: }
193: });
194: }
195:
196: private void checkGetSuiteData(final SuiteData[] dataToUpdate)
197: throws IOException {
198: final StoreOperationManager storeManager = createStoreManager();
199:
200: final AppSuiteDataStore store = createAppSuiteDataStore(storeManager);
201: SuiteData.updateStore(store, dataToUpdate);
202: checkGetSuiteData(store);
203: // And reread it afresh (to emulate real persistence)
204: checkGetSuiteData(createAppSuiteDataStore(storeManager));
205: }
206:
207: private final static SuiteData[] NO_UPDATES = new SuiteData[] {};
208:
209: private final static SuiteData[] ADD_AND_REMOVE = new SuiteData[] {
210: new SuiteData(0, "foo"), new SuiteData(0, null), };
211:
212: private final static SuiteData[] SEVERAL_SUITES = new SuiteData[] {
213: new SuiteData(0, "foo"), new SuiteData(239, "bar"),
214: new SuiteData(1024, "qux"), };
215:
216: private final static SuiteData[] OVERWRITTES = new SuiteData[] {
217: new SuiteData(17, "foo"), new SuiteData(17, "bar") };
218:
219: public void testGetSuiteDataNO_UPDATES() throws IOException {
220: checkGetSuiteData(NO_UPDATES);
221: }
222:
223: public void testGetSuiteDataADD_AND_REMOVE() throws IOException {
224: checkGetSuiteData(ADD_AND_REMOVE);
225: }
226:
227: public void testGetSuiteDataSEVERAL_SUITES() throws IOException {
228: checkGetSuiteData(SEVERAL_SUITES);
229: }
230:
231: public void testGetSuiteDataOVERWRITTES() throws IOException {
232: checkGetSuiteData(OVERWRITTES);
233: }
234:
235: public void testNO_UPDATES() throws IOException {
236: checkTestData(NO_UPDATES, NO_UPDATES);
237: }
238:
239: public void testADD_AND_REMOVE() throws IOException {
240: checkTestData(ADD_AND_REMOVE, new SuiteData[] {});
241: }
242:
243: public void testSEVERAL_SUITES() throws IOException {
244: checkTestData(SEVERAL_SUITES, SEVERAL_SUITES);
245: }
246:
247: public void testOVERWRITTES() throws IOException {
248: checkTestData(OVERWRITTES, OVERWRITTES);
249: }
250: }
|