Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletRegistration;
import nextstep.mvc.DispatcherServlet;
import nextstep.mvc.controller.tobe.AnnotationHandlerAdapter;
import nextstep.mvc.controller.tobe.AnnotationHandlerMapping;
import nextstep.mvc.controller.tobe.ManualHandlerAdapter;
import nextstep.web.WebApplicationInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AppWebApplicationInitializer implements WebApplicationInitializer {

private static final Logger log = LoggerFactory.getLogger(AppWebApplicationInitializer.class);
private static final String BASE_PACKAGE = "com.techcourse";

@Override
public void onStartup(ServletContext servletContext) {
final DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.addHandlerMapping(new ManualHandlerMapping());

dispatcherServlet.addHandlerMapping(new AnnotationHandlerMapping(BASE_PACKAGE));
dispatcherServlet.addHandlerAdapter(new ManualHandlerAdapter());
dispatcherServlet.addHandlerAdapter(new AnnotationHandlerAdapter());
final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
Expand Down
24 changes: 23 additions & 1 deletion learning/src/test/java/reflection/Junit3TestRunner.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;

class Junit3TestRunner {

@Test
void run() throws Exception {
// given
Class<Junit3Test> clazz = Junit3Test.class;
Method[] methods = clazz.getDeclaredMethods();
Constructor<Junit3Test> constructor = clazz.getConstructor();
Junit3Test junit3Test = constructor.newInstance();

// TODO Junit3Test에서 test로 시작하는 메소드 실행
// when, then
Arrays.stream(methods)
.forEach(execute(junit3Test));
}

private Consumer<Method> execute(Junit3Test junit3Test) {
return method -> {
method.setAccessible(true);
try {
method.invoke(junit3Test);
} catch (IllegalAccessException | InvocationTargetException exception) {
exception.printStackTrace();
}
};
}
}
22 changes: 21 additions & 1 deletion learning/src/test/java/reflection/Junit4TestRunner.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
package reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;

class Junit4TestRunner {

@Test
void run() throws Exception {
// given
Class<Junit4Test> clazz = Junit4Test.class;
Method[] methods = clazz.getDeclaredMethods();
Constructor<Junit4Test> constructor = clazz.getDeclaredConstructor();
Junit4Test junit4Test = constructor.newInstance();

// TODO Junit4Test에서 @MyTest 애노테이션이 있는 메소드 실행
// when, then
for (Method method : methods) {
if (method.isAnnotationPresent(MyTest.class)) {
execute(method, junit4Test);
}
}
}

private void execute(Method method, Junit4Test junit4Test) {
try {
method.invoke(junit4Test);
} catch (IllegalAccessException | InvocationTargetException exception) {
exception.printStackTrace();
}
}
}
123 changes: 87 additions & 36 deletions learning/src/test/java/reflection/ReflectionTest.java
Original file line number Diff line number Diff line change
@@ -1,76 +1,111 @@
package reflection;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.assertj.core.api.Assertions.assertThat;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class ReflectionTest {

private static final Logger log = LoggerFactory.getLogger(ReflectionTest.class);

@Test
void givenObject_whenGetsClassName_thenCorrect() {
// given
final Class<Question> clazz = Question.class;

assertThat(clazz.getSimpleName()).isEqualTo("");
assertThat(clazz.getName()).isEqualTo("");
assertThat(clazz.getCanonicalName()).isEqualTo("");
// when, then
assertThat(clazz.getSimpleName()).isEqualTo("Question");
assertThat(clazz.getName()).isEqualTo("reflection.Question");
assertThat(clazz.getCanonicalName()).isEqualTo("reflection.Question");
}

@Test
void givenClassName_whenCreatesObject_thenCorrect() throws ClassNotFoundException {
// given
final Class<?> clazz = Class.forName("reflection.Question");

assertThat(clazz.getSimpleName()).isEqualTo("");
assertThat(clazz.getName()).isEqualTo("");
assertThat(clazz.getCanonicalName()).isEqualTo("");
// when, then
assertThat(clazz.getSimpleName()).isEqualTo("Question");
assertThat(clazz.getName()).isEqualTo("reflection.Question");
assertThat(clazz.getCanonicalName()).isEqualTo("reflection.Question");
}

@Test
void givenObject_whenGetsFieldNamesAtRuntime_thenCorrect() {
// given
final Object student = new Student();
final Field[] fields = null;
final List<String> actualFieldNames = null;
final Field[] fields = student.getClass().getDeclaredFields();

// when
final List<String> actualFieldNames = Arrays.stream(fields)
.map(Field::getName)
.collect(Collectors.toList());

// then
assertThat(actualFieldNames).contains("name", "age");
}

@Test
void givenClass_whenGetsMethods_thenCorrect() {
// given
final Class<?> animalClass = Student.class;
final Method[] methods = null;
final List<String> actualMethods = null;
final Method[] methods = animalClass.getDeclaredMethods();

// when
final List<String> actualMethods = Arrays.stream(methods)
.map(Method::getName)
.collect(Collectors.toList());

// then
assertThat(actualMethods)
.hasSize(3)
.contains("getAge", "toString", "getName");
.hasSize(3)
.contains("getAge", "toString", "getName");
}

@Test
void givenClass_whenGetsAllConstructors_thenCorrect() {
// given
final Class<?> questionClass = Question.class;
final Constructor<?>[] constructors = null;

// when
final Constructor<?>[] constructors = questionClass.getDeclaredConstructors();

// then
assertThat(constructors).hasSize(2);
}

@Test
void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect() throws Exception {
// given
final Class<?> questionClass = Question.class;

final Constructor<?> firstConstructor = null;
final Constructor<?> secondConstructor = null;

final Question firstQuestion = null;
final Question secondQuestion = null;

final Constructor<?> firstConstructor =
questionClass.getDeclaredConstructor(String.class, String.class, String.class);
final Constructor<?> secondConstructor =
questionClass.getDeclaredConstructor(
long.class,
String.class,
String.class,
String.class,
Date.class,
int.class
);

// when
final Question firstQuestion =
(Question) firstConstructor.newInstance("gugu", "제목1", "내용1");
final Question secondQuestion =
(Question) secondConstructor.newInstance(1L, "gugu", "제목2", "내용2", new Date(), 0);

// then
assertThat(firstQuestion.getWriter()).isEqualTo("gugu");
assertThat(firstQuestion.getTitle()).isEqualTo("제목1");
assertThat(firstQuestion.getContents()).isEqualTo("내용1");
Expand All @@ -81,50 +116,66 @@ void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect() throws Exception

@Test
void givenClass_whenGetsPublicFields_thenCorrect() {
// given
final Class<?> questionClass = Question.class;
final Field[] fields = null;

assertThat(fields).hasSize(0);
// when
final Field[] fields = questionClass.getFields();

// then
assertThat(fields).isEmpty();
}

@Test
void givenClass_whenGetsDeclaredFields_thenCorrect() {
// given
final Class<?> questionClass = Question.class;
final Field[] fields = null;

// when
final Field[] fields = questionClass.getDeclaredFields();

// then
assertThat(fields).hasSize(6);
assertThat(fields[0].getName()).isEqualTo("questionId");
}

@Test
void givenClass_whenGetsFieldsByName_thenCorrect() throws Exception {
// given
final Class<?> questionClass = Question.class;
final Field field = null;

// when
final Field field = questionClass.getDeclaredField("questionId");

// then
assertThat(field.getName()).isEqualTo("questionId");
}

@Test
void givenClassField_whenGetsType_thenCorrect() throws Exception {
// given
final Field field = Question.class.getDeclaredField("questionId");
final Class<?> fieldClass = null;

// when
final Class<?> fieldClass = field.getType();

// then
assertThat(fieldClass.getSimpleName()).isEqualTo("long");
}

@Test
void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
// given
final Class<?> studentClass = Student.class;
final Student student = null;
final Field field = null;

// todo field에 접근 할 수 있도록 만든다.
final Student student = (Student) studentClass.getDeclaredConstructor().newInstance();
final Field field = studentClass.getDeclaredField("age");

// when, then
field.setAccessible(true);
assertThat(field.getInt(student)).isZero();
assertThat(student.getAge()).isZero();

field.set(null, null);

field.set(student, 99);
assertThat(field.getInt(student)).isEqualTo(99);
assertThat(student.getAge()).isEqualTo(99);
}
Expand Down
19 changes: 18 additions & 1 deletion learning/src/test/java/reflection/ReflectionsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package reflection;

import annotation.Controller;
import annotation.Repository;
import annotation.Service;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.reflections.Reflections;
import org.slf4j.Logger;
Expand All @@ -11,8 +15,21 @@ class ReflectionsTest {

@Test
void showAnnotationClass() throws Exception {
// given
Reflections reflections = new Reflections("examples");
Set<Class<?>> controllers = reflections.getTypesAnnotatedWith(Controller.class);
Set<Class<?>> services = reflections.getTypesAnnotatedWith(Service.class);
Set<Class<?>> repositories = reflections.getTypesAnnotatedWith(Repository.class);

// TODO 클래스 레벨에 @Controller, @Service, @Repository 애노테이션이 설정되어 모든 클래스 찾아 로그로 출력한다.
// when, then
for (Class<?> controller : controllers) {
log.info("name : {}", controller.getSimpleName());
}
for (Class<?> service : services) {
log.info("name : {}", service.getSimpleName());
}
for (Class<?> repository : repositories) {
log.info("name : {}", repository.getSimpleName());
}
}
}
24 changes: 24 additions & 0 deletions mvc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
plugins {
id 'java'
id 'org.sonarqube' version '3.3'
id 'jacoco'
}

repositories {
Expand Down Expand Up @@ -27,4 +29,26 @@ dependencies {

tasks.named('test') {
useJUnitPlatform()
finalizedBy(jacocoTestReport)
}

sonarqube {
properties {
property "sonar.sourceEncoding", "UTF-8"
property "sonar.projectKey", "woowacourse_be-jwp-http-server"
property "sonar.organization", "woowacourse"
property "sonar.host.url", "https://sonarcloud.io"
property "sonar.coverage.jacoco.xmlReportPaths", "jacoco/jacoco.xml"
}
}

jacoco {
toolVersion = "0.8.5"
}

jacocoTestReport {
reports {
xml.enabled true
xml.destination(new File("jacoco/jacoco.xml"))
}
}
1 change: 1 addition & 0 deletions mvc/jacoco/jacoco.xml

Large diffs are not rendered by default.

Loading