Ejemplo n.º 1
0
  public void testNodeChange() {
    System.out.println("***TestEvent");
    try {
      Preferences userRoot = Preferences.userRoot();

      userRoot.addNodeChangeListener(
          new NodeChangeListener() {
            public void childAdded(NodeChangeEvent evt) {
              System.out.println(
                  "childAdded = " + evt.getParent().name() + " " + evt.getChild().name());
            }

            public void childRemoved(NodeChangeEvent evt) {
              System.out.println(
                  "childRemoved = " + evt.getParent().name() + " " + evt.getChild().name());
            }
          });

      Preferences node = userRoot.node("testAdd");
      node.removeNode();

    } catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }
  }
 public static void main(String[] args) throws Exception {
   String osName = System.getProperty("os.name");
   if (osName.startsWith("Windows")) return;
   Preferences root = Preferences.userRoot();
   Preferences node1 = root.node("node1");
   Preferences node1A = node1.node("node1A");
   Preferences node1B = node1.node("node1B");
   node1B.put("mykey", "myvalue");
   node1.flush();
   String node1BDirName = System.getProperty("user.home") + "/.java/.userPrefs" + "/node1/node1B";
   File node1BDir = new File(node1BDirName);
   node1BDir.setReadOnly();
   try {
     node1.removeNode();
   } catch (BackingStoreException ex) {
     // expected exception
   } finally {
     Runtime.getRuntime().exec("chmod 755 " + node1BDirName).waitFor();
     try {
       node1.removeNode();
     } catch (Exception e) {
     }
   }
 }
  public void testRemoveParentNode() throws BackingStoreException {
    assertNull(storage.toFolder());
    assertNull(storage.toPropertiesFile());

    Preferences subnode = pref.node("subnode");
    assertNull(storage.toFolder());
    assertNull(storage.toPropertiesFile());
    subnode.put("key", "value");
    subnode.flush();
    assertNotNull(storage.toFolder());
    assertNull(storage.toPropertiesFile());
    subnode.removeNode();
    pref.flush();
    assertNull(storage.toPropertiesFile());
    assertNull(storage.toFolder());
    assertFalse(storage.existsNode());
  }
  public void testPreferencesEvents2() throws Exception {
    pref.addNodeChangeListener(new NodeListener());
    pref.addPreferenceChangeListener(new PrefListener());

    Preferences child = pref.node("a");
    nodeAddedEvent.await();
    assertEquals("Missing node added event", 0, nodeAddedEvent.getCount());
    pref.put("key", "value");
    prefChangedEvent.await();
    assertEquals("Missing preference change event", 0, prefChangedEvent.getCount());
    pref.remove("key");
    prefRemovedEvent.await();
    assertEquals("Missing preference removed event", 0, prefRemovedEvent.getCount());
    child.removeNode();
    nodeRemovedEvent.await();
    assertEquals("Missing node removed event", 0, nodeRemovedEvent.getCount());
  }