Example #1
0
 @Test
 public void testTruncatedFileReturnsDefault() throws IOException {
   assertTrue(FILE.createNewFile());
   assertEquals(0, FILE.length());
   BestEffortLongFile f = new BestEffortLongFile(FILE, 12345L);
   try {
     assertEquals(12345L, f.get());
   } finally {
     f.close();
   }
 }
Example #2
0
  @Test
  public void testGetSet() throws IOException {
    BestEffortLongFile f = new BestEffortLongFile(FILE, 12345L);
    try {
      // Before the file exists, should return default.
      assertEquals(12345L, f.get());

      // And first access should open it.
      assertTrue(FILE.exists());

      Random r = new Random();
      for (int i = 0; i < 100; i++) {
        long newVal = r.nextLong();
        // Changing the value should be reflected in the next get() call.
        f.set(newVal);
        assertEquals(newVal, f.get());

        // And should be reflected in a new instance (ie it actually got
        // written to the file)
        BestEffortLongFile f2 = new BestEffortLongFile(FILE, 999L);
        try {
          assertEquals(newVal, f2.get());
        } finally {
          IOUtils.closeStream(f2);
        }
      }
    } finally {
      IOUtils.closeStream(f);
    }
  }