Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# OtusSpringHW
Домашние задания по Spring для Otus.
# OtusSpringHW

Домашнее задание #2:
- Добавить файл настроек в приложение и для тестирования.
- Локализовать выводимые сообщения и вопросы теста.
- Переписать конфигурацию в виде Java + Annotation-based конфигурации.

### 19-03-22 Доработки:
- Убраны лишние тесты.
- Изменена работа с файлом.
- Использование @Data вместо @Getter, @Setter.
- Добавлены *.properties для тестов.
138 changes: 138 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?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>

<name>home-work-01</name>

<groupId>ru.otus.mkulikov</groupId>
<artifactId>home-work</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<encoding>UTF-8</encoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>

<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>

<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${encoding}</encoding>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>${encoding}</encoding>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>

<configuration>
<skipTests>false</skipTests>
</configuration>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.csv</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
33 changes: 33 additions & 0 deletions src/main/java/ru/otus/mkulikov/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.otus.mkulikov;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import ru.otus.mkulikov.exceptions.QuestionsFileLoadingException;
import ru.otus.mkulikov.services.processor.ProcessorService;

/**
* Created by IntelliJ IDEA.
* Developer: Maksim Kulikov
* Date: 2019-03-14
* Time: 15:27
*/

@Configuration
@ComponentScan
@PropertySource("classpath:application.properties")
public class Application {

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
try {
ProcessorService processor = context.getBean(ProcessorService.class);
processor.startTest();
} catch (QuestionsFileLoadingException e) {
e.printStackTrace();
} finally {
context.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.otus.mkulikov.exceptions;

/**
* Created by IntelliJ IDEA.
* Developer: Maksim Kulikov
* Date: 2019-03-17
* Time: 01:40
*/

public class QuestionsFileLoadingException extends RuntimeException {

public QuestionsFileLoadingException(String message) {
super(message);
}

public QuestionsFileLoadingException(String message, Throwable cause) {
super(message, cause);
}
}
39 changes: 39 additions & 0 deletions src/main/java/ru/otus/mkulikov/models/Question.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ru.otus.mkulikov.models;

import lombok.Data;

import java.io.Serializable;

/**
* Created by IntelliJ IDEA.
* Developer: Maksim Kulikov
* Date: 2019-03-14
* Time: 15:54
*/

@Data

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отлично

public class Question implements Serializable {

private String id;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Есть @Data, она сразу сгенерит все нужное :)

private String question;
private String answer1;
private String answer2;
private String answer3;
private String answer4;
private String trueAnswer;
private String userAnswer;

public Question(String id, String question, String answer1, String answer2, String answer3, String answer4, String trueAnswer, String userAnswer) {
this.id = id;
this.question = question;
this.answer1 = answer1;
this.answer2 = answer2;
this.answer3 = answer3;
this.answer4 = answer4;
this.trueAnswer = trueAnswer;
this.userAnswer = userAnswer;
}

public Question() {
}
}
22 changes: 22 additions & 0 deletions src/main/java/ru/otus/mkulikov/models/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.otus.mkulikov.models;

import lombok.Data;

/**
* Created by IntelliJ IDEA.
* Developer: Maksim Kulikov
* Date: 2019-03-14
* Time: 15:29
*/

@Data
public class User {

private final String name;
private final String surname;

public User(String name, String surname) {
this.name = name;
this.surname = surname;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.otus.mkulikov.services.console;

import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;
import java.util.Scanner;

/**
* Created by IntelliJ IDEA.
* Developer: Maksim Kulikov
* Date: 17.03.2019
* Time: 23:21
*/

@Service
public class ConsoleIOServiceImpl implements IOService {

private final Scanner in;

public ConsoleIOServiceImpl() {
in = new Scanner(System.in);
}

@Override
public void write(String text) {
System.out.println(text);
}

@Override
public String read() {
return in.nextLine();
}

@PreDestroy
public void destroy() {
in.close();
}
}
15 changes: 15 additions & 0 deletions src/main/java/ru/otus/mkulikov/services/console/IOService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.otus.mkulikov.services.console;

/**
* Created by IntelliJ IDEA.
* Developer: Maksim Kulikov
* Date: 17.03.2019
* Time: 23:21
*/

public interface IOService {

void write(String text);

String read();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.otus.mkulikov.services.localisation;

/**
* Created by IntelliJ IDEA.
* Developer: Maksim Kulikov
* Date: 2019-03-21
* Time: 14:02
*/

public interface LocalisationService {

String getValue(String key);

String getValueWithParams(String key, String[] params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ru.otus.mkulikov.services.localisation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Service;

import java.util.Locale;

/**
* Created by IntelliJ IDEA.
* Developer: Maksim Kulikov
* Date: 2019-03-21
* Time: 14:02
*/

@Service
public class LocalisationServiceImpl extends ReloadableResourceBundleMessageSource implements LocalisationService {

private final Locale locale;
private final Locale russian = new Locale("ru");

public LocalisationServiceImpl(
@Value("${locale.basename}") String basename,
@Value("${locale.default.encoding}") String defaultEncoding,
@Value("${locale.default}") String localeDefault
) {
super();
setBasename(basename);
setDefaultEncoding(defaultEncoding);

locale = (localeDefault != null)
? new Locale(localeDefault)
: russian;
}

public String getValue(String key) {
return getMessage(key, null, locale);
}

public String getValueWithParams(String key, String[] params) {
return getMessage(key, params, locale);
}
}
Loading