Skip to content

Commit

Permalink
ZOOKEEPER-3839: ReconfigBackupTest Remove getFileContent
Browse files Browse the repository at this point in the history
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 apache#1360 from belugabehr/ZOOKEEPER-3839
  • Loading branch information
belugabehr authored and RokLenarcic committed Sep 3, 2022
1 parent cf16d0e commit ff42622
Showing 1 changed file with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@

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.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import org.apache.zookeeper.PortAssignment;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.admin.ZooKeeperAdmin;
Expand All @@ -50,16 +50,6 @@ public static String getVersionFromConfigStr(String config) throws IOException {
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
public void setup() {
ClientBase.setupTestEnv();
Expand Down Expand Up @@ -93,7 +83,7 @@ public void testBackupStatic() throws Exception {
mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false);
// check that a dynamic configuration file doesn't exist
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();
}

Expand All @@ -102,7 +92,7 @@ public void testBackupStatic() throws Exception {
"waiting for server " + i + " being up");
File backupFile = mt[i].getFileByName("zoo.cfg.bak");
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]);
}

Expand Down Expand Up @@ -313,11 +303,12 @@ public void testVersionOfDynamicFilename() throws Exception {
// All dynamic files created with the same version should have
// same configs, and they should be equal to the config we get from QuorumPeer.
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 {
String otherDynamicFileContent = getFileContent(dynamicConfigFile);
assertEquals(dynamicFileContent, otherDynamicFileContent);
String otherDynamicFileContent = new String(Files.readAllBytes(dynamicConfigFile.toPath()), UTF_8);
assertEquals(dynamicFileContent + "\n", otherDynamicFileContent);
}

zk.close();
Expand Down

0 comments on commit ff42622

Please sign in to comment.