1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.zookeeper.test;
19 
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.Collections;
24 import org.apache.zookeeper.AddWatchMode;
25 import org.apache.zookeeper.KeeperException;
26 import org.apache.zookeeper.Watcher;
27 import org.apache.zookeeper.ZooKeeper;
28 import org.apache.zookeeper.server.watch.IWatchManager;
29 import org.apache.zookeeper.server.watch.WatchManagerFactory;
30 import org.apache.zookeeper.server.watch.WatcherOrBitSet;
31 import org.apache.zookeeper.server.watch.WatchesPathReport;
32 import org.apache.zookeeper.server.watch.WatchesReport;
33 import org.apache.zookeeper.server.watch.WatchesSummary;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 
38 public class UnsupportedAddWatcherTest extends ClientBase {
39 
40     public static class StubbedWatchManager implements IWatchManager {
41         @Override
addWatch(String path, Watcher watcher)42         public boolean addWatch(String path, Watcher watcher) {
43             return false;
44         }
45 
46         @Override
containsWatcher(String path, Watcher watcher)47         public boolean containsWatcher(String path, Watcher watcher) {
48             return false;
49         }
50 
51         @Override
removeWatcher(String path, Watcher watcher)52         public boolean removeWatcher(String path, Watcher watcher) {
53             return false;
54         }
55 
56         @Override
removeWatcher(Watcher watcher)57         public void removeWatcher(Watcher watcher) {
58             // NOP
59         }
60 
61         @Override
triggerWatch(String path, Watcher.Event.EventType type)62         public WatcherOrBitSet triggerWatch(String path, Watcher.Event.EventType type) {
63             return new WatcherOrBitSet(Collections.emptySet());
64         }
65 
66         @Override
triggerWatch(String path, Watcher.Event.EventType type, WatcherOrBitSet suppress)67         public WatcherOrBitSet triggerWatch(String path, Watcher.Event.EventType type, WatcherOrBitSet suppress) {
68             return new WatcherOrBitSet(Collections.emptySet());
69         }
70 
71         @Override
size()72         public int size() {
73             return 0;
74         }
75 
76         @Override
shutdown()77         public void shutdown() {
78             // NOP
79         }
80 
81         @Override
getWatchesSummary()82         public WatchesSummary getWatchesSummary() {
83             return null;
84         }
85 
86         @Override
getWatches()87         public WatchesReport getWatches() {
88             return null;
89         }
90 
91         @Override
getWatchesByPath()92         public WatchesPathReport getWatchesByPath() {
93             return null;
94         }
95 
96         @Override
dumpWatches(PrintWriter pwriter, boolean byPath)97         public void dumpWatches(PrintWriter pwriter, boolean byPath) {
98             // NOP
99         }
100     }
101 
102     @BeforeEach
setUp()103     public void setUp() throws Exception {
104         System.setProperty(WatchManagerFactory.ZOOKEEPER_WATCH_MANAGER_NAME, StubbedWatchManager.class.getName());
105         super.setUp();
106     }
107 
108     @AfterEach
tearDown()109     public void tearDown() throws Exception {
110         try {
111             super.tearDown();
112         } finally {
113             System.clearProperty(WatchManagerFactory.ZOOKEEPER_WATCH_MANAGER_NAME);
114         }
115     }
116 
117     @Test
testBehavior()118     public void testBehavior() throws IOException, InterruptedException, KeeperException {
119         assertThrows(KeeperException.MarshallingErrorException.class, () -> {
120             try (ZooKeeper zk = createClient(hostPort)) {
121                 // the server will generate an exception as our custom watch manager doesn't implement
122                 // the new version of addWatch()
123                 zk.addWatch("/foo", event -> {
124                 }, AddWatchMode.PERSISTENT_RECURSIVE);
125             }
126         });
127     }
128 }
129