Skip to content

Commit 1b22441

Browse files
committed
GP-82 update tests and javadocs
1 parent 59913f7 commit 1b22441

File tree

10 files changed

+166
-68
lines changed

10 files changed

+166
-68
lines changed

3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
<version>4.0.1</version>
2525
<scope>provided</scope>
2626
</dependency>
27+
<dependency>
28+
<groupId>com.bobocode</groupId>
29+
<artifactId>java-web-util</artifactId>
30+
<version>1.0-SNAPSHOT</version>
31+
<scope>compile</scope>
32+
</dependency>
2733
</dependencies>
2834

2935
<build>

3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/config/RootConfig.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.bobocode.config;
22

3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.ComponentScan.Filter;
5+
import org.springframework.context.annotation.Configuration;
36
import org.springframework.stereotype.Controller;
47
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
58

69
/**
7-
* This class provides application root (non-web) configuration.
10+
* This class provides application root (non-web) configuration for a basic spring application context
11+
* (containing middle-tire services, datasource, etc.).
12+
* The configuration must exclude the web layer of the application.
813
* <p>
914
* todo: mark this class as config
1015
* todo: enable component scanning for all packages in "com.bobocode"
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
package com.bobocode.config;
22

3+
import com.bobocode.util.ExerciseNotCompletedException;
34
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
45

56
/**
67
* This class is used to configure DispatcherServlet and links it with application config classes
7-
* <p>
8-
* todo: provide default servlet mapping ("/")
9-
* todo: use {@link WebConfig} as ServletConfig class
10-
* todo: use {@link RootConfig} as root application config class
118
*/
129
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
1310
@Override
1411
protected Class<?>[] getRootConfigClasses() {
15-
throw new UnsupportedOperationException("Method is not implemented yet!");
12+
throw new ExerciseNotCompletedException(); //todo: use {@link RootConfig} as root application config class
1613
}
1714

1815
@Override
1916
protected Class<?>[] getServletConfigClasses() {
20-
throw new UnsupportedOperationException("Method is not implemented yet!");
17+
throw new ExerciseNotCompletedException(); //todo: use {@link WebConfig} as ServletConfig class
2118
}
2219

2320
@Override
2421
protected String[] getServletMappings() {
25-
throw new UnsupportedOperationException("Method is not implemented yet!");
22+
throw new ExerciseNotCompletedException(); //todo: provide default servlet mapping ("/")
2623
}
2724
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package com.bobocode.config;
22

3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
6+
37
/**
4-
* This class provides web (servlet) related configuration.
8+
* This class provides web (servlet) related configuration. In the Web MVC framework,
9+
* each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined
10+
* in the root ApplicationContext.
511
* <p>
612
* todo: mark this class as Spring config class
713
* todo: enable web mvc using annotation
814
* todo: enable component scanning for package "web"
915
*/
16+
1017
public class WebConfig {
1118
}

3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/main/java/com/bobocode/web/controller/WelcomeController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
* <p>
66
* todo: mark this class as Spring controller
77
* todo: configure HTTP GET mapping "/welcome" for method {@link WelcomeController#welcome()}
8-
* todo: tell Spring that {@link WelcomeController#welcome()} method provides response body
8+
* todo: tell Spring that {@link WelcomeController#welcome()} method provides response body without view
99
*/
10+
1011
public class WelcomeController {
1112

1213
public String welcome() {
1314
return "Welcome to Spring MVC!";
1415
}
15-
1616
}

3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/src/test/java/com/bobocode/WelcomeWebAppTest.java

Lines changed: 92 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,136 +3,171 @@
33
import com.bobocode.config.RootConfig;
44
import com.bobocode.config.WebConfig;
55
import com.bobocode.web.controller.WelcomeController;
6-
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.*;
77
import org.springframework.context.annotation.ComponentScan;
88
import org.springframework.context.annotation.ComponentScan.Filter;
99
import org.springframework.context.annotation.Configuration;
1010
import org.springframework.context.annotation.FilterType;
1111
import org.springframework.stereotype.Controller;
1212
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.ResponseBody;
1314
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1415

1516
import java.util.List;
1617
import java.util.stream.Collectors;
1718
import java.util.stream.Stream;
1819

19-
import static org.hamcrest.MatcherAssert.assertThat;
20-
import static org.hamcrest.Matchers.arrayContaining;
21-
import static org.hamcrest.Matchers.arrayWithSize;
22-
import static org.hamcrest.Matchers.containsInAnyOrder;
23-
import static org.hamcrest.Matchers.equalTo;
24-
import static org.hamcrest.Matchers.notNullValue;
20+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2522

23+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
2624
class WelcomeWebAppTest {
2725

2826
@Test
29-
void testDispatcherServletMapping() {
30-
WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper();
31-
32-
assertThat(webAppInitializerWrapper.getServletMappings(), arrayContaining("/"));
33-
}
34-
35-
@Test
36-
void testInitializerRootConfigClasses() {
37-
WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper();
38-
39-
assertThat(webAppInitializerWrapper.getRootConfigClasses(), arrayContaining(RootConfig.class));
40-
}
41-
42-
@Test
43-
void testInitializerWebConfigClasses() {
44-
WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper();
45-
46-
assertThat(webAppInitializerWrapper.getServletConfigClasses(), arrayContaining(WebConfig.class));
47-
}
48-
49-
@Test
50-
void testRootConfigClassIsMarkedAsConfiguration() {
27+
@Order(1)
28+
@DisplayName("RootConfig class is marked as @Configuration")
29+
void rootConfigClassIsMarkedAsConfiguration() {
5130
Configuration configuration = RootConfig.class.getAnnotation(Configuration.class);
5231

53-
assertThat(configuration, notNullValue());
32+
assertNotNull(configuration);
5433
}
5534

5635
@Test
57-
void testRootConfigClassEnablesComponentScan() {
36+
@Order(2)
37+
@DisplayName("RootConfig class enables @ComponentScan")
38+
void rootConfigClassEnablesComponentScan() {
5839
ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class);
5940

60-
assertThat(componentScan, notNullValue());
41+
assertNotNull(componentScan);
6142
}
6243

6344
@Test
64-
void testRootConfigComponentScanPackages() {
45+
@Order(3)
46+
@DisplayName("RootConfig @ComponentScan contains base packages")
47+
void rootConfigComponentScanPackages() {
6548
ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class);
6649
String[] packages = componentScan.basePackages();
6750
if (packages.length == 0) {
6851
packages = componentScan.value();
6952
}
7053

71-
assertThat(packages, arrayContaining("com.bobocode"));
54+
assertThat(packages).contains("com.bobocode");
7255
}
7356

7457
@Test
75-
void testRootConfigComponentScanFilters() {
58+
@Order(4)
59+
@DisplayName("RootConfig @ComponentScan contains web scan filters")
60+
void rootConfigComponentScanFilters() {
7661
ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class);
7762
Filter[] filters = componentScan.excludeFilters();
7863
List<Class> filteredClasses = getFilteredClasses(filters);
7964

80-
assertThat(filters, arrayWithSize(2));
81-
assertThat(filters[0].type(), equalTo(FilterType.ANNOTATION));
82-
assertThat(filters[1].type(), equalTo(FilterType.ANNOTATION));
83-
assertThat(filteredClasses, containsInAnyOrder(EnableWebMvc.class, Controller.class));
84-
}
85-
86-
private List<Class> getFilteredClasses(Filter[] filters) {
87-
return Stream.of(filters).flatMap(filter -> Stream.of(filter.value())).collect(Collectors.toList());
65+
assertThat(filters.length).isEqualTo(2);
66+
assertThat(filters[0].type()).isEqualTo(FilterType.ANNOTATION);
67+
assertThat(filters[1].type()).isEqualTo(FilterType.ANNOTATION);
68+
assertThat(filteredClasses.toArray()).containsExactlyInAnyOrder(EnableWebMvc.class, Controller.class);
8869
}
8970

9071
@Test
91-
void testWebConfigIsMarkedAsConfiguration() {
72+
@Order(5)
73+
@DisplayName("WebConfig is marked as @Configuration")
74+
void webConfigIsMarkedAsConfiguration() {
9275
Configuration configuration = WebConfig.class.getAnnotation(Configuration.class);
9376

94-
assertThat(configuration, notNullValue());
77+
assertNotNull(configuration);
9578
}
9679

9780
@Test
98-
void testWebConfigEnablesComponentScan() {
81+
@Order(6)
82+
@DisplayName("WebConfig enables @ComponentScan")
83+
void webConfigEnablesComponentScan() {
9984
ComponentScan componentScan = WebConfig.class.getAnnotation(ComponentScan.class);
10085

101-
assertThat(componentScan, notNullValue());
86+
assertNotNull(componentScan);
10287
}
10388

10489
@Test
105-
void testWebConfigComponentScanPackages() {
90+
@Order(7)
91+
@DisplayName("WebConfig @ComponentScan contains web packages")
92+
void webConfigComponentScanPackages() {
10693
ComponentScan componentScan = WebConfig.class.getAnnotation(ComponentScan.class);
10794

108-
assertThat(componentScan.basePackages(), arrayContaining("com.bobocode.web"));
95+
assertThat(componentScan.basePackages()).contains("com.bobocode.web");
10996
}
11097

11198
@Test
112-
void testWebConfigEnablesWebMvc() {
99+
@Order(8)
100+
@DisplayName("WebConfig is marked with @EnableWebMvc")
101+
void webConfigEnablesWebMvc() {
113102
EnableWebMvc enableWebMvc = WebConfig.class.getAnnotation(EnableWebMvc.class);
114103

115-
assertThat(enableWebMvc, notNullValue());
104+
assertNotNull(enableWebMvc);
105+
}
106+
107+
@Test
108+
@Order(9)
109+
@DisplayName("Initializer method is overridden with RootConfig class")
110+
void initializerRootConfigClasses() {
111+
WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper();
112+
113+
assertThat(webAppInitializerWrapper.getRootConfigClasses()).contains(RootConfig.class);
116114
}
117115

118116
@Test
119-
void testWelcomeControllerIsMarkedAsController() {
117+
@Order(10)
118+
@DisplayName("Initializer method is overridden with WebConfig class")
119+
void initializerWebConfigClasses() {
120+
WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper();
121+
122+
assertThat(webAppInitializerWrapper.getServletConfigClasses()).contains(WebConfig.class);
123+
}
124+
125+
@Test
126+
@Order(11)
127+
@DisplayName("Proper Dispatcher Servlet mapping")
128+
void dispatcherServletMapping() {
129+
WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper();
130+
131+
assertThat(webAppInitializerWrapper.getServletMappings()).contains("/");
132+
}
133+
134+
@Test
135+
@Order(12)
136+
@DisplayName("WelcomeController is marked as @Controller")
137+
void welcomeControllerIsMarkedAsController() {
120138
Controller controller = WelcomeController.class.getAnnotation(Controller.class);
121139

122-
assertThat(controller, notNullValue());
140+
assertNotNull(controller);
123141
}
124142

125143
@Test
126-
void testWelcomeControllerMethodIsMarkedAsGetMethod() throws NoSuchMethodException {
144+
@Order(13)
145+
@DisplayName("WelcomeController method is marked as Get method")
146+
void welcomeControllerMethodIsMarkedAsGetMethod() throws NoSuchMethodException {
127147
GetMapping getMapping = WelcomeController.class.getDeclaredMethod("welcome").getAnnotation(GetMapping.class);
128148

129-
assertThat(getMapping, notNullValue());
149+
assertNotNull(getMapping);
130150
}
131151

132152
@Test
133-
void testWelcomeControllerMethodMapping() throws NoSuchMethodException {
153+
@Order(14)
154+
@DisplayName("WelcomeController Get method is marked properly")
155+
void welcomeControllerMethodMapping() throws NoSuchMethodException {
134156
GetMapping getMapping = WelcomeController.class.getDeclaredMethod("welcome").getAnnotation(GetMapping.class);
135157

136-
assertThat(getMapping.value(), arrayContaining("/welcome"));
158+
assertThat(getMapping.value()).contains("/welcome");
159+
}
160+
161+
@Test
162+
@Order(15)
163+
@DisplayName("WelcomeController Get method is marked as @ResponseBody")
164+
void welcomeControllerMethodIsMarkedAsResponseBody() throws NoSuchMethodException {
165+
ResponseBody responseBody = WelcomeController.class.getDeclaredMethod("welcome").getAnnotation(ResponseBody.class);
166+
167+
assertNotNull(responseBody);
168+
}
169+
170+
private List<Class> getFilteredClasses(Filter[] filters) {
171+
return Stream.of(filters).flatMap(filter -> Stream.of(filter.value())).collect(Collectors.toList());
137172
}
138173
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-web-course-util</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>java-web-util</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>11</maven.compiler.source>
16+
<maven.compiler.target>11</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>io.codearte.jfairy</groupId>
22+
<artifactId>jfairy</artifactId>
23+
<version>0.5.9</version>
24+
</dependency>
25+
</dependencies>
26+
27+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.bobocode.util;
2+
3+
/**
4+
* This is a custom exception that we throw in every method which should be implemented as a part of the exercise.
5+
* If you see that it was thrown it means that you did not implement all required methods yet.
6+
*/
7+
public class ExerciseNotCompletedException extends RuntimeException {
8+
public ExerciseNotCompletedException() {
9+
super("Implement this method and remove exception OR switch to branch completed if you got stuck.");
10+
}
11+
}

java-web-course-util/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
<version>1.0-SNAPSHOT</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
11+
<packaging>pom</packaging>
1112

1213
<artifactId>java-web-course-util</artifactId>
1314

15+
<modules>
16+
<module>java-web-util</module>
17+
</modules>
18+
1419
</project>

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
<version>3.18.1</version>
4949
<scope>test</scope>
5050
</dependency>
51+
<dependency>
52+
<groupId>org.slf4j</groupId>
53+
<artifactId>slf4j-simple</artifactId>
54+
<version>1.7.24</version>
55+
</dependency>
5156
<dependency>
5257
<groupId>org.reflections</groupId>
5358
<artifactId>reflections</artifactId>

0 commit comments

Comments
 (0)