@Test
  public void badConstructionTest() throws IOException, StoreException {
    // try to construct with a file that does not exist.
    LogSegmentName name = LogSegmentName.generateFirstSegmentName(false);
    File file = new File(tempDir, name.toFilename());
    try {
      new LogSegment(name, file, STANDARD_SEGMENT_SIZE, config, metrics, true);
      fail("Construction should have failed because the backing file does not exist");
    } catch (StoreException e) {
      // expected. Nothing to do.
    }

    try {
      new LogSegment(name, file, config, metrics);
      fail("Construction should have failed because the backing file does not exist");
    } catch (StoreException e) {
      // expected. Nothing to do.
    }

    // try to construct with a file that is a directory
    file = tempDir;
    try {
      new LogSegment(name, file, STANDARD_SEGMENT_SIZE, config, metrics, true);
      fail("Construction should have failed because the backing file does not exist");
    } catch (StoreException e) {
      // expected. Nothing to do.
    }

    try {
      new LogSegment(name, file, config, metrics);
      fail("Construction should have failed because the backing file does not exist");
    } catch (StoreException e) {
      // expected. Nothing to do.
    }

    // unknown version
    LogSegment segment = getSegment(name, STANDARD_SEGMENT_SIZE, true);
    file = segment.getView().getFirst();
    byte[] header = getHeader(segment);
    byte savedByte = header[0];
    // mess with version
    header[0] = (byte) (header[0] + 10);
    writeHeader(segment, header);
    try {
      new LogSegment(name, file, config, metrics);
      fail("Construction should have failed because version is unknown");
    } catch (StoreException e) {
      // expected. Nothing to do.
      Assert.assertEquals(StoreErrorCodes.LogFileFormatError, e.getErrorCode());
    }

    // bad CRC
    // fix version but mess with data after version
    header[0] = savedByte;
    header[2] = header[2] == (byte) 1 ? (byte) 0 : (byte) 1;
    writeHeader(segment, header);
    try {
      new LogSegment(name, file, config, metrics);
      fail("Construction should have failed because crc check should have failed");
    } catch (StoreException e) {
      // expected. Nothing to do.
      Assert.assertEquals(StoreErrorCodes.LogFileFormatError, e.getErrorCode());
    }
    closeSegmentAndDeleteFile(segment);
  }
Tests for bad construction cases of {@link LogSegment}.
@throws IOException
ambry-store/src/test/java/com/github/ambry/store/LogSegmentTest.java