1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.java.util.prefs; 18 19import java.io.File; 20import java.util.prefs.AbstractPreferences; 21import java.util.prefs.BackingStoreException; 22import java.util.prefs.NodeChangeEvent; 23import java.util.prefs.NodeChangeListener; 24import java.util.prefs.Preferences; 25import java.util.prefs.PreferencesFactory; 26 27import junit.framework.TestCase; 28 29import libcore.io.IoUtils; 30 31public final class OldNodeChangeEventTest extends TestCase { 32 33 private PreferencesFactory defaultFactory; 34 35 @Override 36 public void setUp() throws Exception { 37 super.setUp(); 38 File tmpDir = IoUtils.createTemporaryDirectory("OldNodeChangeEventTest"); 39 defaultFactory = Preferences.setPreferencesFactory( 40 new PreferencesTest.TestPreferencesFactory(tmpDir.getAbsolutePath())); 41 } 42 43 @Override 44 public void tearDown() throws Exception { 45 Preferences.setPreferencesFactory(defaultFactory); 46 super.tearDown(); 47 } 48 49 public void testGetChild() throws BackingStoreException { 50 AbstractPreferences parent = (AbstractPreferences) Preferences 51 .userNodeForPackage(Preferences.class); 52 53 AbstractPreferences pref = (AbstractPreferences) parent.node("mock"); 54 55 MockNodeChangeListener nl = new MockNodeChangeListener() { 56 public synchronized void childAdded(NodeChangeEvent e) { 57 Preferences child = e.getChild(); 58 if (child == null) { 59 addResult = false; 60 } else { 61 if (child.name() == "mock1") { 62 addResult = true; 63 } 64 } 65 super.childAdded(e); 66 } 67 68 public synchronized void childRemoved(NodeChangeEvent e) { 69 Preferences child = e.getChild(); 70 if (child == null) { 71 removeResult = false; 72 } else { 73 if (child.name() == "mock1") { 74 removeResult = true; 75 } 76 } 77 super.childRemoved(e); 78 } 79 }; 80 try { 81 pref.addNodeChangeListener(nl); 82 Preferences child1 = pref.node("mock1"); 83 nl.waitForEvent(); 84 assertEquals(1, nl.getAdded()); 85 assertTrue(nl.getAddResult()); 86 nl.reset(); 87 child1.removeNode(); 88 nl.waitForEvent(); 89 assertEquals(1, nl.getRemoved()); 90 assertTrue(nl.getRemoveResult()); 91 nl.reset(); 92 } finally { 93 pref.removeNodeChangeListener(nl); 94 } 95 } 96 97 public void testGetParent() throws BackingStoreException { 98 AbstractPreferences parent = (AbstractPreferences) Preferences 99 .userNodeForPackage(Preferences.class); 100 101 AbstractPreferences pref = (AbstractPreferences) parent.node("mock"); 102 103 MockNodeChangeListener nl = new MockNodeChangeListener() { 104 public synchronized void childAdded(NodeChangeEvent e) { 105 Preferences parent = e.getParent(); 106 if (parent == null) { 107 addResult = false; 108 } else { 109 if (parent.name() == "mock") { 110 addResult = true; 111 } 112 } 113 super.childAdded(e); 114 } 115 116 public synchronized void childRemoved(NodeChangeEvent e) { 117 Preferences parent = e.getParent(); 118 if (parent == null) { 119 removeResult = false; 120 } else { 121 if (parent.name() == "mock") { 122 removeResult = true; 123 } 124 } 125 super.childRemoved(e); 126 } 127 }; 128 try { 129 pref.addNodeChangeListener(nl); 130 Preferences child1 = pref.node("mock1"); 131 nl.waitForEvent(); 132 assertEquals(1, nl.getAdded()); 133 assertTrue(nl.getAddResult()); 134 nl.reset(); 135 child1.removeNode(); 136 nl.waitForEvent(); 137 assertEquals(1, nl.getRemoved()); 138 assertTrue(nl.getRemoveResult()); 139 nl.reset(); 140 } finally { 141 pref.removeNodeChangeListener(nl); 142 } 143 } 144 145 private static class MockNodeChangeListener implements NodeChangeListener { 146 private int added = 0; 147 private int removed = 0; 148 protected boolean addResult = false; 149 protected boolean removeResult = false; 150 151 public synchronized void waitForEvent() { 152 try { 153 wait(500); 154 } catch (InterruptedException expected) { 155 } 156 } 157 158 public synchronized void childAdded(NodeChangeEvent e) { 159 ++added; 160 notifyAll(); 161 } 162 163 public synchronized void childRemoved(NodeChangeEvent e) { 164 removed++; 165 notifyAll(); 166 } 167 168 public synchronized boolean getAddResult() { 169 return addResult; 170 } 171 172 public synchronized boolean getRemoveResult() { 173 return removeResult; 174 } 175 176 public synchronized int getAdded() { 177 return added; 178 } 179 180 public synchronized int getRemoved() { 181 return removed; 182 } 183 184 public void reset() { 185 added = 0; 186 removed = 0; 187 } 188 } 189} 190