mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-06-20 11:14:34 +00:00
All examples are moved to special folder
This commit is contained in:
1 parent
7efe4bbab3
commit
be3ac01fe8
126 files changed
No files matched your search
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.testingExample;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import ru.otus.testingExample.services.GreetingService;
|
||||
|
||||
@SpringBootApplication
|
||||
public class TestingExampleSpringBootApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TestingExampleSpringBootApplication.class, args);
|
||||
GreetingService greetingService = context.getBean(GreetingService.class);
|
||||
greetingService.sayChinaGreeting();
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.testingExample.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.otus.testingExample.dao.GreetingDao;
|
||||
import ru.otus.testingExample.dao.GreetingDaoImpl;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationConfig {
|
||||
|
||||
@Bean
|
||||
GreetingDao greetingDao(ApplicationProperties properties) {
|
||||
return new GreetingDaoImpl(properties.getInitialData());
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.testingExample.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties
|
||||
public class ApplicationProperties {
|
||||
private String initialData;
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.testingExample.dao;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface GreetingDao {
|
||||
Optional<String> findGreetingByCountryCode(String countryCode);
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package ru.otus.testingExample.dao;
|
||||
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.*;
|
||||
|
||||
public class GreetingDaoImpl implements GreetingDao {
|
||||
|
||||
private final String initialDataPropertiesResourceName;
|
||||
private final Map<String, String> greetingsByCountryCode;
|
||||
|
||||
public GreetingDaoImpl(String initialDataPropertiesResourceName) {
|
||||
this.initialDataPropertiesResourceName = initialDataPropertiesResourceName;
|
||||
greetingsByCountryCode = new HashMap<>();
|
||||
loadInitialData();
|
||||
}
|
||||
|
||||
private void loadInitialData() {
|
||||
greetingsByCountryCode.clear();
|
||||
|
||||
try {
|
||||
try (InputStream initialDataPropertiesResourceStream = getClass().getClassLoader().getResourceAsStream(initialDataPropertiesResourceName)) {
|
||||
Properties properties = new Properties();
|
||||
properties.load(new InputStreamReader(Objects.requireNonNull(initialDataPropertiesResourceStream), "UTF-8"));
|
||||
properties.forEach((key, value) -> greetingsByCountryCode.putIfAbsent(((String) key).toUpperCase(), (String) value));
|
||||
}
|
||||
}catch (Exception e) {
|
||||
throw new InitialDataLoadingException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Optional<String> findGreetingByCountryCode(String countryCode) {
|
||||
return Optional.ofNullable(greetingsByCountryCode.get(countryCode.toUpperCase()));
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.testingExample.dao;
|
||||
|
||||
public class InitialDataLoadingException extends RuntimeException {
|
||||
public InitialDataLoadingException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.otus.testingExample.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ConsoleIOService implements IOService {
|
||||
@Override
|
||||
public void out(String message) {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.testingExample.services;
|
||||
|
||||
public class CountryCodes {
|
||||
public static final String COUNTRY_CODE_RU = "RU";
|
||||
public static final String COUNTRY_CODE_EN = "EN";
|
||||
public static final String COUNTRY_CODE_CN = "CN";
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package ru.otus.testingExample.services;
|
||||
|
||||
public class GreetingNotFoundException extends RuntimeException {
|
||||
public GreetingNotFoundException(String countryCode) {
|
||||
super(String.format("No greeting was found for country code %s", countryCode));
|
||||
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package ru.otus.testingExample.services;
|
||||
|
||||
public interface GreetingService {
|
||||
void sayRussianGreeting();
|
||||
void sayEnglishGreeting();
|
||||
void sayChinaGreeting();
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package ru.otus.testingExample.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.otus.testingExample.dao.GreetingDao;
|
||||
|
||||
import static ru.otus.testingExample.services.CountryCodes.*;
|
||||
|
||||
@Service
|
||||
public class GreetingServiceImpl implements GreetingService {
|
||||
private final IOService ioService;
|
||||
private final GreetingDao greetingDao;
|
||||
|
||||
public GreetingServiceImpl(IOService ioService, GreetingDao greetingDao) {
|
||||
this.ioService = ioService;
|
||||
this.greetingDao = greetingDao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sayRussianGreeting() {
|
||||
ioService.out(greetingDao.findGreetingByCountryCode(COUNTRY_CODE_RU).orElseThrow(() -> new GreetingNotFoundException(COUNTRY_CODE_RU)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sayEnglishGreeting() {
|
||||
ioService.out(greetingDao.findGreetingByCountryCode(COUNTRY_CODE_EN).orElseThrow(() -> new GreetingNotFoundException(COUNTRY_CODE_EN)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sayChinaGreeting() {
|
||||
ioService.out(greetingDao.findGreetingByCountryCode(COUNTRY_CODE_CN).orElseThrow(() -> new GreetingNotFoundException(COUNTRY_CODE_CN)));
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.testingExample.services;
|
||||
|
||||
public interface IOService {
|
||||
void out(String message);
|
||||
}
|
||||
Reference in new issue
Block a user