Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
commit
a04bf4ab51
7
.gitignore
vendored
7
.gitignore
vendored
@ -56,4 +56,9 @@ core-java-io/hard_link.txt
|
||||
core-java-io/target_link.txt
|
||||
core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF
|
||||
ethereum/logs/
|
||||
jmeter/src/main/resources/*-JMeter.csv
|
||||
jmeter/src/main/resources/*-JMeter.csv
|
||||
|
||||
**/node_modules/
|
||||
**/dist
|
||||
**/tmp
|
||||
**/out-tsc
|
||||
|
13
JGit/src/main/resources/logback.xml
Normal file
13
JGit/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
Twitter4J/src/main/resources/logback.xml
Normal file
13
Twitter4J/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
activejdbc/src/main/resources/logback.xml
Normal file
13
activejdbc/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
akka-streams/src/main/resources/logback.xml
Normal file
13
akka-streams/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -23,3 +23,4 @@
|
||||
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
|
||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
||||
- [Find the Middle Element of a Linked List](http://www.baeldung.com/java-linked-list-middle-element)
|
||||
- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation)
|
||||
|
13
algorithms/src/main/resources/logback.xml
Normal file
13
algorithms/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
animal-sniffer-mvn-plugin/src/main/resources/logback.xml
Normal file
13
animal-sniffer-mvn-plugin/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
annotations/annotation-user/src/main/resources/logback.xml
Normal file
13
annotations/annotation-user/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
antlr/src/main/resources/logback.xml
Normal file
13
antlr/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
2
apache-avro/README.md
Normal file
2
apache-avro/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Guide to Apache Avro](http://www.baeldung.com/java-apache-avro)
|
13
apache-avro/src/main/resources/logback.xml
Normal file
13
apache-avro/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-bval/src/main/resources/logback.xml
Normal file
13
apache-bval/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-cayenne/src/main/resources/logback.xml
Normal file
13
apache-cayenne/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-curator/src/main/resources/logback.xml
Normal file
13
apache-curator/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-cxf/cxf-aegis/src/main/resources/logback.xml
Normal file
13
apache-cxf/cxf-aegis/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-cxf/cxf-introduction/src/main/resources/logback.xml
Normal file
13
apache-cxf/cxf-introduction/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-cxf/cxf-spring/src/main/resources/logback.xml
Normal file
13
apache-cxf/cxf-spring/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-meecrowave/src/main/resources/logback.xml
Normal file
13
apache-meecrowave/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-opennlp/src/main/resources/logback.xml
Normal file
13
apache-opennlp/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-poi/src/main/resources/logback.xml
Normal file
13
apache-poi/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -8,9 +8,10 @@
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.2.RELEASE</version>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-1</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
13
apache-solrj/src/main/resources/logback.xml
Normal file
13
apache-solrj/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-spark/src/main/resources/logback.xml
Normal file
13
apache-spark/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-thrift/src/main/resources/logback.xml
Normal file
13
apache-thrift/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-tika/src/main/resources/logback.xml
Normal file
13
apache-tika/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
apache-zookeeper/src/main/resources/logback.xml
Normal file
13
apache-zookeeper/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
asciidoctor/src/main/resources/logback.xml
Normal file
13
asciidoctor/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
asm/src/main/resources/logback.xml
Normal file
13
asm/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
atomix/src/main/resources/logback.xml
Normal file
13
atomix/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
autovalue/src/main/resources/logback.xml
Normal file
13
autovalue/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
aws-lambda/src/main/resources/logback.xml
Normal file
13
aws-lambda/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
aws/src/main/resources/logback.xml
Normal file
13
aws/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
axon/src/main/resources/logback.xml
Normal file
13
axon/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
azure/src/main/resources/logback.xml
Normal file
13
azure/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
bootique/src/main/resources/logback.xml
Normal file
13
bootique/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -10,10 +10,10 @@
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.13.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-1</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
13
cas/cas-secured-app/src/main/resources/logback.xml
Normal file
13
cas/cas-secured-app/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
cdi/src/main/resources/logback.xml
Normal file
13
cdi/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
checker-plugin/src/main/resources/logback.xml
Normal file
13
checker-plugin/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
core-groovy/src/main/resources/logback.xml
Normal file
13
core-groovy/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -3,3 +3,4 @@
|
||||
|
||||
- [Java 10 LocalVariable Type-Inference](http://www.baeldung.com/java-10-local-variable-type-inference)
|
||||
- [Guide to Java 10](http://www.baeldung.com/java-10-overview)
|
||||
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
|
||||
|
13
core-java-10/src/main/resources/logback.xml
Normal file
13
core-java-10/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
13
core-java-11/src/main/resources/logback.xml
Normal file
13
core-java-11/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -56,3 +56,7 @@
|
||||
- [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference)
|
||||
- [Image to Base64 String Conversion](http://www.baeldung.com/java-base64-image-string)
|
||||
- [Calculate Age in Java](http://www.baeldung.com/java-get-age)
|
||||
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
|
||||
- [Increment Date in Java](http://www.baeldung.com/java-increment-date)
|
||||
- [Add Hours To a Date In Java](http://www.baeldung.com/java-add-hours-date)
|
||||
- [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time)
|
||||
|
13
core-java-8/src/main/resources/logback.xml
Normal file
13
core-java-8/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -0,0 +1,72 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseTimeZoneUnitTest {
|
||||
|
||||
/* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones */
|
||||
|
||||
String timeZone = "Asia/Singapore";
|
||||
|
||||
private static final String PATTERN = "E yyyy-MM-dd HH:mm:ss a";
|
||||
|
||||
@Test
|
||||
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava7_ThenTimeZoneIsSetSuccessfully() {
|
||||
Date nowUtc = new Date();
|
||||
TimeZone asiaSingapore = TimeZone.getTimeZone(timeZone);
|
||||
|
||||
Calendar nowAsiaSingapore = Calendar.getInstance(asiaSingapore);
|
||||
nowAsiaSingapore.setTime(nowUtc);
|
||||
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
|
||||
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
|
||||
|
||||
System.out.println(String.format("Java7: Time now in '%s' is '%s'", nowAsiaSingapore.getTimeZone()
|
||||
.getID(), simpleDateFormat.format(nowAsiaSingapore.getTime())));
|
||||
|
||||
Assert.assertEquals(nowUtc.toInstant().getEpochSecond(), nowAsiaSingapore.toInstant().getEpochSecond());
|
||||
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getTimeZone());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJava8_ThenTimeZoneIsSetSuccessfully() {
|
||||
Instant nowUtc = Instant.now();
|
||||
ZoneId asiaSingapore = ZoneId.of(timeZone);
|
||||
|
||||
ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore);
|
||||
|
||||
System.out.println(String.format("Java8: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
|
||||
nowAsiaSingapore.format(DateTimeFormatter.ofPattern(PATTERN))));
|
||||
|
||||
Assert.assertEquals(nowUtc.getEpochSecond(), nowAsiaSingapore.toEpochSecond());
|
||||
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateWithoutTimeZone_WhenSetTimeZoneUsingJodaTime_ThenTimeZoneIsSetSuccessfully() {
|
||||
org.joda.time.Instant nowUtc = org.joda.time.Instant.now();
|
||||
DateTimeZone asiaSingapore = DateTimeZone.forID(timeZone);
|
||||
|
||||
DateTime nowAsiaSingapore = nowUtc.toDateTime(asiaSingapore);
|
||||
|
||||
System.out.println(String.format("Joda-time: Time now in '%s' is '%s'", nowAsiaSingapore.getZone(),
|
||||
nowAsiaSingapore.toString(PATTERN)));
|
||||
|
||||
Assert.assertEquals(nowUtc.toInstant().getMillis(), nowAsiaSingapore.toInstant().getMillis());
|
||||
Assert.assertEquals(asiaSingapore, nowAsiaSingapore.getZone());
|
||||
}
|
||||
|
||||
}
|
@ -1,208 +0,0 @@
|
||||
package com.baeldung.list;
|
||||
|
||||
import static com.baeldung.list.RemoveAll.removeWithCallingRemoveUntilModifies;
|
||||
import static com.baeldung.list.RemoveAll.removeWithCollectingAndReturningRemainingElements;
|
||||
import static com.baeldung.list.RemoveAll.removeWithCollectingRemainingElementsAndAddingToOriginalList;
|
||||
import static com.baeldung.list.RemoveAll.removeWithForEachLoop;
|
||||
import static com.baeldung.list.RemoveAll.removeWithForLoopDecrementOnRemove;
|
||||
import static com.baeldung.list.RemoveAll.removeWithForLoopIncrementIfRemains;
|
||||
import static com.baeldung.list.RemoveAll.removeWithIterator;
|
||||
import static com.baeldung.list.RemoveAll.removeWithRemoveIf;
|
||||
import static com.baeldung.list.RemoveAll.removeWithStandardForLoopUsingIndex;
|
||||
import static com.baeldung.list.RemoveAll.removeWithStreamFilter;
|
||||
import static com.baeldung.list.RemoveAll.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RemoveAllUnitTest {
|
||||
|
||||
private List<Integer> list(Integer... elements) {
|
||||
return new ArrayList<>(Arrays.asList(elements));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithWhileLoopUsingPrimitiveElement_thenTheResultCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
assertThatThrownBy(() -> removeWithWhileLoopPrimitiveElement(list, valueToRemove))
|
||||
.isInstanceOf(IndexOutOfBoundsException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithWhileLoopUsingNonPrimitiveElement_thenTheResultCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithWhileLoopNonPrimitiveElement(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithWhileLoopStoringFirstOccurrenceIndex_thenTheResultCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithWhileLoopStoringFirstOccurrenceIndex(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCallingRemoveUntilModifies_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithCallingRemoveUntilModifies(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithoutDuplication_whenRemovingElementsWithStandardForLoopUsingIndex_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithStandardForLoopUsingIndex(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithAdjacentElements_whenRemovingElementsWithStandardForLoop_thenTheResultIsInCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithStandardForLoopUsingIndex(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(1, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithAdjacentElements_whenRemovingElementsWithForLoopAndDecrementOnRemove_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithForLoopDecrementOnRemove(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithAdjacentElements_whenRemovingElementsWithForLoopAndIncrementIfRemains_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithForLoopIncrementIfRemains(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithForEachLoop_thenExceptionIsThrown() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
assertThatThrownBy(() -> removeWithForEachLoop(list, valueToRemove))
|
||||
.isInstanceOf(ConcurrentModificationException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithIterator_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithIterator(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCollectingAndReturningRemainingElements_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
List<Integer> result = removeWithCollectingAndReturningRemainingElements(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCollectingRemainingAndAddingToOriginalList_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithCollectingRemainingElementsAndAddingToOriginalList(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithStreamFilter_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
List<Integer> result = removeWithStreamFilter(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCallingRemoveIf_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithRemoveIf(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
}
|
@ -31,3 +31,8 @@
|
||||
- [How to Store Duplicate Keys in a Map in Java?](http://www.baeldung.com/java-map-duplicate-keys)
|
||||
- [Getting the Size of an Iterable in Java](http://www.baeldung.com/java-iterable-size)
|
||||
- [Iterating Backward Through a List](http://www.baeldung.com/java-list-iterate-backwards)
|
||||
- [How to Filter a Collection in Java](http://www.baeldung.com/java-collection-filtering)
|
||||
- [Add Multiple Items to an Java ArrayList](http://www.baeldung.com/java-add-items-array-list)
|
||||
- [Remove the First Element from a List](http://www.baeldung.com/java-remove-first-element-from-list)
|
||||
- [How to Convert List to Map in Java](http://www.baeldung.com/java-list-to-map)
|
||||
- [Initializing HashSet at the Time of Construction](http://www.baeldung.com/java-initialize-hashset)
|
||||
|
@ -36,11 +36,6 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.collections</groupId>
|
||||
<artifactId>eclipse-collections-api</artifactId>
|
||||
<version>${eclipse.collections.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.collections</groupId>
|
||||
<artifactId>eclipse-collections</artifactId>
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.list;
|
||||
package com.baeldung.list.removeall;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Stream;
|
||||
import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNull {
|
||||
|
||||
/**
|
||||
* This method shows how to make a null safe stream from a collection through the use of
|
||||
* emptyIfNull() method from Apache Commons CollectionUtils library
|
||||
*
|
||||
* @param collection The collection that is to be converted into a stream
|
||||
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||
*/
|
||||
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||
return emptyIfNull(collection).stream();
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingJava8OptionalContainer {
|
||||
|
||||
/**
|
||||
* This method shows how to make a null safe stream from a collection through the use of
|
||||
* Java SE 8’s Optional Container
|
||||
*
|
||||
* @param collection The collection that is to be converted into a stream
|
||||
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||
*/
|
||||
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||
return Optional.ofNullable(collection)
|
||||
.map(Collection::stream)
|
||||
.orElseGet(Stream::empty);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingNullDereferenceCheck {
|
||||
|
||||
/**
|
||||
* This method shows how to make a null safe stream from a collection through the use of a check
|
||||
* to prevent null dereferences
|
||||
*
|
||||
* @param collection The collection that is to be converted into a stream
|
||||
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||
*/
|
||||
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||
return collection == null ? Stream.empty() : collection.stream();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baeldung.thread_safe_lifo;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
|
||||
/**
|
||||
* Deque Based Stack implementation.
|
||||
*/
|
||||
public class DequeBasedSynchronizedStack<T> {
|
||||
|
||||
// Internal Deque which gets decorated for synchronization.
|
||||
private ArrayDeque<T> dequeStore = new ArrayDeque<>();
|
||||
|
||||
public DequeBasedSynchronizedStack(int initialCapacity) {
|
||||
this.dequeStore = new ArrayDeque<>(initialCapacity);
|
||||
}
|
||||
|
||||
public DequeBasedSynchronizedStack() {
|
||||
|
||||
}
|
||||
|
||||
public synchronized T pop() {
|
||||
return this.dequeStore.pop();
|
||||
}
|
||||
|
||||
public synchronized void push(T element) {
|
||||
this.dequeStore.push(element);
|
||||
}
|
||||
|
||||
public synchronized T peek() {
|
||||
return this.dequeStore.peek();
|
||||
}
|
||||
|
||||
public synchronized int size() {
|
||||
return this.dequeStore.size();
|
||||
}
|
||||
}
|
13
core-java-collections/src/main/resources/logback.xml
Normal file
13
core-java-collections/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -0,0 +1,210 @@
|
||||
package com.baeldung.list.removeall;
|
||||
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithCallingRemoveUntilModifies;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithCollectingAndReturningRemainingElements;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithCollectingRemainingElementsAndAddingToOriginalList;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithForEachLoop;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithForLoopDecrementOnRemove;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithForLoopIncrementIfRemains;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithIterator;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithRemoveIf;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithStandardForLoopUsingIndex;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithStreamFilter;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithWhileLoopNonPrimitiveElement;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithWhileLoopPrimitiveElement;
|
||||
import static com.baeldung.list.removeall.RemoveAll.removeWithWhileLoopStoringFirstOccurrenceIndex;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RemoveAllUnitTest {
|
||||
|
||||
private List<Integer> list(Integer... elements) {
|
||||
return new ArrayList<>(Arrays.asList(elements));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithWhileLoopUsingPrimitiveElement_thenTheResultCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
assertThatThrownBy(() -> removeWithWhileLoopPrimitiveElement(list, valueToRemove))
|
||||
.isInstanceOf(IndexOutOfBoundsException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithWhileLoopUsingNonPrimitiveElement_thenTheResultCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithWhileLoopNonPrimitiveElement(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithWhileLoopStoringFirstOccurrenceIndex_thenTheResultCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithWhileLoopStoringFirstOccurrenceIndex(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCallingRemoveUntilModifies_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithCallingRemoveUntilModifies(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithoutDuplication_whenRemovingElementsWithStandardForLoopUsingIndex_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithStandardForLoopUsingIndex(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithAdjacentElements_whenRemovingElementsWithStandardForLoop_thenTheResultIsInCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithStandardForLoopUsingIndex(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(1, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithAdjacentElements_whenRemovingElementsWithForLoopAndDecrementOnRemove_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithForLoopDecrementOnRemove(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListWithAdjacentElements_whenRemovingElementsWithForLoopAndIncrementIfRemains_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithForLoopIncrementIfRemains(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithForEachLoop_thenExceptionIsThrown() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
assertThatThrownBy(() -> removeWithForEachLoop(list, valueToRemove))
|
||||
.isInstanceOf(ConcurrentModificationException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithIterator_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithIterator(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCollectingAndReturningRemainingElements_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
List<Integer> result = removeWithCollectingAndReturningRemainingElements(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCollectingRemainingAndAddingToOriginalList_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithCollectingRemainingElementsAndAddingToOriginalList(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithStreamFilter_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
List<Integer> result = removeWithStreamFilter(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(result).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenRemovingElementsWithCallingRemoveIf_thenTheResultIsCorrect() {
|
||||
// given
|
||||
List<Integer> list = list(1, 1, 2, 3);
|
||||
int valueToRemove = 1;
|
||||
|
||||
// when
|
||||
removeWithRemoveIf(list, valueToRemove);
|
||||
|
||||
// then
|
||||
assertThat(list).isEqualTo(list(2, 3));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest {
|
||||
|
||||
private final NullSafeCollectionStreamsUsingCommonsEmptyIfNull instance =
|
||||
new NullSafeCollectionStreamsUsingCommonsEmptyIfNull();
|
||||
@Test
|
||||
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
|
||||
Collection<String> collection = null;
|
||||
Stream<String> expResult = Stream.empty();
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
|
||||
Collection<String> collection = Arrays.asList("a", "b", "c");
|
||||
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
}
|
||||
|
||||
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
|
||||
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
|
||||
while (iter1.hasNext() && iter2.hasNext())
|
||||
assertEquals(iter1.next(), iter2.next());
|
||||
assert !iter1.hasNext() && !iter2.hasNext();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest {
|
||||
|
||||
private final NullSafeCollectionStreamsUsingJava8OptionalContainer instance =
|
||||
new NullSafeCollectionStreamsUsingJava8OptionalContainer();
|
||||
@Test
|
||||
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
|
||||
Collection<String> collection = null;
|
||||
Stream<String> expResult = Stream.empty();
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
|
||||
Collection<String> collection = Arrays.asList("a", "b", "c");
|
||||
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
}
|
||||
|
||||
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
|
||||
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
|
||||
while (iter1.hasNext() && iter2.hasNext())
|
||||
assertEquals(iter1.next(), iter2.next());
|
||||
assert !iter1.hasNext() && !iter2.hasNext();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest {
|
||||
|
||||
private final NullSafeCollectionStreamsUsingNullDereferenceCheck instance =
|
||||
new NullSafeCollectionStreamsUsingNullDereferenceCheck();
|
||||
|
||||
@Test
|
||||
public void whenCollectionIsNull_thenExpectAnEmptyStream() {
|
||||
Collection<String> collection = null;
|
||||
Stream<String> expResult = Stream.empty();
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() {
|
||||
Collection<String> collection = Arrays.asList("a", "b", "c");
|
||||
Stream<String> expResult = Arrays.stream(new String[] { "a", "b", "c" });
|
||||
Stream<String> result = instance.collectionAsStream(collection);
|
||||
assertStreamEquals(expResult, result);
|
||||
}
|
||||
|
||||
private static void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
|
||||
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
|
||||
while (iter1.hasNext() && iter2.hasNext())
|
||||
assertEquals(iter1.next(), iter2.next());
|
||||
assert !iter1.hasNext() && !iter2.hasNext();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
package com.baeldung.stack_tests;
|
||||
|
||||
import com.baeldung.thread_safe_lifo.DequeBasedSynchronizedStack;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
|
||||
import static java.util.stream.IntStream.range;
|
||||
|
||||
/**
|
||||
* Correctness tests for Stack in multi threaded environment.
|
||||
*/
|
||||
public class MultithreadingCorrectnessStackUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedDeque_whenExecutedParallel_thenWorkRight() {
|
||||
|
||||
DequeBasedSynchronizedStack<Integer> deque = new DequeBasedSynchronizedStack<>();
|
||||
|
||||
// Serial execution of push on ConcurrentLinkedQueue will always result in correct execution.
|
||||
range(1, 10000).forEach(value -> deque.push(value));
|
||||
|
||||
int sum = 0;
|
||||
while(deque.peek() != null) {
|
||||
sum += deque.pop();
|
||||
}
|
||||
|
||||
Assert.assertEquals(49995000, sum);
|
||||
|
||||
// Parallel execution of push on ConcurrentLinkedQueue will always result in correct execution.
|
||||
range(1, 10000).parallel().forEach(value -> deque.push(value));
|
||||
|
||||
sum = 0;
|
||||
while(deque.peek() != null) {
|
||||
sum += deque.pop();
|
||||
}
|
||||
|
||||
Assert.assertEquals(49995000, sum);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentLinkedQueue_whenExecutedParallel_thenWorkRight() {
|
||||
|
||||
ConcurrentLinkedDeque<Integer> deque = new ConcurrentLinkedDeque<>();
|
||||
|
||||
// Serial execution of push on ConcurrentLinkedQueue will always result in correct execution.
|
||||
range(1, 10000).forEach(value -> deque.push(value));
|
||||
|
||||
int sum = 0;
|
||||
while(deque.peek() != null) {
|
||||
sum += deque.pop();
|
||||
}
|
||||
|
||||
Assert.assertEquals(49995000, sum);
|
||||
|
||||
// Parallel execution of push on ConcurrentLinkedQueue will always result in correct execution.
|
||||
range(1, 10000).parallel().forEach(value -> deque.push(value));
|
||||
|
||||
sum = 0;
|
||||
while(deque.peek() != null) {
|
||||
sum += deque.pop();
|
||||
}
|
||||
|
||||
Assert.assertEquals(49995000, sum);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayDeque_whenExecutedParallel_thenShouldFail() {
|
||||
|
||||
ArrayDeque<Integer> deque = new ArrayDeque<>();
|
||||
|
||||
// Serial execution of push on ArrayDeque will always result in correct execution.
|
||||
range(1, 10000).forEach(value -> deque.push(value));
|
||||
|
||||
int sum = 0;
|
||||
while(deque.peek() != null) {
|
||||
sum += deque.pop();
|
||||
}
|
||||
|
||||
Assert.assertEquals(49995000, sum);
|
||||
|
||||
// Parallel execution of push on ArrayDeque will not result in correct execution.
|
||||
range(1, 10000).parallel().forEach(value -> deque.push(value));
|
||||
|
||||
sum = 0;
|
||||
while(deque.peek() != null) {
|
||||
sum += deque.pop();
|
||||
}
|
||||
|
||||
// This shouldn't happen.
|
||||
if(sum == 49995000) {
|
||||
System.out.println("Something wrong in the environment, Please try some big value and check");
|
||||
// To safe-guard build without test failures.
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.assertNotEquals(49995000, sum);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.baeldung.stack_tests;
|
||||
|
||||
import com.baeldung.thread_safe_lifo.DequeBasedSynchronizedStack;
|
||||
import com.google.common.collect.Streams;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Stack;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.IntStream.range;
|
||||
|
||||
/**
|
||||
* These tests are to understand the Stack implementation in Java Collections.
|
||||
*/
|
||||
public class StackUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenStack_whenPushPopPeek_thenWorkRight() {
|
||||
Stack<String> namesStack = new Stack<>();
|
||||
|
||||
namesStack.push("Bill Gates");
|
||||
namesStack.push("Elon Musk");
|
||||
|
||||
Assert.assertEquals("Elon Musk", namesStack.peek());
|
||||
Assert.assertEquals("Elon Musk", namesStack.pop());
|
||||
Assert.assertEquals("Bill Gates", namesStack.pop());
|
||||
|
||||
Assert.assertEquals(0, namesStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSynchronizedDeque_whenPushPopPeek_thenWorkRight() {
|
||||
DequeBasedSynchronizedStack<String> namesStack = new DequeBasedSynchronizedStack<>();
|
||||
|
||||
namesStack.push("Bill Gates");
|
||||
namesStack.push("Elon Musk");
|
||||
|
||||
Assert.assertEquals("Elon Musk", namesStack.peek());
|
||||
Assert.assertEquals("Elon Musk", namesStack.pop());
|
||||
Assert.assertEquals("Bill Gates", namesStack.pop());
|
||||
|
||||
Assert.assertEquals(0, namesStack.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConcurrentLinkedDeque_whenPushPopPeek_thenWorkRight() {
|
||||
ConcurrentLinkedDeque<String> namesStack = new ConcurrentLinkedDeque<>();
|
||||
|
||||
namesStack.push("Bill Gates");
|
||||
namesStack.push("Elon Musk");
|
||||
|
||||
Assert.assertEquals("Elon Musk", namesStack.peek());
|
||||
Assert.assertEquals("Elon Musk", namesStack.pop());
|
||||
Assert.assertEquals("Bill Gates", namesStack.pop());
|
||||
|
||||
Assert.assertEquals(0, namesStack.size());
|
||||
}
|
||||
}
|
@ -38,3 +38,4 @@
|
||||
- [Priority-based Job Scheduling in Java](http://www.baeldung.com/java-priority-job-schedule)
|
||||
- [A Custom Spring SecurityConfigurer](http://www.baeldung.com/spring-security-custom-configurer)
|
||||
- [Life Cycle of a Thread in Java](http://www.baeldung.com/java-thread-lifecycle)
|
||||
- [Runnable vs. Callable in Java](http://www.baeldung.com/java-runnable-callable)
|
||||
|
@ -0,0 +1,48 @@
|
||||
package com.baeldung.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FilenameFilterManualTest {
|
||||
|
||||
private static File directory;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupClass() {
|
||||
directory = new File(FilenameFilterManualTest.class.getClassLoader()
|
||||
.getResource("testFolder")
|
||||
.getFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilteringFilesEndingWithJson_thenEqualExpectedFiles() {
|
||||
FilenameFilter filter = (dir, name) -> name.endsWith(".json");
|
||||
|
||||
String[] expectedFiles = { "people.json", "students.json" };
|
||||
String[] actualFiles = directory.list(filter);
|
||||
|
||||
Assert.assertArrayEquals(expectedFiles, actualFiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilteringFilesEndingWithXml_thenEqualExpectedFiles() {
|
||||
Predicate<String> predicate = (name) -> name.endsWith(".xml");
|
||||
|
||||
String[] expectedFiles = { "teachers.xml", "workers.xml" };
|
||||
List<String> files = Arrays.stream(directory.list())
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
String[] actualFiles = files.toArray(new String[files.size()]);
|
||||
|
||||
Assert.assertArrayEquals(expectedFiles, actualFiles);
|
||||
}
|
||||
|
||||
}
|
1
core-java-io/src/test/resources/testFolder/people.json
Normal file
1
core-java-io/src/test/resources/testFolder/people.json
Normal file
@ -0,0 +1 @@
|
||||
{}
|
1
core-java-io/src/test/resources/testFolder/students.json
Normal file
1
core-java-io/src/test/resources/testFolder/students.json
Normal file
@ -0,0 +1 @@
|
||||
{}
|
1
core-java-io/src/test/resources/testFolder/teachers.xml
Normal file
1
core-java-io/src/test/resources/testFolder/teachers.xml
Normal file
@ -0,0 +1 @@
|
||||
<xml></xml>
|
1
core-java-io/src/test/resources/testFolder/workers.xml
Normal file
1
core-java-io/src/test/resources/testFolder/workers.xml
Normal file
@ -0,0 +1 @@
|
||||
<xml></xml>
|
13
core-java-persistence/src/main/resources/logback.xml
Normal file
13
core-java-persistence/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -168,3 +168,6 @@
|
||||
- [Encrypting and Decrypting Files in Java](http://www.baeldung.com/java-cipher-input-output-stream)
|
||||
- [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object)
|
||||
- [Exception Handling in Java](http://www.baeldung.com/java-exceptions)
|
||||
- [Guide to Java Instrumentation](http://www.baeldung.com/java-instrumentation)
|
||||
- [Getting a File’s Mime Type in Java](http://www.baeldung.com/java-file-mime-type)
|
||||
- [Common Java Exceptions](http://www.baeldung.com/java-common-exceptions)
|
||||
|
@ -0,0 +1,212 @@
|
||||
package com.baeldung.exceptionhandling;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Exceptions {
|
||||
|
||||
private final static Logger logger = Logger.getLogger("ExceptionLogging");
|
||||
|
||||
public static List<Player> getPlayers() throws IOException {
|
||||
Path path = Paths.get("players.dat");
|
||||
List<String> players = Files.readAllLines(path);
|
||||
|
||||
return players.stream()
|
||||
.map(Player::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Player> loadAllPlayers(String playersFile) throws IOException{
|
||||
try {
|
||||
throw new IOException();
|
||||
} catch(IOException ex) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScoreThrows(String playerFile) throws FileNotFoundException {
|
||||
Scanner contents = new Scanner(new File(playerFile));
|
||||
return Integer.parseInt(contents.nextLine());
|
||||
}
|
||||
|
||||
public int getPlayerScoreTryCatch(String playerFile) {
|
||||
try {
|
||||
Scanner contents = new Scanner(new File(playerFile));
|
||||
return Integer.parseInt(contents.nextLine());
|
||||
} catch (FileNotFoundException noFile) {
|
||||
throw new IllegalArgumentException("File not found");
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScoreTryCatchRecovery(String playerFile) {
|
||||
try {
|
||||
Scanner contents = new Scanner(new File(playerFile));
|
||||
return Integer.parseInt(contents.nextLine());
|
||||
} catch ( FileNotFoundException noFile ) {
|
||||
logger.warning("File not found, resetting score.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScoreFinally(String playerFile) throws FileNotFoundException {
|
||||
Scanner contents = null;
|
||||
try {
|
||||
contents = new Scanner(new File(playerFile));
|
||||
return Integer.parseInt(contents.nextLine());
|
||||
} finally {
|
||||
if (contents != null) {
|
||||
contents.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScoreTryWithResources(String playerFile) {
|
||||
try (Scanner contents = new Scanner(new File(playerFile))) {
|
||||
return Integer.parseInt(contents.nextLine());
|
||||
} catch (FileNotFoundException e ) {
|
||||
logger.warning("File not found, resetting score.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScoreMultipleCatchBlocks(String playerFile) {
|
||||
try (Scanner contents = new Scanner(new File(playerFile))) {
|
||||
return Integer.parseInt(contents.nextLine());
|
||||
} catch (IOException e) {
|
||||
logger.warning("Player file wouldn't load!");
|
||||
return 0;
|
||||
} catch (NumberFormatException e) {
|
||||
logger.warning("Player file was corrupted!");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScoreMultipleCatchBlocksAlternative(String playerFile) {
|
||||
try (Scanner contents = new Scanner(new File(playerFile)) ) {
|
||||
return Integer.parseInt(contents.nextLine());
|
||||
} catch (FileNotFoundException e) {
|
||||
logger.warning("Player file not found!");
|
||||
return 0;
|
||||
} catch (IOException e) {
|
||||
logger.warning("Player file wouldn't load!");
|
||||
return 0;
|
||||
} catch (NumberFormatException e) {
|
||||
logger.warning("Player file was corrupted!");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScoreUnionCatchBlocks(String playerFile) {
|
||||
try (Scanner contents = new Scanner(new File(playerFile))) {
|
||||
return Integer.parseInt(contents.nextLine());
|
||||
} catch (IOException | NumberFormatException e) {
|
||||
logger.warning("Failed to load score!");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Player> loadAllPlayersThrowingChecked(String playersFile) throws TimeoutException {
|
||||
boolean tooLong = true;
|
||||
|
||||
while (!tooLong) {
|
||||
// ... potentially long operation
|
||||
}
|
||||
throw new TimeoutException("This operation took too long");
|
||||
}
|
||||
|
||||
public List<Player> loadAllPlayersThrowingUnchecked(String playersFile) throws TimeoutException {
|
||||
if(!isFilenameValid(playersFile)) {
|
||||
throw new IllegalArgumentException("Filename isn't valid!");
|
||||
}
|
||||
return null;
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
public List<Player> loadAllPlayersWrapping(String playersFile) throws IOException {
|
||||
try {
|
||||
throw new IOException();
|
||||
} catch (IOException io) {
|
||||
throw io;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Player> loadAllPlayersRethrowing(String playersFile) throws PlayerLoadException {
|
||||
try {
|
||||
throw new IOException();
|
||||
} catch (IOException io) {
|
||||
throw new PlayerLoadException(io);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Player> loadAllPlayersThrowable(String playersFile) {
|
||||
try {
|
||||
throw new NullPointerException();
|
||||
} catch ( Throwable t ) {
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
class FewerExceptions extends Exceptions {
|
||||
@Override
|
||||
public List<Player> loadAllPlayers(String playersFile) { //can't add "throws MyCheckedException
|
||||
return null;
|
||||
// overridden
|
||||
}
|
||||
}
|
||||
|
||||
public void throwAsGotoAntiPattern() {
|
||||
try {
|
||||
// bunch of code
|
||||
throw new MyException();
|
||||
// second bunch of code
|
||||
} catch ( MyException e ) {
|
||||
// third bunch of code
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScoreSwallowingExceptionAntiPattern(String playerFile) {
|
||||
try {
|
||||
// ...
|
||||
} catch (Exception e) {} // <== catch and swallow
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getPlayerScoreSwallowingExceptionAntiPatternAlternative(String playerFile) {
|
||||
try {
|
||||
// ...
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getPlayerScoreSwallowingExceptionAntiPatternAlternative2(String playerFile) throws PlayerScoreException {
|
||||
try {
|
||||
throw new IOException();
|
||||
} catch (IOException e) {
|
||||
throw new PlayerScoreException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public int getPlayerScoreReturnInFinallyAntiPattern(String playerFile) {
|
||||
int score = 0;
|
||||
try {
|
||||
throw new IOException();
|
||||
} finally {
|
||||
return score; // <== the IOException is dropped
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isFilenameValid(String name) {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.baeldung.exceptionhandling;
|
||||
|
||||
public class MyException extends Throwable {
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.baeldung.exceptionhandling;
|
||||
|
||||
public class Player {
|
||||
|
||||
public int id;
|
||||
public String name;
|
||||
|
||||
public Player(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.baeldung.exceptionhandling;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class PlayerLoadException extends Exception {
|
||||
|
||||
public PlayerLoadException(IOException io) {
|
||||
super(io);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.exceptionhandling;
|
||||
|
||||
public class PlayerScoreException extends Exception {
|
||||
|
||||
public PlayerScoreException(Exception e) {
|
||||
super(e);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.exceptionhandling;
|
||||
|
||||
public class TimeoutException extends Exception {
|
||||
|
||||
public TimeoutException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.fileparser;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BufferedReaderExample {
|
||||
|
||||
protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException {
|
||||
|
||||
ArrayList<String> result = new ArrayList<>();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
|
||||
|
||||
while (br.ready()) {
|
||||
result.add(br.readLine());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.fileparser;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class FileReaderExample {
|
||||
|
||||
protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException {
|
||||
|
||||
ArrayList<String> result = new ArrayList<>();
|
||||
|
||||
try (FileReader f = new FileReader(filename)) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
while (f.ready()) {
|
||||
char c = (char) f.read();
|
||||
if (c == '\n') {
|
||||
result.add(sb.toString());
|
||||
sb = new StringBuffer();
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
result.add(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.fileparser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class FilesReadLinesExample {
|
||||
|
||||
protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException {
|
||||
|
||||
List<String> result = Files.readAllLines(Paths.get(filename));
|
||||
|
||||
return (ArrayList<String>) result;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.fileparser;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ScannerIntExample {
|
||||
|
||||
protected static ArrayList<Integer> generateArrayListFromFile(String filename) throws IOException {
|
||||
|
||||
ArrayList<Integer> result = new ArrayList<>();
|
||||
|
||||
try (Scanner s = new Scanner(new FileReader(filename))) {
|
||||
|
||||
while (s.hasNext()) {
|
||||
result.add(s.nextInt());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.fileparser;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ScannerStringExample {
|
||||
|
||||
protected static ArrayList<String> generateArrayListFromFile(String filename) throws IOException {
|
||||
|
||||
ArrayList<String> result = new ArrayList<>();
|
||||
|
||||
try (Scanner s = new Scanner(new FileReader(filename))) {
|
||||
|
||||
while (s.hasNext()) {
|
||||
result.add(s.nextLine());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package com.baeldung.logging;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class LogUsingSlf4J {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Logger logger = LoggerFactory.getLogger(LogUsingSlf4J.class);
|
||||
|
||||
logger.error("An exception occurred!");
|
||||
logger.error("An exception occurred!", new Exception("Custom exception"));
|
||||
logger.error("{}, {}! An exception occurred!", "Hello", "World", new Exception("Custom exception"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.objectsize;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
|
||||
public class InstrumentationAgent {
|
||||
private static volatile Instrumentation globalInstrumentation;
|
||||
|
||||
public static void premain(final String agentArgs, final Instrumentation inst) {
|
||||
globalInstrumentation = inst;
|
||||
}
|
||||
|
||||
public static long getObjectSize(final Object object) {
|
||||
if (globalInstrumentation == null) {
|
||||
throw new IllegalStateException("Agent not initialized.");
|
||||
}
|
||||
return globalInstrumentation.getObjectSize(object);
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.baeldung.objectsize;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class InstrumentationExample {
|
||||
|
||||
public static void printObjectSize(Object object) {
|
||||
System.out.println("Object type: " + object.getClass() + ", size: " + InstrumentationAgent.getObjectSize(object) + " bytes");
|
||||
}
|
||||
|
||||
public static void main(String[] arguments) {
|
||||
String emptyString = "";
|
||||
String string = "Estimating Object Size Using Instrumentation";
|
||||
String[] stringArray = { emptyString, string, "com.baeldung" };
|
||||
String[] anotherStringArray = new String[100];
|
||||
List<String> stringList = new ArrayList<>();
|
||||
StringBuilder stringBuilder = new StringBuilder(100);
|
||||
int maxIntPrimitive = Integer.MAX_VALUE;
|
||||
int minIntPrimitive = Integer.MIN_VALUE;
|
||||
Integer maxInteger = Integer.MAX_VALUE;
|
||||
Integer minInteger = Integer.MIN_VALUE;
|
||||
long zeroLong = 0L;
|
||||
double zeroDouble = 0.0;
|
||||
boolean falseBoolean = false;
|
||||
Object object = new Object();
|
||||
|
||||
class EmptyClass {
|
||||
}
|
||||
EmptyClass emptyClass = new EmptyClass();
|
||||
|
||||
class StringClass {
|
||||
public String s;
|
||||
}
|
||||
StringClass stringClass = new StringClass();
|
||||
|
||||
printObjectSize(emptyString);
|
||||
printObjectSize(string);
|
||||
printObjectSize(stringArray);
|
||||
printObjectSize(anotherStringArray);
|
||||
printObjectSize(stringList);
|
||||
printObjectSize(stringBuilder);
|
||||
printObjectSize(maxIntPrimitive);
|
||||
printObjectSize(minIntPrimitive);
|
||||
printObjectSize(maxInteger);
|
||||
printObjectSize(minInteger);
|
||||
printObjectSize(zeroLong);
|
||||
printObjectSize(zeroDouble);
|
||||
printObjectSize(falseBoolean);
|
||||
printObjectSize(Day.TUESDAY);
|
||||
printObjectSize(object);
|
||||
printObjectSize(emptyClass);
|
||||
printObjectSize(stringClass);
|
||||
}
|
||||
|
||||
public enum Day {
|
||||
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
Premain-class: com.baeldung.objectsize.InstrumentationAgent
|
@ -0,0 +1,42 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class RemoveFirstElementUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenStringArray_whenRemovingFirstElement_thenArrayIsSmallerAndElementRemoved() {
|
||||
String[] stringArray = {"foo", "bar", "baz"};
|
||||
|
||||
String[] modifiedArray = Arrays.copyOfRange(stringArray, 1, stringArray.length);
|
||||
|
||||
assertThat(modifiedArray.length).isEqualTo(2);
|
||||
assertThat(modifiedArray[0]).isEqualTo("bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayList_whenRemovingFirstElement_thenListSmallerAndElementRemoved() {
|
||||
List<String> stringList = new ArrayList<>(Arrays.asList("foo", "bar", "baz"));
|
||||
stringList.remove(0);
|
||||
|
||||
assertThat(stringList.size()).isEqualTo(2);
|
||||
assertThat(stringList.get(0)).isEqualTo("bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedList_whenRemovingFirstElement_thenListSmallerAndElementRemoved() {
|
||||
List<String> stringList = new LinkedList<>(Arrays.asList("foo", "bar", "baz"));
|
||||
stringList.remove(0);
|
||||
|
||||
assertThat(stringList.size()).isEqualTo(2);
|
||||
assertThat(stringList.get(0)).isEqualTo("bar");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package com.baeldung.exceptionhandling;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
public class ExceptionsUnitTest {
|
||||
|
||||
Exceptions exceptions = new Exceptions();
|
||||
|
||||
@Test
|
||||
public void getPlayers() {
|
||||
assertThatThrownBy(() -> exceptions.getPlayers())
|
||||
.isInstanceOf(NoSuchFileException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayers() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayers(""))
|
||||
.isInstanceOf(IOException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPlayerScoreThrows() {
|
||||
assertThatThrownBy(() -> exceptions.getPlayerScoreThrows(""))
|
||||
.isInstanceOf(FileNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPlayerScoreTryCatch() {
|
||||
assertThatThrownBy(() -> exceptions.getPlayerScoreTryCatch(""))
|
||||
.isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPlayerScoreFinally() {
|
||||
assertThatThrownBy(() -> exceptions.getPlayerScoreFinally(""))
|
||||
.isInstanceOf(FileNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayersThrowingChecked() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayersThrowingChecked(""))
|
||||
.isInstanceOf(TimeoutException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayersThrowingUnchecked() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayersThrowingUnchecked(""))
|
||||
.isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayersWrapping() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayersWrapping(""))
|
||||
.isInstanceOf(IOException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayersRethrowing() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayersRethrowing(""))
|
||||
.isInstanceOf(PlayerLoadException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayersThrowable() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayersThrowable(""))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void throwAsGotoAntiPattern() {
|
||||
assertThatThrownBy(() -> exceptions.throwAsGotoAntiPattern())
|
||||
.isInstanceOf(MyException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPlayerScoreSwallowingExceptionAntiPatternAlternative2() {
|
||||
assertThatThrownBy(() -> exceptions.getPlayerScoreSwallowingExceptionAntiPatternAlternative2(""))
|
||||
.isInstanceOf(PlayerScoreException.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.fileparser;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BufferedReaderUnitTest {
|
||||
|
||||
protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt";
|
||||
|
||||
@Test
|
||||
public void whenParsingExistingTextFile_thenGetArrayList() throws IOException {
|
||||
List<String> lines = BufferedReaderExample.generateArrayListFromFile(TEXT_FILENAME);
|
||||
assertTrue("File does not has 2 lines", lines.size() == 2);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.fileparser;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FileReaderUnitTest {
|
||||
|
||||
protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt";
|
||||
|
||||
@Test
|
||||
public void whenParsingExistingTextFile_thenGetArrayList() throws IOException {
|
||||
List<String> lines = FileReaderExample.generateArrayListFromFile(TEXT_FILENAME);
|
||||
assertTrue("File does not has 2 lines", lines.size() == 2);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.fileparser;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FilesReadAllLinesUnitTest {
|
||||
|
||||
protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt";
|
||||
|
||||
@Test
|
||||
public void whenParsingExistingTextFile_thenGetArrayList() throws IOException {
|
||||
List<String> lines = FilesReadLinesExample.generateArrayListFromFile(TEXT_FILENAME);
|
||||
assertTrue("File does not has 2 lines", lines.size() == 2);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.fileparser;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ScannerIntUnitTest {
|
||||
|
||||
protected static final String NUMBER_FILENAME = "src/test/resources/sampleNumberFile.txt";
|
||||
|
||||
@Test
|
||||
public void whenParsingExistingTextFile_thenGetIntArrayList() throws IOException {
|
||||
List<Integer> numbers = ScannerIntExample.generateArrayListFromFile(NUMBER_FILENAME);
|
||||
assertTrue("File does not has 2 lines", numbers.size() == 2);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.fileparser;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ScannerStringUnitTest {
|
||||
|
||||
protected static final String TEXT_FILENAME = "src/test/resources/sampleTextFile.txt";
|
||||
|
||||
@Test
|
||||
public void whenParsingExistingTextFile_thenGetArrayList() throws IOException {
|
||||
List<String> lines = ScannerStringExample.generateArrayListFromFile(TEXT_FILENAME);
|
||||
assertTrue("File does not has 2 lines", lines.size() == 2);
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package com.baeldung.java.listInitialization;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import lombok.extern.java.Log;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
@Log
|
||||
public class ListInitializationUnitTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void givenAnonymousInnerClass_thenInitialiseList() {
|
||||
List<String> cities = new ArrayList() {
|
||||
// Inside declaration of the subclass
|
||||
|
||||
// You can have multiple initializer block
|
||||
{
|
||||
log.info("Inside the first initializer block.");
|
||||
}
|
||||
|
||||
{
|
||||
log.info("Inside the second initializer block.");
|
||||
add("New York");
|
||||
add("Rio");
|
||||
add("Tokyo");
|
||||
}
|
||||
};
|
||||
|
||||
Assert.assertTrue(cities.contains("New York"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysAsList_thenInitialiseList() {
|
||||
List<String> list = Arrays.asList("foo", "bar");
|
||||
|
||||
Assert.assertTrue(list.contains("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysAsList_whenAdd_thenUnsupportedException() {
|
||||
List<String> list = Arrays.asList("foo", "bar");
|
||||
|
||||
exception.expect(UnsupportedOperationException.class);
|
||||
list.add("baz");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayAsList_whenCreated_thenShareReference() {
|
||||
String[] array = { "foo", "bar" };
|
||||
List<String> list = Arrays.asList(array);
|
||||
array[0] = "baz";
|
||||
Assert.assertEquals("baz", list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStream_thenInitializeList() {
|
||||
List<String> list = Stream.of("foo", "bar")
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.assertTrue(list.contains("foo"));
|
||||
}
|
||||
}
|
2
core-java/src/test/resources/sampleNumberFile.txt
Normal file
2
core-java/src/test/resources/sampleNumberFile.txt
Normal file
@ -0,0 +1,2 @@
|
||||
111
|
||||
222
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user