|
| 1 | +package com.baeldung.applicationcontext; |
| 2 | + |
| 3 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.Locale; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | +import org.springframework.context.ApplicationContext; |
| 11 | +import org.springframework.context.ConfigurableApplicationContext; |
| 12 | +import org.springframework.context.MessageSource; |
| 13 | +import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 14 | + |
| 15 | +public class ClasspathXmlApplicationContextTest { |
| 16 | + @Test |
| 17 | + public void testBasicUsage() { |
| 18 | + ApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml"); |
| 19 | + Student student = (Student) context.getBean("student"); |
| 20 | + assertThat(student.getNo(), equalTo(15)); |
| 21 | + assertThat(student.getName(), equalTo("Tom")); |
| 22 | + |
| 23 | + Student sameStudent = context.getBean("student", Student.class);// do not need cast class |
| 24 | + assertThat(sameStudent.getNo(), equalTo(15)); |
| 25 | + assertThat(sameStudent.getName(), equalTo("Tom")); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testRegisterShutdownHook() { |
| 30 | + ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml"); |
| 31 | + context.registerShutdownHook(); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testInternationalization() { |
| 36 | + MessageSource resources = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-internationalization.xml"); |
| 37 | + |
| 38 | + String enHello = resources.getMessage("hello", null, "Default", Locale.ENGLISH); |
| 39 | + String enYou = resources.getMessage("you", null, Locale.ENGLISH); |
| 40 | + String enThanks = resources.getMessage("thanks", new Object[] { enYou }, Locale.ENGLISH); |
| 41 | + assertThat(enHello, equalTo("hello")); |
| 42 | + assertThat(enThanks, equalTo("thank you")); |
| 43 | + |
| 44 | + String chHello = resources.getMessage("hello", null, "Default", Locale.SIMPLIFIED_CHINESE); |
| 45 | + String chYou = resources.getMessage("you", null, Locale.SIMPLIFIED_CHINESE); |
| 46 | + String chThanks = resources.getMessage("thanks", new Object[] { chYou }, Locale.SIMPLIFIED_CHINESE); |
| 47 | + assertThat(chHello, equalTo("你好")); |
| 48 | + assertThat(chThanks, equalTo("谢谢你")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testApplicationContextAware() { |
| 53 | + ApplicationContext context = new ClassPathXmlApplicationContext("classpathxmlapplicationcontext-example.xml"); |
| 54 | + Teacher teacher = context.getBean("teacher", Teacher.class); |
| 55 | + List<Course> courses = teacher.getCourses(); |
| 56 | + assertThat(courses.size(), equalTo(1)); |
| 57 | + assertThat(courses.get(0).getName(), equalTo("math")); |
| 58 | + } |
| 59 | +} |
0 commit comments