storetest.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 440 行 · 第 1/2 页
JAVA
440 行
/* * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package com.sun.midp.push.persistence;import com.sun.midp.push.controller.ConnectionInfo;import java.io.IOException;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import junit.framework.TestCase;public final class StoreTest extends TestCase { private static final String [][] SAMPLE_CONNECTIONS = { new String [] {"sms://:500", "com.sun.FooMidlet", "12?4*"}, new String [] {"socket://:5000", "com.sun.BarMidlet", "12?4*"}, new String [] {"socket://", "com.sun.QuxMidlet", "12?4*"}, new String [] {"datagram://:239", "com.sun.QuxMidlet", "???.???*"}, new String [] {"datagram://", "com.sun.QuuxMidlet", "???.???*"}, new String [] {"socket://:2006", "com.sun.QuuuxMidlet", "*"}, new String [] {"datagram://:17", "com.sun.QuuuuxMidlet", "12.34"}, }; private static ConnectionInfo createConnectionInfo(final int index) { final String [] info = SAMPLE_CONNECTIONS[index]; return new ConnectionInfo(info[0], info[1], info[2]); } private static ConnectionInfo [] createConnectionInfos( final int [] indices) { final ConnectionInfo [] cns = new ConnectionInfo[indices.length]; for (int i = 0; i < cns.length; i++) { cns[i] = createConnectionInfo(indices[i]); } return cns; } private static HashSet toSet(final ConnectionInfo [] cns) { return new HashSet(Arrays.asList(cns)); } private static class ExpectedConnections { final Map map = new HashMap(); ExpectedConnections add(final int suiteId, final int [] indices) { map.put(new Integer(suiteId), toSet(createConnectionInfos(indices))); return this; } } private static class ExpectedAlarms { final Map map = new HashMap(); ExpectedAlarms add( final int suiteId, final String midlet, final long time) { final Integer key = new Integer(suiteId); Map suiteMap = (Map) map.get(key); if (suiteMap == null) { suiteMap = new HashMap(); map.put(key, suiteMap); } suiteMap.put(midlet, new Long(time)); return this; } } static private final class Checker { final AbstractStoreUtils.Refresher refresher = StoreUtils.getInstance().getRefresher(); final Store store; Checker() throws IOException { store = refresher.getStore(); } private void checkConnections( final Store store, final Map expectedConnections) { final Map actual = new HashMap(); store.listConnections(new Store.ConnectionsConsumer() { public void consume( final int suiteId, final ConnectionInfo [] cns) { assertNull("reported twice: " + suiteId, actual.put(new Integer(suiteId), toSet(cns))); } }); assertEquals(expectedConnections, actual); } private void checkAlarms( final Store store, final Map expectedAlarms) { final Map actual = new HashMap(); store.listAlarms(new Store.AlarmsConsumer() { public void consume(final int suiteId, final Map alarms) { assertNull("reported twice: " + suiteId, actual.put(new Integer(suiteId), alarms)); } }); assertEquals(expectedAlarms, actual); } void checkStore( final Map expectedConnections, final Map expectedAlarms) throws IOException { checkConnections(store, expectedConnections); checkAlarms(store, expectedAlarms); // And check that when the data are reread, we're ok final Store fresh = refresher.getStore(); checkConnections(fresh, expectedConnections); checkAlarms(fresh, expectedAlarms); } void addConnection( final int suiteId, final int connectionIndex) throws IOException { store.addConnection(suiteId, createConnectionInfo(connectionIndex)); } void removeConnection( final int suiteId, final int connectionIndex) throws IOException { store.removeConnection(suiteId, createConnectionInfo(connectionIndex)); } void addConnections( final int suiteId, final int [] connections) throws IOException { store.addConnections(suiteId, createConnectionInfos(connections)); } void removeConnections( final int suiteId) throws IOException { store.removeConnections(suiteId); } void addAlarm(final int suiteId, final String midlet, final long time) throws IOException { store.addAlarm(suiteId, midlet, time); } void removeAlarm(final int suiteId, final String midlet) throws IOException { store.removeAlarm(suiteId, midlet); } } private static final Map EMPTY_MAP = new HashMap(); public void testEmptyStore() throws Exception { final Checker checker = new Checker(); // No mutations checker.checkStore(EMPTY_MAP, EMPTY_MAP); } public void testAddOneConnection() throws Exception { final Checker checker = new Checker(); checker.addConnection(0, 0); checker.checkStore( new ExpectedConnections().add(0, new int [] {0}).map, EMPTY_MAP); } public void testAddAndRemoveConnection() throws Exception { final Checker checker = new Checker(); checker.addConnection(1, 1); checker.removeConnection(1, 1); checker.checkStore(EMPTY_MAP, EMPTY_MAP); } private static final int suiteId = 1; private static final int [] cns = {1, 3, 4}; private static final Map ec = new ExpectedConnections() .add(suiteId, cns) .map; public void testAddConnections() throws Exception { final Checker checker = new Checker(); checker.addConnections(suiteId, cns); checker.checkStore(ec, EMPTY_MAP); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?