Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-12800

This commit is contained in:
amit2103 2019-05-04 11:38:44 +05:30
commit 8193cd26bf
41 changed files with 788 additions and 8 deletions

25
core-java-8-2/.gitignore vendored Normal file
View File

@ -0,0 +1,25 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml

View File

@ -21,9 +21,16 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<icu.version>64.2</icu.version>
</properties>
<dependencies>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu.version}</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,18 @@
package com.baeldung.jarArguments;
public class JarExample {
public static void main(String[] args) {
System.out.println("Hello Baeldung Reader in JarExample!");
if(args == null) {
System.out.println("You have not provided any arguments!");
}else {
System.out.println("There are "+args.length+" argument(s)!");
for(int i=0; i<args.length; i++) {
System.out.println("Argument("+(i+1)+"):" + args[i]);
}
}
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.localization;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class App {
/**
* Runs all available formatter
* @throws ParseException
*/
public static void main(String[] args) {
List<Locale> locales = Arrays.asList(new Locale[] { Locale.UK, Locale.ITALY, Locale.FRANCE, Locale.forLanguageTag("pl-PL") });
Localization.run(locales);
JavaSEFormat.run(locales);
ICUFormat.run(locales);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.localization;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import com.ibm.icu.text.MessageFormat;
public class ICUFormat {
public static String getLabel(Locale locale, Object[] data) {
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
String format = bundle.getString("label-icu");
MessageFormat formatter = new MessageFormat(format, locale);
return formatter.format(data);
}
public static void run(List<Locale> locales) {
System.out.println("ICU formatter");
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 0 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 1 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 2 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Alice", "female", 3 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 0 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 1 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 2 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { "Bob", "male", 3 })));
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.localization;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class JavaSEFormat {
public static String getLabel(Locale locale, Object[] data) {
ResourceBundle bundle = ResourceBundle.getBundle("formats", locale);
final String pattern = bundle.getString("label");
final MessageFormat formatter = new MessageFormat(pattern, locale);
return formatter.format(data);
}
public static void run(List<Locale> locales) {
System.out.println("Java formatter");
final Date date = new Date(System.currentTimeMillis());
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 0 })));
locales.forEach(locale -> System.out.println(getLabel(locale, new Object[] { date, "Alice", 2 })));
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.localization;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class Localization {
public static String getLabel(Locale locale) {
final ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
return bundle.getString("label");
}
public static void run(List<Locale> locales) {
locales.forEach(locale -> System.out.println(getLabel(locale)));
}
}

View File

@ -0,0 +1 @@
Main-Class: com.baeldung.jarArguments.JarExample

View File

@ -0,0 +1,2 @@
label=On {0, date, short} {1} has sent you {2, choice, 0#no messages|1#a message|2#two messages|2<{2,number,integer} messages}.
label-icu={0} has sent you {2, plural, =0 {no messages} =1 {a message} other {{2, number, integer} messages}}.

View File

@ -0,0 +1,2 @@
label={0, date, short}, {1}{2, choice, 0# ne|0<} vous a envoyé {2, choice, 0#aucun message|1#un message|2#deux messages|2<{2,number,integer} messages}.
label-icu={0} {2, plural, =0 {ne } other {}}vous a envoyé {2, plural, =0 {aucun message} =1 {un message} other {{2, number, integer} messages}}.

View File

@ -0,0 +1,2 @@
label={0, date, short} {1} ti ha inviato {2, choice, 0#nessun messagio|1#un messaggio|2#due messaggi|2<{2, number, integer} messaggi}.
label-icu={0} {2, plural, =0 {non } other {}}ti ha inviato {2, plural, =0 {nessun messaggio} =1 {un messaggio} other {{2, number, integer} messaggi}}.

View File

@ -0,0 +1,2 @@
label=W {0, date, short} {1}{2, choice, 0# nie|0<} wys\u0142a\u0142a ci {2, choice, 0#\u017Cadnych wiadomo\u015Bci|1#wiadomo\u015B\u0107|2#dwie wiadomo\u015Bci|2<{2, number, integer} wiadomo\u015Bci}.
label-icu={0} {2, plural, =0 {nie } other {}}{1, select, male {wys\u0142a\u0142} female {wys\u0142a\u0142a} other {wys\u0142a\u0142o}} ci {2, plural, =0 {\u017Cadnej wiadomo\u015Bci} =1 {wiadomo\u015B\u0107} other {{2, number, integer} wiadomo\u015Bci}}.

View File

@ -0,0 +1 @@
label=Alice has sent you a message.

View File

@ -0,0 +1 @@
label=Alice vous a envoyé un message.

View File

@ -0,0 +1 @@
label=Alice ti ha inviato un messaggio.

View File

@ -0,0 +1 @@
label=Alice wys\u0142a\u0142a ci wiadomo\u015B\u0107.

View File

@ -0,0 +1,74 @@
package com.baeldung.localization;
import static org.junit.Assert.assertEquals;
import java.util.Locale;
import org.junit.Test;
import com.baeldung.localization.ICUFormat;
public class ICUFormatUnitTest {
@Test
public void givenInUK_whenAliceSendsNothing_thenCorrectMessage() {
assertEquals("Alice has sent you no messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 0 }));
}
@Test
public void givenInUK_whenAliceSendsOneMessage_thenCorrectMessage() {
assertEquals("Alice has sent you a message.", ICUFormat.getLabel(Locale.UK, new Object[] { "Alice", "female", 1 }));
}
@Test
public void givenInUK_whenBobSendsSixMessages_thenCorrectMessage() {
assertEquals("Bob has sent you 6 messages.", ICUFormat.getLabel(Locale.UK, new Object[] { "Bob", "male", 6 }));
}
@Test
public void givenInItaly_whenAliceSendsNothing_thenCorrectMessage() {
assertEquals("Alice non ti ha inviato nessun messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 0 }));
}
@Test
public void givenInItaly_whenAliceSendsOneMessage_thenCorrectMessage() {
assertEquals("Alice ti ha inviato un messaggio.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Alice", "female", 1 }));
}
@Test
public void givenInItaly_whenBobSendsSixMessages_thenCorrectMessage() {
assertEquals("Bob ti ha inviato 6 messaggi.", ICUFormat.getLabel(Locale.ITALY, new Object[] { "Bob", "male", 6 }));
}
@Test
public void givenInFrance_whenAliceSendsNothing_thenCorrectMessage() {
assertEquals("Alice ne vous a envoyé aucun message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 0 }));
}
@Test
public void givenInFrance_whenAliceSendsOneMessage_thenCorrectMessage() {
assertEquals("Alice vous a envoyé un message.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Alice", "female", 1 }));
}
@Test
public void givenInFrance_whenBobSendsSixMessages_thenCorrectMessage() {
assertEquals("Bob vous a envoyé 6 messages.", ICUFormat.getLabel(Locale.FRANCE, new Object[] { "Bob", "male", 6 }));
}
@Test
public void givenInPoland_whenAliceSendsNothing_thenCorrectMessage() {
assertEquals("Alice nie wysłała ci żadnej wiadomości.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 0 }));
}
@Test
public void givenInPoland_whenAliceSendsOneMessage_thenCorrectMessage() {
assertEquals("Alice wysłała ci wiadomość.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Alice", "female", 1 }));
}
@Test
public void givenInPoland_whenBobSendsSixMessages_thenCorrectMessage() {
assertEquals("Bob wysłał ci 6 wiadomości.", ICUFormat.getLabel(Locale.forLanguageTag("pl-PL"), new Object[] { "Bob", "male", 6 }));
}
}

View File

@ -0,0 +1,27 @@
<?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>
<parent>
<groupId>com.baeldung.multimodulemavenproject</groupId>
<artifactId>multimodulemavenproject</artifactId>
<version>1.0</version>
</parent>
<groupId>com.baeldung.daomodule</groupId>
<artifactId>daomodule</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,11 @@
package com.baeldung.daomodule;
import java.util.List;
import java.util.Optional;
public interface Dao<T> {
Optional<T> findById(int id);
List<T> findAll();
}

View File

@ -0,0 +1,3 @@
module com.baeldung.daomodule {
exports com.baeldung.daomodule;
}

View File

@ -0,0 +1,28 @@
<?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>
<parent>
<groupId>com.baeldung.multimodulemavenproject</groupId>
<artifactId>multimodulemavenproject</artifactId>
<version>1.0</version>
</parent>
<groupId>com.baeldung.entitymodule</groupId>
<artifactId>entitymodule</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,19 @@
package com.baeldung.entitymodule;
public class User {
private final String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "User{" + "name=" + name + '}';
}
}

View File

@ -0,0 +1,3 @@
module com.baeldung.entitymodule {
exports com.baeldung.entitymodule;
}

View File

@ -0,0 +1,45 @@
<?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>
<parent>
<groupId>com.baeldung.multimodulemavenproject</groupId>
<artifactId>multimodulemavenproject</artifactId>
<version>1.0</version>
</parent>
<groupId>com.baeldung.mainappmodule</groupId>
<artifactId>mainappmodule</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.baeldung.entitymodule</groupId>
<artifactId>entitymodule</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.baeldung.daomodule</groupId>
<artifactId>daomodule</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.baeldung.userdaomodule</groupId>
<artifactId>userdaomodule</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,19 @@
package com.baeldung.mainappmodule;
import com.baeldung.daomodule.Dao;
import com.baeldung.entitymodule.User;
import com.baeldung.userdaomodule.UserDao;
import java.util.HashMap;
import java.util.Map;
public class Application {
public static void main(String args[]) {
Map<Integer, User> users = new HashMap<>();
users.put(1, new User("Julie"));
users.put(2, new User("David"));
Dao userDao = new UserDao(users);
userDao.findAll().forEach(System.out::println);
}
}

View File

@ -0,0 +1,8 @@
module com.baeldung.mainppmodule {
requires com.baeldung.entitymodule;
requires com.baeldung.daomodule;
requires com.baeldung.userdaomodule;
uses com.baeldung.userdaomodule.UserDao;
}

View File

@ -0,0 +1,62 @@
<?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>
<groupId>com.baeldung.multimodulemavenproject</groupId>
<artifactId>multimodulemavenproject</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>multimodulemavenproject</name>
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.12.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>entitymodule</module>
<module>daomodule</module>
<module>userdaomodule</module>
<module>mainappmodule</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,41 @@
<?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>
<parent>
<groupId>com.baeldung.multimodulemavenproject</groupId>
<artifactId>multimodulemavenproject</artifactId>
<version>1.0</version>
</parent>
<groupId>com.baeldung.userdaomodule</groupId>
<artifactId>userdaomodule</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.baeldung.entitymodule</groupId>
<artifactId>entitymodule</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.baeldung.daomodule</groupId>
<artifactId>daomodule</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,32 @@
package com.baeldung.userdaomodule;
import com.baeldung.daomodule.Dao;
import com.baeldung.entitymodule.User;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class UserDao implements Dao<User> {
private final Map<Integer, User> users;
public UserDao() {
users = new HashMap<>();
}
public UserDao(Map<Integer, User> users) {
this.users = users;
}
@Override
public List<User> findAll() {
return new ArrayList<>(users.values());
}
@Override
public Optional<User> findById(int id) {
return Optional.ofNullable(users.get(id));
}
}

View File

@ -0,0 +1,6 @@
module com.baeldung.userdaomodule {
requires com.baeldung.entitymodule;
requires com.baeldung.daomodule;
provides com.baeldung.daomodule.Dao with com.baeldung.userdaomodule.UserDao;
exports com.baeldung.userdaomodule;
}

View File

@ -2,6 +2,7 @@
<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>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<name>core-java-modules</name>
<packaging>pom</packaging>
@ -15,5 +16,5 @@
<modules>
<module>pre-jpms</module>
</modules>
</project>
</project>

View File

@ -59,18 +59,43 @@ public class Graph {
Vertex(String label) {
this.label = label;
}
@Override
public boolean equals(Object obj) {
Vertex vertex = (Vertex) obj;
return vertex.label == label;
}
@Override
public int hashCode() {
return label.hashCode();
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + ((label == null) ? 0 : label.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Vertex other = (Vertex) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (label == null) {
if (other.label != null)
return false;
} else if (!label.equals(other.label))
return false;
return true;
}
@Override
public String toString() {
return label;
}
private Graph getOuterType() {
return Graph.this;
}
}
}

View File

@ -1,3 +1,5 @@
## Relevant articles:
- [InputStream to String in Kotlin](https://www.baeldung.com/kotlin-inputstream-to-string)
- [Console I/O in Kotlin](https://www.baeldung.com/kotlin-console-io)

View File

@ -17,4 +17,8 @@ class FileReader {
File(fileName).inputStream().readBytes().toString(Charsets.UTF_8)
fun readFileDirectlyAsText(fileName: String): String = File(fileName).readText(Charsets.UTF_8)
fun readFileUsingGetResource(fileName: String) = this::class.java.getResource(fileName).readText(Charsets.UTF_8)
fun readFileAsLinesUsingGetResourceAsStream(fileName: String) = this::class.java.getResourceAsStream(fileName).bufferedReader().readLines()
}

View File

@ -48,4 +48,20 @@ internal class FileReaderTest {
assertTrue { text.contains("Hello to Kotlin") }
}
@Test
fun whenReadFileAsTextUsingGetResource_thenCorrect() {
val text = fileReader.readFileUsingGetResource("/Kotlin.in")
assertTrue { text.contains("1. Concise") }
}
@Test
fun whenReadFileUsingGetResourceAsStream_thenCorrect() {
val lines = fileReader.readFileAsLinesUsingGetResourceAsStream("/Kotlin.in")
assertTrue { lines.contains("3. Interoperable") }
}
}

View File

@ -15,6 +15,12 @@
</parent>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
@ -49,6 +55,7 @@
<properties>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<joda-time.version>2.10</joda-time.version>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>

View File

@ -0,0 +1,59 @@
package com.baeldung.convert;
import org.joda.time.Instant;
import org.junit.Assert;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
public class ConvertDateTimeUnitTest {
public final long millis = 1556175797428L;
@Test
public void givenLocalDateTime_WhenToEpochMillis_ThenMillis() {
ZoneId id = ZoneId.systemDefault();
LocalDateTime localDateTime =
LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(millis), id);
ZonedDateTime zdt = ZonedDateTime.of(localDateTime, id);
Assert.assertEquals(millis, zdt.toInstant().toEpochMilli());
}
@Test
public void givenJava8Instant_WhenGToEpochMillis_ThenMillis() {
java.time.Instant instant = java.time.Instant.ofEpochMilli(millis);
Assert.assertEquals(millis, instant.toEpochMilli());
}
@Test
public void givenDate_WhenGetTime_ThenMillis() {
Date date = new Date(millis);
Assert.assertEquals(millis, date.getTime());
}
@Test
public void givenCalendar_WhenGetTimeInMillis_ThenMillis() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(millis));
Assert.assertEquals(millis, calendar.getTimeInMillis());
}
@Test
public void givenJodaInstant_WhenGetMillis_ThenMillis() {
Instant jodaInstant = Instant.ofEpochMilli(millis);
Assert.assertEquals(millis, jodaInstant.getMillis());
}
@Test
public void givenJODADateTime_WhenGetMillis_ThenMillis() {
org.joda.time.DateTime jodaDateTime = new org.joda.time.DateTime(millis);
Assert.assertEquals(millis, jodaDateTime.getMillis());
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.jpadefaultvalues.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Application {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.jpadefaultvalues.application;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
@Column(columnDefinition = "varchar(255) default 'John Snow'")
private String name = "John Snow";
@Column(columnDefinition = "integer default 25")
private Integer age = 25;
@Column(columnDefinition = "boolean default false")
private Boolean locked = false;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getLocked() {
return locked;
}
public void setLocked(Boolean locked) {
this.locked = locked;
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.jpadefaultvalues.application;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

View File

@ -0,0 +1,56 @@
package com.baeldung.jpadefaultvalues.application;
import com.baeldung.jpadefaultvalues.application.User;
import com.baeldung.jpadefaultvalues.application.UserRepository;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDefaultValuesUnitTest {
@Autowired
private UserRepository userRepository;
@Test
@Ignore // SQL default values are also defined
public void saveUser_shouldSaveWithDefaultFieldValues() {
User user = new User();
user = userRepository.save(user);
assertEquals(user.getName(), "John Snow");
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
}
@Test
@Ignore // SQL default values are also defined
public void saveUser_shouldSaveWithNullName() {
User user = new User();
user.setName(null);
user = userRepository.save(user);
assertNull(user.getName());
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
}
@Test
public void saveUser_shouldSaveWithDefaultSqlValues() {
User user = new User();
user = userRepository.save(user);
assertEquals(user.getName(), "John Snow");
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
}
}