public void testDifferentEncoding() throws Exception {
    Map map = Charset.availableCharsets();
    if (map.containsKey("UTF-16BE")) {
      FileWriter fw1 = null;
      FileWriterWithEncoding fw2 = null;
      try {
        fw1 = new FileWriter(file1); // default encoding
        fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
        assertEquals(true, file1.exists());
        assertEquals(true, file2.exists());

        fw1.write(textContent);
        fw2.write(textContent);

        fw1.flush();
        fw2.flush();
        try {
          checkFile(file1, file2);
          fail();
        } catch (AssertionFailedError ex) {
          // success
        }

      } finally {
        IOUtils.closeQuietly(fw1);
        IOUtils.closeQuietly(fw2);
      }
      assertEquals(true, file1.exists());
      assertEquals(true, file2.exists());
    }
    if (map.containsKey("UTF-16LE")) {
      FileWriter fw1 = null;
      FileWriterWithEncoding fw2 = null;
      try {
        fw1 = new FileWriter(file1); // default encoding
        fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
        assertEquals(true, file1.exists());
        assertEquals(true, file2.exists());

        fw1.write(textContent);
        fw2.write(textContent);

        fw1.flush();
        fw2.flush();
        try {
          checkFile(file1, file2);
          fail();
        } catch (AssertionFailedError ex) {
          // success
        }

      } finally {
        IOUtils.closeQuietly(fw1);
        IOUtils.closeQuietly(fw2);
      }
      assertEquals(true, file1.exists());
      assertEquals(true, file2.exists());
    }
  }
  // -----------------------------------------------------------------------
  public void testSameEncoding() throws Exception {
    FileWriter fw1 = null;
    FileWriterWithEncoding fw2 = null;
    try {
      fw1 = new FileWriter(file1); // default encoding
      fw2 = new FileWriterWithEncoding(file2, defaultEncoding);
      assertEquals(true, file1.exists());
      assertEquals(true, file2.exists());

      fw1.write(textContent);
      fw2.write(textContent);

      fw1.flush();
      fw2.flush();
      checkFile(file1, file2);

    } finally {
      IOUtils.closeQuietly(fw1);
      IOUtils.closeQuietly(fw2);
    }
    assertEquals(true, file1.exists());
    assertEquals(true, file2.exists());
  }
  private void writeTestPayload(FileWriter fw1, FileWriterWithEncoding fw2) throws IOException {
    assertTrue(file1.exists());
    assertTrue(file2.exists());

    fw1.write(textContent);
    fw2.write(textContent);
    fw1.write(65);
    fw2.write(65);
    fw1.write(anotherTestContent);
    fw2.write(anotherTestContent);
    fw1.write(anotherTestContent, 1, 2);
    fw2.write(anotherTestContent, 1, 2);
    fw1.write("CAFE", 1, 2);
    fw2.write("CAFE", 1, 2);

    fw1.flush();
    fw2.flush();
  }