合并 Java -11 版本

This commit is contained in:
YuCheng Hu 2024-04-30 12:11:52 -04:00
parent dc220b25ff
commit a7b2f168b7
No known key found for this signature in database
GPG Key ID: 942395299055675C
42 changed files with 22 additions and 376 deletions

View File

@ -12,7 +12,6 @@
<module name="core-java-annotations" />
<module name="core-java-uuid" />
<module name="core-java-numbers" />
<module name="core-java-11-2" />
<module name="core-java-strings" />
<module name="core-java-11" />
<module name="core-java-collections-conversions" />
@ -38,6 +37,7 @@
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="core-java-11-2" target="11" />
<module name="core-java-8-2" target="11" />
<module name="core-java-collections-list-2" target="11" />
<module name="core-java-collections-list-3" target="1.5" />

View File

@ -1,12 +0,0 @@
## Java 11 核心Core Java 11
本模块中包含有关 Java 11 核心新增功能的的一些文章
### 相关文章
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
- [Guide to Java 8s Collectors](https://www.baeldung.com/java-8-collectors)
- [New Features in Java 11](https://www.baeldung.com/java-11-new-features)
- [Getting the Java Version at Runtime](https://www.baeldung.com/get-java-version-runtime)
- [Invoking a SOAP Web Service in Java](https://www.baeldung.com/java-soap-web-service)
- [Java HTTPS Client Certificate Authentication](https://www.baeldung.com/java-https-client-certificate-authentication)
- [Call Methods at Runtime Using Java Reflection](https://www.baeldung.com/java-method-reflection)

View File

@ -1,76 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-11-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-11-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.ossez.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.2-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${version.guava}</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-jupiter</artifactId>
<version>${mockserver.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>${jakarta.ws-api.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
</configuration>
</plugin>
<!-- jax-ws maven plugin -->
<plugin>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>${jaxws-maven-plugin.version}</version>
<configuration>
<wsdlUrls>
<wsdlUrl>http://localhost:8888/ws/country?wsdl</wsdlUrl>
</wsdlUrls>
<keep>true</keep>
<packageName>com.baeldung.soap.ws.client.generated</packageName>
<sourceDestDir>src/main/java</sourceDestDir>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source.version>11</maven.compiler.source.version>
<maven.compiler.target.version>11</maven.compiler.target.version>
<mockserver.version>5.11.1</mockserver.version>
<jakarta.ws-api.version>3.0.1</jakarta.ws-api.version>
<jaxws-maven-plugin.version>3.0.2</jaxws-maven-plugin.version>
</properties>
</project>

View File

@ -1,272 +0,0 @@
package com.ossez.optional;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import static org.junit.Assert.*;
/**
* Test for Java Optional
*
* <p><a href="https://www.ossez.com/t/java-8-optional/13964">https://www.ossez.com/t/java-8-optional/13964</a></p>
*
* @author YuCheng
*/
public class OptionalUnitTest {
private static final Logger LOG = LoggerFactory.getLogger(OptionalUnitTest.class);
// creating Optional
@Test
public void whenCreatesEmptyOptional_thenCorrect() {
Optional<String> empty = Optional.empty();
assertFalse(empty.isPresent());
}
@Test
public void givenNonNull_whenCreatesNonNullable_thenCorrect() {
String name = "HoneyMoose";
Optional<String> opt = Optional.of(name);
assertTrue(opt.isPresent());
}
@Test(expected = NullPointerException.class)
public void givenNull_whenThrowsErrorOnCreate_thenCorrect() {
String name = null;
Optional.of(name);
}
@Test
public void givenNonNull_whenCreatesOptional_thenCorrect() {
String name = "HoneyMoose";
Optional<String> opt = Optional.of(name);
assertTrue(opt.isPresent());
}
@Test
public void givenNonNull_whenCreatesNullable_thenCorrect() {
String name = "HoneyMoose";
Optional<String> opt = Optional.ofNullable(name);
assertTrue(opt.isPresent());
}
@Test
public void givenNull_whenCreatesNullable_thenCorrect() {
String name = null;
Optional<String> opt = Optional.ofNullable(name);
assertFalse(opt.isPresent());
}
// Checking Value With isPresent()
@Test
public void givenOptional_whenIsPresentWorks_thenCorrect() {
Optional<String> opt = Optional.of("HoneyMoose");
assertTrue(opt.isPresent());
opt = Optional.ofNullable(null);
assertFalse(opt.isPresent());
}
// Condition Action With ifPresent()
@Test
public void givenOptional_whenIfPresentWorks_thenCorrect() {
Optional<String> opt = Optional.of("HoneyMoose");
opt.ifPresent(name -> LOG.debug("{}", name.length()));
}
// returning Value With get()
@Test
public void givenOptional_whenGetsValue_thenCorrect() {
Optional<String> opt = Optional.of("HoneyMoose");
String name = opt.get();
assertEquals("HoneyMoose", name);
}
@Test(expected = NoSuchElementException.class)
public void givenOptionalWithNull_whenGetThrowsException_thenCorrect() {
Optional<String> opt = Optional.ofNullable(null);
String name = opt.get();
}
@Test
public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected() {
Optional<String> opt = Optional.of("HoneyMoose");
assertTrue(opt.isPresent());
opt = Optional.ofNullable(null);
assertFalse(opt.isPresent());
}
// This code only can be run in JDK 11
@Test
public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected_JDK11() {
Optional<String> opt = Optional.of("Baeldung");
assertFalse(opt.isEmpty());
opt = Optional.ofNullable(null);
assertTrue(opt.isEmpty());
}
// Conditional Return With filter()
@Test
public void whenOptionalFilterWorks_thenCorrect() {
Integer year = 2016;
Optional<Integer> yearOptional = Optional.of(year);
boolean is2016 = yearOptional.filter(y -> y == 2016).isPresent();
assertTrue(is2016);
boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent();
assertFalse(is2017);
}
@Test
public void whenFiltersWithoutOptional_thenCorrect() {
assertTrue(priceIsInRange1(new Modem(10.0)));
assertFalse(priceIsInRange1(new Modem(9.9)));
assertFalse(priceIsInRange1(new Modem(null)));
assertFalse(priceIsInRange1(new Modem(15.5)));
assertFalse(priceIsInRange1(null));
}
@Test
public void whenFiltersWithOptional_thenCorrect() {
assertTrue(priceIsInRange2(new Modem(10.0)));
assertFalse(priceIsInRange2(new Modem(9.9)));
assertFalse(priceIsInRange2(new Modem(null)));
assertFalse(priceIsInRange2(new Modem(15.5)));
assertFalse(priceIsInRange1(null));
}
public boolean priceIsInRange1(Modem modem) {
boolean isInRange = false;
if (modem != null && modem.getPrice() != null && (modem.getPrice() >= 10 && modem.getPrice() <= 15)) {
isInRange = true;
}
return isInRange;
}
public boolean priceIsInRange2(Modem modem2) {
return Optional.ofNullable(modem2).map(Modem::getPrice).filter(p -> p >= 10).filter(p -> p <= 15).isPresent();
}
// Transforming Value With map()
@Test
public void givenOptional_whenMapWorks_thenCorrect() {
List<String> companyNames = Arrays.asList("paypal", "oracle", "", "microsoft", "", "apple");
Optional<List<String>> listOptional = Optional.of(companyNames);
int size = listOptional.map(List::size).orElse(0);
assertEquals(6, size);
}
@Test
public void givenOptional_whenMapWorks_thenCorrect2() {
String name = "HoneyMoose";
Optional<String> nameOptional = Optional.of(name);
int len = nameOptional.map(String::length).orElse(0);
assertEquals(10, len);
}
@Test
public void givenOptional_whenMapWorksWithFilter_thenCorrect() {
String password = " password ";
Optional<String> passOpt = Optional.of(password);
boolean correctPassword = passOpt.filter(pass -> pass.equals("password")).isPresent();
assertFalse(correctPassword);
correctPassword = passOpt.map(String::trim).filter(pass -> pass.equals("password")).isPresent();
assertTrue(correctPassword);
}
// Transforming Value With flatMap()
@Test
public void givenOptional_whenFlatMapWorks_thenCorrect2() {
Person person = new Person("john", 26);
Optional<Person> personOptional = Optional.of(person);
Optional<Optional<String>> nameOptionalWrapper = personOptional.map(Person::getName);
Optional<String> nameOptional = nameOptionalWrapper.orElseThrow(IllegalArgumentException::new);
String name1 = nameOptional.orElseThrow(IllegalArgumentException::new);
assertEquals("john", name1);
String name = personOptional.flatMap(Person::getName).orElseThrow(IllegalArgumentException::new);
assertEquals("john", name);
}
@Test
public void givenOptional_whenFlatMapWorksWithFilter_thenCorrect() {
Person person = new Person("john", 26);
person.setPassword("password");
Optional<Person> personOptional = Optional.of(person);
String password = personOptional.flatMap(Person::getPassword).filter(cleanPass -> cleanPass.equals("password")).orElseThrow(IllegalArgumentException::new);
assertEquals("password", password);
}
// Default Value With orElse
@Test
public void whenOrElseWorks_thenCorrect() {
String nullName = null;
String name = Optional.ofNullable(nullName).orElse("john");
assertEquals("john", name);
}
// Default Value With orElseGet
@Test
public void whenOrElseGetWorks_thenCorrect() {
String nullName = null;
String name = Optional.ofNullable(nullName).orElseGet(() -> "john");
assertEquals("john", name);
}
@Test
public void whenOrElseGetAndOrElseOverlap_thenCorrect() {
String text = null;
LOG.debug("Using orElseGet:");
String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault);
assertEquals("Default Value", defaultText);
LOG.debug("Using orElse:");
defaultText = Optional.ofNullable(text).orElse(getMyDefault());
assertEquals("Default Value", defaultText);
}
@Test
public void whenOrElseGetAndOrElseDiffer_thenCorrect() {
String text = "Text present";
LOG.debug("Using orElseGet:");
String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault);
assertEquals("Text present", defaultText);
LOG.debug("Using orElse:");
defaultText = Optional.ofNullable(text).orElse(getMyDefault());
assertEquals("Text present", defaultText);
}
// Exceptions With orElseThrow
@Test(expected = IllegalArgumentException.class)
public void whenOrElseThrowWorks_thenCorrect() {
String nullName = null;
String name = Optional.ofNullable(nullName).orElseThrow(IllegalArgumentException::new);
}
@Test(expected = NoSuchElementException.class)
public void whenNoArgOrElseThrowWorks_thenCorrect() {
String nullName = null;
String name = Optional.ofNullable(nullName).orElseThrow();
}
public String getMyDefault() {
LOG.debug("Getting default value...");
return "Default Value";
}
}

View File

@ -14,4 +14,11 @@ This module contains articles about Java 11 core features
- [Negate a Predicate Method Reference with Java 11](https://www.baeldung.com/java-negate-predicate-method-reference)
- [Benchmark JDK Collections vs Eclipse Collections](https://www.baeldung.com/jdk-collections-vs-eclipse-collections)
- [Pre-compile Regex Patterns Into Pattern Objects](https://www.baeldung.com/java-regex-pre-compile)
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
- [Guide to Java 8s Collectors](https://www.baeldung.com/java-8-collectors)
- [New Features in Java 11](https://www.baeldung.com/java-11-new-features)
- [Getting the Java Version at Runtime](https://www.baeldung.com/get-java-version-runtime)
- [Invoking a SOAP Web Service in Java](https://www.baeldung.com/java-soap-web-service)
- [Java HTTPS Client Certificate Authentication](https://www.baeldung.com/java-https-client-certificate-authentication)
- [Call Methods at Runtime Using Java Reflection](https://www.baeldung.com/java-method-reflection)

View File

@ -1,5 +1,5 @@
package com.ossez.soap.ws.client.generated;
package com.ossez.ws.client.generated;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;

View File

@ -1,5 +1,5 @@
package com.ossez.soap.ws.client.generated;
package com.ossez.ws.client.generated;
import jakarta.jws.WebMethod;
import jakarta.jws.WebParam;

View File

@ -1,5 +1,5 @@
package com.ossez.soap.ws.client.generated;
package com.ossez.ws.client.generated;
import java.net.MalformedURLException;
import java.net.URL;

View File

@ -1,5 +1,5 @@
package com.ossez.soap.ws.client.generated;
package com.ossez.ws.client.generated;
import jakarta.xml.bind.annotation.XmlEnum;
import jakarta.xml.bind.annotation.XmlType;

View File

@ -1,5 +1,5 @@
package com.ossez.soap.ws.client.generated;
package com.ossez.ws.client.generated;
import jakarta.xml.bind.annotation.XmlRegistry;

View File

@ -1,2 +1,2 @@
@jakarta.xml.bind.annotation.XmlSchema(namespace = "http://server.ws.soap.baeldung.com/")
package com.ossez.soap.ws.client.generated;
package com.ossez.ws.client.generated;

View File

@ -1,4 +1,4 @@
package com.ossez.soap.ws.server;
package com.ossez.ws.server;
public class Country {
protected String name;

View File

@ -1,4 +1,4 @@
package com.ossez.soap.ws.server;
package com.ossez.ws.server;
import java.util.HashMap;
import java.util.Map;

View File

@ -1,4 +1,4 @@
package com.ossez.soap.ws.server;
package com.ossez.ws.server;
import jakarta.jws.WebMethod;
import jakarta.jws.WebService;

View File

@ -1,8 +1,8 @@
package com.ossez.soap.ws.server;
package com.ossez.ws.server;
import jakarta.jws.WebService;
@WebService(endpointInterface = "com.ossez.soap.ws.server.CountryService")
@WebService(endpointInterface = "com.ossez.ws.server.CountryService")
public class CountryServiceImpl implements CountryService {
private CountryRepository countryRepository = new CountryRepository();

View File

@ -1,4 +1,4 @@
package com.ossez.soap.ws.server;
package com.ossez.ws.server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -1,4 +1,4 @@
package com.ossez.soap.ws.server;
package com.ossez.ws.server;
public enum Currency {

View File

@ -25,7 +25,7 @@ class HttpClientIntegrationTest {
@BeforeAll
static void startServer() {
port = PortFactory.findFreePort();
mockServer = startClientAndServer(port);
mockServer = ClientAndServer.startClientAndServer(port);
mockServer.when(new org.mockserver.model.HttpRequest().withMethod("GET"))
.respond(new org.mockserver.model.HttpResponse()
.withStatusCode(HttpStatusCode.OK_200.code())

View File

@ -18,7 +18,6 @@
<module>core-java</module>
<module>core-java-8</module>
<module>core-java-11</module>
<module>core-java-11-2</module>
<module>core-java-annotations</module>
<module>core-java-collections</module>
<module>core-java-collections-2</module>