### Question:
QuorumPeerMain { protected QuorumPeer getQuorumPeer() throws SaslException { return new QuorumPeer(); }  static void main(String[] args); void runFromConfig(QuorumPeerConfig config);  }### Answer:
@Test public void testLeaderOutOfView() throws Exception { ClientBase.setupTestEnv(); int numServers = 3; boolean foundLeading = false; boolean foundFollowing = false; ByteArrayOutputStream os = new ByteArrayOutputStream(); WriterAppender appender = getConsoleAppender(os, Level.DEBUG); Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); qlogger.addAppender(appender); try { Servers svrs = new Servers(); svrs.clientPorts = new int[numServers]; for (int i = 0; i < numServers; i++) { svrs.clientPorts[i] = PortAssignment.unique(); } String quorumCfgIncomplete = getUniquePortCfgForId(1) + "\n" + getUniquePortCfgForId(2); String quorumCfgComplete = quorumCfgIncomplete + "\n" + getUniquePortCfgForId(3); svrs.mt = new MainThread[3]; svrs.mt[0] = new MainThread(1, svrs.clientPorts[0], quorumCfgIncomplete); for (int i = 1; i < numServers; i++) { svrs.mt[i] = new MainThread(i + 1, svrs.clientPorts[i], quorumCfgComplete); } svrs.mt[0].start(); QuorumPeer quorumPeer1 = waitForQuorumPeer(svrs.mt[0], CONNECTION_TIMEOUT); assertTrue(quorumPeer1.getPeerState() == QuorumPeer.ServerState.LOOKING); int highestServerIndex = numServers - 1; svrs.mt[highestServerIndex].start(); QuorumPeer quorumPeer3 = waitForQuorumPeer(svrs.mt[highestServerIndex], CONNECTION_TIMEOUT); assertTrue(quorumPeer3.getPeerState() == QuorumPeer.ServerState.LOOKING); for (int i = 1; i < highestServerIndex; i++) { svrs.mt[i].start(); } for (int i = 1; i < numServers; i++) { assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + svrs.clientPorts[i], CONNECTION_TIMEOUT), "waiting for server to start"); } assertTrue(svrs.mt[0].getQuorumPeer().getPeerState() == QuorumPeer.ServerState.LOOKING); assertTrue(svrs.mt[highestServerIndex].getQuorumPeer().getPeerState() == QuorumPeer.ServerState.LEADING); for (int i = 1; i < highestServerIndex; i++) { assertTrue(svrs.mt[i].getQuorumPeer().getPeerState() == QuorumPeer.ServerState.FOLLOWING); } LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); Pattern leading = Pattern.compile(".*myid=1.*QuorumPeer.*LEADING.*"); Pattern following = Pattern.compile(".*myid=1.*QuorumPeer.*FOLLOWING.*"); String line; while ((line = r.readLine()) != null && !foundLeading && !foundFollowing) { foundLeading = leading.matcher(line).matches(); foundFollowing = following.matcher(line).matches(); } } finally { qlogger.removeAppender(appender); } assertFalse(foundLeading, "Corrupt peer should never become leader"); assertFalse(foundFollowing, "Corrupt peer should not attempt connection to out of view leader"); }