-
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
Changes from 1 commit
998ca1f
241a41e
8a9d042
5e93ac9
43b4f6d
ccc8b4c
3f50e1e
6969dcf
4cd8d50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import org.cactoos.Output; | ||
import org.cactoos.text.FormattedText; | ||
|
||
/** | ||
* 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 source File to which content will be appended. | ||
*/ | ||
public AppendTo(final File source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public OutputStream stream() throws Exception { | ||
if (!this.source.exists()) { | ||
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 this check is already baked into |
||
throw new IOException( | ||
new FormattedText( | ||
"Can not append to %s file. It does not exist", | ||
this.source.getAbsolutePath() | ||
).asString() | ||
); | ||
} | ||
return Files.newOutputStream( | ||
this.source.toPath(), StandardOpenOption.APPEND | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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.IOException; | ||
import org.cactoos.text.FormattedText; | ||
import org.cactoos.text.JoinedText; | ||
import org.cactoos.text.RandomText; | ||
import org.cactoos.text.TextOf; | ||
import org.hamcrest.MatcherAssert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.llorllale.cactoos.matchers.TextIs; | ||
|
||
/** | ||
* Test case for {@link AppendTo}. | ||
* | ||
* @since 1.0 | ||
* @checkstyle MagicNumber (500 line) | ||
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 Please, use |
||
* @checkstyle ClassDataAbstractionCouplingCheck (100 lines) | ||
*/ | ||
public final class AppendToTest { | ||
|
||
/** | ||
* Temporary files and folders generator. | ||
*/ | ||
@Rule | ||
public final TemporaryFolder folder = new TemporaryFolder(); | ||
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. @llorllale is there an alternative to this 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 Please, go ahead here. |
||
|
||
/** | ||
* A rule for handling an exception. | ||
*/ | ||
@Rule | ||
public final ExpectedException exception = ExpectedException.none(); | ||
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 remove this |
||
|
||
/** | ||
* Ensures that AppendTo is failing on a negative predicate result. | ||
* @throws Exception if fails | ||
*/ | ||
@Test | ||
public void failsIfFileDoesNotExist() throws Exception { | ||
final File source = new File(new RandomText(5).asString()); | ||
this.exception.expect(IOException.class); | ||
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 remove this and use an alternative provided by 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. @fabriciofx There is no cactoos-matchers release with that mechanism provided(namely, Throws class). 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 wait for #1023. |
||
this.exception.expectMessage( | ||
new FormattedText( | ||
"Can not append to %s file. It does not exist", | ||
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 Change "Can not" to "Can't". |
||
source.getAbsolutePath() | ||
).asString() | ||
); | ||
new AppendTo(source).stream(); | ||
} | ||
|
||
/** | ||
* 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()); | ||
MatcherAssert.assertThat( | ||
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 use 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. @fabriciofx Same as above. 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 The same above. |
||
"Does not contain expected text", | ||
new TextOf(source), | ||
new TextIs(new JoinedText("", first, second)) | ||
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 Please, use |
||
); | ||
} | ||
} |
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 use a different constructor parameter name than attribute name.