-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#939 AppendTo for appending content to files #1021
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
998ca1f
AppendTo with tests (#939)
241a41e
Refactored AppendTo, added unicode file test (#939)
8a9d042
Merge branch 'master' into 939
5e93ac9
Refactored AppendToTest (#939)
43b4f6d
Continue to refactor AppendToTest (#939)
ccc8b4c
Refactored AppendToTest (#939)
3f50e1e
(#939) Refactored AppendTo and AppendToTest
6969dcf
Merge branch 'master' into 939
4cd8d50
(#939) Use Randomized instead of RandomText in AppendToTest
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2018 Yegor Bugayenko | ||
* | ||
* 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 NON-INFRINGEMENT. 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. | ||
*/ | ||
package org.cactoos.io; | ||
|
||
import java.io.File; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import org.cactoos.Output; | ||
|
||
/** | ||
* Output that appends content to a given file. | ||
* | ||
* <p>There is no thread-safety guarantee. | ||
* | ||
* @since 1.0 | ||
*/ | ||
public final class AppendTo implements Output { | ||
|
||
/** | ||
* Source of the output. | ||
*/ | ||
private final File source; | ||
|
||
/** | ||
* Ctor. | ||
* @param path Path as a source of a File. | ||
*/ | ||
public AppendTo(final Path path) { | ||
this(path.toFile()); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param src File to which content will be appended. | ||
*/ | ||
public AppendTo(final File src) { | ||
this.source = src; | ||
} | ||
|
||
@Override | ||
public OutputStream stream() throws Exception { | ||
return Files.newOutputStream( | ||
this.source.toPath(), StandardOpenOption.APPEND | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2018 Yegor Bugayenko | ||
* | ||
* 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 NON-INFRINGEMENT. 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. | ||
*/ | ||
package org.cactoos.io; | ||
|
||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.NoSuchFileException; | ||
import org.cactoos.text.JoinedText; | ||
import org.cactoos.text.Randomized; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.llorllale.cactoos.matchers.Assertion; | ||
import org.llorllale.cactoos.matchers.InputHasContent; | ||
import org.llorllale.cactoos.matchers.Throws; | ||
|
||
/** | ||
* Test case for {@link AppendTo}. | ||
* | ||
* @since 1.0 | ||
* @checkstyle ClassDataAbstractionCouplingCheck (100 lines) | ||
*/ | ||
public final class AppendToTest { | ||
|
||
/** | ||
* Temporary files and folders generator. | ||
*/ | ||
@Rule | ||
public final TemporaryFolder folder = new TemporaryFolder(); | ||
|
||
/** | ||
* Ensures that AppendTo is failing on a negative predicate result. | ||
*/ | ||
@Test | ||
public void failsIfFileDoesNotExist() { | ||
final File source = new File( | ||
new Randomized( | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' | ||
).asString() | ||
); | ||
new Assertion<>( | ||
"Can't throw exception with proper message", | ||
() -> () -> new AppendTo(source).stream(), | ||
new Throws<>( | ||
source.getPath(), | ||
NoSuchFileException.class | ||
) | ||
).affirm(); | ||
} | ||
|
||
/** | ||
* Ensures that AppendTo is appending to a given file. | ||
* @throws Exception if fails | ||
*/ | ||
@Test | ||
public void appendsToFile() throws Exception { | ||
final File source = this.folder.newFile(); | ||
final String first = "abdcd"; | ||
new OutputTo(source).stream().write(first.getBytes()); | ||
final String second = "efgh"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Iprogrammerr let's check with some unicode text too. |
||
new AppendTo(source).stream().write(second.getBytes()); | ||
new Assertion<>( | ||
"Does not contain expected text", | ||
() -> new InputOf(source), | ||
new InputHasContent(new JoinedText("", first, second)) | ||
).affirm(); | ||
} | ||
|
||
/** | ||
* Ensures that AppendTo is appending unicode text to a given file. | ||
* @throws Exception if fails | ||
*/ | ||
@Test | ||
public void appendsUnicodeToFile() throws Exception { | ||
final File source = this.folder.newFile(); | ||
final String first = "Hello, товарищ output #3 äÄ "; | ||
new OutputTo(source) | ||
.stream() | ||
.write(first.getBytes(StandardCharsets.UTF_8)); | ||
final String second = "#4 äÄ üÜ öÖ and ß"; | ||
new AppendTo(source) | ||
.stream() | ||
.write(second.getBytes(StandardCharsets.UTF_8)); | ||
new Assertion<>( | ||
"Can't find expected unicode text content", | ||
() -> new InputOf(source), | ||
new InputHasContent(new JoinedText("", first, second)) | ||
).affirm(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@llorllale is there an alternative to this
TemporaryFolder
@Rule
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Iprogrammerr Please, go ahead here.