public void testDefensiveCopyOfChildren() {

    Node<Object, Object> rootNode = cache.getRoot();

    Fqn childFqn = Fqn.fromString("/child");
    rootNode.addChild(childFqn).put("k", "v");
    Set<Node<Object, Object>> children = rootNode.getChildren();
    Set<Object> childrenNames = rootNode.getChildrenNames();

    assert childrenNames.size() == 1;
    assert childrenNames.contains(childFqn.getLastElement());

    assert children.size() == 1;
    assert children.iterator().next().getFqn().equals(childFqn);

    // now change stuff.

    rootNode.addChild(Fqn.fromString("/child2"));

    // assert that the collections we initially got have not changed.
    assert childrenNames.size() == 1;
    assert childrenNames.contains(childFqn.getLastElement());

    assert children.size() == 1;
    assert children.iterator().next().getFqn().equals(childFqn);
  }
  public void testDefensiveCopyOfData() {

    Node<Object, Object> rootNode = cache.getRoot();

    rootNode.put("key", "value");
    Map<Object, Object> data = rootNode.getData();
    Set<Object> keys = rootNode.getKeys();

    assert keys.size() == 1;
    assert keys.contains("key");

    assert data.size() == 1;
    assert data.containsKey("key");

    // now change stuff.

    rootNode.put("key2", "value2");

    // assert that the collections we initially got have not changed.
    assert keys.size() == 1;
    assert keys.contains("key");

    assert data.size() == 1;
    assert data.containsKey("key");
  }
  @Override
  public void removeProductImage(ProductImage productImage) throws ServiceException {

    if (cacheManager.getTreeCache() == null) {
      throw new ServiceException(
          "CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }

    try {

      StringBuilder nodePath = new StringBuilder();
      nodePath
          .append(productImage.getProduct().getMerchantStore().getCode())
          .append(Constants.SLASH)
          .append(productImage.getProduct().getSku());

      Node<String, Object> productNode = this.getNode(nodePath.toString());
      productNode.remove(productImage.getProductImage());

    } catch (Exception e) {
      throw new ServiceException(e);
    } finally {

    }
  }
  /** Test method behaves according to javadoc. */
  public void testGetParent() {
    Node<Object, Object> rootNode = cache.getRoot();
    assertEquals(rootNode, rootNode.getParent());

    Node<Object, Object> nodeA = rootNode.addChild(A);
    assertEquals(rootNode, nodeA.getParent());
  }
  public void testAddingData() {
    Node<Object, Object> rootNode = cache.getRoot();
    Node<Object, Object> nodeA = rootNode.addChild(A);
    nodeA.put("key", "value");

    assertEquals("value", nodeA.get("key"));
  }
  public void testAddingDataTx() throws Exception {
    Node<Object, Object> rootNode = cache.getRoot();
    tm.begin();
    Node<Object, Object> nodeA = rootNode.addChild(A);
    nodeA.put("key", "value");

    assertEquals("value", nodeA.get("key"));
    tm.commit();
  }
  @Override
  public List<OutputContentFile> getImages(
      final String merchantStoreCode, FileContentType imageContentType) throws ServiceException {
    if (cacheManager.getTreeCache() == null) {
      throw new ServiceException(
          "CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    List<OutputContentFile> images = new ArrayList<OutputContentFile>();
    FileNameMap fileNameMap = URLConnection.getFileNameMap();

    try {

      StringBuilder nodePath = new StringBuilder();
      nodePath.append(merchantStoreCode);

      Node<String, Object> merchantNode = this.getNode(nodePath.toString());

      Set<Node<String, Object>> childs = merchantNode.getChildren();

      Iterator<Node<String, Object>> iterator = childs.iterator();
      // TODO image sizes
      while (iterator.hasNext()) {

        Node<String, Object> node = iterator.next();

        for (String key : node.getKeys()) {

          byte[] imageBytes = (byte[]) merchantNode.get(key);

          OutputContentFile contentImage = new OutputContentFile();

          InputStream input = new ByteArrayInputStream(imageBytes);
          ByteArrayOutputStream output = new ByteArrayOutputStream();
          IOUtils.copy(input, output);

          String contentType = fileNameMap.getContentTypeFor(key);

          contentImage.setFile(output);
          contentImage.setMimeType(contentType);
          contentImage.setFileName(key);

          images.add(contentImage);
        }
      }

    } catch (Exception e) {
      throw new ServiceException(e);
    } finally {

    }

    return images;
  }
 @Test
 public void testCacheManager() {
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.transaction().invocationBatching().enable();
   EmbeddedCacheManager cm = new DefaultCacheManager(builder.build());
   Cache<String, String> cache = cm.getCache();
   TreeCacheFactory tcf = new TreeCacheFactory();
   TreeCache<String, String> tree = tcf.createTreeCache(cache);
   Fqn leafFqn = Fqn.fromElements("branch", "leaf");
   Node<String, String> leaf = tree.getRoot().addChild(leafFqn);
   leaf.put("fruit", "orange");
   cm.stop();
 }
  public void testImmutabilityOfChildren() {

    Node<Object, Object> rootNode = cache.getRoot();

    rootNode.addChild(A);

    try {
      rootNode.getChildren().clear();
      fail("Collection of child nodes returned in getChildren() should be immutable");
    } catch (Exception e) {
      // expected
    }
  }
  private OutputContentFile getProductImage(
      String merchantStoreCode, String productCode, String imageName, String size)
      throws ServiceException {

    if (cacheManager.getTreeCache() == null) {
      throw new ServiceException(
          "CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }
    InputStream input = null;
    OutputContentFile contentImage = new OutputContentFile();
    try {

      FileNameMap fileNameMap = URLConnection.getFileNameMap();

      // SMALL by default
      StringBuilder nodePath = new StringBuilder();
      nodePath
          .append(merchantStoreCode)
          .append(Constants.SLASH)
          .append(productCode)
          .append(Constants.SLASH)
          .append(size);

      Node<String, Object> productNode = this.getNode(nodePath.toString());

      byte[] imageBytes = (byte[]) productNode.get(imageName);

      input = new ByteArrayInputStream(imageBytes);
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      IOUtils.copy(input, output);

      String contentType = fileNameMap.getContentTypeFor(imageName);

      contentImage.setFile(output);
      contentImage.setMimeType(contentType);
      contentImage.setFileName(imageName);

    } catch (Exception e) {
      throw new ServiceException(e);
    } finally {
      if (input != null) {
        try {
          input.close();
        } catch (Exception ignore) {
        }
      }
    }

    return contentImage;
  }
  @Override
  public List<OutputContentFile> getImages(Product product) throws ServiceException {

    if (cacheManager.getTreeCache() == null) {
      throw new ServiceException(
          "CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }

    List<OutputContentFile> images = new ArrayList<OutputContentFile>();

    try {

      FileNameMap fileNameMap = URLConnection.getFileNameMap();
      StringBuilder nodePath = new StringBuilder();
      nodePath.append(product.getMerchantStore().getCode());

      Node<String, Object> merchantNode = this.getNode(nodePath.toString());

      if (merchantNode == null) {
        return null;
      }

      for (String key : merchantNode.getKeys()) {

        byte[] imageBytes = (byte[]) merchantNode.get(key);

        OutputContentFile contentImage = new OutputContentFile();

        InputStream input = new ByteArrayInputStream(imageBytes);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        IOUtils.copy(input, output);

        String contentType = fileNameMap.getContentTypeFor(key);

        contentImage.setFile(output);
        contentImage.setMimeType(contentType);
        contentImage.setFileName(key);

        images.add(contentImage);
      }

    } catch (Exception e) {
      throw new ServiceException(e);
    } finally {

    }

    return images;
  }
  /**
   * root -productFiles -merchant-id PRODUCT-ID(key) -> CacheAttribute(value) - image 1 - image 2 -
   * image 3
   */
  @Override
  public void addProductImage(ProductImage productImage, ImageContentFile contentImage)
      throws ServiceException {

    if (cacheManager.getTreeCache() == null) {
      throw new ServiceException(
          "CmsImageFileManagerInfinispan has a null cacheManager.getTreeCache()");
    }

    try {

      // node
      StringBuilder nodePath = new StringBuilder();
      nodePath
          .append(productImage.getProduct().getMerchantStore().getCode())
          .append(Constants.SLASH)
          .append(productImage.getProduct().getSku())
          .append(Constants.SLASH);

      if (contentImage.getFileContentType().name().equals(FileContentType.PRODUCT.name())) {
        nodePath.append(SMALL);
      } else if (contentImage
          .getFileContentType()
          .name()
          .equals(FileContentType.PRODUCTLG.name())) {
        nodePath.append(LARGE);
      }

      Node<String, Object> productNode = this.getNode(nodePath.toString());

      InputStream isFile = contentImage.getFile();

      ByteArrayOutputStream output = new ByteArrayOutputStream();
      IOUtils.copy(isFile, output);

      // object for a given product containing all images
      productNode.put(contentImage.getFileName(), output.toByteArray());

    } catch (Exception e) {

      throw new ServiceException(e);
    }
  }
  public void testOverwritingDataTx() throws Exception {
    Node<Object, Object> rootNode = cache.getRoot();

    Node<Object, Object> nodeA = rootNode.addChild(A);
    nodeA.put("key", "value");
    assertEquals("value", nodeA.get("key"));
    tm.begin();
    rootNode.removeChild(A);
    cache.put(A, "k2", "v2");
    tm.commit();
    assertNull(nodeA.get("key"));
    assertEquals("v2", nodeA.get("k2"));
  }
  public void testImmutabilityOfData() {

    Node<Object, Object> rootNode = cache.getRoot();

    rootNode.put("key", "value");
    Map<Object, Object> m = rootNode.getData();
    try {
      m.put("x", "y");
      fail("Map should be immutable!!");
    } catch (Exception e) {
      // expected
    }

    try {
      rootNode.getKeys().add(new Object());
      fail("Key set should be immutable");
    } catch (Exception e) {
      // expected
    }
  }
  public void testClearingDataTx() throws Exception {

    Node<Object, Object> rootNode = cache.getRoot();

    tm.begin();
    rootNode.put("k", "v");
    rootNode.put("k2", "v2");
    assertEquals(2, rootNode.getKeys().size());
    rootNode.clearData();
    assertEquals(0, rootNode.getKeys().size());
    assertTrue(rootNode.getData().isEmpty());
    tm.commit();
    assertTrue(rootNode.getData().isEmpty());
  }
  public void testGetChildAPI() {

    Node<Object, Object> rootNode = cache.getRoot();

    // creates a Node<Object, Object> with fqn /a/b/c
    Node childA = rootNode.addChild(A);
    childA.addChild(B).addChild(C);

    rootNode.getChild(A).put("key", "value");
    rootNode.getChild(A).getChild(B).put("key", "value");
    rootNode.getChild(A).getChild(B).getChild(C).put("key", "value");

    assertEquals("value", rootNode.getChild(A).get("key"));
    assertEquals("value", rootNode.getChild(A).getChild(B).get("key"));
    assertEquals("value", rootNode.getChild(A).getChild(B).getChild(C).get("key"));

    assertNull(rootNode.getChild(Fqn.fromElements("nonexistent")));
  }
  public void testClearingData() {

    Node<Object, Object> rootNode = cache.getRoot();

    rootNode.put("k", "v");
    rootNode.put("k2", "v2");
    assertEquals(2, rootNode.getKeys().size());
    rootNode.clearData();
    assertEquals(0, rootNode.getKeys().size());
    assertTrue(rootNode.getData().isEmpty());
  }
Exemple #18
0
  public void testBasicOperation()
      throws SystemException, NotSupportedException, HeuristicMixedException,
          HeuristicRollbackException, RollbackException {
    assertClusterSize("Should only be 2  caches in the cluster!!!", 2);

    Fqn f = Fqn.fromString("/test/data");
    String k = "key", v = "value";

    assertNull("Should be null", cache1.getRoot().getChild(f));
    assertNull("Should be null", cache2.getRoot().getChild(f));

    Node<Object, Object> node = cache1.getRoot().addChild(f);

    assertNotNull("Should not be null", node);

    TransactionManager tm = beginTransaction(cache1.getCache());
    node.put(k, v);
    tm.commit();

    assertEquals(v, node.get(k));
    assertEquals(v, cache1.get(f, k));
    assertEquals("Should have replicated", v, cache2.get(f, k));
  }
  public void testGetChildrenNames() throws Exception {

    Node<Object, Object> rootNode = cache.getRoot();

    rootNode.addChild(A).put("k", "v");
    rootNode.addChild(B).put("k", "v");

    Set<Object> childrenNames = new HashSet<Object>();
    childrenNames.add(A.getLastElement());
    childrenNames.add(B.getLastElement());

    assertEquals(childrenNames, rootNode.getChildrenNames());

    // now delete a child, within a tx
    tm.begin();
    rootNode.removeChild(B);
    assertFalse(rootNode.hasChild(B));
    childrenNames.remove(B.getLastElement());
    assertEquals(childrenNames, rootNode.getChildrenNames());
    tm.commit();
    assertEquals(childrenNames, rootNode.getChildrenNames());
  }
  /** Remember, Fqns are relative!! */
  public void testParentsAndChildren() {

    Node<Object, Object> rootNode = cache.getRoot();

    Node<Object, Object> nodeA = rootNode.addChild(A);
    Node<Object, Object> nodeB = nodeA.addChild(B);
    Node<Object, Object> nodeC = nodeA.addChild(C);
    Node<Object, Object> nodeD = rootNode.addChild(D);

    assertEquals(rootNode, nodeA.getParent());
    assertEquals(nodeA, nodeB.getParent());
    assertEquals(nodeA, nodeC.getParent());
    assertEquals(rootNode, nodeD.getParent());

    assertTrue(rootNode.hasChild(A));
    assertFalse(rootNode.hasChild(B));
    assertFalse(rootNode.hasChild(C));
    assertTrue(rootNode.hasChild(D));

    assertTrue(nodeA.hasChild(B));
    assertTrue(nodeA.hasChild(C));

    assertEquals(nodeA, rootNode.getChild(A));
    assertEquals(nodeD, rootNode.getChild(D));
    assertEquals(nodeB, nodeA.getChild(B));
    assertEquals(nodeC, nodeA.getChild(C));

    assertTrue(nodeA.getChildren().contains(nodeB));
    assertTrue(nodeA.getChildren().contains(nodeC));
    assertEquals(2, nodeA.getChildren().size());

    assertTrue(rootNode.getChildren().contains(nodeA));
    assertTrue(rootNode.getChildren().contains(nodeD));
    assertEquals(2, rootNode.getChildren().size());

    assertEquals(true, rootNode.removeChild(A));
    assertFalse(rootNode.getChildren().contains(nodeA));
    assertTrue(rootNode.getChildren().contains(nodeD));
    assertEquals(1, rootNode.getChildren().size());

    assertEquals("double remove", false, rootNode.removeChild(A));
    assertEquals("double remove", false, rootNode.removeChild(A.getLastElement()));
  }
  public void testPutData() {

    Node<Object, Object> rootNode = cache.getRoot();

    assertTrue(rootNode.getData().isEmpty());

    Map<Object, Object> map = new HashMap<Object, Object>();
    map.put("k1", "v1");
    map.put("k2", "v2");

    rootNode.putAll(map);

    assertEquals(2, rootNode.getData().size());
    assertEquals("v1", rootNode.get("k1"));
    assertEquals("v2", rootNode.get("k2"));

    map.clear();
    map.put("k3", "v3");

    rootNode.putAll(map);
    assertEquals(3, rootNode.getData().size());
    assertEquals("v1", rootNode.get("k1"));
    assertEquals("v2", rootNode.get("k2"));
    assertEquals("v3", rootNode.get("k3"));

    map.clear();
    map.put("k4", "v4");
    map.put("k5", "v5");

    rootNode.replaceAll(map);
    assertEquals(2, rootNode.getData().size());
    assertEquals("v4", rootNode.get("k4"));
    assertEquals("v5", rootNode.get("k5"));
  }