/*
* 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.cassandra.locator;

import java.net.InetAddress;
import java.util.ArrayList;
import com.google.common.collect.Iterators;

import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

import static org.apache.cassandra.Util.token;

import org.apache.cassandra.dht.Token;
import org.apache.cassandra.locator.TokenMetadata;
import org.apache.cassandra.service.StorageService;

public class TokenMetadataTest
{
    public final static String ONE = "1";
    public final static String SIX = "6";

    public static ArrayList<Token> RING;

    @BeforeClass
    public static void beforeClass() throws Throwable
    {
        TokenMetadata tmd = StorageService.instance.getTokenMetadata();
        tmd.updateNormalToken(token(ONE), InetAddress.getByName("127.0.0.1"));
        tmd.updateNormalToken(token(SIX), InetAddress.getByName("127.0.0.6"));
        RING = tmd.sortedTokens();
    }

    private void testRingIterator(String start, boolean includeMin, String... expected)
    {
        ArrayList<Token> actual = new ArrayList<Token>();
        Iterators.addAll(actual, TokenMetadata.ringIterator(RING, token(start), includeMin));
        assertEquals(actual.toString(), expected.length, actual.size());
        for (int i = 0; i < expected.length; i++)
            assertEquals("Mismatch at index " + i + ": " + actual, token(expected[i]), actual.get(i));
    }

    @Test
    public void testRingIterator()
    {
        testRingIterator("2", false, "6", "1");
        testRingIterator("7", false, "1", "6");
        testRingIterator("0", false, "1", "6");
        testRingIterator("", false, "1", "6");
    }

    @Test
    public void testRingIteratorIncludeMin()
    {
        testRingIterator("2", true, "6", "", "1");
        testRingIterator("7", true, "", "1", "6");
        testRingIterator("0", true, "1", "6", "");
        testRingIterator("", true, "1", "6", "");
    }

    @Test
    public void testRingIteratorEmptyRing()
    {
        RING.clear();
        testRingIterator("2", false);
    }
}

@@ -1342,43 +1342,32 @@
     
-public static ArrayList%3CToken%3E RING
+static TokenMetadata tmd
 ;%0A%0A 
@@ -1450,30 +1450,16 @@
         
-TokenMetadata 
 tmd = St
@@ -1662,43 +1662,8 @@
 ));%0A
-        RING = tmd.sortedTokens();%0A
     
@@ -1699,16 +1699,39 @@
 terator(
+ArrayList%3CToken%3E ring, 
 String s
@@ -1900,20 +1900,20 @@
 terator(
-RING
+ring
 , token(
@@ -2210,32 +2210,84 @@
 terator()%0A    %7B%0A
+        ArrayList%3CToken%3E ring = tmd.sortedTokens();%0A
         testRing
@@ -2287,32 +2287,38 @@
 estRingIterator(
+ring, 
 %222%22, false, %226%22,
@@ -2345,24 +2345,30 @@
 ingIterator(
+ring, 
 %227%22, false, 
@@ -2399,24 +2399,30 @@
 ingIterator(
+ring, 
 %220%22, false, 
@@ -2453,24 +2453,30 @@
 ingIterator(
+ring, 
 %22%22, false, %22
@@ -2545,32 +2545,84 @@
 ludeMin()%0A    %7B%0A
+        ArrayList%3CToken%3E ring = tmd.sortedTokens();%0A
         testRing
@@ -2626,24 +2626,30 @@
 ingIterator(
+ring, 
 %222%22, true, %22
@@ -2687,16 +2687,22 @@
 terator(
+ring, 
 %227%22, tru
@@ -2744,16 +2744,22 @@
 terator(
+ring, 
 %220%22, tru
@@ -2801,16 +2801,22 @@
 terator(
+ring, 
 %22%22, true
@@ -2911,47 +2911,49 @@
     
-RING.clear();%0A        testRingIterator(
+testRingIterator(new ArrayList%3CToken%3E(), 
 %222%22,

Fix TokenMetadataTest