Skip to content

Commit

Permalink
fix: validation 과정에서 NullPointException이 발생할 수 있는 버그 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
redcarrot1 committed Jan 16, 2024
1 parent e817075 commit 398b278
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ public void initialize(ValidEnum constraintAnnotation) {
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
Object[] enumValues = this.annotation.enumClass().getEnumConstants();

if (enumValues != null) {
for (Object enumValue : enumValues) {
// 대소문자 구분하지 않기
if (value.equalsIgnoreCase(enumValue.toString())) {
if (enumValue.toString().equalsIgnoreCase(value)) {
return true;
}
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public void initialize(Latitude constraintAnnotation) {

@Override
public boolean isValid(Double value, ConstraintValidatorContext context) {
if (value == null) return false;
return -90 <= value && value <= 90;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public void initialize(Longitude constraintAnnotation) {

@Override
public boolean isValid(Double value, ConstraintValidatorContext context) {
if (value == null) return false;
return -180 <= value && value <= 180;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -12,7 +13,7 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidEnum {

String message() default "Enum 에 없는 값입니다.";
String message() default "잘못된 데이터입니다.";

Class<?>[] groups() default {};

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/playkuround/playkuroundserver/JavaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.playkuround.playkuroundserver;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

public class JavaTest {

@Test
@DisplayName("Wrapper가 null일 때는 비교 연산자를 사용할 수 없다.(NullPointerException)")
void wrapperTest() {
Double value = null;

assertThatThrownBy(() -> {
boolean result = value <= 90.0;
}).isInstanceOf(NullPointerException.class);

assertThatThrownBy(() -> {
boolean result = value == 90.0;
}).isInstanceOf(NullPointerException.class);
}
}

0 comments on commit 398b278

Please sign in to comment.