AddNodeChangeListener.java revision 8729:0242fce0f717
1/*
2 * Copyright (c) 2012, 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 /* @test
25  * @bug  7160252 7197662
26  * @summary Checks if events are delivered to a listener
27  *          when a child node is added or removed
28  * @run main/othervm -Djava.util.prefs.userRoot=. AddNodeChangeListener
29  */
30
31import java.util.prefs.*;
32
33 public class AddNodeChangeListener {
34
35     private static boolean failed = false;
36     private static Preferences userRoot, N2;
37     private static NodeChangeListenerAdd ncla;
38
39     public static void main(String[] args)
40         throws BackingStoreException, InterruptedException
41     {
42        userRoot = Preferences.userRoot();
43        ncla = new NodeChangeListenerAdd();
44        userRoot.addNodeChangeListener(ncla);
45        //Should initiate a node added event
46        addNode();
47        // Should not initiate a node added event
48        addNode();
49        //Should initate a child removed event
50        removeNode();
51
52        if (failed)
53            throw new RuntimeException("Failed");
54    }
55
56    private static void addNode()
57        throws BackingStoreException, InterruptedException
58    {
59        N2 = userRoot.node("N2");
60        userRoot.flush();
61        Thread.sleep(3000);
62        if (ncla.getAddNumber() != 1)
63            failed = true;
64    }
65
66    private static void removeNode()
67        throws BackingStoreException, InterruptedException
68    {
69        N2.removeNode();
70        userRoot.flush();
71        Thread.sleep(3000);
72        if (ncla.getAddNumber() != 0)
73            failed = true;
74    }
75
76    private static class NodeChangeListenerAdd implements NodeChangeListener {
77        private int totalNode = 0;
78
79        @Override
80        public void childAdded(NodeChangeEvent evt) {
81            totalNode++;
82        }
83
84        @Override
85        public void childRemoved(NodeChangeEvent evt) {
86            totalNode--;
87        }
88
89        public int getAddNumber(){
90            return totalNode;
91        }
92    }
93 }
94