Skip to content

Commit 09f8895

Browse files
committed
Add Common submodule (shared code for subprojects)
1 parent 0dc3327 commit 09f8895

File tree

10 files changed

+53
-14
lines changed

10 files changed

+53
-14
lines changed

JavaReleases/src/test/java/pl/mperor/lab/TestUtils.java renamed to Common/src/main/java/pl/mperor/lab/common/TestUtils.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package pl.mperor.lab;
1+
package pl.mperor.lab.common;
22

33
import java.io.*;
44
import java.util.ArrayList;
@@ -26,19 +26,19 @@ public String all() {
2626
return out.toString();
2727
}
2828

29-
public TestList<String> lines() {
29+
public ReadableList<String> lines() {
3030
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(out.toByteArray())))) {
31-
return reader.lines().collect(Collectors.toCollection(TestArrayList::new));
31+
return reader.lines().collect(Collectors.toCollection(ReadableArrayList::new));
3232
} catch (IOException e) {
3333
throw new RuntimeException(e);
3434
}
3535
}
3636
}
3737

38-
public static class TestArrayList<E> extends ArrayList<E> implements TestList<E> {
38+
public static class ReadableArrayList<E> extends ArrayList<E> implements ReadableList<E> {
3939
}
4040

41-
public interface TestList<E> extends List<E> {
41+
public interface ReadableList<E> extends List<E> {
4242
default E getSecond() {
4343
return getNext(1);
4444
}
@@ -47,11 +47,11 @@ default E getThird() {
4747
return getNext(2);
4848
}
4949

50-
private E getNext(int id) {
51-
if (this.isEmpty()) {
50+
private E getNext(int index) {
51+
if (this.isEmpty() && index >= size()) {
5252
throw new NoSuchElementException();
5353
} else {
54-
return this.get(id);
54+
return this.get(index);
5555
}
5656
}
5757
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package pl.mperor.lab.common;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.NoSuchElementException;
7+
import java.util.stream.Stream;
8+
9+
class UtilsTest {
10+
11+
@Test
12+
public void testReadableOutputReturnsReadableList() {
13+
var out = TestUtils.setTempSystemOut();
14+
Stream.of("One", "Two", "Three")
15+
.forEach(System.out::println);
16+
var readableList = out.lines();
17+
Assertions.assertEquals("One", readableList.getFirst());
18+
Assertions.assertEquals("Two", readableList.getSecond());
19+
Assertions.assertEquals("Three", readableList.getThird());
20+
TestUtils.resetSystemOut();
21+
}
22+
23+
@Test
24+
public void testNoOutPrintsReturnsEmptyReadableList() {
25+
var out = TestUtils.setTempSystemOut();
26+
var readableList = out.lines();
27+
Assertions.assertThrows(NoSuchElementException.class, () -> readableList.getFirst());
28+
Assertions.assertThrows(NoSuchElementException.class, () -> readableList.getSecond());
29+
Assertions.assertThrows(NoSuchElementException.class, () -> readableList.getThird());
30+
Assertions.assertEquals("", out.all());
31+
TestUtils.resetSystemOut();
32+
}
33+
34+
}

DesignPatterns/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dependencies {
2+
testImplementation project(':Common')
3+
}
4+

JavaReleases/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ dependencies {
66
testRuntimeOnly 'com.h2database:h2:2.2.224'
77
testRuntimeOnly "org.graalvm.js:js:${graaljsVersion}"
88
testImplementation "org.graalvm.js:js-scriptengine:${graaljsVersion}"
9+
implementation project(':Common')
910
}
1011

JavaReleases/src/test/java/pl/mperor/lab/java/Java16.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.junit.jupiter.api.Test;
66
import org.junit.jupiter.api.condition.EnabledOnOs;
77
import org.junit.jupiter.api.condition.OS;
8-
import pl.mperor.lab.TestUtils;
8+
import pl.mperor.lab.common.TestUtils;
99

1010
import java.io.IOException;
1111
import java.net.StandardProtocolFamily;

JavaReleases/src/test/java/pl/mperor/lab/java/Java18.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.sun.net.httpserver.HttpServer;
44
import org.junit.jupiter.api.Assertions;
55
import org.junit.jupiter.api.Test;
6-
import pl.mperor.lab.TestUtils;
6+
import pl.mperor.lab.common.TestUtils;
77

88
import java.io.BufferedReader;
99
import java.io.IOException;

JavaReleases/src/test/java/pl/mperor/lab/java/Java21.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
5-
import pl.mperor.lab.TestUtils;
5+
import pl.mperor.lab.common.TestUtils;
66

77
import java.util.Collections;
88
import java.util.List;

JavaReleases/src/test/java/pl/mperor/lab/java/Java5.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
5-
import pl.mperor.lab.TestUtils;
5+
import pl.mperor.lab.common.TestUtils;
66
import pl.mperor.lab.java.generic.Box;
77

88
import java.lang.annotation.*;

JavaReleases/src/test/java/pl/mperor/lab/java/Java8.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import org.junit.jupiter.api.Assertions;
55
import org.junit.jupiter.api.Test;
6-
import pl.mperor.lab.TestUtils;
6+
import pl.mperor.lab.common.TestUtils;
77

88
import java.lang.reflect.Method;
99
import java.lang.reflect.Modifier;

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
rootProject.name = 'JavaLab'
2-
include 'JavaReleases', 'FirstSteps', 'DesignPatterns'
2+
include 'JavaReleases', 'FirstSteps', 'DesignPatterns', 'Common'

0 commit comments

Comments
 (0)