@Test
  public void scanTest() {
    Map<String, Map<String, ByteIterator>> keyMap = new HashMap<>();
    for (int i = 0; i < 5; i++) {
      String insertKey = KEY_PREFIX + i;
      keyMap.put(insertKey, insertRow(insertKey));
    }

    Set<String> fieldSet = new HashSet<>();
    fieldSet.add("FIELD0");
    fieldSet.add("FIELD1");
    int startIndex = 1;
    int resultRows = 3;

    Vector<HashMap<String, ByteIterator>> resultVector = new Vector<>();
    orientDBClient.scan(CLASS, KEY_PREFIX + startIndex, resultRows, fieldSet, resultVector);

    // Check the resultVector is the correct size
    assertEquals(
        "Assert the correct number of results rows were returned", resultRows, resultVector.size());
    // Check each vector row to make sure we have the correct fields
    int testIndex = startIndex;
    for (HashMap<String, ByteIterator> result : resultVector) {
      assertEquals(
          "Assert that this row has the correct number of fields", fieldSet.size(), result.size());
      for (String field : fieldSet) {
        assertEquals(
            "Assert this field is correct in this row",
            keyMap.get(KEY_PREFIX + testIndex).get(field).toString(),
            result.get(field).toString());
      }
      testIndex++;
    }
  }
  @Test(timeout = TIMEOUT)
  public void testQuickSort() {
    DValue[] arrZero = new DValue[0];
    Sorting.quickSort(arrZero, new BasicComparator(), new Random());
    assertEquals(0, arrZero.length);
    DValue[] arrOne = new DValue[1];
    arrOne[0] = new DValue(4, 0);
    Sorting.quickSort(arrOne, new BasicComparator(), new Random());
    assertEquals(1, arrOne.length);
    assertEquals(4, arrOne[0].val.intValue());

    Random rand = new Random();
    HashMap<Integer, Integer> values = new HashMap<Integer, Integer>();
    for (int i = 0; i < 50; i++) {
      int arrlen = rand.nextInt(1000) + 2;
      DValue[] arrMany = new DValue[arrlen];
      DValue[] arrManySorted = new DValue[arrlen];
      for (int j = 0; j < arrlen; j++) {
        arrMany[j] = new DValue(rand.nextInt(200) - 90, 0);
        if (values.containsKey(arrMany[j].val)) {
          arrMany[j].count = values.get(arrMany[j].val) + 1;
          values.put(arrMany[j].val, arrMany[j].count);
        } else {
          values.put(arrMany[j].val, 0);
        }
      }
      System.arraycopy(arrMany, 0, arrManySorted, 0, arrlen);
      Arrays.sort(arrManySorted);
      Sorting.quickSort(arrMany, new BasicComparator(), new Random());
      assertArrayEquals(arrManySorted, arrMany);
    }
  }
  @Test
  public void readTest() {
    String insertKey = "user0";
    Map<String, ByteIterator> insertMap = insertRow(insertKey);
    HashSet<String> readFields = new HashSet<>();
    HashMap<String, ByteIterator> readResultMap = new HashMap<>();

    // Test reading a single field
    readFields.add("FIELD0");
    orientDBClient.read(CLASS, insertKey, readFields, readResultMap);
    assertEquals(
        "Assert that result has correct number of fields", readFields.size(), readResultMap.size());
    for (String field : readFields) {
      assertEquals(
          "Assert " + field + " was read correctly",
          insertMap.get(field).toString(),
          readResultMap.get(field).toString());
    }

    readResultMap = new HashMap<>();

    // Test reading all fields
    readFields.add("FIELD1");
    readFields.add("FIELD2");
    orientDBClient.read(CLASS, insertKey, readFields, readResultMap);
    assertEquals(
        "Assert that result has correct number of fields", readFields.size(), readResultMap.size());
    for (String field : readFields) {
      assertEquals(
          "Assert " + field + " was read correctly",
          insertMap.get(field).toString(),
          readResultMap.get(field).toString());
    }
  }
  /*
     Inserts a row of deterministic values for the given insertKey using the orientDBClient.
  */
  private Map<String, ByteIterator> insertRow(String insertKey) {
    HashMap<String, ByteIterator> insertMap = new HashMap<>();
    for (int i = 0; i < 3; i++) {
      insertMap.put(
          FIELD_PREFIX + i,
          new StringByteIterator(buildDeterministicValue(insertKey, FIELD_PREFIX + i)));
    }
    orientDBClient.insert(CLASS, insertKey, insertMap);

    return insertMap;
  }
  @Test
  public void updateTest() {
    String preupdateString = "preupdate";
    String user0 = "user0";
    String user1 = "user1";
    String user2 = "user2";

    // Manually insert three documents
    for (String key : Arrays.asList(user0, user1, user2)) {
      ODocument doc = new ODocument(CLASS);
      for (int i = 0; i < NUM_FIELDS; i++) {
        doc.field(FIELD_PREFIX + i, preupdateString);
      }
      doc.save();
      orientDBDictionary.put(key, doc);
    }

    HashMap<String, ByteIterator> updateMap = new HashMap<>();
    for (int i = 0; i < NUM_FIELDS; i++) {
      updateMap.put(
          FIELD_PREFIX + i,
          new StringByteIterator(buildDeterministicValue(user1, FIELD_PREFIX + i)));
    }

    orientDBClient.update(CLASS, user1, updateMap);

    // Ensure that user0 record was not changed
    ODocument result = orientDBDictionary.get(user0);
    for (int i = 0; i < NUM_FIELDS; i++) {
      assertEquals(
          "Assert first row fields contain preupdateString",
          result.field(FIELD_PREFIX + i),
          preupdateString);
    }

    // Check that all the columns have expected values for user1 record
    result = orientDBDictionary.get(user1);
    for (int i = 0; i < NUM_FIELDS; i++) {
      assertEquals(
          "Assert updated row fields are correct",
          result.field(FIELD_PREFIX + i),
          updateMap.get(FIELD_PREFIX + i).toString());
    }

    // Ensure that user2 record was not changed
    result = orientDBDictionary.get(user2);
    for (int i = 0; i < NUM_FIELDS; i++) {
      assertEquals(
          "Assert third row fields contain preupdateString",
          result.field(FIELD_PREFIX + i),
          preupdateString);
    }
  }
示例#6
0
  public Sql2oTest(Driver driverToRegister, String url, String user, String pass, String testName) {

    if (driverToRegister != null) {
      try {
        DriverManager.registerDriver(driverToRegister);
      } catch (SQLException e) {
        throw new RuntimeException(
            "could not register driver '" + driverToRegister.getClass().getName() + "'", e);
      }
    }

    this.sql2o = new Sql2o(url, user, pass);

    HashMap<String, String> defaultColumnMap = new HashMap<String, String>();
    defaultColumnMap.put("ID", "id");
    defaultColumnMap.put("NAME", "name");
    defaultColumnMap.put("EMAIL", "email");
    defaultColumnMap.put("TEXT", "text");
    defaultColumnMap.put("ANUMBER", "aNumber");
    defaultColumnMap.put("ALONGNUMBER", "aLongNumber");
    sql2o.setDefaultColumnMappings(defaultColumnMap);

    this.url = url;
    this.user = user;
    this.pass = pass;

    if ("HyperSQL DB test".equals(testName)) {
      sql2o.createQuery("set database sql syntax MSS true").executeUpdate();
    }
  }
示例#7
0
  private static void assertSameNodesCollections(
      String objectName, Iterable<SNode> expected, Iterable<SNode> actual) {
    HashMap<SNodeId, SNode> actualIdToNodeMap = new HashMap<SNodeId, SNode>();
    for (SNode actualNode : actual) {
      actualIdToNodeMap.put(actualNode.getNodeId(), actualNode);
    }

    for (SNode expectedNode : expected) {
      SNodeId rootId = expectedNode.getNodeId();
      SNode actualNode = actualIdToNodeMap.get(rootId);
      assertNotNull("Not found expected " + objectName + " " + expectedNode, actualNode);
      assertDeepNodeEquals(expectedNode, actualNode);
      actualIdToNodeMap.remove(rootId);
    }

    assertTrue(
        "Found not expected " + objectName + " " + actualIdToNodeMap, actualIdToNodeMap.isEmpty());
  }
示例#8
0
  /*
     github issue 304
  */
  @Test
  public void testIssue304EvictionDespitePut() throws InterruptedException {
    Config c = new Config();
    c.getGroupConfig().setName("testIssue304EvictionDespitePut");
    final HashMap<String, MapConfig> mapConfigs = new HashMap<String, MapConfig>();
    final MapConfig value = new MapConfig();
    value.setMaxIdleSeconds(3);
    mapConfigs.put("default", value);
    c.setMapConfigs(mapConfigs);
    final Properties properties = new Properties();
    properties.setProperty("hazelcast.map.cleanup.delay.seconds", "1"); // we need faster cleanups
    c.setProperties(properties);
    int n = 1;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);
    final HazelcastInstance hazelcastInstance = factory.newHazelcastInstance(c);

    IMap<String, Long> map = hazelcastInstance.getMap("testIssue304EvictionDespitePutMap");
    final AtomicInteger evictCount = new AtomicInteger(0);
    map.addEntryListener(
        new EntryListener<String, Long>() {
          public void entryAdded(EntryEvent<String, Long> event) {}

          public void entryRemoved(EntryEvent<String, Long> event) {}

          public void entryUpdated(EntryEvent<String, Long> event) {}

          public void entryEvicted(EntryEvent<String, Long> event) {
            evictCount.incrementAndGet();
          }
        },
        true);

    String key = "key";
    for (int i = 0; i < 5; i++) {
      map.put(key, System.currentTimeMillis());
      Thread.sleep(1000);
    }
    assertEquals(evictCount.get(), 0);
    assertNotNull(map.get(key));
    hazelcastInstance.getLifecycleService().shutdown();
  }
示例#9
0
  @org.junit.Test
  public void testGenerateValuesZeroCountIngredientDependency() throws Exception {
    mappingCollector.setValueBefore("a", 2);
    mappingCollector.setValueBefore("b", 3);
    mappingCollector.setValueBefore("notConsume1", 1);
    HashMap<String, Integer> ingredients = new HashMap<>();
    ingredients.put("a", 1);
    ingredients.put("b", 1);
    ingredients.put("notConsume1", 0);
    mappingCollector.addConversion(1, "c1", ingredients);
    ingredients.remove("notConsume1");
    ingredients.put("notConsume2", 0);
    mappingCollector.addConversion(1, "c2", ingredients);

    Map<String, Integer> values = valueGenerator.generateValues();
    assertEquals(2, getValue(values, "a"));
    assertEquals(3, getValue(values, "b"));
    assertEquals(1, getValue(values, "notConsume1"));
    assertEquals(0, getValue(values, "notConsume2"));
    assertEquals(5, getValue(values, "c1"));
    assertEquals(0, getValue(values, "c2"));
  }
示例#10
0
 @Before
 public void setUp() {
   this.r = new HashMap();
   r.put(Resource.GOLD, 100);
 }