|
| 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 | +package org.apache.tomcat.util.modeler; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import javax.management.Attribute; |
| 23 | +import javax.management.AttributeChangeNotification; |
| 24 | +import javax.management.ListenerNotFoundException; |
| 25 | +import javax.management.NotificationListener; |
| 26 | + |
| 27 | +import org.junit.Assert; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +public class TestBaseModelMBean { |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testRemoveAttributeChangeNotificationListener() throws Exception { |
| 34 | + BaseModelMBean mbean = new BaseModelMBean(); |
| 35 | + List<String> attributes = new ArrayList<>(); |
| 36 | + NotificationListener listener = (notification, handback) -> attributes.add( |
| 37 | + ((AttributeChangeNotification) notification).getAttributeName()); |
| 38 | + |
| 39 | + mbean.addAttributeChangeNotificationListener(listener, "first", null); |
| 40 | + mbean.addAttributeChangeNotificationListener(listener, "second", null); |
| 41 | + mbean.removeAttributeChangeNotificationListener(listener, "first"); |
| 42 | + |
| 43 | + sendAttributeChangeNotification(mbean, "first"); |
| 44 | + sendAttributeChangeNotification(mbean, "second"); |
| 45 | + |
| 46 | + Assert.assertEquals(List.of("second"), attributes); |
| 47 | + |
| 48 | + attributes.clear(); |
| 49 | + mbean.removeAttributeChangeNotificationListener(listener, "second"); |
| 50 | + sendAttributeChangeNotification(mbean, "second"); |
| 51 | + Assert.assertTrue(attributes.isEmpty()); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testRemoveAttributeFromListenerForAllAttributes() throws Exception { |
| 57 | + BaseModelMBean mbean = new BaseModelMBean(); |
| 58 | + List<String> attributes = new ArrayList<>(); |
| 59 | + NotificationListener listener = (notification, handback) -> attributes.add( |
| 60 | + ((AttributeChangeNotification) notification).getAttributeName()); |
| 61 | + |
| 62 | + mbean.addAttributeChangeNotificationListener(listener, null, null); |
| 63 | + mbean.removeAttributeChangeNotificationListener(listener, "first"); |
| 64 | + |
| 65 | + sendAttributeChangeNotification(mbean, "first"); |
| 66 | + sendAttributeChangeNotification(mbean, "second"); |
| 67 | + |
| 68 | + Assert.assertEquals(List.of("second"), attributes); |
| 69 | + |
| 70 | + attributes.clear(); |
| 71 | + mbean.addAttributeChangeNotificationListener(listener, "first", null); |
| 72 | + sendAttributeChangeNotification(mbean, "first"); |
| 73 | + Assert.assertEquals(List.of("first"), attributes); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testRemoveAttributeChangeRegistrationPreservesGeneralRegistration() throws Exception { |
| 79 | + BaseModelMBean mbean = new BaseModelMBean(); |
| 80 | + List<String> attributes = new ArrayList<>(); |
| 81 | + NotificationListener listener = (notification, handback) -> attributes.add( |
| 82 | + ((AttributeChangeNotification) notification).getAttributeName()); |
| 83 | + |
| 84 | + mbean.addNotificationListener(listener, null, null); |
| 85 | + mbean.addAttributeChangeNotificationListener(listener, "first", null); |
| 86 | + mbean.removeAttributeChangeNotificationListener(listener, "first"); |
| 87 | + sendAttributeChangeNotification(mbean, "first"); |
| 88 | + |
| 89 | + Assert.assertEquals(List.of("first"), attributes); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testRemoveUnknownAttributeChangeNotificationListener() throws Exception { |
| 95 | + BaseModelMBean mbean = new BaseModelMBean(); |
| 96 | + NotificationListener listener = (notification, handback) -> { |
| 97 | + // NO-OP |
| 98 | + }; |
| 99 | + |
| 100 | + Assert.assertThrows(ListenerNotFoundException.class, |
| 101 | + () -> mbean.removeAttributeChangeNotificationListener(listener, "first")); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + private static void sendAttributeChangeNotification(BaseModelMBean mbean, String name) throws Exception { |
| 106 | + mbean.sendAttributeChangeNotification(new Attribute(name, "old"), new Attribute(name, "new")); |
| 107 | + } |
| 108 | +} |
0 commit comments