Преглед изворни кода

ZOOKEEPER-3836: Use Commons and JDK Functions in ClientBase

Related to #1357

Author: David Mollitor <dmollitor@apache.org>

Reviewers: Enrico Olivelli <eolivelli@apache.org>, Norbert Kalmar <nkalmar@apache.org>

Closes #1358 from belugabehr/ZOOKEEPER-3836
David Mollitor пре 4 година
родитељ
комит
7f66c7680f

+ 9 - 7
zookeeper-server/src/test/java/org/apache/zookeeper/test/AtomicFileOutputStreamTest.java

@@ -18,6 +18,7 @@
 
 
 package org.apache.zookeeper.test;
 package org.apache.zookeeper.test;
 
 
+import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -27,6 +28,7 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStream;
+import java.nio.file.Files;
 import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.common.AtomicFileOutputStream;
 import org.apache.zookeeper.common.AtomicFileOutputStream;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.AfterEach;
@@ -64,7 +66,7 @@ public class AtomicFileOutputStreamTest extends ZKTestCase {
         fos.close();
         fos.close();
         assertTrue(dstFile.exists());
         assertTrue(dstFile.exists());
 
 
-        String readBackData = ClientBase.readFile(dstFile);
+        String readBackData = new String(Files.readAllBytes(dstFile.toPath()), UTF_8);
         assertEquals(TEST_STRING, readBackData);
         assertEquals(TEST_STRING, readBackData);
     }
     }
 
 
@@ -82,12 +84,12 @@ public class AtomicFileOutputStreamTest extends ZKTestCase {
         fos.flush();
         fos.flush();
 
 
         // Original contents still in place
         // Original contents still in place
-        assertEquals("", ClientBase.readFile(dstFile));
+        assertEquals("", new String(Files.readAllBytes(dstFile.toPath()), UTF_8));
 
 
         fos.close();
         fos.close();
 
 
         // New contents replace original file
         // New contents replace original file
-        String readBackData = ClientBase.readFile(dstFile);
+        String readBackData = new String(Files.readAllBytes(dstFile.toPath()), UTF_8);
         assertEquals(TEST_STRING, readBackData);
         assertEquals(TEST_STRING, readBackData);
     }
     }
 
 
@@ -112,9 +114,9 @@ public class AtomicFileOutputStreamTest extends ZKTestCase {
         }
         }
 
 
         // Should not have touched original file
         // Should not have touched original file
-        assertEquals(TEST_STRING_2, ClientBase.readFile(dstFile));
+        assertEquals(TEST_STRING_2, new String(Files.readAllBytes(dstFile.toPath()), UTF_8));
 
 
-        assertEquals(dstFile.getName(), ClientBase.join(",", testDir.list()), "Temporary file should have been cleaned up");
+        assertEquals(dstFile.getName(), String.join(",", testDir.list()), "Temporary file should have been cleaned up");
     }
     }
 
 
     /**
     /**
@@ -172,7 +174,7 @@ public class AtomicFileOutputStreamTest extends ZKTestCase {
         fos2.abort();
         fos2.abort();
 
 
         // Should not have touched original file
         // Should not have touched original file
-        assertEquals(TEST_STRING, ClientBase.readFile(dstFile));
+        assertEquals(TEST_STRING, new String(Files.readAllBytes(dstFile.toPath()), UTF_8));
         assertEquals(1, testDir.list().length);
         assertEquals(1, testDir.list().length);
     }
     }
 
 
@@ -193,7 +195,7 @@ public class AtomicFileOutputStreamTest extends ZKTestCase {
         fos2.abort();
         fos2.abort();
 
 
         // Should not have touched original file
         // Should not have touched original file
-        assertEquals(TEST_STRING, ClientBase.readFile(dstFile));
+        assertEquals(TEST_STRING, new String(Files.readAllBytes(dstFile.toPath()), UTF_8));
         assertEquals(1, testDir.list().length);
         assertEquals(1, testDir.list().length);
     }
     }
 
 

+ 0 - 28
zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java

@@ -23,10 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 import static org.junit.jupiter.api.Assertions.fail;
-import java.io.BufferedInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.IOException;
 import java.net.ConnectException;
 import java.net.ConnectException;
 import java.net.ProtocolException;
 import java.net.ProtocolException;
@@ -52,7 +49,6 @@ import org.apache.zookeeper.Watcher.Event.KeeperState;
 import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.client.ZKClientConfig;
 import org.apache.zookeeper.client.ZKClientConfig;
-import org.apache.zookeeper.common.IOUtils;
 import org.apache.zookeeper.common.Time;
 import org.apache.zookeeper.common.Time;
 import org.apache.zookeeper.common.X509Exception.SSLContextException;
 import org.apache.zookeeper.common.X509Exception.SSLContextException;
 import org.apache.zookeeper.server.ServerCnxnFactory;
 import org.apache.zookeeper.server.ServerCnxnFactory;
@@ -700,30 +696,6 @@ public abstract class ClientBase extends ZKTestCase {
         }
         }
     }
     }
 
 
-    public static String readFile(File file) throws IOException {
-        ByteArrayOutputStream os = new ByteArrayOutputStream();
-        BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
-        try {
-            IOUtils.copyBytes(is, os, 1024, true);
-        } finally {
-            is.close();
-        }
-        return os.toString();
-    }
-
-    public static String join(String separator, Object[] parts) {
-        StringBuilder sb = new StringBuilder();
-        boolean first = true;
-        for (Object part : parts) {
-            if (!first) {
-                sb.append(separator);
-                first = false;
-            }
-            sb.append(part);
-        }
-        return sb.toString();
-    }
-
     public static ZooKeeper createZKClient(String cxnString) throws Exception {
     public static ZooKeeper createZKClient(String cxnString) throws Exception {
         return createZKClient(cxnString, CONNECTION_TIMEOUT);
         return createZKClient(cxnString, CONNECTION_TIMEOUT);
     }
     }