alarmcontrollertest.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 508 行 · 第 1/2 页
JAVA
508 行
/* * 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.controller;import com.sun.midp.push.persistence.StoreUtils;import junit.framework.*;import com.sun.midp.push.persistence.Store;import java.io.IOException;import java.util.Map;import javax.microedition.io.ConnectionNotFoundException;public final class AlarmControllerTest extends TestCase { public AlarmControllerTest(String testName) { super(testName); } private static Store createStore() throws IOException { return StoreUtils.getInstance().getStore(); } private static AlarmController createAlarmController( final Store store, final LifecycleAdapter lifecycleAdapter) throws IOException { return new AlarmController(store, lifecycleAdapter); } private static AlarmController createAlarmController( final LifecycleAdapter lifecycleAdapter) throws IOException { return createAlarmController(createStore(), lifecycleAdapter); } private static AlarmController createAlarmController(final Store store) throws IOException { return createAlarmController(store, new ListingLifecycleAdapter()); } private static AlarmController createAlarmController() throws IOException { return createAlarmController(createStore()); } private static long registerAlarmWithDelta( final AlarmController alarmController, final int midletSuiteID, final String midlet, final long delta) throws ConnectionNotFoundException { return alarmController.registerAlarm( midletSuiteID, midlet, System.currentTimeMillis() + delta); } public void testFirstRegistration() throws IOException { final int DUMMY_SUITE_ID = 13; final String DUMMY_MIDLET = "foo.bar.Dummy"; final AlarmController alarmController = createAlarmController(); final long previous = alarmController .registerAlarm(DUMMY_SUITE_ID, DUMMY_MIDLET, 239L); alarmController.dispose(); assertEquals(0L, previous); } public void testSuiteWithNoAlarmsUninstallingA() throws IOException { final int DUMMY_SUITE_ID = 13; final AlarmController alarmController = createAlarmController(); alarmController.removeSuiteAlarms(DUMMY_SUITE_ID); alarmController.dispose(); } public void testSuiteWithNoAlarmsUninstallingB() throws IOException { final int DUMMY_SUITE_ID = 13; final String DUMMY_MIDLET = "foo.bar.Dummy"; // should be different from DUMMY_MIDLET final int ANOTHER_SUITE_ID = 17; assert DUMMY_SUITE_ID != ANOTHER_SUITE_ID; final AlarmController alarmController = createAlarmController(); alarmController.registerAlarm(DUMMY_SUITE_ID, DUMMY_MIDLET, 239L); alarmController.removeSuiteAlarms(ANOTHER_SUITE_ID); alarmController.dispose(); } public void testSuiteWithSeveralMidletsUninstall() throws IOException { final int MIDLET_SUITE_ID = 17; final String MIDLET_1 = "com.sun.Foo"; final String MIDLET_2 = "com.sun.Bar"; final String MIDLET_3 = "com.sun.Qux"; final AlarmController alarmController = createAlarmController(); registerAlarmWithDelta(alarmController, MIDLET_SUITE_ID, MIDLET_1, 1001L); registerAlarmWithDelta(alarmController, MIDLET_SUITE_ID, MIDLET_2, 2001L); registerAlarmWithDelta(alarmController, MIDLET_SUITE_ID, MIDLET_3, 3001L); alarmController.removeSuiteAlarms(MIDLET_SUITE_ID); alarmController.dispose(); } public void testAlarmFired() throws IOException, InterruptedException { final long ALARM_DELTA = 101L; // in ms final long WAIT_DELAY = 3*ALARM_DELTA; final int MIDLET_SUITE_ID = 17; final String MIDLET = "com.sun.Foo"; final ListingLifecycleAdapter lifecycleAdapter = new ListingLifecycleAdapter(); final AlarmController alarmController = createAlarmController(lifecycleAdapter); registerAlarmWithDelta(alarmController, MIDLET_SUITE_ID, MIDLET, ALARM_DELTA); Thread.sleep(WAIT_DELAY); alarmController.dispose(); assertTrue(lifecycleAdapter.hasBeenInvokedOnceFor(MIDLET_SUITE_ID, MIDLET)); } private static class FiredChecker { final ListingLifecycleAdapter lifecycleAdapter; final int midletSuiteID; final String midlet; final AlarmController alarmController; FiredChecker(final int midletSuiteID, final String midlet) throws IOException { this.midletSuiteID = midletSuiteID; this.midlet = midlet; this.lifecycleAdapter = new ListingLifecycleAdapter(); this.alarmController = createAlarmController(lifecycleAdapter); } FiredChecker(final Store store, final int midletSuiteID, final String midlet) throws IOException { this.midletSuiteID = midletSuiteID; this.midlet = midlet; this.lifecycleAdapter = new ListingLifecycleAdapter(); this.alarmController = createAlarmController(store, lifecycleAdapter); } long registerCheckedAlarm(final long delta) throws ConnectionNotFoundException { return registerAlarmWithDelta( alarmController, midletSuiteID, midlet, delta); } boolean hasBeenFired() { return lifecycleAdapter.hasBeenInvokedOnceFor(midletSuiteID, midlet); } } public void testSecondRegistration() throws IOException { final int MIDLET_SUITE_ID = 17; final String MIDLET = "com.sun.Foo"; final FiredChecker firedChecker = new FiredChecker(MIDLET_SUITE_ID, MIDLET); final AlarmController alarmController = firedChecker.alarmController; /* * IMPL_NOTE: ALARM_TIME below must be big enough for alarm not * to fire before second registration */ final long ALARM_TIME = System.currentTimeMillis() + 10239L; alarmController.registerAlarm(MIDLET_SUITE_ID, MIDLET, ALARM_TIME); final long previous = alarmController .registerAlarm(MIDLET_SUITE_ID, MIDLET, 2*ALARM_TIME); if (firedChecker.hasBeenFired()) { fail("Test is not reliable: the alarm has been fired." + " Please, increase ALARM_TIME"); } alarmController.dispose(); assertEquals(ALARM_TIME, previous); } public void testUninstall() throws IOException, InterruptedException { /* * ALARM_DELTA should be big enough for removeSuiteAlarms to finish */ final long ALARM_DELTA = 1001L; // in ms final long WAIT_DELAY = 3*ALARM_DELTA; final int MIDLET_SUITE_ID = 17; final String MIDLET = "com.sun.Foo"; final FiredChecker firedChecker = new FiredChecker(MIDLET_SUITE_ID, MIDLET); final AlarmController alarmController = firedChecker.alarmController; registerAlarmWithDelta(alarmController, MIDLET_SUITE_ID, MIDLET, ALARM_DELTA); alarmController.removeSuiteAlarms(MIDLET_SUITE_ID); if (firedChecker.hasBeenFired()) { fail("Test is not reliable: the alarm has been fired." + " Please, increase ALARM_DELTA"); } Thread.sleep(WAIT_DELAY); alarmController.dispose(); assertFalse(firedChecker.hasBeenFired()); } private void registerAndWait(final FiredChecker firedChecker) throws ConnectionNotFoundException, InterruptedException { final long ALARM_DELTA = 101L; // in ms final long WAIT_DELAY = 3*ALARM_DELTA; firedChecker.registerCheckedAlarm(ALARM_DELTA); Thread.sleep(WAIT_DELAY); if (!firedChecker.hasBeenFired()) { fail("Test is not reliable: the alarm hasn't been fired"); } } public void testResetAfterFiring() throws IOException,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?