5286N/A/*
5286N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5286N/A *
5286N/A * This code is free software; you can redistribute it and/or modify it
5286N/A * under the terms of the GNU General Public License version 2 only, as
5286N/A * published by the Free Software Foundation. Oracle designates this
5286N/A * particular file as subject to the "Classpath" exception as provided
5286N/A * by Oracle in the LICENSE file that accompanied this code.
5286N/A *
5286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5286N/A * version 2 for more details (a copy is included in the LICENSE file that
5286N/A * accompanied this code).
5286N/A *
5286N/A * You should have received a copy of the GNU General Public License version
5286N/A * 2 along with this work; if not, write to the Free Software Foundation,
5286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5286N/A *
5286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5286N/A * or visit www.oracle.com if you need additional information or have any
5286N/A * questions.
5286N/A */
5286N/A
5286N/A /* @test
5853N/A * @bug 7160252 7197662
5286N/A * @summary Checks if events are delivered to a listener
5286N/A * when a child node is added or removed
5853N/A * @run main/othervm -Djava.util.prefs.userRoot=. AddNodeChangeListener
5286N/A */
5286N/A
5286N/Aimport java.util.prefs.*;
5286N/A
5286N/A public class AddNodeChangeListener {
5286N/A
5286N/A private static boolean failed = false;
5286N/A private static Preferences userRoot, N2;
5286N/A private static NodeChangeListenerAdd ncla;
5286N/A
5286N/A public static void main(String[] args)
5286N/A throws BackingStoreException, InterruptedException
5286N/A {
5286N/A userRoot = Preferences.userRoot();
5286N/A ncla = new NodeChangeListenerAdd();
5286N/A userRoot.addNodeChangeListener(ncla);
5286N/A //Should initiate a node added event
5286N/A addNode();
5286N/A // Should not initiate a node added event
5286N/A addNode();
5286N/A //Should initate a child removed event
5286N/A removeNode();
5286N/A
5286N/A if (failed)
5286N/A throw new RuntimeException("Failed");
5286N/A }
5286N/A
5286N/A private static void addNode()
5286N/A throws BackingStoreException, InterruptedException
5286N/A {
5286N/A N2 = userRoot.node("N2");
5286N/A userRoot.flush();
5286N/A Thread.sleep(3000);
5286N/A if (ncla.getAddNumber() != 1)
5286N/A failed = true;
5286N/A }
5286N/A
5286N/A private static void removeNode()
5286N/A throws BackingStoreException, InterruptedException
5286N/A {
5286N/A N2.removeNode();
5286N/A userRoot.flush();
5286N/A Thread.sleep(3000);
5286N/A if (ncla.getAddNumber() != 0)
5286N/A failed = true;
5286N/A }
5286N/A
5286N/A private static class NodeChangeListenerAdd implements NodeChangeListener {
5286N/A private int totalNode = 0;
5286N/A
5286N/A @Override
5286N/A public void childAdded(NodeChangeEvent evt) {
5286N/A totalNode++;
5286N/A }
5286N/A
5286N/A @Override
5286N/A public void childRemoved(NodeChangeEvent evt) {
5286N/A totalNode--;
5286N/A }
5286N/A
5286N/A public int getAddNumber(){
5286N/A return totalNode;
5286N/A }
5286N/A }
5286N/A }