/**
   * @throws java.io.IOException
   * @throws IOException
   */
  @Test
  public void cachedVariableFragment() throws IOException {
    logging.setLogLevel("log4j.category.net.sf.ehcache", "DEBUG");

    Fragments.setDefaultFragmentCacheType(CacheType.EHCACHE);
    FileFragment ff = new FileFragment(tf.newFolder("cachedVariableFragmentTest"), "testfrag.cdf");
    Configuration cacheManagerConfig = new Configuration();
    CacheManager manager = CacheManager.newInstance(cacheManagerConfig);
    CacheConfiguration config =
        new CacheConfiguration(ff.getName() + "-variable-fragment-cache", 100);
    Ehcache cache = new Cache(config);
    manager.addCache(cache);
    for (int j = 0; j < 100; j++) {
      VariableFragment vf1 = new VariableFragment(ff, "a" + j);
      vf1.setArray(new ArrayDouble.D2(10, 39));
      VariableFragment vfIndex = new VariableFragment(ff, "index" + j);
      vfIndex.setArray(new ArrayInt.D1(20));
      VariableFragment vf2 = new VariableFragment(ff, "b" + j, vfIndex);
      List<Array> l = new ArrayList<>();
      Array indexArray = vfIndex.getArray();
      int offset = 0;
      for (int i = 0; i < 20; i++) {
        l.add(new ArrayDouble.D1(10));
        indexArray.setInt(i, offset);
        offset += 10;
      }
      vf2.setIndexedArray(l);
      Assert.assertNotNull(vf1.getArray());
      Assert.assertNotNull(vf2.getIndexedArray());
      Assert.assertEquals(20, vf2.getIndexedArray().size());
      Assert.assertNotNull(vfIndex.getArray());
      log.info("In memory: {}; On disk: {}", cache.getSize(), cache.getDiskStoreSize());
    }
    for (IVariableFragment var : ff) {
      Assert.assertNotNull(var.getArray());
      log.info(var.getName() + ": " + var.getArray());
    }
    logging.setLogLevel("log4j.category.net.sf.ehcache", "INFO");
  }
  /** @throws IOException */
  @Test
  public void customCachedVariableFragment() throws IOException {
    logging.setLogLevel("log4j.category.net.sf.ehcache", "DEBUG");
    Fragments.setDefaultFragmentCacheType(CacheType.EHCACHE);
    FileFragment ff = new FileFragment(tf.newFolder("cachedVariableFragmentTest"), "testfrag.cdf");
    File cacheLocation = createCacheDir();
    Configuration cacheManagerConfig =
        new Configuration()
            .diskStore(new DiskStoreConfiguration().path(cacheLocation.getAbsolutePath()));
    CacheManager manager = CacheManager.newInstance(cacheManagerConfig);
    CacheConfiguration config =
        new CacheConfiguration(ff.getName() + "-variable-fragment-cache-custom", 100);
    config.persistence(
        new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP));
    config.setMaxElementsInMemory(10);
    config.setMaxElementsOnDisk(1000);
    config.setDiskSpoolBufferSizeMB(10);
    Ehcache cache = new Cache(config);
    manager.addCache(cache);

    log.info(
        "Storing cache on disk at {}", cacheManagerConfig.getDiskStoreConfiguration().getPath());
    log.info("Using disk store size of {}", cache.getDiskStoreSize());
    log.info("Overflowing to disk: {}", config.isOverflowToDisk());
    ff.setCache(new VariableFragmentArrayCache(cache));
    for (int j = 0; j < 100; j++) {
      VariableFragment vf1 = new VariableFragment(ff, "a" + j);
      vf1.setArray(new ArrayDouble.D2(10, 39));
      VariableFragment vfIndex = new VariableFragment(ff, "index" + j);
      vfIndex.setArray(new ArrayInt.D1(20));
      VariableFragment vf2 = new VariableFragment(ff, "b" + j, vfIndex);
      List<Array> l = new ArrayList<>();
      Array indexArray = vfIndex.getArray();
      int offset = 0;
      for (int i = 0; i < 20; i++) {
        l.add(new ArrayDouble.D1(10));
        indexArray.setInt(i, offset);
        offset += 10;
      }
      vf2.setIndexedArray(l);
      Assert.assertNotNull(vf1.getArray());
      Assert.assertNotNull(vf2.getIndexedArray());
      Assert.assertEquals(20, vf2.getIndexedArray().size());
      Assert.assertNotNull(vfIndex.getArray());
      log.info("In memory: {}; On disk: {}", cache.getSize(), cache.getDiskStoreSize());
    }
    for (IVariableFragment var : ff) {
      Assert.assertNotNull(var.getArray());
      log.info(var.getName() + ": " + var.getArray());
    }
    logging.setLogLevel("log4j.category.net.sf.ehcache", "INFO");
  }