2019-10-22 07:15:35 +02:00
|
|
|
/*
|
2016-01-27 22:20:42 +00:00
|
|
|
* The MIT License
|
2019-10-12 20:05:54 +03:00
|
|
|
* Copyright © 2014-2019 Ilkka Seppälä
|
2016-01-27 22:20:42 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-10-22 07:15:35 +02:00
|
|
|
|
2015-12-12 20:14:59 +01:00
|
|
|
package com.iluwatar.execute.around;
|
|
|
|
|
2019-12-09 22:33:30 +05:30
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
2015-12-12 20:14:59 +01:00
|
|
|
import org.junit.Rule;
|
2017-12-31 16:29:48 +09:00
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
|
2015-12-12 20:14:59 +01:00
|
|
|
import org.junit.rules.TemporaryFolder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Date: 12/12/15 - 3:21 PM
|
|
|
|
*
|
|
|
|
* @author Jeroen Meulemeester
|
|
|
|
*/
|
2017-12-31 16:29:48 +09:00
|
|
|
@EnableRuleMigrationSupport
|
2015-12-12 20:14:59 +01:00
|
|
|
public class SimpleFileWriterTest {
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public final TemporaryFolder testFolder = new TemporaryFolder();
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testWriterNotNull() throws Exception {
|
2019-12-09 22:33:30 +05:30
|
|
|
final var temporaryFile = this.testFolder.newFile();
|
2017-12-31 16:29:48 +09:00
|
|
|
new SimpleFileWriter(temporaryFile.getPath(), Assertions::assertNotNull);
|
2015-12-12 20:14:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-10-23 13:45:41 +05:30
|
|
|
public void testCreatesNonExistentFile() throws Exception {
|
2019-12-09 22:33:30 +05:30
|
|
|
final var nonExistingFile = new File(this.testFolder.getRoot(), "non-existing-file");
|
2015-12-12 20:14:59 +01:00
|
|
|
assertFalse(nonExistingFile.exists());
|
|
|
|
|
2017-12-31 16:29:48 +09:00
|
|
|
new SimpleFileWriter(nonExistingFile.getPath(), Assertions::assertNotNull);
|
2015-12-12 20:14:59 +01:00
|
|
|
assertTrue(nonExistingFile.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-10-23 13:45:41 +05:30
|
|
|
public void testContentsAreWrittenToFile() throws Exception {
|
2019-12-09 22:33:30 +05:30
|
|
|
final var testMessage = "Test message";
|
2015-12-12 20:14:59 +01:00
|
|
|
|
2019-12-09 22:33:30 +05:30
|
|
|
final var temporaryFile = this.testFolder.newFile();
|
2015-12-12 20:14:59 +01:00
|
|
|
assertTrue(temporaryFile.exists());
|
|
|
|
|
|
|
|
new SimpleFileWriter(temporaryFile.getPath(), writer -> writer.write(testMessage));
|
|
|
|
assertTrue(Files.lines(temporaryFile.toPath()).allMatch(testMessage::equals));
|
|
|
|
}
|
|
|
|
|
2017-12-31 16:29:48 +09:00
|
|
|
@Test
|
2018-10-23 13:45:41 +05:30
|
|
|
public void testRipplesIoExceptionOccurredWhileWriting() {
|
2019-12-09 22:33:30 +05:30
|
|
|
var message = "Some error";
|
2017-12-31 16:29:48 +09:00
|
|
|
assertThrows(IOException.class, () -> {
|
2019-12-09 22:33:30 +05:30
|
|
|
final var temporaryFile = this.testFolder.newFile();
|
2017-12-31 16:29:48 +09:00
|
|
|
new SimpleFileWriter(temporaryFile.getPath(), writer -> {
|
2018-10-23 13:45:41 +05:30
|
|
|
throw new IOException(message);
|
2017-12-31 16:29:48 +09:00
|
|
|
});
|
2018-10-23 13:45:41 +05:30
|
|
|
}, message);
|
2015-12-12 20:14:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|