Merge pull request 'Java 对象如何安全的 toString' (#36) from assertion into main

Reviewed-on: https://src.ossez.com/cwiki-us-docs/java-tutorials/pulls/36
This commit is contained in:
honeymoose 2022-06-09 01:19:21 -04:00
commit 46f9cae81e
29 changed files with 116 additions and 32 deletions

View File

@ -0,0 +1,55 @@
<?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-strings</artifactId>
<name>core-java-strings</name>
<packaging>jar</packaging>
<parent>
<groupId>com.ossez</groupId>
<artifactId>parent-modules</artifactId>
<version>0.0.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu4j.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-strings</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<icu4j.version>61.1</icu4j.version>
<maven.compiler.release>15</maven.compiler.release>
</properties>
</project>

View File

@ -1,4 +1,4 @@
package com.baeldung.java9.compactstring;
package com.ossez.java9.compactstring;
import java.util.List;
import java.util.stream.IntStream;

View File

@ -1,4 +1,4 @@
package com.baeldung.localization;
package com.ossez.localization;
import java.text.ParseException;
import java.util.Arrays;

View File

@ -1,4 +1,4 @@
package com.baeldung.localization;
package com.ossez.localization;
import com.ibm.icu.text.MessageFormat;

View File

@ -1,4 +1,4 @@
package com.baeldung.localization;
package com.ossez.localization;
import java.text.MessageFormat;
import java.util.Date;

View File

@ -1,4 +1,4 @@
package com.baeldung.localization;
package com.ossez.localization;
import java.util.List;
import java.util.Locale;

View File

@ -1,7 +1,8 @@
package com.baeldung.multiline;
package com.ossez.multiline;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.io.PrintWriter;
@ -65,10 +66,11 @@ public class MultiLineString {
}
public String textBlocks() {
return """
Get busy living
or
get busy dying.
--Stephen King""";
// return """
// Get busy living
// or
// get busy dying.
// --Stephen King""";
return StringUtils.EMPTY;
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.stringperformance;
package com.ossez.stringperformance;
import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;

View File

@ -0,0 +1,27 @@
package com.ossez.interview;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import java.util.Objects;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
public class StringNullCheckUnitTest {
@Test
public void toString_null_safe() {
Object obj = null;
ObjectUtils.toString(obj, StringUtils.EMPTY);
ObjectUtils.toString(obj, ()->StringUtils.EMPTY);
assertThat("null").isEqualTo(Objects.toString(obj));
assertThat("").isEqualTo(Objects.toString(obj,StringUtils.EMPTY));
assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->{
obj.toString();
});
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.interview;
package com.ossez.interview;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package com.baeldung.randomstrings;
package com.ossez.randomstrings;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package com.baeldung.stringnotempty;
package com.ossez.stringnotempty;
import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;

View File

@ -1,4 +1,4 @@
package com.baeldung.stringpool;
package com.ossez.stringpool;
import static org.assertj.core.api.Assertions.assertThat;

View File

@ -1,4 +1,4 @@
package com.baeldung.stringtoolong;
package com.ossez.stringtoolong;
import static org.assertj.core.api.Assertions.assertThat;