예제 #1
1
 @Test
 public void testPutRemoveGet() {
   Map<Integer, Integer> myMap = new MyMap<>();
   Map<Integer, Integer> control = new HashMap<>();
   for (int i = 0; i < N; i++) {
     int k = random.nextInt();
     int v = random.nextInt();
     myMap.put(k, v);
     control.put(k, v);
   }
   Set<Integer> keysToRemove = new HashSet<>();
   for (int k : control.keySet()) {
     if (random.nextBoolean()) {
       keysToRemove.add(k);
     }
   }
   for (int k : keysToRemove) {
     control.remove(k);
     myMap.remove(k);
   }
   assertEquals(myMap.size(), control.size());
   for (int k : control.keySet()) {
     assertEquals(myMap.get(k), control.get(k));
     int r = random.nextInt();
     assertEquals(myMap.get(r), control.get(r));
   }
 }
예제 #2
0
  @Test
  public void dataset() {
    Document doc =
        Jsoup.parse(
            "<div id=1 data-name=jsoup class=new data-package=jar>Hello</div><p id=2>Hello</p>");
    Element div = doc.select("div").first();
    Map<String, String> dataset = div.dataset();
    Attributes attributes = div.attributes();

    // size, get, set, add, remove
    assertEquals(2, dataset.size());
    assertEquals("jsoup", dataset.get("name"));
    assertEquals("jar", dataset.get("package"));

    dataset.put("name", "jsoup updated");
    dataset.put("language", "java");
    dataset.remove("package");

    assertEquals(2, dataset.size());
    assertEquals(4, attributes.size());
    assertEquals("jsoup updated", attributes.get("data-name"));
    assertEquals("jsoup updated", dataset.get("name"));
    assertEquals("java", attributes.get("data-language"));
    assertEquals("java", dataset.get("language"));

    attributes.put("data-food", "bacon");
    assertEquals(3, dataset.size());
    assertEquals("bacon", dataset.get("food"));

    attributes.put("data-", "empty");
    assertEquals(null, dataset.get("")); // data- is not a data attribute

    Element p = doc.select("p").first();
    assertEquals(0, p.dataset().size());
  }
 @Test
 public void getPuttedValueFromTheMap() {
   HazelcastClient hClient = getHazelcastClient();
   Map<String, String> clientMap = hClient.getMap("getPuttedValueFromTheMap");
   int size = clientMap.size();
   clientMap.put("1", "Z");
   String value = clientMap.get("1");
   assertEquals("Z", value);
   assertEquals(size + 1, clientMap.size());
 }
 @Test
 public void testSize() {
   dict = new HashTable<>(31);
   assertEquals(0, dict.size());
   dict.put("duncan", "is a person.");
   assertEquals(1, dict.size());
   dict.put("one", "last time");
   assertEquals(2, dict.size());
   dict.remove("duncan");
   assertEquals(1, dict.size());
   dict.remove("duncan");
   assertEquals(1, dict.size());
 }
예제 #5
0
  @Test
  public void getDistinctKeysAndCounts_SortByKeyDescending() {

    Connection connection = null;
    ResultSet resultSet = null;
    try {
      ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true);
      initWithTestData(connectionManager);

      connection = connectionManager.getConnection(null);
      resultSet = DBQueries.getDistinctKeysAndCounts(true, NAME, connection);

      Map<String, Integer> resultSetToMap = resultSetToMap(resultSet);
      assertEquals(3, resultSetToMap.size());

      Iterator<Map.Entry<String, Integer>> entriesIterator = resultSetToMap.entrySet().iterator();

      Map.Entry entry = entriesIterator.next();
      assertEquals("gps", entry.getKey());
      assertEquals(1, entry.getValue());

      entry = entriesIterator.next();
      assertEquals("airbags", entry.getKey());
      assertEquals(1, entry.getValue());

      entry = entriesIterator.next();
      assertEquals("abs", entry.getKey());
      assertEquals(2, entry.getValue());

    } finally {
      DBUtils.closeQuietly(resultSet);
      DBUtils.closeQuietly(connection);
    }
  }
 @Test
 public void putToTheMap() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   Map<String, String> clientMap = hClient.getMap("putToTheMap");
   assertEquals(0, clientMap.size());
   String result = clientMap.put("1", "CBDEF");
   assertNull(result);
   assertEquals("CBDEF", clientMap.get("1"));
   assertEquals("CBDEF", clientMap.get("1"));
   assertEquals("CBDEF", clientMap.get("1"));
   assertEquals(1, clientMap.size());
   result = clientMap.put("1", "B");
   assertEquals("CBDEF", result);
   assertEquals("B", clientMap.get("1"));
   assertEquals("B", clientMap.get("1"));
 }
 @Test
 public void putAll() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("putAll");
   int counter = 100;
   Set keys = new HashSet(counter);
   for (int i = 0; i < counter; i++) {
     keys.add(i);
   }
   Map all = map.getAll(keys);
   assertEquals(0, all.size());
   Map tempMap = new HashMap();
   for (int i = 0; i < counter; i++) {
     tempMap.put(i, i);
   }
   map.putAll(tempMap);
   for (int i = 0; i < counter; i++) {
     assertEquals(i, map.get(i));
   }
   all = map.getAll(keys);
   assertEquals(counter, all.size());
 }
예제 #8
0
 @Test
 public void shouldFetchAllIngredients() {
   Iterable<Ingredient> ingredients = flavorController.getIngredients();
   assertNotNull(ingredients);
   Map<String, Ingredient> ing = new HashMap<>();
   for (Ingredient ingredient : ingredients) {
     ing.put(ingredient.getName(), ingredient);
   }
   assertEquals(5, ing.size());
   assertEquals(ingredientRepository.findByName("Chicken").iterator().next(), ing.get("Chicken"));
   assertEquals(ingredientRepository.findByName("Carrot").iterator().next(), ing.get("Carrot"));
   assertEquals(ingredientRepository.findByName("Butter").iterator().next(), ing.get("Butter"));
   assertEquals(
       ingredientRepository.findByName("Coriander").iterator().next(), ing.get("Coriander"));
   assertEquals(ingredientRepository.findByName("Yoghurt").iterator().next(), ing.get("Yoghurt"));
 }
예제 #9
0
  /** test methods that return Map of ids to Records and no sysout stuff */
  @Test
  public void testGettingOutputAsMapOfRecords() throws IOException {
    String mhldRecFileName = testDataParentPath + File.separator + "summaryHld_1-1000.mrc";
    String bibRecFileName = testDataParentPath + File.separator + "u335.mrc";

    Map<String, Record> mergedRecs =
        MergeSummaryHoldings.mergeMhldsIntoBibRecordsAsMap(bibRecFileName, mhldRecFileName);

    junit.framework.Assert.assertEquals("results should have 1 record", 1, mergedRecs.size());
    String expId = "u335";
    assertTrue("Record with id " + expId + " should be in results", mergedRecs.containsKey(expId));

    Record resultRec = mergedRecs.get(expId);
    RecordTestingUtils.assertEqualsIgnoreLeader(mergedSummaryHoldingsOutputNoUmlaut, resultRec);
    System.out.println("Test testGettingOutputAsMapOfRecords() successful");
  }
예제 #10
0
  /**
   * Тестирование на HashMap
   *
   * @throws Exception
   */
  @Test
  public void testOnHashMap() throws Exception {
    Map<UserHashcode, Picture> users = new HashMap<>();
    users.put(user1, user1.getPicture());
    assertTrue(users.containsKey(user1));

    // user3 копия user1 но не содержится в ключах Map. Поиск идет по equals, а он не переопределен
    assertFalse(users.containsKey(user3));

    // В Map есть только user1, но присутствует value для user3,
    // т.к. у класса Picture переопределены оба метода, хотя объекты pic1 и pic3 разные по ссылкам
    assertTrue(users.containsValue(pic3));
    // Объекты разные, а хэш-коды - одинаковые
    assertFalse(pic1 == pic3);
    assertEquals(pic1.hashCode(), pic3.hashCode());

    users.put(user2, user2.getPicture());
    users.put(user3, user3.getPicture());

    // user3 не перезатирает user1, т.к. ключи не равны по equals, а только по hashCode
    assertEquals(3, users.size());
  }
 @Test
 public void iterateOverMapKeys() {
   HazelcastClient hClient = getHazelcastClient();
   Map<String, String> map = hClient.getMap("iterateOverMapKeys");
   map.put("1", "A");
   map.put("2", "B");
   map.put("3", "C");
   Set<String> keySet = map.keySet();
   assertEquals(3, keySet.size());
   Set<String> s = new HashSet<String>();
   for (String string : keySet) {
     s.add(string);
     assertTrue(Arrays.asList("1", "2", "3").contains(string));
   }
   assertEquals(3, s.size());
   Iterator<String> iterator = keySet.iterator();
   while (iterator.hasNext()) {
     iterator.next();
     iterator.remove();
   }
   assertEquals(0, map.size());
 }
예제 #12
0
  @Test
  public void getDistinctKeysAndCounts() {

    Connection connection = null;
    ResultSet resultSet = null;
    try {
      ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true);
      initWithTestData(connectionManager);

      connection = connectionManager.getConnection(null);
      resultSet = DBQueries.getDistinctKeysAndCounts(false, NAME, connection);

      Map<String, Integer> resultSetToMap = resultSetToMap(resultSet);
      assertEquals(3, resultSetToMap.size());
      assertEquals(new Integer(2), resultSetToMap.get("abs"));
      assertEquals(new Integer(1), resultSetToMap.get("airbags"));
      assertEquals(new Integer(1), resultSetToMap.get("gps"));

    } finally {
      DBUtils.closeQuietly(resultSet);
      DBUtils.closeQuietly(connection);
    }
  }
예제 #13
0
 @Test
 public void testEvictionPerPartition() throws InterruptedException {
   final int k = 2;
   final int size = 10;
   final String mapName = "testEvictionPerPartition";
   Config cfg = new Config();
   final MapConfig mc = cfg.getMapConfig(mapName);
   mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU);
   mc.setEvictionPercentage(25);
   final MaxSizeConfig msc = new MaxSizeConfig();
   msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_PARTITION);
   msc.setSize(size);
   mc.setMaxSizeConfig(msc);
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
   final HazelcastInstance[] instances = factory.newInstances(cfg);
   final int pnum = instances[0].getPartitionService().getPartitions().size();
   int insertCount = size * pnum * 2;
   Map map = instances[0].getMap(mapName);
   for (int i = 0; i < insertCount; i++) {
     map.put(i, i);
   }
   Thread.sleep(2000);
   assertTrue(map.size() < size * pnum * (100 - mc.getEvictionPercentage()) / 100);
 }