Skip to content

Commit

Permalink
#30 extra test for massive write
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Sep 18, 2022
1 parent 385c457 commit e67a48a
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 37 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SOFTWARE.
<parent>
<groupId>com.jcabi</groupId>
<artifactId>parent</artifactId>
<version>0.59.0</version>
<version>0.61.0</version>
</parent>
<groupId>com.yegor256</groupId>
<artifactId>tojos</artifactId>
Expand Down Expand Up @@ -105,10 +105,16 @@ SOFTWARE.
<version>5.7.0</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.6.2</version>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/yegor256/tojos/MnJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.json.JsonReader;
Expand Down Expand Up @@ -105,7 +106,7 @@ public Collection<Map<String, String>> read() {
if (this.file.toFile().exists()) {
try (
Reader reader = Files.newBufferedReader(this.file);
JsonReader json = javax.json.Json.createReader(reader)
JsonReader json = Json.createReader(reader)
) {
json
.readArray()
Expand All @@ -124,9 +125,9 @@ public Collection<Map<String, String>> read() {

@Override
public void write(final Collection<Map<String, String>> rows) {
final JsonArrayBuilder array = javax.json.Json.createArrayBuilder();
final JsonArrayBuilder array = Json.createArrayBuilder();
for (final Map<String, String> row : rows) {
final JsonObjectBuilder obj = javax.json.Json.createObjectBuilder();
final JsonObjectBuilder obj = Json.createObjectBuilder();
if (row.containsKey(Tojos.KEY)) {
obj.add(Tojos.KEY, row.get(Tojos.KEY));
}
Expand Down Expand Up @@ -183,7 +184,7 @@ private static String asString(final JsonValue value) {
private static JsonWriterFactory factory() {
final Map<String, Object> properties = new HashMap<>(1);
properties.put(JsonGenerator.PRETTY_PRINTING, true);
return javax.json.Json.createWriterFactory(properties);
return Json.createWriterFactory(properties);
}

}
6 changes: 4 additions & 2 deletions src/main/java/com/yegor256/tojos/MnMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/
package com.yegor256.tojos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

Expand All @@ -44,7 +44,9 @@ public final class MnMemory implements Mono {

@Override
public Collection<Map<String, String>> read() {
return Collections.unmodifiableCollection(this.mem);
final Collection<Map<String, String>> list = new ArrayList<>(this.mem.size());
list.addAll(this.mem);
return list;
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/yegor256/tojos/Mono.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public interface Mono {

/**
* Read them all.
*
* The list returned is modifiable.
*
* @return The list of all lines
*/
Collection<Map<String, String>> read();
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/yegor256/tojos/MnCsvTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
*
* @since 0.3.0
*/
public final class MnCsvTest {
final class MnCsvTest {

@Test
public void simpleScenario(@TempDir final Path temp) {
void simpleScenario(@TempDir final Path temp) {
final Mono csv = new MnCsv(temp.resolve("foo/bar/a.csv"));
final Collection<Map<String, String>> rows = csv.read();
MatcherAssert.assertThat(
Expand All @@ -64,7 +64,7 @@ public void simpleScenario(@TempDir final Path temp) {
}

@Test
public void ignoresEmptyElements(@TempDir final Path temp) {
void ignoresEmptyElements(@TempDir final Path temp) {
final Mono csv = new MnCsv(temp.resolve("foo/bar/xx.csv"));
final Collection<Map<String, String>> rows = csv.read();
final Map<String, String> row = new HashMap<>(0);
Expand All @@ -79,7 +79,7 @@ public void ignoresEmptyElements(@TempDir final Path temp) {
}

@Test
public void keepsBackslash(@TempDir final Path temp) {
void keepsBackslash(@TempDir final Path temp) {
final Mono csv = new MnCsv(temp.resolve("foo/bar/slash.csv"));
final Collection<Map<String, String>> rows = csv.read();
final Map<String, String> row = new HashMap<>(0);
Expand All @@ -94,7 +94,7 @@ public void keepsBackslash(@TempDir final Path temp) {
}

@Test
public void putsKeyFirst(@TempDir final Path temp) throws IOException {
void putsKeyFirst(@TempDir final Path temp) throws IOException {
final Path path = temp.resolve("key-test.json");
final Mono csv = new MnCsv(path);
final Collection<Map<String, String>> rows = csv.read();
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/yegor256/tojos/MnJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
*
* @since 0.3.0
*/
public final class MnJsonTest {
final class MnJsonTest {

@Test
public void simpleScenario(@TempDir final Path temp) {
void simpleScenario(@TempDir final Path temp) {
final Mono json = new MnJson(temp.resolve("foo/bar/a.json"));
final Collection<Map<String, String>> rows = json.read();
MatcherAssert.assertThat(
Expand All @@ -65,7 +65,7 @@ public void simpleScenario(@TempDir final Path temp) {
}

@Test
public void writesEmptyCollection(@TempDir final Path temp) {
void writesEmptyCollection(@TempDir final Path temp) {
final Mono json = new MnJson(temp.resolve("foo/bar/b.json"));
json.write(Collections.emptyList());
MatcherAssert.assertThat(
Expand All @@ -75,7 +75,7 @@ public void writesEmptyCollection(@TempDir final Path temp) {
}

@Test
public void prettyPrint(@TempDir final Path temp) throws IOException {
void prettyPrint(@TempDir final Path temp) throws IOException {
final Path path = temp.resolve("z.json");
final Mono json = new MnJson(path);
final Collection<Map<String, String>> rows = json.read();
Expand All @@ -93,7 +93,7 @@ public void prettyPrint(@TempDir final Path temp) throws IOException {
}

@Test
public void keyAtFirstPosition(@TempDir final Path temp) throws IOException {
void keyAtFirstPosition(@TempDir final Path temp) throws IOException {
final Path path = temp.resolve("key-test.json");
final Mono json = new MnJson(path);
final Collection<Map<String, String>> rows = json.read();
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/yegor256/tojos/MnMemoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
*
* @since 0.12.0
*/
public final class MnMemoryTest {
final class MnMemoryTest {

@Test
public void readsEmpty() {
void readsEmpty() {
final Mono mono = new MnMemory();
MatcherAssert.assertThat(
mono.read().size(),
Expand All @@ -48,7 +48,7 @@ public void readsEmpty() {
}

@Test
public void simpleScenario() {
void simpleScenario() {
final Mono mono = new MnMemory();
final Map<String, String> row = new HashMap<>(0);
final String key = Tojos.KEY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
*
* @since 0.12.0
*/
public final class StickyMonoTest {
final class MnStickyTest {

@Test
public void emptyRead() {
void emptyRead() {
final Mono mono = new MnSticky(new MnMemory());
MatcherAssert.assertThat(
mono.read().size(),
Expand All @@ -50,7 +50,7 @@ public void emptyRead() {
}

@Test
public void simpleScenario(@TempDir final Path temp) {
void simpleScenario(@TempDir final Path temp) {
final Mono sticky = new MnSticky(new MnCsv(temp.resolve("x.csv")));
final Map<String, String> row = new HashMap<>(0);
final String key = Tojos.KEY;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/yegor256/tojos/MnTabsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
*
* @since 0.7.0
*/
public final class MnTabsTest {
final class MnTabsTest {

@Test
public void simpleScenario(@TempDir final Path temp) {
void simpleScenario(@TempDir final Path temp) {
final Mono tabs = new MnTabs(temp.resolve("foo/bar/a.tabs"));
final Collection<Map<String, String>> rows = tabs.read();
MatcherAssert.assertThat(
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/yegor256/tojos/MnYamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
* Test case for {@link MnYaml}.
* @since 0.9.2
*/
public class MnYamlTest {
final class MnYamlTest {

@Test
public void writesAndReads(@TempDir final Path temp) {
void writesAndReads(@TempDir final Path temp) {
final File file = temp.resolve("test.yml").toFile();
final Map<String, String> keys = new HashMap<>();
final List<Map<String, String>> tojo = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
import org.junit.jupiter.api.io.TempDir;

/**
* Test for cached tojos. Not thread-safe.
* Test for {@link TjCached}.
*
* @since 1.0
* @checkstyle MagicNumberCheck (70 lines)
*/
class CachedTojosTest {
final class TjCachedTest {

@Test
void testAddClearsCache(@TempDir final Path temp) {
final Tojos tojos = new TjDefault(new MnCsv(temp.resolve("my-tojos-1.csv")));
final String[] keys = new String[] {"k10", "k20"};
final String[] keys = {"k10", "k20"};
tojos.add("A0").set(keys[0], "v10").set(keys[1], "vv10");
tojos.add("B0").set(keys[0], "v20").set(keys[1], "vv20");
tojos.add("C0").set(keys[0], "v30").set(keys[1], "vv30");
Expand All @@ -55,7 +55,7 @@ void testAddClearsCache(@TempDir final Path temp) {
@Test
void testSelectFromCached(@TempDir final Path temp) {
final Tojos tojos = new TjDefault(new MnCsv(temp.resolve("my-tojos-2.csv")));
final String[] keys = new String[] {"k11", "k21"};
final String[] keys = {"k11", "k21"};
tojos.add("A1").set(keys[0], "v11").set(keys[1], "vv11");
tojos.add("B1").set(keys[0], "v21").set(keys[1], "vv21");
tojos.add("C1").set(keys[0], "v31").set(keys[1], "vv31");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.file.Path;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -35,11 +36,11 @@
*
* @since 0.3.0
*/
public final class MonoTojosTest {
final class TjDefaultTest {

@ParameterizedTest
@ValueSource(strings = {"a.csv", "a.json"})
public void simpleScenario(final String file, @TempDir final Path temp) {
void simpleScenario(final String file, @TempDir final Path temp) {
final Tojos tojos = new TjDefault(new MnCsv(temp.resolve(file)));
tojos.add("foo").set("k", "v").set("a", "b");
tojos.select(t -> t.exists("k")).iterator().next();
Expand All @@ -51,7 +52,7 @@ public void simpleScenario(final String file, @TempDir final Path temp) {

@ParameterizedTest
@ValueSource(strings = {"x.csv", "x.json"})
public void addTojo(final String file, @TempDir final Path temp) {
void addTojo(final String file, @TempDir final Path temp) {
final Tojos tojos = new TjDefault(new MnJson(temp.resolve(file)));
tojos.add("foo-1");
MatcherAssert.assertThat(
Expand All @@ -62,7 +63,7 @@ public void addTojo(final String file, @TempDir final Path temp) {

@ParameterizedTest
@ValueSource(strings = {"y.csv", "y.json"})
public void uniqueIds(final String file, @TempDir final Path temp) {
void uniqueIds(final String file, @TempDir final Path temp) {
final Tojos tojos = new TjDefault(new MnTabs(temp.resolve(file)));
final String name = "foo11";
tojos.add(name);
Expand All @@ -73,4 +74,27 @@ public void uniqueIds(final String file, @TempDir final Path temp) {
);
}

@Test
void massiveWrite(@TempDir final Path temp) {
final Tojos tojos = new TjDefault(
new MnSticky(
new MnJson(temp.resolve("big-data.json"))
)
);
final int total = 100;
for (int idx = 0; idx < total; ++idx) {
final String key = String.format("k%d", idx);
tojos.add(String.format("key-%d", idx))
.set(key, String.format("v%d", idx));
MatcherAssert.assertThat(
tojos.select(r -> r.exists(key)),
Matchers.iterableWithSize(1)
);
}
MatcherAssert.assertThat(
tojos.select(r -> true),
Matchers.iterableWithSize(total)
);
}

}

0 comments on commit e67a48a

Please sign in to comment.