浏览代码

ZOOKEEPER-3839: ReconfigBackupTest Remove getFileContent

Author: David Mollitor <dmollitor@apache.org>

Reviewers: Norbert Kalmar <nkalmar@apache.org>, Enrico Olivelli <eolivelli@apache.org>, Christopher Tubbs <ctubbsii@apache.org>, Damien Diederen <ddiederen@apache.org>

Closes #1360 from belugabehr/ZOOKEEPER-3839
David Mollitor 4 年之前
父节点
当前提交
33bf364573

+ 9 - 18
zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigBackupTest.java

@@ -18,21 +18,21 @@
 
 
 package org.apache.zookeeper.server.quorum;
 package org.apache.zookeeper.server.quorum;
 
 
+import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT;
 import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import java.io.File;
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.IOException;
 import java.io.StringReader;
 import java.io.StringReader;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Collections;
 import java.util.List;
 import java.util.List;
 import java.util.Properties;
 import java.util.Properties;
-import java.util.Scanner;
 import org.apache.zookeeper.PortAssignment;
 import org.apache.zookeeper.PortAssignment;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.admin.ZooKeeperAdmin;
 import org.apache.zookeeper.admin.ZooKeeperAdmin;
@@ -50,16 +50,6 @@ public class ReconfigBackupTest extends QuorumPeerTestBase {
         return props.getProperty("version", "");
         return props.getProperty("version", "");
     }
     }
 
 
-    // upgrade this once we have Google-Guava or Java 7+
-    public static String getFileContent(File file) throws FileNotFoundException {
-        Scanner sc = new Scanner(file);
-        StringBuilder sb = new StringBuilder();
-        while (sc.hasNextLine()) {
-            sb.append(sc.nextLine() + "\n");
-        }
-        return sb.toString();
-    }
-
     @BeforeEach
     @BeforeEach
     public void setup() {
     public void setup() {
         ClientBase.setupTestEnv();
         ClientBase.setupTestEnv();
@@ -93,7 +83,7 @@ public class ReconfigBackupTest extends QuorumPeerTestBase {
             mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false);
             mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false);
             // check that a dynamic configuration file doesn't exist
             // check that a dynamic configuration file doesn't exist
             assertNull(mt[i].getFileByName("zoo.cfg.bak"), "static file backup shouldn't exist before bootup");
             assertNull(mt[i].getFileByName("zoo.cfg.bak"), "static file backup shouldn't exist before bootup");
-            staticFileContent[i] = getFileContent(mt[i].confFile);
+            staticFileContent[i] = new String(Files.readAllBytes(mt[i].confFile.toPath()), UTF_8);
             mt[i].start();
             mt[i].start();
         }
         }
 
 
@@ -102,7 +92,7 @@ public class ReconfigBackupTest extends QuorumPeerTestBase {
                     "waiting for server " + i + " being up");
                     "waiting for server " + i + " being up");
             File backupFile = mt[i].getFileByName("zoo.cfg.bak");
             File backupFile = mt[i].getFileByName("zoo.cfg.bak");
             assertNotNull(backupFile, "static file backup should exist");
             assertNotNull(backupFile, "static file backup should exist");
-            staticBackupContent[i] = getFileContent(backupFile);
+            staticBackupContent[i] = new String(Files.readAllBytes(backupFile.toPath()), UTF_8);
             assertEquals(staticFileContent[i], staticBackupContent[i]);
             assertEquals(staticFileContent[i], staticBackupContent[i]);
         }
         }
 
 
@@ -313,11 +303,12 @@ public class ReconfigBackupTest extends QuorumPeerTestBase {
             // All dynamic files created with the same version should have
             // All dynamic files created with the same version should have
             // same configs, and they should be equal to the config we get from QuorumPeer.
             // same configs, and they should be equal to the config we get from QuorumPeer.
             if (i == 0) {
             if (i == 0) {
-                dynamicFileContent = getFileContent(dynamicConfigFile);
-                assertEquals(sortedConfigStr, dynamicFileContent + "version=200000000");
+                dynamicFileContent = new String(Files.readAllBytes(dynamicConfigFile.toPath()), UTF_8);
+                // last line in file should be version number
+                assertEquals(sortedConfigStr, dynamicFileContent + "\n" + "version=200000000");
             } else {
             } else {
-                String otherDynamicFileContent = getFileContent(dynamicConfigFile);
-                assertEquals(dynamicFileContent, otherDynamicFileContent);
+                String otherDynamicFileContent = new String(Files.readAllBytes(dynamicConfigFile.toPath()), UTF_8);
+                assertEquals(dynamicFileContent + "\n", otherDynamicFileContent);
             }
             }
 
 
             zk.close();
             zk.close();