示例#1
0
 @Test
 public void testShouldNotGrowLargerThanMaxSize() throws Exception {
   final long maxCacheSize = 1000;
   final T cache = getInstance(Integer.MAX_VALUE, (int) maxCacheSize);
   final List<ParallelTestRunner.Worker> workers = new ArrayList<>();
   final int numThreads = 50;
   final int numRoundsPerThread = 50000;
   final int minKey = 10000;
   final int maxKey = 99999;
   for (int i = 0; i < numThreads; ++i) {
     final Random random = new Random(i);
     workers.add(
         new ParallelTestRunner.Worker() {
           @Override
           public void run() throws Exception {
             for (int i = 0; i < numRoundsPerThread; ++i) {
               final int key = random.nextInt(maxKey - minKey) + minKey;
               cache.put(key, Integer.toBinaryString(key));
             }
           }
         });
   }
   final ParallelTestRunner parallelTestRunner = new ParallelTestRunner(workers);
   parallelTestRunner.run();
   assertThat(cache.size(), isLessThanOrEqualTo(maxCacheSize));
 }
示例#2
0
 @Nonnull
 protected T getInstance(Duration maxLifeTime, int maxEntries) {
   final T cache = getInstance();
   cache.setMaximumLifetime(maxLifeTime);
   cache.setCapacity((long) maxEntries);
   return cache;
 }
示例#3
0
 private static <T extends Label> void runOnTwoArrays(
     TokenizerFactory<T> tokFactory, String[] inputs, String[][] desired) {
   assertEquals("Test data arrays don't match in length", inputs.length, desired.length);
   for (int sent = 0; sent < inputs.length; sent++) {
     Tokenizer<T> tok = tokFactory.getTokenizer(new StringReader(inputs[sent]));
     for (int i = 0; tok.hasNext() || i < desired[sent].length; i++) {
       if (!tok.hasNext()) {
         fail(
             "PTBTokenizer generated too few tokens for sentence "
                 + sent
                 + "! Missing "
                 + desired[sent][i]);
       }
       T w = tok.next();
       if (i >= desired[sent].length) {
         fail(
             "PTBTokenizer generated too many tokens for sentence "
                 + sent
                 + "! Added "
                 + w.value());
       } else {
         assertEquals("PTBTokenizer got wrong token", desired[sent][i], w.value());
       }
     }
   }
 }
  @Test
  public void testCreateAndReadOne() {
    String name = "crud controller read one";
    assertFalse(findByName(name).isPresent());
    T entity = createEntity(name);
    getService().create(entity);

    assertNotNull(entity.getId());
    assertTrue(findByName(name).isPresent());

    JsonNode result =
        performAuthenticatedRequestAndReturnJsonNode(
            getAuthentication(), get(getEndPoint() + "/" + entity.getId()));
    assertEquals(entity.getId().toString(), result.findPath("id").asText());
    assertEquals(name, result.findPath("name").asText());
  }
示例#5
0
 @SuppressWarnings("unchecked")
 private static <T extends Writable> T restore(T writable) {
   try {
     return read((T) writable.getClass().newInstance(), write(writable));
   } catch (Exception e) {
     throw new AssertionError(e);
   }
 }
 /**
  * Puts at the specified URI the specified new state of the resource.
  *
  * @param <T> the type of the resource's state.
  * @param uri the URI at which the resource is.
  * @param newResourceState the new state of the resource.
  * @return
  */
 public <T> T putAt(String uri, T newResourceState) {
   Class<T> c = (Class<T>) newResourceState.getClass();
   return resource()
       .path(uri)
       .header(HTTP_SESSIONKEY, getSessionKey())
       .accept(MediaType.APPLICATION_JSON)
       .type(MediaType.APPLICATION_JSON)
       .put(c, newResourceState);
 }
  @Test
  public void testdelete() {
    String name = "crud controller delete";
    assertFalse(findByName(name).isPresent());

    T entity = createEntity(name);
    getService().create(entity);

    assertNotNull(entity.getId());
    assertTrue(getService().getById(entity.getId()).isPresent());

    getService().create(entity);
    MvcResult result =
        performAuthenticatedRequestAndReturn(
            getAuthentication(), delete(getEndPoint() + "/" + entity.getId()));

    assertFalse(getService().getById(entity.getId()).isPresent());
  }
  @Test
  public void testupdate() throws IOException {
    String name = "crud controller update";
    assertFalse(findByName(name).isPresent());
    T entity = createEntity(name);
    getService().create(entity);

    assertNotNull(entity.getId());
    assertTrue(getService().getById(entity.getId()).isPresent());

    name = name + "d";
    updateEntity(entity, name);
    MvcResult result =
        performAuthenticatedRequestAndReturn(
            getAuthentication(),
            put(getEndPoint() + "/" + entity.getId())
                .contentType(MediaType.APPLICATION_JSON)
                .content(JsonUtils.JSON_MAPPER.writeValueAsString(createEntity(name))));
    assertTrue(findByName(name).isPresent());
  }
示例#9
0
 static <T extends Writable> T read(T writable, byte[] bytes) {
   DataInputBuffer buffer = new DataInputBuffer();
   buffer.reset(bytes, bytes.length);
   try {
     writable.readFields(buffer);
     assertThat("Enf of Stream", buffer.read(), is(-1));
   } catch (IOException e) {
     throw new AssertionError(e);
   }
   return writable;
 }
  @Test
  public void testreadAll() {
    int before = getService().getAll(0, 10).getNumberOfElements();
    assertTrue(before < 10);
    String name = "crud controller read all 1";
    assertFalse(findByName(name).isPresent());
    T entity = createEntity(name);
    getService().create(entity);

    assertNotNull(entity.getId());
    assertTrue(findByName(name).isPresent());

    JsonNode result =
        performAuthenticatedRequestAndReturnJsonNode(
            getAuthentication(), get(getEndPoint()).param("page", "0").param("page_size", "10"));

    result = result.findPath("data");
    assertTrue(result.isArray());
    assertEquals(before + 1, result.size());
    assertTrue(result.findValuesAsText("id").contains(entity.getId().toString()));
    assertTrue(result.findValuesAsText("name").contains(name));
  }
示例#11
0
 @Test
 public void testMaxLifeTimeSupport() throws Exception {
   final T cache = getInstance(100, 100);
   cache.put("varA", "valueA");
   cache.put("varB", "valueB");
   Thread.sleep(70);
   cache.put("varC", "valueC");
   assertEquals("valueA", cache.get("varA"));
   assertEquals("valueB", cache.get("varB"));
   assertEquals("valueC", cache.get("varC"));
   Thread.sleep(70);
   assertNull("valueA", cache.get("varA"));
   assertNull("valueB", cache.get("varB"));
   assertEquals("valueC", cache.get("varC"));
   Thread.sleep(70);
   assertNull("valueA", cache.get("varA"));
   assertNull("valueB", cache.get("varB"));
   assertNull("valueC", cache.get("varC"));
 }
示例#12
0
 @Test
 public void testWithMaxSize1() {
   final T cache = getInstance(1000, 1);
   final Object o1 = new Object();
   cache.put(o1, o1);
   final Object o2 = new Object();
   cache.put(o2, o2);
   assertEquals(1, (long) cache.size());
   assertFalse(cache.contains(o1));
   assertTrue(cache.contains(o2));
 }
示例#13
0
 private <T extends Cell> void indexProperties(T cell, ImgIndex<T> index) {
   for (String key : cell.getAttributeKeys()) index.put(key, cell.getAttribute(key), cell);
 }
 private <T> String tableName(T entity) {
   final Table table = entity.getClass().getAnnotation(Table.class);
   return table == null ? entity.getClass().getSimpleName() : table.value();
 }
 @SuppressWarnings("unchecked")
 protected <T extends Entity> T findEntity(T entity) {
   Select selection = select().all().from(tableName(entity));
   selection.where(eq("id", entity.getId()));
   return (T) template.selectOne(selection, entity.getClass());
 }
 public <T> T serialize(T o) {
   String jsonString = jsonStreamMapper.write(o);
   System.out.println(jsonString);
   return (T) jsonStreamMapper.readString(jsonString, o.getClass());
 }
示例#17
0
 @Test
 public void testWithNullAsValue() throws Exception {
   final T cache = getInstance(1000, 1000);
   final Object o = new Object();
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(o));
   assertNull(cache.get(o));
   // Add entry with null as value ...
   cache.put(o, null);
   assertEquals(1, (long) cache.size());
   assertTrue(cache.contains(o));
   assertNull(cache.get(o));
   // Remove entry with null as value ...
   assertNull(cache.remove(o).getValue());
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(o));
   assertNull(cache.get(o));
   // Try to remove entry with null as value again ...
   assertNull(cache.remove(o));
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(o));
   assertNull(cache.get(o));
 }
示例#18
0
 @Test
 public void testWithNullAsKeyAndValue() throws Exception {
   final T cache = getInstance(1000, 1000);
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(null));
   assertNull(cache.get(null));
   // Add entry with null as key and value ...
   cache.put(null, null);
   assertEquals(1, (long) cache.size());
   assertTrue(cache.contains(null));
   assertNull(cache.get(null));
   // Add another entry ...
   cache.put(new Object(), new Object());
   assertEquals(2, (long) cache.size());
   // Remove entry with null as key ...
   assertNull(cache.remove(null).getValue());
   assertEquals(1, (long) cache.size());
   assertFalse(cache.contains(null));
   assertNull(cache.get(null));
   // Try to remove entry with null as key again ...
   assertNull(cache.remove(null));
   assertEquals(1, (long) cache.size());
   assertFalse(cache.contains(null));
   assertNull(cache.get(null));
 }
 public int compare(T left, T right) {
   return left.compareTo(right);
 }