You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/05/28 13:14:30 UTC
(commons-net) branch master updated: Add org.apache.commons.net.SocketClient.checkOpenOutputStream()
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
The following commit(s) were added to refs/heads/master by this push:
new 28f2886a Add org.apache.commons.net.SocketClient.checkOpenOutputStream()
28f2886a is described below
commit 28f2886ad6cfa79a9816d3a2fc51656262788892
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue May 28 09:14:25 2024 -0400
Add org.apache.commons.net.SocketClient.checkOpenOutputStream()
Add tests
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/net/SocketClient.java | 15 +++++
.../apache/commons/net/finger/FingerClient.java | 2 +-
.../commons/net/finger/FingerClientTest.java | 70 ++++++++++++++++++++++
4 files changed, 87 insertions(+), 1 deletion(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 01793784..93921bea 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -79,6 +79,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SubnetUtils.toString().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Maven property project.build.outputTimestamp for build reproducibility.</action>
<action type="add" dev="ggregory" due-to="Georg Voss, Gary Gregory">Add FTP.DEFLATE_TRANSFER_MODE to support the "deflate" compression format in FTPClient.setFileTransferMode(int).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.net.SocketClient.checkOpenOutputStream().</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump commons-parent from 62 to 70 #238.</action>
<action type="update" dev="ggregory" due-to="Dependabot">Bump org.codehaus.mojo:exec-maven-plugin from 3.1.0 to 3.2.0, #221.</action>
diff --git a/src/main/java/org/apache/commons/net/SocketClient.java b/src/main/java/org/apache/commons/net/SocketClient.java
index bfd32c05..d7571da2 100644
--- a/src/main/java/org/apache/commons/net/SocketClient.java
+++ b/src/main/java/org/apache/commons/net/SocketClient.java
@@ -27,6 +27,7 @@ import java.net.Proxy;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.Charset;
+import java.util.Objects;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
@@ -180,6 +181,20 @@ public abstract class SocketClient {
_socket_.setSoTimeout(_timeout_);
}
+ /**
+ * Gets the non-null OutputStream or throws {@link NullPointerException}.
+ *
+ * <p>
+ * This method does not allocate resources.
+ * </p>
+ *
+ * @return the non-null OutputStream.
+ * @since 3.11.0
+ */
+ protected OutputStream checkOpenOutputStream() {
+ return Objects.requireNonNull(_output_, "OutputStream");
+ }
+
private void closeQuietly(final Closeable close) {
if (close != null) {
try {
diff --git a/src/main/java/org/apache/commons/net/finger/FingerClient.java b/src/main/java/org/apache/commons/net/finger/FingerClient.java
index 0798c910..c29dd180 100644
--- a/src/main/java/org/apache/commons/net/finger/FingerClient.java
+++ b/src/main/java/org/apache/commons/net/finger/FingerClient.java
@@ -113,7 +113,7 @@ public class FingerClient extends SocketClient {
final byte[] encodedQuery = buffer.toString().getBytes(Charsets.toCharset(encoding).name()); // Java 1.6 can use
// charset directly
- output = new DataOutputStream(new BufferedOutputStream(_output_, 1024));
+ output = new DataOutputStream(new BufferedOutputStream(checkOpenOutputStream(), 1024));
output.write(encodedQuery, 0, encodedQuery.length);
output.flush();
diff --git a/src/test/java/org/apache/commons/net/finger/FingerClientTest.java b/src/test/java/org/apache/commons/net/finger/FingerClientTest.java
new file mode 100644
index 00000000..dbef7494
--- /dev/null
+++ b/src/test/java/org/apache/commons/net/finger/FingerClientTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.finger;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.IOException;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link FingerClient}.
+ */
+public class FingerClientTest {
+
+ @Test
+ public void testConstructor() {
+ assertDoesNotThrow(FingerClient::new);
+ }
+
+ @Test
+ public void testDefaultPort() {
+ assertEquals(FingerClient.DEFAULT_PORT, new FingerClient().getDefaultPort());
+ }
+
+ @Test
+ public void testDisconnect() throws IOException {
+ new FingerClient().disconnect();
+ }
+
+ @Test
+ public void testGetInputStream() {
+ final FingerClient fingerClient = new FingerClient();
+ // Not connected throws NullPointerException
+ assertThrows(NullPointerException.class, () -> fingerClient.getInputStream(false));
+ assertThrows(NullPointerException.class, () -> fingerClient.getInputStream(true));
+ assertThrows(NullPointerException.class, () -> fingerClient.getInputStream(false, ""));
+ assertThrows(NullPointerException.class, () -> fingerClient.getInputStream(true, ""));
+ assertThrows(NullPointerException.class, () -> fingerClient.getInputStream(false, "", null));
+ assertThrows(NullPointerException.class, () -> fingerClient.getInputStream(true, "", null));
+ }
+
+ @Test
+ public void testQuery() {
+ final FingerClient fingerClient = new FingerClient();
+ // Not connected throws NullPointerException
+ assertThrows(NullPointerException.class, () -> fingerClient.query(false));
+ assertThrows(NullPointerException.class, () -> fingerClient.query(true));
+ assertThrows(NullPointerException.class, () -> fingerClient.query(false, ""));
+ assertThrows(NullPointerException.class, () -> fingerClient.query(true, ""));
+ }
+
+}