Revize 5df617eb
Přidáno uživatelem Lukas Cerny před asi 6 roky(ů)
.gitignore | ||
---|---|---|
1 |
# Compiled class file |
|
2 |
*.class |
|
3 |
|
|
4 |
# Log file |
|
5 |
*.log |
|
6 |
|
|
7 |
# BlueJ files |
|
8 |
*.ctxt |
|
9 |
|
|
10 |
# Mobile Tools for Java (J2ME) |
|
11 |
.mtj.tmp/ |
|
12 |
|
|
13 |
# Package Files # |
|
14 |
*.jar |
|
15 |
*.war |
|
16 |
*.nar |
|
17 |
*.ear |
|
18 |
*.zip |
|
19 |
*.tar.gz |
|
20 |
*.rar |
|
21 |
|
|
22 |
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml |
|
23 |
hs_err_pid* |
|
1 |
# Compiled class file |
|
2 |
*.class |
|
3 |
|
|
4 |
# Log file |
|
5 |
*.log |
|
6 |
|
|
7 |
# BlueJ files |
|
8 |
*.ctxt |
|
9 |
|
|
10 |
# Mobile Tools for Java (J2ME) |
|
11 |
.mtj.tmp/ |
|
12 |
|
|
13 |
# Package Files # |
|
14 |
*.jar |
|
15 |
*.war |
|
16 |
*.nar |
|
17 |
*.ear |
|
18 |
*.zip |
|
19 |
*.tar.gz |
|
20 |
*.rar |
|
21 |
|
|
22 |
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml |
|
23 |
hs_err_pid* |
|
24 |
|
|
25 |
# IntelliJ |
|
26 |
.idea/* |
|
27 |
*.iml |
|
28 |
|
build.sh | ||
---|---|---|
1 |
mvn package && docker restart yamanager_app |
docker-compose.yml | ||
---|---|---|
1 |
version: '3.1' |
|
2 |
|
|
3 |
services: |
|
4 |
|
|
5 |
app: |
|
6 |
image: openjdk:13-jdk-alpine |
|
7 |
container_name: yamanager_app |
|
8 |
volumes: |
|
9 |
- /tmp |
|
10 |
- ./target/ymanager-1.0-SNAPSHOT.jar:/app.jar |
|
11 |
entrypoint: java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -Djava.security.egd=file:/dev/./urandom -jar /app.jar |
|
12 |
restart: always |
|
13 |
ports: |
|
14 |
- 9080:8080 |
|
15 |
- 6005:5005 |
|
16 |
depends_on: |
|
17 |
- db |
|
18 |
links: |
|
19 |
- db |
|
20 |
|
|
21 |
db: |
|
22 |
image: mariadb |
|
23 |
container_name: yamanager_db |
|
24 |
restart: always |
|
25 |
volumes: |
|
26 |
- ./init.sql:/docker-entrypoint-initdb.d/init.sql |
|
27 |
environment: |
|
28 |
MYSQL_ROOT_PASSWORD: root |
|
29 |
MYSQL_USER: user |
|
30 |
MYSQL_PASSWORD: passwd |
|
31 |
MYSQL_DATABASE: yamanager |
|
32 |
ports: |
|
33 |
- 3306:3306 |
init.sql | ||
---|---|---|
1 |
CREATE TABLE test ( |
|
2 |
id MEDIUMINT NOT NULL AUTO_INCREMENT, |
|
3 |
message VARCHAR(255), |
|
4 |
PRIMARY KEY (id) |
|
5 |
); |
|
6 |
|
|
7 |
INSERT INTO test (message) VALUES ('test 1'); |
|
8 |
INSERT INTO test (message) VALUES ('test 2'); |
|
9 |
INSERT INTO test (message) VALUES ('test 3'); |
|
10 |
INSERT INTO test (message) VALUES ('test 4'); |
install.sh | ||
---|---|---|
1 |
docker-compose up |
pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|
3 |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
4 |
<modelVersion>4.0.0</modelVersion> |
|
5 |
|
|
6 |
<groupId>cz.zcu.fav.kiv</groupId> |
|
7 |
<artifactId>ymanager</artifactId> |
|
8 |
<version>1.0-SNAPSHOT</version> |
|
9 |
|
|
10 |
<parent> |
|
11 |
<groupId>org.springframework.boot</groupId> |
|
12 |
<artifactId>spring-boot-starter-parent</artifactId> |
|
13 |
<version>2.0.5.RELEASE</version> |
|
14 |
</parent> |
|
15 |
|
|
16 |
<properties> |
|
17 |
<java.version>1.8</java.version> |
|
18 |
</properties> |
|
19 |
|
|
20 |
<dependencies> |
|
21 |
<dependency> |
|
22 |
<groupId>org.springframework.boot</groupId> |
|
23 |
<artifactId>spring-boot-starter-web</artifactId> |
|
24 |
</dependency> |
|
25 |
|
|
26 |
<!-- DATABASE --> |
|
27 |
<dependency> |
|
28 |
<groupId>org.springframework.boot</groupId> |
|
29 |
<artifactId>spring-boot-starter-jdbc</artifactId> |
|
30 |
</dependency> |
|
31 |
<dependency> |
|
32 |
<groupId>org.mariadb.jdbc</groupId> |
|
33 |
<artifactId>mariadb-java-client</artifactId> |
|
34 |
<version>2.4.1</version> |
|
35 |
</dependency> |
|
36 |
|
|
37 |
|
|
38 |
<!-- TESTS --> |
|
39 |
<dependency> |
|
40 |
<groupId>org.springframework.boot</groupId> |
|
41 |
<artifactId>spring-boot-starter-test</artifactId> |
|
42 |
<scope>test</scope> |
|
43 |
</dependency> |
|
44 |
<dependency> |
|
45 |
<groupId>org.junit.jupiter</groupId> |
|
46 |
<artifactId>junit-jupiter-api</artifactId> |
|
47 |
<scope>test</scope> |
|
48 |
</dependency> |
|
49 |
<dependency> |
|
50 |
<groupId>org.mockito</groupId> |
|
51 |
<artifactId>mockito-core</artifactId> |
|
52 |
<version>2.25.1</version> |
|
53 |
<scope>test</scope> |
|
54 |
</dependency> |
|
55 |
</dependencies> |
|
56 |
|
|
57 |
|
|
58 |
<build> |
|
59 |
<plugins> |
|
60 |
<plugin> |
|
61 |
<groupId>org.springframework.boot</groupId> |
|
62 |
<artifactId>spring-boot-maven-plugin</artifactId> |
|
63 |
</plugin> |
|
64 |
</plugins> |
|
65 |
</build> |
|
66 |
|
|
67 |
</project> |
src/main/java/cz/zcu/yamanager/Application.java | ||
---|---|---|
1 |
package cz.zcu.yamanager; |
|
2 |
|
|
3 |
import cz.zcu.yamanager.util.localization.Language; |
|
4 |
import cz.zcu.yamanager.util.localization.Message; |
|
5 |
import org.springframework.boot.SpringApplication; |
|
6 |
import org.springframework.boot.autoconfigure.SpringBootApplication; |
|
7 |
|
|
8 |
@SpringBootApplication |
|
9 |
public class Application { |
|
10 |
|
|
11 |
public static void main(String[] args) { |
|
12 |
|
|
13 |
Message.config() |
|
14 |
.addLanguage(Language.EN) |
|
15 |
.addLanguage(Language.CZ); |
|
16 |
|
|
17 |
SpringApplication.run(Application.class, args); |
|
18 |
} |
|
19 |
} |
src/main/java/cz/zcu/yamanager/domain/Test.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.domain; |
|
2 |
|
|
3 |
public class Test { |
|
4 |
|
|
5 |
private final long id; |
|
6 |
private final String message; |
|
7 |
|
|
8 |
public Test(long id, String message) { |
|
9 |
this.id = id; |
|
10 |
this.message = message; |
|
11 |
} |
|
12 |
|
|
13 |
public long getId() { |
|
14 |
return id; |
|
15 |
} |
|
16 |
|
|
17 |
public String getMessage() { |
|
18 |
return message; |
|
19 |
} |
|
20 |
|
|
21 |
@Override |
|
22 |
public String toString() { |
|
23 |
return String.format("Test[id=%d, message='%s']", id, message); |
|
24 |
} |
|
25 |
} |
src/main/java/cz/zcu/yamanager/repository/HelloRepository.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.repository; |
|
2 |
|
|
3 |
import cz.zcu.yamanager.domain.Test; |
|
4 |
import org.slf4j.Logger; |
|
5 |
import org.slf4j.LoggerFactory; |
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
8 |
import org.springframework.stereotype.Repository; |
|
9 |
|
|
10 |
import java.util.List; |
|
11 |
|
|
12 |
@Repository |
|
13 |
public class HelloRepository { |
|
14 |
|
|
15 |
private static final Logger log = LoggerFactory.getLogger(HelloRepository.class); |
|
16 |
|
|
17 |
private final JdbcTemplate jdbc; |
|
18 |
|
|
19 |
@Autowired |
|
20 |
public HelloRepository(JdbcTemplate jdbc) { |
|
21 |
this.jdbc = jdbc; |
|
22 |
} |
|
23 |
|
|
24 |
public List<Test> all() { |
|
25 |
return jdbc.query("SELECT id, message FROM test", |
|
26 |
(rs, rowNum) -> new Test(rs.getLong("id"), rs.getString("message"))); |
|
27 |
} |
|
28 |
} |
src/main/java/cz/zcu/yamanager/util/localization/Language.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.util.localization; |
|
2 |
|
|
3 |
import java.util.Locale; |
|
4 |
|
|
5 |
public enum Language { |
|
6 |
|
|
7 |
EN (Locale.ENGLISH), |
|
8 |
CZ (new Locale("cs", "")), |
|
9 |
|
|
10 |
; |
|
11 |
private Locale locale; |
|
12 |
Language(Locale locale) { |
|
13 |
this.locale = locale; |
|
14 |
} |
|
15 |
|
|
16 |
public Locale getLocale() { |
|
17 |
return locale; |
|
18 |
} |
|
19 |
|
|
20 |
public static Language get(String lang) { |
|
21 |
return valueOf(lang.toUpperCase()); |
|
22 |
} |
|
23 |
} |
src/main/java/cz/zcu/yamanager/util/localization/Message.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.util.localization; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.Map; |
|
5 |
import java.util.ResourceBundle; |
|
6 |
|
|
7 |
import static java.util.ResourceBundle.getBundle; |
|
8 |
|
|
9 |
public final class Message { |
|
10 |
|
|
11 |
private static Map<Language, ResourceBundle> BUNDLES; |
|
12 |
|
|
13 |
static { |
|
14 |
BUNDLES = new HashMap<>(); |
|
15 |
|
|
16 |
config().addLanguage(Language.EN); |
|
17 |
} |
|
18 |
|
|
19 |
public interface Config { |
|
20 |
Config addLanguage(Language language); |
|
21 |
} |
|
22 |
|
|
23 |
public static Config config() { |
|
24 |
return new Config() { |
|
25 |
|
|
26 |
@Override |
|
27 |
public Config addLanguage(Language language) { |
|
28 |
if (!BUNDLES.containsKey(language)) { |
|
29 |
BUNDLES.put(language, getBundle("Message", language.getLocale())); |
|
30 |
} |
|
31 |
return this; |
|
32 |
} |
|
33 |
}; |
|
34 |
} |
|
35 |
|
|
36 |
public static String getString(Language language, String key) { |
|
37 |
if (!BUNDLES.containsKey(language)) return language.name()+" SOURCE MISSING"; |
|
38 |
return BUNDLES.get(language).getString(key); |
|
39 |
} |
|
40 |
|
|
41 |
public static String getString(String key) { |
|
42 |
return getString(Language.EN, key); |
|
43 |
} |
|
44 |
} |
src/main/java/cz/zcu/yamanager/ws/rest/HelloController.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.ws.rest; |
|
2 |
|
|
3 |
import cz.zcu.yamanager.domain.Test; |
|
4 |
import cz.zcu.yamanager.repository.HelloRepository; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
7 |
import org.springframework.web.bind.annotation.RequestParam; |
|
8 |
import org.springframework.web.bind.annotation.RestController; |
|
9 |
|
|
10 |
import java.util.List; |
|
11 |
|
|
12 |
import static cz.zcu.yamanager.util.localization.Language.get; |
|
13 |
import static cz.zcu.yamanager.util.localization.Message.getString; |
|
14 |
import static org.springframework.web.bind.annotation.RequestMethod.GET; |
|
15 |
|
|
16 |
@RestController |
|
17 |
public class HelloController { |
|
18 |
|
|
19 |
private final HelloRepository repository; |
|
20 |
|
|
21 |
@Autowired |
|
22 |
public HelloController(HelloRepository repository) { |
|
23 |
this.repository = repository; |
|
24 |
} |
|
25 |
|
|
26 |
@RequestMapping(value = "/hello", method=GET) |
|
27 |
public String hello(@RequestParam(value = "lang", required = false, defaultValue = "en") String lang) { |
|
28 |
return getString(get(lang), "hello"); |
|
29 |
} |
|
30 |
|
|
31 |
@RequestMapping(value = "/database", method=GET) |
|
32 |
public List<Test> database() { |
|
33 |
return repository.all(); |
|
34 |
} |
|
35 |
} |
src/main/java/cz/zcu/yamanager/ws/rest/TestController.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.ws.rest; |
|
2 |
|
|
3 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
4 |
import org.springframework.web.bind.annotation.RestController; |
|
5 |
|
|
6 |
import static org.springframework.web.bind.annotation.RequestMethod.GET; |
|
7 |
import static org.springframework.web.bind.annotation.RequestMethod.POST; |
|
8 |
|
|
9 |
@RestController("/test") |
|
10 |
public class TestController { |
|
11 |
|
|
12 |
@RequestMapping(method=GET) |
|
13 |
public String get() { |
|
14 |
return "test GET"; |
|
15 |
} |
|
16 |
|
|
17 |
@RequestMapping(method=POST) |
|
18 |
public String post() { |
|
19 |
return "test POST"; |
|
20 |
} |
|
21 |
} |
src/main/resources/Message_cs.properties | ||
---|---|---|
1 |
hello=Ahoj sv\u011Bte |
src/main/resources/Message_en.properties | ||
---|---|---|
1 |
hello=Hello world |
src/main/resources/application.properties | ||
---|---|---|
1 |
spring.datasource.url=jdbc:mariadb://db:3306/yamanager |
|
2 |
spring.datasource.username=user |
|
3 |
spring.datasource.password=passwd |
|
4 |
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver |
src/test/java/cz/zcu/yamanager/util/localization/MessageTest.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.util.localization; |
|
2 |
|
|
3 |
import org.junit.jupiter.api.Test; |
|
4 |
|
|
5 |
import static org.junit.jupiter.api.Assertions.*; |
|
6 |
|
|
7 |
class MessageTest { |
|
8 |
|
|
9 |
@Test |
|
10 |
void getString_inCzech_true() { |
|
11 |
Message.config().addLanguage(Language.CZ); |
|
12 |
|
|
13 |
assertEquals("český jazyk", Message.getString(Language.CZ, "hello")); |
|
14 |
} |
|
15 |
|
|
16 |
@Test |
|
17 |
void getString_inEnglish_true() { |
|
18 |
Message.config().addLanguage(Language.EN); |
|
19 |
|
|
20 |
assertEquals("english language", Message.getString(Language.EN, "hello")); |
|
21 |
} |
|
22 |
|
|
23 |
@Test |
|
24 |
void getString_general_true() { |
|
25 |
assertEquals("english language", Message.getString("hello")); |
|
26 |
} |
|
27 |
|
|
28 |
@Test |
|
29 |
void getString_multiple_true() { |
|
30 |
Message.config() |
|
31 |
.addLanguage(Language.CZ) |
|
32 |
.addLanguage(Language.EN); |
|
33 |
|
|
34 |
assertEquals("český jazyk", Message.getString(Language.CZ, "hello")); |
|
35 |
assertEquals("english language", Message.getString(Language.EN, "hello")); |
|
36 |
assertEquals("english language", Message.getString("hello")); |
|
37 |
} |
|
38 |
|
|
39 |
@Test |
|
40 |
void getString_missingSource_true() { |
|
41 |
assertEquals("CZ SOURCE MISSING", Message.getString(Language.CZ, "hello")); |
|
42 |
} |
|
43 |
} |
src/test/java/cz/zcu/yamanager/ws/rest/HelloControllerTest.java | ||
---|---|---|
1 |
package cz.zcu.yamanager.ws.rest; |
|
2 |
|
|
3 |
import cz.zcu.yamanager.util.localization.Language; |
|
4 |
import cz.zcu.yamanager.util.localization.Message; |
|
5 |
import org.junit.jupiter.api.BeforeEach; |
|
6 |
import org.junit.jupiter.api.Test; |
|
7 |
|
|
8 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
9 |
|
|
10 |
class HelloControllerTest { |
|
11 |
|
|
12 |
private HelloController helloController; |
|
13 |
|
|
14 |
@BeforeEach |
|
15 |
void setUp() { |
|
16 |
Message.config() |
|
17 |
.addLanguage(Language.EN) |
|
18 |
.addLanguage(Language.CZ); |
|
19 |
|
|
20 |
helloController = new HelloController(null); |
|
21 |
} |
|
22 |
|
|
23 |
@Test |
|
24 |
void hello_inCzech_true() { |
|
25 |
assertEquals("český jazyk", helloController.hello("cz")); |
|
26 |
} |
|
27 |
|
|
28 |
@Test |
|
29 |
void hello_inEnglish_true() { |
|
30 |
assertEquals("english language", helloController.hello("en")); |
|
31 |
} |
|
32 |
} |
src/test/resources/Message_cs.properties | ||
---|---|---|
1 |
hello=\u010Desk\u00FD jazyk |
src/test/resources/Message_en.properties | ||
---|---|---|
1 |
hello=english language |
Také k dispozici: Unified diff
Re #7143 Prepared developper's environment