@Test
    public void testInPlaceModificationErrorHandling() throws IOException {
        File tmpFile = File.createTempFile("credentials.", null);

        Profile[] abcd = {
                new Profile("a", basicCredA),
                new Profile("b", basicCredB),
                new Profile("c", sessionCredC),
                new Profile("d", sessionCredD)
                };
        ProfilesConfigFileWriter.dumpToFile(tmpFile, true, abcd);
        String originalContent = FileUtils.readFileToString(tmpFile);

        // Insert [e] profile, which throws RuntimeException when the getProperties method is called.
        Profile e = new ProfileWithException("e", basicCredA);
        try {
            ProfilesConfigFileWriter.modifyOrInsertProfiles(tmpFile, e);
            fail("An exception is expected.");
        } catch(AmazonClientException expected) {}

        // Check that the original file is restored
        assertTrue(tmpFile.exists());
        String restoredContent = FileUtils.readFileToString(tmpFile);
        assertEquals(originalContent, restoredContent);
    }
Tests that the original credentials file is properly restored if the
in-place modification fails with error.
aws-java-sdk-core/src/test/java/com/amazonaws/auth/profile/ProfilesConfigFileWriterTest.java