All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.cache.DiskInitFileJUnitTest Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.internal.cache;

import java.io.File;

import junit.framework.TestCase;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;

import java.util.Collections;
import com.gemstone.gemfire.StatisticsFactory;
import com.gemstone.gemfire.internal.FileUtil;
import com.gemstone.gemfire.internal.cache.persistence.DiskRegionView;
import com.gemstone.gemfire.internal.cache.persistence.DiskStoreID;

public class DiskInitFileJUnitTest extends TestCase {
  
  private File testDirectory;
  private Mockery context = new Mockery() {{
    setImposteriser(ClassImposteriser.INSTANCE);
  }};
  
  public void setUp() throws Exception {
    testDirectory = new File("_DiskInitFileJUnitTest");
    FileUtil.delete(testDirectory);
    FileUtil.mkdirs(testDirectory);
  }
  
  public void tearDown() throws Exception {
    FileUtil.delete(testDirectory);
  }
  
  /**
   * Test the behavior of canonical ids in the init file.
   */
  public void testCanonicalIds() {
    //create a mock statistics factory for creating directory holders
    final StatisticsFactory sf = context.mock(StatisticsFactory.class);
    context.checking(new Expectations() {{
      ignoring(sf);
    }});
    
    final DiskStoreID diskStoreID = DiskStoreID.random();

    //Create a mock disk store impl. All we need to do is return
    //this init file directory.
    final DiskStoreImpl parent = context.mock(DiskStoreImpl.class);
    context.checking(new Expectations() {{
      allowing(parent).getInfoFileDir();
      will(returnValue(new DirectoryHolder(sf, testDirectory, 0, 0)));
      allowing(parent).getDiskStoreID();
      will(returnValue(diskStoreID));
      ignoring(parent);
    }});
    
    //Create an init file and add some canonical ids
    DiskInitFile dif = new DiskInitFile("testFile", parent, false, Collections.emptySet());
    assertEquals(null, dif.getCanonicalObject(5));
    assertNull(dif.getCanonicalObject(0));
    int id1 = dif.getOrCreateCanonicalId("object1");
    int id2 = dif.getOrCreateCanonicalId("object2");
    
    assertEquals("object1", dif.getCanonicalObject(id1));
    assertEquals("object2", dif.getCanonicalObject(id2));
    assertEquals(id2, dif.getOrCreateCanonicalId("object2"));
    
    
    //Add a mock region to the init file so it doesn't
    //delete the file when the init file is closed
    final DiskRegionView drv = context.mock(DiskRegionView.class);
    context.checking(new Expectations() {{
      ignoring(drv);
    }});
    dif.createRegion(drv);
    
    //close the init file
    dif.close();
    
    //recover the init file from disk
    dif = new DiskInitFile("testFile", parent, true, Collections.emptySet());
    
    //make sure we can recover the ids from disk
    assertEquals("object1", dif.getCanonicalObject(id1));
    assertEquals("object2", dif.getCanonicalObject(id2));
    assertEquals(id2, dif.getOrCreateCanonicalId("object2"));
    
    //Make sure we can add new ids
    int id3 = dif.getOrCreateCanonicalId("object3");
    assertTrue(id3 > id2);
    assertEquals("object1", dif.getCanonicalObject(id1));
    assertEquals("object2", dif.getCanonicalObject(id2));
    assertEquals("object3", dif.getCanonicalObject(id3));
    
    dif.close();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy