Exemple #1
0
  /**
   * Compare if the two ArrayList is the same
   *
   * @return true/false
   */
  public static boolean isCompareList(
      ArrayList<String> unsortedList, ArrayList<String> sortedList) {

    if (unsortedList.size() != sortedList.size()) {
      return false;
    } else {
      for (int i = 0; i < unsortedList.size(); i++) {
        if (!unsortedList.get(i).equals(sortedList.get(i))) {
          return false;
        }
      }
    }
    return true;
  }
  @Test
  public void testWarnings() throws ParseException, TokeniserException {
    ArrayList<Warning> warnings = new ArrayList<>();
    Policy p1 = Parser.parse("frame-src aaa", "https://origin", warnings);

    assertEquals("frame-src aaa", p1.show());
    assertEquals(1, warnings.size());
    assertEquals(
        "The frame-src directive is deprecated as of CSP version 1.1. Authors who wish to govern nested browsing contexts SHOULD use the child-src directive instead.",
        warnings.iterator().next().message);
  }
  @Test
  public void shouldGive3PositionsForP15_15() {
    center = new Position(15, 15);
    iter = Utility.get8NeighborhoodIterator(center);
    neighborhood = convertIteration2List(iter);

    assertTrue("Must contain (14,15)", neighborhood.contains(new Position(14, 15)));
    assertTrue("Must contain (14,14)", neighborhood.contains(new Position(14, 14)));
    assertTrue("Must contain (15,14)", neighborhood.contains(new Position(15, 14)));

    assertEquals("Should be 3 positions in the iterator", 3, neighborhood.size());
  }
  @Test
  public void shouldGive8PositionsForP8_8() {
    center = new Position(8, 8);
    iter = Utility.get8NeighborhoodIterator(center);
    neighborhood = convertIteration2List(iter);

    assertTrue("Must contain (7,7)", neighborhood.contains(new Position(7, 7)));
    assertTrue("Must contain (9,9)", neighborhood.contains(new Position(9, 9)));
    assertTrue("Must contain (7,9)", neighborhood.contains(new Position(7, 9)));
    assertTrue("Must contain (8,7)", neighborhood.contains(new Position(8, 7)));

    assertFalse("Must not contain center position", neighborhood.contains(center));

    assertFalse("Must not contain (5,5) position", neighborhood.contains(new Position(5, 5)));

    assertEquals("Should be 8 positions in the iterator", 8, neighborhood.size());
  }
  @Test
  public void test1() throws Exception {
    File historyFile = new File("src/main/java/history/EURUSDD1");
    MarketDataFromFile mdff = new MarketDataFromFile("EURUSD", 1440, historyFile);
    MarketData data1 =
        new MarketData("EURUSD", "2015-02-26 06:00:00", 1.1100, 1.1110, 1.1150, 1.1000, 1000);
    mdff.Update(data1);
    ArrayList<MarketData> series = mdff.getMarketData();
    MarketData currData = series.get(series.size() - 1);
    MarketData prevData = series.get(series.size() - 2);

    assertEquals(currData.getHigh(), 1.13895, 0.000001);
    assertEquals(currData.getLow(), 1.13364, 0.000001);
    assertEquals(currData.getOpen(), 1.13422, 0.000001);
    assertEquals(currData.getClose(), 1.13619, 0.000001);
    assertEquals(prevData.getHigh(), 1.13589, 0.000001);
    assertEquals(prevData.getLow(), 1.12890, 0.000001);
    assertEquals(prevData.getOpen(), 1.13353, 0.000001);
    assertEquals(prevData.getClose(), 1.13422, 0.000001);
  }
  /** Test pending ranges and write endpoints when multiple nodes are on the move simultaneously */
  @Test
  public void testSimultaneousMove() throws UnknownHostException {
    StorageService ss = StorageService.instance;
    final int RING_SIZE = 10;
    TokenMetadata tmd = ss.getTokenMetadata();
    tmd.clearUnsafe();
    IPartitioner partitioner = RandomPartitioner.instance;
    VersionedValue.VersionedValueFactory valueFactory =
        new VersionedValue.VersionedValueFactory(partitioner);

    ArrayList<Token> endpointTokens = new ArrayList<Token>();
    ArrayList<Token> keyTokens = new ArrayList<Token>();
    List<InetAddress> hosts = new ArrayList<InetAddress>();
    List<UUID> hostIds = new ArrayList<UUID>();

    // create a ring or 10 nodes
    Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, RING_SIZE);

    // nodes 6, 8 and 9 leave
    final int[] LEAVING = new int[] {6, 8, 9};
    for (int leaving : LEAVING)
      ss.onChange(
          hosts.get(leaving),
          ApplicationState.STATUS,
          valueFactory.leaving(Collections.singleton(endpointTokens.get(leaving))));

    // boot two new nodes with keyTokens.get(5) and keyTokens.get(7)
    InetAddress boot1 = InetAddress.getByName("127.0.1.1");
    Gossiper.instance.initializeNodeUnsafe(boot1, UUID.randomUUID(), 1);
    Gossiper.instance.injectApplicationState(
        boot1,
        ApplicationState.TOKENS,
        valueFactory.tokens(Collections.singleton(keyTokens.get(5))));
    ss.onChange(
        boot1,
        ApplicationState.STATUS,
        valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(5))));
    InetAddress boot2 = InetAddress.getByName("127.0.1.2");
    Gossiper.instance.initializeNodeUnsafe(boot2, UUID.randomUUID(), 1);
    Gossiper.instance.injectApplicationState(
        boot2,
        ApplicationState.TOKENS,
        valueFactory.tokens(Collections.singleton(keyTokens.get(7))));
    ss.onChange(
        boot2,
        ApplicationState.STATUS,
        valueFactory.bootstrapping(Collections.<Token>singleton(keyTokens.get(7))));

    Collection<InetAddress> endpoints = null;

    /* don't require test update every time a new keyspace is added to test/conf/cassandra.yaml */
    Map<String, AbstractReplicationStrategy> keyspaceStrategyMap =
        new HashMap<String, AbstractReplicationStrategy>();
    for (int i = 1; i <= 4; i++) {
      keyspaceStrategyMap.put(
          "LeaveAndBootstrapTestKeyspace" + i,
          getStrategy("LeaveAndBootstrapTestKeyspace" + i, tmd));
    }

    // pre-calculate the results.
    Map<String, Multimap<Token, InetAddress>> expectedEndpoints =
        new HashMap<String, Multimap<Token, InetAddress>>();
    expectedEndpoints.put(KEYSPACE1, HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints
        .get(KEYSPACE1)
        .putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8"));
    expectedEndpoints
        .get(KEYSPACE1)
        .putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints
        .get(KEYSPACE1)
        .putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1"));
    expectedEndpoints.get(KEYSPACE1).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1"));
    expectedEndpoints.put(KEYSPACE2, HashMultimap.<Token, InetAddress>create());
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("35"), makeAddrs("127.0.0.5"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("45"), makeAddrs("127.0.0.6"));
    expectedEndpoints
        .get(KEYSPACE2)
        .putAll(new BigIntegerToken("55"), makeAddrs("127.0.0.7", "127.0.0.8", "127.0.1.1"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("65"), makeAddrs("127.0.0.8"));
    expectedEndpoints
        .get(KEYSPACE2)
        .putAll(new BigIntegerToken("75"), makeAddrs("127.0.0.9", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints
        .get(KEYSPACE2)
        .putAll(new BigIntegerToken("85"), makeAddrs("127.0.0.10", "127.0.0.1"));
    expectedEndpoints.get(KEYSPACE2).putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1"));
    expectedEndpoints.put(KEYSPACE3, HashMultimap.<Token, InetAddress>create());
    expectedEndpoints
        .get(KEYSPACE3)
        .putAll(
            new BigIntegerToken("5"),
            makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints
        .get(KEYSPACE3)
        .putAll(
            new BigIntegerToken("15"),
            makeAddrs(
                "127.0.0.3",
                "127.0.0.4",
                "127.0.0.5",
                "127.0.0.6",
                "127.0.0.7",
                "127.0.1.1",
                "127.0.0.8"));
    expectedEndpoints
        .get(KEYSPACE3)
        .putAll(
            new BigIntegerToken("25"),
            makeAddrs(
                "127.0.0.4",
                "127.0.0.5",
                "127.0.0.6",
                "127.0.0.7",
                "127.0.0.8",
                "127.0.1.2",
                "127.0.0.1",
                "127.0.1.1"));
    expectedEndpoints
        .get(KEYSPACE3)
        .putAll(
            new BigIntegerToken("35"),
            makeAddrs(
                "127.0.0.5",
                "127.0.0.6",
                "127.0.0.7",
                "127.0.0.8",
                "127.0.0.9",
                "127.0.1.2",
                "127.0.0.1",
                "127.0.0.2",
                "127.0.1.1"));
    expectedEndpoints
        .get(KEYSPACE3)
        .putAll(
            new BigIntegerToken("45"),
            makeAddrs(
                "127.0.0.6",
                "127.0.0.7",
                "127.0.0.8",
                "127.0.0.9",
                "127.0.0.10",
                "127.0.1.2",
                "127.0.0.1",
                "127.0.0.2",
                "127.0.1.1",
                "127.0.0.3"));
    expectedEndpoints
        .get(KEYSPACE3)
        .putAll(
            new BigIntegerToken("55"),
            makeAddrs(
                "127.0.0.7",
                "127.0.0.8",
                "127.0.0.9",
                "127.0.0.10",
                "127.0.0.1",
                "127.0.0.2",
                "127.0.0.3",
                "127.0.0.4",
                "127.0.1.1",
                "127.0.1.2"));
    expectedEndpoints
        .get(KEYSPACE3)
        .putAll(
            new BigIntegerToken("65"),
            makeAddrs(
                "127.0.0.8",
                "127.0.0.9",
                "127.0.0.10",
                "127.0.0.1",
                "127.0.0.2",
                "127.0.1.2",
                "127.0.0.3",
                "127.0.0.4"));
    expectedEndpoints
        .get(KEYSPACE3)
        .putAll(
            new BigIntegerToken("75"),
            makeAddrs(
                "127.0.0.9",
                "127.0.0.10",
                "127.0.0.1",
                "127.0.0.2",
                "127.0.0.3",
                "127.0.1.2",
                "127.0.0.4",
                "127.0.0.5"));
    expectedEndpoints
        .get(KEYSPACE3)
        .putAll(
            new BigIntegerToken("85"),
            makeAddrs(
                "127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints
        .get(KEYSPACE3)
        .putAll(
            new BigIntegerToken("95"),
            makeAddrs("127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints.put(KEYSPACE4, HashMultimap.<Token, InetAddress>create());
    expectedEndpoints
        .get(KEYSPACE4)
        .putAll(new BigIntegerToken("5"), makeAddrs("127.0.0.2", "127.0.0.3", "127.0.0.4"));
    expectedEndpoints
        .get(KEYSPACE4)
        .putAll(new BigIntegerToken("15"), makeAddrs("127.0.0.3", "127.0.0.4", "127.0.0.5"));
    expectedEndpoints
        .get(KEYSPACE4)
        .putAll(new BigIntegerToken("25"), makeAddrs("127.0.0.4", "127.0.0.5", "127.0.0.6"));
    expectedEndpoints
        .get(KEYSPACE4)
        .putAll(
            new BigIntegerToken("35"),
            makeAddrs("127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.1.1", "127.0.0.8"));
    expectedEndpoints
        .get(KEYSPACE4)
        .putAll(
            new BigIntegerToken("45"),
            makeAddrs(
                "127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.1.2", "127.0.0.1", "127.0.1.1"));
    expectedEndpoints
        .get(KEYSPACE4)
        .putAll(
            new BigIntegerToken("55"),
            makeAddrs(
                "127.0.0.7",
                "127.0.0.8",
                "127.0.0.9",
                "127.0.0.1",
                "127.0.0.2",
                "127.0.1.1",
                "127.0.1.2"));
    expectedEndpoints
        .get(KEYSPACE4)
        .putAll(
            new BigIntegerToken("65"),
            makeAddrs(
                "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.1.2", "127.0.0.1", "127.0.0.2"));
    expectedEndpoints
        .get(KEYSPACE4)
        .putAll(
            new BigIntegerToken("75"),
            makeAddrs(
                "127.0.0.9", "127.0.0.10", "127.0.0.1", "127.0.1.2", "127.0.0.2", "127.0.0.3"));
    expectedEndpoints
        .get(KEYSPACE4)
        .putAll(
            new BigIntegerToken("85"),
            makeAddrs("127.0.0.10", "127.0.0.1", "127.0.0.2", "127.0.0.3"));
    expectedEndpoints
        .get(KEYSPACE4)
        .putAll(new BigIntegerToken("95"), makeAddrs("127.0.0.1", "127.0.0.2", "127.0.0.3"));

    PendingRangeCalculatorService.instance.blockUntilFinished();

    for (Map.Entry<String, AbstractReplicationStrategy> keyspaceStrategy :
        keyspaceStrategyMap.entrySet()) {
      String keyspaceName = keyspaceStrategy.getKey();
      AbstractReplicationStrategy strategy = keyspaceStrategy.getValue();

      for (int i = 0; i < keyTokens.size(); i++) {
        endpoints =
            tmd.getWriteEndpoints(
                keyTokens.get(i), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(i)));
        assertEquals(
            expectedEndpoints.get(keyspaceName).get(keyTokens.get(i)).size(), endpoints.size());
        assertTrue(
            expectedEndpoints.get(keyspaceName).get(keyTokens.get(i)).containsAll(endpoints));
      }

      // just to be sure that things still work according to the old tests, run them:
      if (strategy.getReplicationFactor() != 3) continue;
      // tokens 5, 15 and 25 should go three nodes
      for (int i = 0; i < 3; ++i) {
        endpoints =
            tmd.getWriteEndpoints(
                keyTokens.get(i), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(i)));
        assertEquals(3, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(i + 1)));
        assertTrue(endpoints.contains(hosts.get(i + 2)));
        assertTrue(endpoints.contains(hosts.get(i + 3)));
      }

      // token 35 should go to nodes 4, 5, 6, 7 and boot1
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(3), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(3)));
      assertEquals(5, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(4)));
      assertTrue(endpoints.contains(hosts.get(5)));
      assertTrue(endpoints.contains(hosts.get(6)));
      assertTrue(endpoints.contains(hosts.get(7)));
      assertTrue(endpoints.contains(boot1));

      // token 45 should go to nodes 5, 6, 7, 0, boot1 and boot2
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(4), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(4)));
      assertEquals(6, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(5)));
      assertTrue(endpoints.contains(hosts.get(6)));
      assertTrue(endpoints.contains(hosts.get(7)));
      assertTrue(endpoints.contains(hosts.get(0)));
      assertTrue(endpoints.contains(boot1));
      assertTrue(endpoints.contains(boot2));

      // token 55 should go to nodes 6, 7, 8, 0, 1, boot1 and boot2
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(5), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(5)));
      assertEquals(7, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(6)));
      assertTrue(endpoints.contains(hosts.get(7)));
      assertTrue(endpoints.contains(hosts.get(8)));
      assertTrue(endpoints.contains(hosts.get(0)));
      assertTrue(endpoints.contains(hosts.get(1)));
      assertTrue(endpoints.contains(boot1));
      assertTrue(endpoints.contains(boot2));

      // token 65 should go to nodes 7, 8, 9, 0, 1 and boot2
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(6), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(6)));
      assertEquals(6, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(7)));
      assertTrue(endpoints.contains(hosts.get(8)));
      assertTrue(endpoints.contains(hosts.get(9)));
      assertTrue(endpoints.contains(hosts.get(0)));
      assertTrue(endpoints.contains(hosts.get(1)));
      assertTrue(endpoints.contains(boot2));

      // token 75 should to go nodes 8, 9, 0, 1, 2 and boot2
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(7), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(7)));
      assertEquals(6, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(8)));
      assertTrue(endpoints.contains(hosts.get(9)));
      assertTrue(endpoints.contains(hosts.get(0)));
      assertTrue(endpoints.contains(hosts.get(1)));
      assertTrue(endpoints.contains(hosts.get(2)));
      assertTrue(endpoints.contains(boot2));

      // token 85 should go to nodes 9, 0, 1 and 2
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(8), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(8)));
      assertEquals(4, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(9)));
      assertTrue(endpoints.contains(hosts.get(0)));
      assertTrue(endpoints.contains(hosts.get(1)));
      assertTrue(endpoints.contains(hosts.get(2)));

      // token 95 should go to nodes 0, 1 and 2
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(9), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(9)));
      assertEquals(3, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(0)));
      assertTrue(endpoints.contains(hosts.get(1)));
      assertTrue(endpoints.contains(hosts.get(2)));
    }

    // Now finish node 6 and node 9 leaving, as well as boot1 (after this node 8 is still
    // leaving and boot2 in progress
    ss.onChange(
        hosts.get(LEAVING[0]),
        ApplicationState.STATUS,
        valueFactory.left(
            Collections.singleton(endpointTokens.get(LEAVING[0])), Gossiper.computeExpireTime()));
    ss.onChange(
        hosts.get(LEAVING[2]),
        ApplicationState.STATUS,
        valueFactory.left(
            Collections.singleton(endpointTokens.get(LEAVING[2])), Gossiper.computeExpireTime()));
    ss.onChange(
        boot1,
        ApplicationState.STATUS,
        valueFactory.normal(Collections.singleton(keyTokens.get(5))));

    // adjust precalcuated results.  this changes what the epected endpoints are.
    expectedEndpoints
        .get(KEYSPACE1)
        .get(new BigIntegerToken("55"))
        .removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints
        .get(KEYSPACE1)
        .get(new BigIntegerToken("85"))
        .removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints
        .get(KEYSPACE2)
        .get(new BigIntegerToken("55"))
        .removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints
        .get(KEYSPACE2)
        .get(new BigIntegerToken("85"))
        .removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints
        .get(KEYSPACE3)
        .get(new BigIntegerToken("15"))
        .removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints
        .get(KEYSPACE3)
        .get(new BigIntegerToken("25"))
        .removeAll(makeAddrs("127.0.0.7", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints
        .get(KEYSPACE3)
        .get(new BigIntegerToken("35"))
        .removeAll(makeAddrs("127.0.0.7", "127.0.0.2"));
    expectedEndpoints
        .get(KEYSPACE3)
        .get(new BigIntegerToken("45"))
        .removeAll(makeAddrs("127.0.0.7", "127.0.0.10", "127.0.0.3"));
    expectedEndpoints
        .get(KEYSPACE3)
        .get(new BigIntegerToken("55"))
        .removeAll(makeAddrs("127.0.0.7", "127.0.0.10", "127.0.0.4"));
    expectedEndpoints
        .get(KEYSPACE3)
        .get(new BigIntegerToken("65"))
        .removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints
        .get(KEYSPACE3)
        .get(new BigIntegerToken("75"))
        .removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints
        .get(KEYSPACE3)
        .get(new BigIntegerToken("85"))
        .removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints
        .get(KEYSPACE4)
        .get(new BigIntegerToken("35"))
        .removeAll(makeAddrs("127.0.0.7", "127.0.0.8"));
    expectedEndpoints
        .get(KEYSPACE4)
        .get(new BigIntegerToken("45"))
        .removeAll(makeAddrs("127.0.0.7", "127.0.1.2", "127.0.0.1"));
    expectedEndpoints
        .get(KEYSPACE4)
        .get(new BigIntegerToken("55"))
        .removeAll(makeAddrs("127.0.0.2", "127.0.0.7"));
    expectedEndpoints
        .get(KEYSPACE4)
        .get(new BigIntegerToken("65"))
        .removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints
        .get(KEYSPACE4)
        .get(new BigIntegerToken("75"))
        .removeAll(makeAddrs("127.0.0.10"));
    expectedEndpoints
        .get(KEYSPACE4)
        .get(new BigIntegerToken("85"))
        .removeAll(makeAddrs("127.0.0.10"));

    PendingRangeCalculatorService.instance.blockUntilFinished();

    for (Map.Entry<String, AbstractReplicationStrategy> keyspaceStrategy :
        keyspaceStrategyMap.entrySet()) {
      String keyspaceName = keyspaceStrategy.getKey();
      AbstractReplicationStrategy strategy = keyspaceStrategy.getValue();

      for (int i = 0; i < keyTokens.size(); i++) {
        endpoints =
            tmd.getWriteEndpoints(
                keyTokens.get(i), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(i)));
        assertEquals(
            expectedEndpoints.get(keyspaceName).get(keyTokens.get(i)).size(), endpoints.size());
        assertTrue(
            expectedEndpoints.get(keyspaceName).get(keyTokens.get(i)).containsAll(endpoints));
      }

      if (strategy.getReplicationFactor() != 3) continue;
      // leave this stuff in to guarantee the old tests work the way they were supposed to.
      // tokens 5, 15 and 25 should go three nodes
      for (int i = 0; i < 3; ++i) {
        endpoints =
            tmd.getWriteEndpoints(
                keyTokens.get(i), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(i)));
        assertEquals(3, endpoints.size());
        assertTrue(endpoints.contains(hosts.get(i + 1)));
        assertTrue(endpoints.contains(hosts.get(i + 2)));
        assertTrue(endpoints.contains(hosts.get(i + 3)));
      }

      // token 35 goes to nodes 4, 5 and boot1
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(3), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(3)));
      assertEquals(3, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(4)));
      assertTrue(endpoints.contains(hosts.get(5)));
      assertTrue(endpoints.contains(boot1));

      // token 45 goes to nodes 5, boot1 and node7
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(4), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(4)));
      assertEquals(3, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(5)));
      assertTrue(endpoints.contains(boot1));
      assertTrue(endpoints.contains(hosts.get(7)));

      // token 55 goes to boot1, 7, boot2, 8 and 0
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(5), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(5)));
      assertEquals(5, endpoints.size());
      assertTrue(endpoints.contains(boot1));
      assertTrue(endpoints.contains(hosts.get(7)));
      assertTrue(endpoints.contains(boot2));
      assertTrue(endpoints.contains(hosts.get(8)));
      assertTrue(endpoints.contains(hosts.get(0)));

      // token 65 goes to nodes 7, boot2, 8, 0 and 1
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(6), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(6)));
      assertEquals(5, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(7)));
      assertTrue(endpoints.contains(boot2));
      assertTrue(endpoints.contains(hosts.get(8)));
      assertTrue(endpoints.contains(hosts.get(0)));
      assertTrue(endpoints.contains(hosts.get(1)));

      // token 75 goes to nodes boot2, 8, 0, 1 and 2
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(7), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(7)));
      assertEquals(5, endpoints.size());
      assertTrue(endpoints.contains(boot2));
      assertTrue(endpoints.contains(hosts.get(8)));
      assertTrue(endpoints.contains(hosts.get(0)));
      assertTrue(endpoints.contains(hosts.get(1)));
      assertTrue(endpoints.contains(hosts.get(2)));

      // token 85 goes to nodes 0, 1 and 2
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(8), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(8)));
      assertEquals(3, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(0)));
      assertTrue(endpoints.contains(hosts.get(1)));
      assertTrue(endpoints.contains(hosts.get(2)));

      // token 95 goes to nodes 0, 1 and 2
      endpoints =
          tmd.getWriteEndpoints(
              keyTokens.get(9), keyspaceName, strategy.getNaturalEndpoints(keyTokens.get(9)));
      assertEquals(3, endpoints.size());
      assertTrue(endpoints.contains(hosts.get(0)));
      assertTrue(endpoints.contains(hosts.get(1)));
      assertTrue(endpoints.contains(hosts.get(2)));
    }
  }
Exemple #7
0
  public PalindromeTest() {
    ArrayList<String> words = new ArrayList<String>();
    ArrayList<String> palis;

    try {
      for (String line : Files.readAllLines(Paths.get("com/jsanders/web2"))) {
        words.add(line);
      }
    } catch (IOException ex) {
      System.out.println("IO error");
      System.exit(1);
    }

    palis = Palindrome.palindromes(words);
    assertEquals(palis.size(), 161, 0.01);

    int shortest = Word.shortestLength(words);
    assertEquals(shortest, 1, 0.01);

    int longest = Word.longestLength(words);
    assertEquals(longest, 24, 0.01);

    ArrayList<String> shortestWords = Word.shortestWords(words);
    assertEquals(shortestWords.size(), 52, 0.01);

    ArrayList<String> longestWords = Word.longestWords(words);
    assertEquals(longestWords.size(), 5, 0.01);

    int totalWords = Word.totalWords(words);
    double avgLen = Word.averageLength(words);

    assertEquals(totalWords, 235886, 0.01);
    assertEquals(avgLen, 9.56, 0.01);

    ArrayList<Double> letterFreq = Word.letterFrequency(words);
    assertEquals(letterFreq.get(0), 0.087, 0.01);

    double properFreq = Word.properFrequency(words);
    assertEquals(properFreq, 0.106, 0.01);

    ArrayList<Integer> startFreq = Word.startFrequency(words);
    assertEquals(startFreq.get(0), 17096, 0.01);

    ArrayList<String> sameStartEnd = Word.startEndWords(words);
    assertEquals(sameStartEnd.size(), 11505, 0.01);

    try {
      PrintWriter f = new PrintWriter("short.txt");
      for (String w : shortestWords) f.println(w);
      f.close();

      f = new PrintWriter("long.txt");
      for (String w : longestWords) f.println(w);
      f.close();

      f = new PrintWriter("same.txt");
      for (String w : sameStartEnd) f.println(w);
      f.close();

      f = new PrintWriter("statistics.txt");
      f.println("avg word len: " + avgLen);
      f.println("freq of letters: " + letterFreq);
      f.println("freq of proper nouns/names: " + properFreq);
      f.println("words that start with each letter:: " + startFreq);

      f.close();
    } catch (IOException ex) {
      System.out.println("IO error");
      System.exit(1);
    }
  }
  @Test
  public void testParseMulti() throws ParseException, TokeniserException {
    List<Policy> pl;
    ArrayList<Warning> warnings;

    pl =
        Parser.parseMulti(
            "script-src a; script-src b, , script-src c; script-src d", "https://origin.com");
    assertEquals(2, pl.size());
    assertEquals("script-src a", pl.get(0).show());
    assertEquals("script-src c", pl.get(1).show());

    pl = Parser.parseMulti("script-src a,", URI.parse("https://origin.com"));
    assertEquals(2, pl.size());
    assertEquals("script-src a", pl.get(0).show());
    assertEquals("", pl.get(1).show());

    warnings = new ArrayList<>();
    pl = Parser.parseMulti("script-src a,", URI.parse("https://origin.com"), warnings);
    assertEquals(2, pl.size());
    assertEquals("script-src a", pl.get(0).show());
    assertEquals("", pl.get(1).show());
    assertEquals(0, warnings.size());

    warnings = new ArrayList<>();
    pl = Parser.parseMulti("script-src a, sandbox", "https://origin.com", warnings);
    assertEquals(2, pl.size());
    assertEquals("script-src a", pl.get(0).show());
    assertEquals("sandbox", pl.get(1).show());
    assertEquals(0, warnings.size());

    warnings = new ArrayList<>();
    pl =
        ParserWithLocation.parseMulti(
            "   plugin-types  a/b  , script-src 'unsafe-redirect'", "https://origin.com", warnings);
    assertEquals(2, pl.size());
    assertEquals("plugin-types a/b", pl.get(0).show());
    assertEquals("script-src 'unsafe-redirect'", pl.get(1).show());
    assertEquals(1, warnings.size());
    assertEquals(
        "1:36: 'unsafe-redirect' has been removed from CSP as of version 2.0",
        warnings.get(0).show());

    warnings = new ArrayList<>();
    pl =
        ParserWithLocation.parseMulti(
            "script-src a, frame-src b", URI.parse("https://origin.com"), warnings);
    assertEquals(2, pl.size());
    assertEquals("script-src a", pl.get(0).show());
    assertEquals("frame-src b", pl.get(1).show());
    assertEquals(1, warnings.size());
    assertEquals(
        "1:15: The frame-src directive is deprecated as of CSP version 1.1. Authors who wish to govern nested browsing contexts SHOULD use the child-src directive instead.",
        warnings.get(0).show());

    try {
      pl.clear();
      pl = Parser.parseMulti("script-src a,b", "https://origin.com");
      fail();
    } catch (IllegalArgumentException e1) {
      assertEquals(0, pl.size());
      assertEquals("Unrecognised directive name: b", e1.getMessage());
    }

    try {
      ParserWithLocation.parse(
          "script-src a, script-src b", "https://origin.com", new ArrayList<>());
      fail();
    } catch (ParseException e1) {
      assertEquals(0, pl.size());
      assertEquals("1:13: expecting end of policy but found ,", e1.getMessage());
    }

    try {
      Parser.parse("script-src a, script-src b", "https://origin.com");
      fail();
    } catch (ParseException e1) {
      assertEquals(0, pl.size());
      assertEquals("expecting end of policy but found ,", e1.getMessage());
    }

    try {
      pl.clear();
      pl = ParserWithLocation.parseMulti("allow 'none', options", "https://origin.com");
      fail();
    } catch (ParseException e1) {
      assertEquals(0, pl.size());
      assertEquals(
          "1:1: The allow directive has been replaced with default-src and is not in the CSP specification.",
          e1.getMessage());
    }

    try {
      pl.clear();
      pl = ParserWithLocation.parseMulti("allow 'none', referrer", URI.parse("https://origin.com"));
      fail();
    } catch (ParseException e1) {
      assertEquals(0, pl.size());
      assertEquals(
          "1:1: The allow directive has been replaced with default-src and is not in the CSP specification.",
          e1.getMessage());
    }

    failsToParse("script-src *, ");
  }
  public void checkStringSerializers(ArrayList keys) throws IOException {
    Collections.sort(keys);
    // first check clone on both
    checkKeyClone(Serializer.STRING, keys.toArray());
    checkKeyClone(Serializer.STRING_DELTA, keys.toArray());
    checkKeyClone(Serializer.STRING_DELTA2, keys.toArray());
    //    TODO compatible format between STRING DELTA SER?
    //        //now serializer and deserialize with other and compare
    //        {
    //            DataOutput2 out = new DataOutput2();
    //            Serializer.STRING_DELTA.valueArraySerialize(out,
    // Serializer.STRING_DELTA.valueArrayFromArray(keys.toArray()));
    //
    //            DataInput2.ByteArray in = new DataInput2.ByteArray(out.buf);
    //            Object[] keys2 =
    // Serializer.STRING_DELTA2.valueArrayToArray(Serializer.STRING_DELTA2.valueArrayDeserialize(in,
    // keys.size()));
    //
    //            assertArrayEquals(keys.toArray(), keys2);
    //        }
    //
    //        {
    //            DataOutput2 out = new DataOutput2();
    //            Serializer.STRING_DELTA2.valueArraySerialize(out,
    // Serializer.STRING_DELTA2.valueArrayFromArray(keys.toArray()));
    //
    //            DataInput2.ByteArray in = new DataInput2.ByteArray(out.buf);
    //            Object[] keys2 =
    // Serializer.STRING_DELTA.valueArrayToArray(Serializer.STRING_DELTA.valueArrayDeserialize(in,
    // keys.size()));
    //
    //            assertArrayEquals(keys.toArray(), keys2);
    //        }

    // convert to byte[] and check with BYTE_ARRAY serializers
    for (int i = 0; i < keys.size(); i++) {
      keys.set(i, ((String) keys.get(i)).getBytes());
    }

    // first check clone on both
    checkKeyClone(Serializer.BYTE_ARRAY, keys.toArray());
    checkKeyClone(Serializer.BYTE_ARRAY_DELTA, keys.toArray());
    checkKeyClone(Serializer.BYTE_ARRAY_DELTA2, keys.toArray());
    //    TODO compatible format between byte[] DELTA SER?
    //        //now serializer and deserialize with other and compare
    //        {
    //            DataOutput2 out = new DataOutput2();
    //            Serializer.BYTE_ARRAY.valueArraySerialize(out,
    // Serializer.BYTE_ARRAY.valueArrayFromArray(keys.toArray()));
    //
    //            DataInput2.ByteArray in = new DataInput2.ByteArray(out.buf);
    //            Object[] keys2 =
    // Serializer.BYTE_ARRAY_DELTA2.valueArrayToArray(Serializer.BYTE_ARRAY_DELTA2.valueArrayDeserialize(in, keys.size()));
    //
    //            assertArrayEquals(keys.toArray(), keys2);
    //        }
    //
    //        {
    //            DataOutput2 out = new DataOutput2();
    //            Serializer.BYTE_ARRAY_DELTA2.valueArraySerialize(out,
    // Serializer.BYTE_ARRAY_DELTA2.valueArrayFromArray(keys.toArray()));
    //
    //            DataInput2.ByteArray in = new DataInput2.ByteArray(out.buf);
    //            Object[] keys2 =
    // Serializer.BYTE_ARRAY.valueArrayToArray(Serializer.BYTE_ARRAY.valueArrayDeserialize(in,
    // keys.size()));
    //
    //            assertArrayEquals(keys.toArray(), keys2);
    //        }

  }