Skip to content
This repository was archived by the owner on Mar 26, 2023. It is now read-only.

Commit c741d13

Browse files
authored
Merge pull request #3 from g4s8/2
Tests with rxjava
2 parents 162c6c5 + f4919cf commit c741d13

2 files changed

Lines changed: 135 additions & 1 deletion

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
2727
<modelVersion>4.0.0</modelVersion>
2828
<groupId>wtf.g4s8</groupId>
2929
<artifactId>rio</artifactId>
30-
<version>1.0-SNAPSHOT</version>
30+
<version>0.1.2</version>
3131
<parent>
3232
<groupId>com.artipie</groupId>
3333
<artifactId>ppom</artifactId>
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2020 artipie.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package wtf.g4s8.rio.file;
25+
26+
import io.reactivex.Flowable;
27+
import java.nio.ByteBuffer;
28+
import java.nio.file.Path;
29+
import org.hamcrest.MatcherAssert;
30+
import org.hamcrest.Matchers;
31+
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.io.TempDir;
34+
import org.reactivestreams.Publisher;
35+
36+
/**
37+
* Tests with RxJava flowables.
38+
* @since 0.1
39+
*/
40+
public final class FileWithRxJavaTest {
41+
42+
@Test
43+
void shouldSave(@TempDir final Path tmp) throws Exception {
44+
final byte[] data = "0".getBytes();
45+
final File file = new File(tmp.resolve("save"));
46+
file.write(Flowable.just(ByteBuffer.wrap(data)))
47+
.toCompletableFuture().get();
48+
MatcherAssert.assertThat(
49+
readFully(file.content()),
50+
Matchers.equalTo(data)
51+
);
52+
}
53+
54+
@Test
55+
void shouldSaveFromMultipleBuffers(@TempDir final Path tmp) throws Exception {
56+
final File file = new File(tmp.resolve("saveFromMultipleBuffers"));
57+
file.write(
58+
Flowable.fromArray(
59+
ByteBuffer.wrap("12".getBytes()),
60+
ByteBuffer.wrap("34".getBytes()),
61+
ByteBuffer.wrap("5".getBytes())
62+
)
63+
).toCompletableFuture().get();
64+
MatcherAssert.assertThat(
65+
readFully(file.content()),
66+
Matchers.equalTo("12345".getBytes())
67+
);
68+
}
69+
70+
@Test
71+
void shouldSaveEmpty(@TempDir final Path tmp) throws Exception {
72+
final File file = new File(tmp.resolve("shouldSaveEmpty"));
73+
file.write(Flowable.empty()).toCompletableFuture().get();
74+
MatcherAssert.assertThat(
75+
readFully(file.content()),
76+
Matchers.equalTo(new byte[0])
77+
);
78+
}
79+
80+
@Test
81+
void shouldSaveWhenValueAlreadyExists(@TempDir final Path tmp) throws Exception {
82+
final byte[] original = "1".getBytes();
83+
final byte[] updated = "2".getBytes();
84+
final File file = new File(tmp.resolve("shouldSaveWhenValueAlreadyExists"));
85+
file.write(Flowable.just(ByteBuffer.wrap(original))).toCompletableFuture().get();
86+
file.write(Flowable.just(ByteBuffer.wrap(updated))).toCompletableFuture().get();
87+
MatcherAssert.assertThat(
88+
readFully(file.content()),
89+
Matchers.equalTo(updated)
90+
);
91+
}
92+
93+
@Test
94+
void shouldFailToSaveErrorContent(@TempDir final Path tmp) {
95+
Assertions.assertThrows(
96+
Exception.class,
97+
() -> new File(
98+
tmp.resolve("shouldFailToSaveErrorContent")
99+
).write(Flowable.error(new IllegalStateException()))
100+
.toCompletableFuture().get()
101+
);
102+
}
103+
104+
@Test
105+
void shouldFailToLoadAbsentValue(@TempDir final Path tmp) {
106+
final File file = new File(tmp.resolve("shouldFailToLoadAbsentValue"));
107+
Assertions.assertThrows(RuntimeException.class, () -> readFully(file.content()));
108+
}
109+
110+
private static byte[] readFully(final Publisher<ByteBuffer> pub) {
111+
return Flowable.fromPublisher(pub).reduce(
112+
ByteBuffer.allocate(0),
113+
FileWithRxJavaTest::concat
114+
).map(FileWithRxJavaTest::remaining).blockingGet();
115+
}
116+
117+
private static byte[] remaining(final ByteBuffer buf) {
118+
final byte[] bytes = new byte[buf.remaining()];
119+
buf.get(bytes);
120+
return bytes;
121+
}
122+
123+
private static ByteBuffer concat(final ByteBuffer left, final ByteBuffer right) {
124+
left.mark();
125+
right.mark();
126+
final ByteBuffer concat = ByteBuffer.allocate(
127+
left.remaining() + right.remaining()
128+
).put(left).put(right);
129+
left.reset();
130+
right.reset();
131+
concat.flip();
132+
return concat;
133+
}
134+
}

0 commit comments

Comments
 (0)