1 /* 2 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6659215 27 * @summary Test on timer start method with past notifications 28 * @author Shanliang JIANG 29 * @modules java.management 30 * @run clean StartTest 31 * @run build StartTest 32 * @run main StartTest 33 */ 34 35 import java.util.Date; 36 import javax.management.timer.Timer; 37 import javax.management.Notification; 38 import javax.management.NotificationListener; 39 40 public class StartTest { 41 public static void main(String[] args) throws Exception { 42 System.out.println( 43 ">>> Test on timer start method with past notifications."); 44 45 System.out.println(">>> Create a Timer object."); 46 Timer timer = new Timer(); 47 48 System.out.println( 49 ">>> Set the flag (setSendPastNotification) to true."); 50 timer.setSendPastNotifications(true); 51 52 timer.addNotificationListener(myListener, null, null); 53 54 System.out.println(">>> Add notifications: " + SENT); 55 56 Date date = new Date(); 57 for (int i = 0; i < SENT; i++) { 58 timer.addNotification( 59 "testType" + i, "testMsg" + i, "testData" + i, date); 60 } 61 62 System.out.println(">>> The notifications should be sent at " + date); 63 System.out.println(">>> Sleep 100 ms to have past notifications."); 64 Thread.sleep(100); 65 66 System.out.println(">>> Start the timer at " + new Date()); 67 timer.start(); 68 69 System.out.println(">>> Stop the timer."); 70 Thread.sleep(100); 71 stopping = true; 72 timer.stop(); 73 74 if (received != SENT) { 75 throw new RuntimeException( 76 "Expected to receive " + SENT + " but got " + received); 77 } 78 79 System.out.println(">>> Received all expected notifications."); 80 81 System.out.println(">>> Bye bye!"); 82 } 83 84 private static NotificationListener myListener = 85 new NotificationListener() { 86 public void handleNotification(Notification n, Object hb) { 87 if (!stopping) { 88 received++; 89 System.out.println( 90 ">>> myListener-handleNotification: received " + 91 n.getSequenceNumber()); 92 } 93 } 94 }; 95 96 private static int SENT = 10; 97 private static volatile int received = 0; 98 private static volatile boolean stopping = false; 99 }