Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 각 테스트가 독립적으로 실행되지 않는 오류 해결 (#23) #24

Merged
merged 1 commit into from
Feb 17, 2024
Merged
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
52 changes: 49 additions & 3 deletions src/main/resources/code_sample/TestHelper.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
* 이 테스트코드는 <a href="https://github.com/PENEKhun/Baekjoon-java-starter">Baekjoon-java-starter </a>를
* 사용하여 생성되었습니다.
* 이 테스트코드는 <a href="https://github.com/PENEKhun/Baekjoon-java-starter">Baekjoon-java-starter </a>를 사용하여
* 생성되었습니다.
*
* @Author : PENEKhun
*/
public class TestHelper {

private static final HashMap<Field, Object> initialStates = new HashMap<>();

public static void main(String[] args) {
TestCase[] testCases = new TestCase[]{
captureInitialState();

TestCase[] testCases = new TestCase[] {
// {{test_case}}
};

int passedCases = 0;

for (int i = 0; i < testCases.length; i++) {
resetToInitialState();
TestCase testCase = testCases[i];

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Expand Down Expand Up @@ -54,6 +64,42 @@ public static void main(String[] args) {
}
}

private static void captureInitialState() {
try {
Class<?> clazz = Main.class;
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (Modifier.isStatic(field.getModifiers())) {
field.setAccessible(true);
initialStates.put(field, field.get(null));
}
}
} catch (Exception e) {
System.out.println(red("Main 클래스에 접근할 수 없습니다."));
}
}

private static void resetToInitialState() {
try {
for (Map.Entry<Field, Object> entry : initialStates.entrySet()) {
Field field = entry.getKey();
field.setAccessible(true);
Object value = entry.getValue();
if (value instanceof Collection) {
((Collection<?>) value).clear();
} else if (value instanceof Map) {
((Map<?, ?>) value).clear();
} else if (value instanceof StringBuilder) {
((StringBuilder) value).setLength(0);
} else {
field.set(null, value);
}
}
} catch (IllegalAccessException e) {
System.out.println(red("Main 클래스에 접근할 수 없습니다."));
}
}

public static void printFail(int caseNumber, TestCase testCase, String message) {
System.out.printf("""
====== %s ======
Expand Down
Loading