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

This commit is contained in:
Loredana Crusoveanu 2018-08-17 14:04:58 +03:00
commit cf4e8735b5
4 changed files with 119 additions and 1 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.constructorsstaticfactorymethods.application;
import com.baeldung.constructorsstaticfactorymethods.entities.User;
public class Application {
public static void main(String[] args) {
User user1 = User.createWithDefaultCountry("John", "john@domain.com");
User user2 = User.createWithLoggedInstantiationTime("John", "john@domain.com", "Argentina");
User user3 = User.getSingletonInstance("John", "john@domain.com", "Argentina");
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.constructorsstaticfactorymethods.entities;
import java.time.LocalTime;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class User {
private static User instance = null;
private static final Logger LOGGER = Logger.getLogger(User.class.getName());
private final String name;
private final String email;
private final String country;
public static User createWithDefaultCountry(String name, String email) {
return new User(name, email, "Argentina");
}
public static User createWithLoggedInstantiationTime(String name, String email, String country) {
setLoggerProperties();
LOGGER.log(Level.INFO, "Creating User instance at : {0}", LocalTime.now());
return new User(name, email, country);
}
public static User getSingletonInstance(String name, String email, String country) {
if (instance == null) {
synchronized (User.class) {
if (instance == null) {
instance = new User(name, email, country);
}
}
}
return instance;
}
private User(String name, String email, String country) {
this.name = name;
this.email = email;
this.country = country;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getCountry() {
return country;
}
private static void setLoggerProperties() {
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.INFO);
handler.setFormatter(new SimpleFormatter());
LOGGER.addHandler(handler);
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.constructorsstaticfactorymethods;
import com.baeldung.constructorsstaticfactorymethods.entities.User;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class UserUnitTest {
@Test
public void givenUserClass_whenCalledcreateWithDefaultCountry_thenCorrect() {
assertThat(User.createWithDefaultCountry("John", "john@domain.com")).isInstanceOf(User.class);
}
@Test
public void givenUserIntanceCreatedWithcreateWithDefaultCountry_whenCalledgetName_thenCorrect() {
User user = User.createWithDefaultCountry("John", "john@domain.com");
assertThat(user.getName()).isEqualTo("John");
}
@Test
public void givenUserIntanceCreatedWithcreateWithDefaultCountry_whenCalledgetEmail_thenCorrect() {
User user = User.createWithDefaultCountry("John", "john@domain.com");
assertThat(user.getEmail()).isEqualTo("john@domain.com");
}
@Test
public void givenUserIntanceCreatedWithcreateWithDefaultCountry_whenCalledgetCountry_thenCorrect() {
User user = User.createWithDefaultCountry("John", "john@domain.com");
assertThat(user.getCountry()).isEqualTo("Argentina");
}
@Test
public void givenUserInstanceCreatedWithcreateWithInstantiationTime_whenCalledcreateWithInstantiationTime_thenCorrect() {
assertThat(User.createWithLoggedInstantiationTime("John", "john@domain.com", "Argentina")).isInstanceOf(User.class);
}
@Test
public void givenUserInstanceCreatedWithgetSingletonIntance_whenCalledgetSingletonInstance_thenCorrect() {
User user1 = User.getSingletonInstance("John", "john@domain.com", "Argentina");
User user2 = User.getSingletonInstance("John", "john@domain.com", "Argentina");
assertThat(user1).isEqualTo(user2);
}
}

View File

@ -41,5 +41,5 @@ mysql -u root -p
### Use the REST Service
```
curl http://localhost:8080/spring-rest-full/foos
curl http://localhost:8082/spring-rest-full/auth/foos
```