Skip to content

Commit 11b1cd3

Browse files
Egima profilepivovarit
authored andcommitted
fixed optional (eugenp#877)
* made changes to java reflection * removed redundant method makeSound in Animal abstract class * added project for play-framework article * added project for regex * changed regex project from own model to core-java * added project for routing in play * made changes to regex project * refactored code for REST API with Play project * refactored student store indexing to zero base * added unit tests, removed bad names * added NIO Selector project under core-java module * requested changes made * added project for nio2 * standardized exception based tests * fixed exception based tests * removed redundant files * added network interface project * used UUID other than timestamps * fixed network interface tests * removed filetest change * made changes to NIO2 FileTest names * added project for asyncronous channel apis * added project for NIO2 advanced filesystems APIS * merge conflicts * merged changes to asyncfiletest with future get API * removed while loops from async client and server * added project for java8 optional * fixed merge conflicts in spring-core * fixed optional
1 parent 9d3b8df commit 11b1cd3

4 files changed

Lines changed: 74 additions & 34 deletions

File tree

‎core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoClient.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@ public static void main(String[] args) throws Exception {
8383
}
8484
}
8585

86-
}
86+
}

‎core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,4 @@ public static Process start() throws IOException, InterruptedException {
9797

9898
return builder.start();
9999
}
100-
}
100+
}
Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,77 @@
11
package com.baeldung.java.nio2.async;
22

3-
import org.junit.After;
4-
import org.junit.Before;
5-
import org.junit.Test;
6-
3+
import java.io.File;
74
import java.io.IOException;
5+
import java.net.InetSocketAddress;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.AsynchronousServerSocketChannel;
8+
import java.nio.channels.AsynchronousSocketChannel;
9+
import java.util.concurrent.ExecutionException;
10+
import java.util.concurrent.Future;
811

9-
import static org.junit.Assert.assertEquals;
12+
public class AsyncEchoServer {
13+
private AsynchronousServerSocketChannel serverChannel;
14+
private Future<AsynchronousSocketChannel> acceptResult;
15+
private AsynchronousSocketChannel clientChannel;
1016

11-
public class AsyncEchoTest {
17+
public AsyncEchoServer() {
18+
try {
19+
serverChannel = AsynchronousServerSocketChannel.open();
20+
InetSocketAddress hostAddress = new InetSocketAddress("localhost", 4999);
21+
serverChannel.bind(hostAddress);
22+
acceptResult = serverChannel.accept();
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
}
1227

13-
Process server;
14-
AsyncEchoClient client;
28+
public void runServer() {
29+
try {
30+
clientChannel = acceptResult.get();
31+
if ((clientChannel != null) && (clientChannel.isOpen())) {
32+
while (true) {
1533

16-
@Before
17-
public void setup() throws IOException, InterruptedException {
18-
server = AsyncEchoServer2.start();
19-
client = AsyncEchoClient.getInstance();
20-
}
34+
ByteBuffer buffer = ByteBuffer.allocate(32);
35+
Future<Integer> readResult = clientChannel.read(buffer);
36+
37+
//do other things as operation continues in background
38+
readResult.get();
39+
40+
buffer.flip();
41+
String message = new String(buffer.array()).trim();
42+
if (message.equals("bye")) {
43+
break; // while loop
44+
}
45+
buffer = ByteBuffer.wrap(new String(message).getBytes());
46+
Future<Integer> writeResult = clientChannel.write(buffer);
47+
//run other code
48+
writeResult.get();
49+
buffer.clear();
50+
51+
} // while()
52+
53+
clientChannel.close();
54+
serverChannel.close();
55+
56+
}
57+
} catch (InterruptedException | ExecutionException | IOException e) {
58+
e.printStackTrace();
59+
}
2160

22-
@Test
23-
public void givenServerClient_whenServerEchosMessage_thenCorrect() throws Exception {
24-
String resp1 = client.sendMessage("hello");
25-
String resp2 = client.sendMessage("world");
26-
assertEquals("hello", resp1);
27-
assertEquals("world", resp2);
2861
}
2962

30-
@After
31-
public void teardown() throws IOException {
32-
server.destroy();
33-
client.stop();
63+
public static void main(String[] args) {
64+
AsyncEchoServer server = new AsyncEchoServer();
65+
server.runServer();
3466
}
67+
public static Process start() throws IOException, InterruptedException {
68+
String javaHome = System.getProperty("java.home");
69+
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
70+
String classpath = System.getProperty("java.class.path");
71+
String className = AsyncEchoServer.class.getCanonicalName();
3572

73+
ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath, className);
74+
75+
return builder.start();
76+
}
3677
}

‎core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.List;
99
import java.util.NoSuchElementException;
1010
import java.util.Optional;
11-
import com.baeldung.java_8_features.Person;
1211

1312
import org.junit.Test;
1413

@@ -102,7 +101,7 @@ public void givenOptional_whenMapWorks_thenCorrect() {
102101
List<String> companyNames = Arrays.asList("paypal", "oracle", "", "microsoft", "", "apple");
103102
Optional<List<String>> listOptional = Optional.of(companyNames);
104103

105-
int size = listOptional.map(list -> list.size()).get();
104+
int size = listOptional.map(List::size).orElse(0);
106105
assertEquals(6, size);
107106
}
108107

@@ -111,7 +110,7 @@ public void givenOptional_whenMapWorks_thenCorrect2() {
111110
String name = "baeldung";
112111
Optional<String> nameOptional = Optional.of(name);
113112

114-
int len = nameOptional.map(s -> s.length()).get();
113+
int len = nameOptional.map(String::length).orElse(0);
115114
assertEquals(8, len);
116115
}
117116

@@ -122,7 +121,7 @@ public void givenOptional_whenMapWorksWithFilter_thenCorrect() {
122121
boolean correctPassword = passOpt.filter(pass -> pass.equals("password")).isPresent();
123122
assertFalse(correctPassword);
124123

125-
correctPassword = passOpt.map(pass -> pass.trim()).filter(pass -> pass.equals("password")).isPresent();
124+
correctPassword = passOpt.map(String::trim).filter(pass -> pass.equals("password")).isPresent();
126125
assertTrue(correctPassword);
127126
}
128127

@@ -132,12 +131,12 @@ public void givenOptional_whenFlatMapWorks_thenCorrect2() {
132131
Person person = new Person("john", 26);
133132
Optional<Person> personOptional = Optional.of(person);
134133

135-
Optional<Optional<String>> nameOptionalWrapper = personOptional.map(p -> p.getName());
136-
Optional<String> nameOptional = nameOptionalWrapper.get();
137-
String name1 = nameOptional.get();
134+
Optional<Optional<String>> nameOptionalWrapper = personOptional.map(Person::getName);
135+
Optional<String> nameOptional = nameOptionalWrapper.orElseThrow(IllegalArgumentException::new);
136+
String name1 = nameOptional.orElseThrow(IllegalArgumentException::new);
138137
assertEquals("john", name1);
139138

140-
String name = personOptional.flatMap(p -> p.getName()).get();
139+
String name = personOptional.flatMap(Person::getName).orElseThrow(IllegalArgumentException::new);
141140
assertEquals("john", name);
142141
}
143142

@@ -147,7 +146,7 @@ public void givenOptional_whenFlatMapWorksWithFilter_thenCorrect() {
147146
person.setPassword("password");
148147
Optional<Person> personOptional = Optional.of(person);
149148

150-
String password = personOptional.flatMap(p -> p.getPassword()).filter(cleanPass -> cleanPass.equals("password")).get();
149+
String password = personOptional.flatMap(Person::getPassword).filter(cleanPass -> cleanPass.equals("password")).orElseThrow(IllegalArgumentException::new);
151150
assertEquals("password", password);
152151
}
153152

0 commit comments

Comments
 (0)