위에서 볼 수 있듯이 예제 6-1은 그저 메뉴만 기술하는 일반 텍스트 파일이다. 다음으로 메뉴를 로드하는 방법을 살펴보고 콘솔에 메뉴를 출력하도록 테스트를 수정할 것이다. 예제 6-2를 보자.
예제 6-2 MyDocumentsTest.java
package com.apress.isf.spring.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
public class MyDocumentsTest {
private static final Logger log =
LoggerFactory.getLogger(MyDocumentsTest.class);
private ClassPathXmlApplicationContext context;
@Before
public void setup() {
context = new ClassPathXmlApplicationContext(
"META-INF/spring/mydocuments-context.xml");
}
@Test
public void testMenu() {
log.debug("About to read the Resource file: menu.txt ");
Resource resource = context.getResource(
"classpath:META-INF/data/menu.txt");
try {
InputStream stream = resource.getInputStream();
Scanner scanner = new Scanner(stream);
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
scanner.close();
stream.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}