This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-net.git
commit 08218d1c2c612f0975638e91e7a167ffbc8cc242 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Tue May 28 07:55:10 2024 -0400 Add missing tests --- .../java/org/apache/commons/net/util/Charsets.java | 2 +- .../org/apache/commons/net/util/ListenerList.java | 2 +- .../apache/commons/net/util/ListenerListTest.java | 71 ++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/net/util/Charsets.java b/src/main/java/org/apache/commons/net/util/Charsets.java index 14e3429d..fc40743e 100644 --- a/src/main/java/org/apache/commons/net/util/Charsets.java +++ b/src/main/java/org/apache/commons/net/util/Charsets.java @@ -35,7 +35,7 @@ public class Charsets { public static Charset toCharset(final String charsetName) { return charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName); } - + /** * Returns a charset object for the given charset name. * diff --git a/src/main/java/org/apache/commons/net/util/ListenerList.java b/src/main/java/org/apache/commons/net/util/ListenerList.java index fe8d51d6..15927975 100644 --- a/src/main/java/org/apache/commons/net/util/ListenerList.java +++ b/src/main/java/org/apache/commons/net/util/ListenerList.java @@ -26,8 +26,8 @@ import java.util.concurrent.CopyOnWriteArrayList; /** */ - public class ListenerList implements Serializable, Iterable<EventListener> { + private static final long serialVersionUID = -1934227607974228213L; private final CopyOnWriteArrayList<EventListener> listeners; diff --git a/src/test/java/org/apache/commons/net/util/ListenerListTest.java b/src/test/java/org/apache/commons/net/util/ListenerListTest.java new file mode 100644 index 00000000..e47fda66 --- /dev/null +++ b/src/test/java/org/apache/commons/net/util/ListenerListTest.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.net.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.EventListener; +import java.util.Iterator; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link ListenerList}. + */ +public class ListenerListTest { + + static class EventListenerImpl implements EventListener { + // empty + } + + @Test + public void testAdd() { + final EventListenerImpl eventListenerImpl = new EventListenerImpl(); + final ListenerList listenerList = new ListenerList(); + listenerList.addListener(eventListenerImpl); + assertEquals(1, listenerList.getListenerCount()); + } + + @Test + public void testConstructor() { + assertEquals(0, new ListenerList().getListenerCount()); + } + + @Test + public void testIterator() { + final EventListenerImpl eventListenerImpl = new EventListenerImpl(); + final ListenerList listenerList = new ListenerList(); + listenerList.addListener(eventListenerImpl); + final Iterator<EventListener> iterator = listenerList.iterator(); + assertTrue(iterator.hasNext()); + assertSame(eventListenerImpl, iterator.next()); + } + @Test + public void testRemove() { + final EventListenerImpl eventListenerImpl = new EventListenerImpl(); + final ListenerList listenerList = new ListenerList(); + listenerList.addListener(eventListenerImpl); + assertEquals(1, listenerList.getListenerCount()); + listenerList.removeListener(eventListenerImpl); + assertEquals(0, listenerList.getListenerCount()); + listenerList.iterator(); + } + +}