fixed optional (#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
This commit is contained in:
Egima profile 2016-12-04 14:30:28 +03:00 committed by Grzegorz Piwowarek
parent 9d3b8dfb72
commit 11b1cd3edd
4 changed files with 75 additions and 35 deletions

View File

@ -1,36 +1,77 @@
package com.baeldung.java.nio2.async; package com.baeldung.java.nio2.async;
import org.junit.After; import java.io.File;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import static org.junit.Assert.assertEquals; public class AsyncEchoServer {
private AsynchronousServerSocketChannel serverChannel;
private Future<AsynchronousSocketChannel> acceptResult;
private AsynchronousSocketChannel clientChannel;
public class AsyncEchoTest { public AsyncEchoServer() {
try {
Process server; serverChannel = AsynchronousServerSocketChannel.open();
AsyncEchoClient client; InetSocketAddress hostAddress = new InetSocketAddress("localhost", 4999);
serverChannel.bind(hostAddress);
@Before acceptResult = serverChannel.accept();
public void setup() throws IOException, InterruptedException { } catch (IOException e) {
server = AsyncEchoServer2.start(); e.printStackTrace();
client = AsyncEchoClient.getInstance(); }
} }
@Test public void runServer() {
public void givenServerClient_whenServerEchosMessage_thenCorrect() throws Exception { try {
String resp1 = client.sendMessage("hello"); clientChannel = acceptResult.get();
String resp2 = client.sendMessage("world"); if ((clientChannel != null) && (clientChannel.isOpen())) {
assertEquals("hello", resp1); while (true) {
assertEquals("world", resp2);
}
@After ByteBuffer buffer = ByteBuffer.allocate(32);
public void teardown() throws IOException { Future<Integer> readResult = clientChannel.read(buffer);
server.destroy();
client.stop(); //do other things as operation continues in background
readResult.get();
buffer.flip();
String message = new String(buffer.array()).trim();
if (message.equals("bye")) {
break; // while loop
}
buffer = ByteBuffer.wrap(new String(message).getBytes());
Future<Integer> writeResult = clientChannel.write(buffer);
//run other code
writeResult.get();
buffer.clear();
} // while()
clientChannel.close();
serverChannel.close();
}
} catch (InterruptedException | ExecutionException | IOException e) {
e.printStackTrace();
} }
} }
public static void main(String[] args) {
AsyncEchoServer server = new AsyncEchoServer();
server.runServer();
}
public static Process start() throws IOException, InterruptedException {
String javaHome = System.getProperty("java.home");
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
String classpath = System.getProperty("java.class.path");
String className = AsyncEchoServer.class.getCanonicalName();
ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath, className);
return builder.start();
}
}

View File

@ -8,7 +8,6 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Optional; import java.util.Optional;
import com.baeldung.java_8_features.Person;
import org.junit.Test; import org.junit.Test;
@ -102,7 +101,7 @@ public class OptionalTest {
List<String> companyNames = Arrays.asList("paypal", "oracle", "", "microsoft", "", "apple"); List<String> companyNames = Arrays.asList("paypal", "oracle", "", "microsoft", "", "apple");
Optional<List<String>> listOptional = Optional.of(companyNames); Optional<List<String>> listOptional = Optional.of(companyNames);
int size = listOptional.map(list -> list.size()).get(); int size = listOptional.map(List::size).orElse(0);
assertEquals(6, size); assertEquals(6, size);
} }
@ -111,7 +110,7 @@ public class OptionalTest {
String name = "baeldung"; String name = "baeldung";
Optional<String> nameOptional = Optional.of(name); Optional<String> nameOptional = Optional.of(name);
int len = nameOptional.map(s -> s.length()).get(); int len = nameOptional.map(String::length).orElse(0);
assertEquals(8, len); assertEquals(8, len);
} }
@ -122,7 +121,7 @@ public class OptionalTest {
boolean correctPassword = passOpt.filter(pass -> pass.equals("password")).isPresent(); boolean correctPassword = passOpt.filter(pass -> pass.equals("password")).isPresent();
assertFalse(correctPassword); assertFalse(correctPassword);
correctPassword = passOpt.map(pass -> pass.trim()).filter(pass -> pass.equals("password")).isPresent(); correctPassword = passOpt.map(String::trim).filter(pass -> pass.equals("password")).isPresent();
assertTrue(correctPassword); assertTrue(correctPassword);
} }
@ -132,12 +131,12 @@ public class OptionalTest {
Person person = new Person("john", 26); Person person = new Person("john", 26);
Optional<Person> personOptional = Optional.of(person); Optional<Person> personOptional = Optional.of(person);
Optional<Optional<String>> nameOptionalWrapper = personOptional.map(p -> p.getName()); Optional<Optional<String>> nameOptionalWrapper = personOptional.map(Person::getName);
Optional<String> nameOptional = nameOptionalWrapper.get(); Optional<String> nameOptional = nameOptionalWrapper.orElseThrow(IllegalArgumentException::new);
String name1 = nameOptional.get(); String name1 = nameOptional.orElseThrow(IllegalArgumentException::new);
assertEquals("john", name1); assertEquals("john", name1);
String name = personOptional.flatMap(p -> p.getName()).get(); String name = personOptional.flatMap(Person::getName).orElseThrow(IllegalArgumentException::new);
assertEquals("john", name); assertEquals("john", name);
} }
@ -147,7 +146,7 @@ public class OptionalTest {
person.setPassword("password"); person.setPassword("password");
Optional<Person> personOptional = Optional.of(person); Optional<Person> personOptional = Optional.of(person);
String password = personOptional.flatMap(p -> p.getPassword()).filter(cleanPass -> cleanPass.equals("password")).get(); String password = personOptional.flatMap(Person::getPassword).filter(cleanPass -> cleanPass.equals("password")).orElseThrow(IllegalArgumentException::new);
assertEquals("password", password); assertEquals("password", password);
} }