Skip to content

Commit

Permalink
#9 chap02 : 암호 검사기 구현을 통해 TDD 경험해보기
Browse files Browse the repository at this point in the history
  • Loading branch information
sangminee committed Jan 8, 2023
1 parent 64a65e1 commit 6996deb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package chap02;

public enum PasswordStrength {
NORMAL, STRONG
WEAK, INVALID, NORMAL, STRONG
}
37 changes: 37 additions & 0 deletions src/main/java/chap02/PasswordStrengthMeter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package chap02;

public class PasswordStrengthMeter {
public PasswordStrength meter(String s) {
if(s == null || s.isEmpty()) return PasswordStrength.INVALID; // 값이 없는 경우
int metCounts = getMetCriteriaCounts(s);
if(metCounts <= 1) return PasswordStrength.WEAK;
if(metCounts == 2) return PasswordStrength.NORMAL;
return PasswordStrength.STRONG;
}

private int getMetCriteriaCounts(String s) {
int metCounts = 0;
if(s.length() >= 8) metCounts++; // 길이가 8글자 이상인 조건만 충족하는 경우
if(meetsContainingNumberCriteria(s)) metCounts++; // 숫자를 포함하지 않고 나머지 조건은 충족하는 경우
if(meetsContainingUppercaseCriteria(s)) metCounts++; // 대문자를 포함하지 않고 나머지 조건을 충족시킬 경우
return metCounts;
}

private boolean meetsContainingUppercaseCriteria(String s) {
for(char ch : s.toCharArray()){
if(Character.isUpperCase(ch)){
return true;
}
}
return false;
}

private boolean meetsContainingNumberCriteria(String s) {
for(char ch : s.toCharArray()){
if(ch >= '0' && ch <='9'){
return true;
}
}
return false;
}
}
8 changes: 0 additions & 8 deletions src/test/java/chap02/PasswordStrengthMeter.java

This file was deleted.

57 changes: 47 additions & 10 deletions src/test/java/chap02/PasswordStrengthMeterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,58 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

public class PasswordStrengthMeterTest {
private PasswordStrengthMeter meter = new PasswordStrengthMeter();

private void assertStrength(String password, PasswordStrength expStr){
PasswordStrength result = meter.meter(password);
assertEquals(expStr, result);
}

@Test
void meetsAllCriteria_Then_Strong(){
PasswordStrengthMeter meter = new PasswordStrengthMeter();
PasswordStrength result = meter.meter("abcd!@AB");
assertEquals(PasswordStrength.STRONG, result);
PasswordStrength result2 = meter.meter("abcd!Add");
assertEquals(PasswordStrength.STRONG, result2);
assertStrength("ab12!@AB", PasswordStrength.STRONG);
assertStrength("ab12!Add", PasswordStrength.STRONG);
}

@Test
void meetsOtherCriteria_except_for_Length_Then_Normal(){
PasswordStrengthMeter meter = new PasswordStrengthMeter();
PasswordStrength result = meter.meter("abcd!@A");
assertEquals(PasswordStrength.NORMAL, result);
PasswordStrength result2 = meter.meter("abcd!c");
assertEquals(PasswordStrength.NORMAL, result2);
assertStrength("ab12!@A", PasswordStrength.NORMAL);
assertStrength("abc3!B", PasswordStrength.NORMAL);
}

@Test
void meetsOtherCriteria_except_for_number_Then_Normal(){
assertStrength("ab!@ABqwer", PasswordStrength.NORMAL);
}

@Test
void nullInput_Then_Invalid(){
assertStrength(null, PasswordStrength.INVALID);
assertStrength("", PasswordStrength.INVALID);
}

@Test
void meetsOtherCriteria_except_for_Uppercase_Then_Normal(){
assertStrength("ab12!@df", PasswordStrength.NORMAL);
}

@Test
void meetsOnlyLengthCriteria_Then_Weak(){
assertStrength("abdefghi",PasswordStrength.WEAK);
}

@Test
void meetsOnlyNumCriteria_Then_Weak(){
assertStrength("12345",PasswordStrength.WEAK);
}

@Test
void meetsOnlyUpperCriteria_Then_Weak(){
assertStrength("ABZEF",PasswordStrength.WEAK);
}

@Test
void meetsNoCriteria_Then_Weak(){
assertStrength("abc",PasswordStrength.WEAK);
}
}

0 comments on commit 6996deb

Please sign in to comment.