1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.xtuer.beans.User; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IdentifierTest { private static ApplicationContext context; private static ObjectMapper mapper = new ObjectMapper();
@BeforeClass public static void setup() { context = new ClassPathXmlApplicationContext("spring-beans.xml"); }
@AfterClass public static void tearDown() { }
@Test public void testBeanById() { User alice = context.getBean("alice", User.class); output(alice); }
@Test public void testBeanByName() { User robert = context.getBean("robert", User.class); output(robert);
User hacker = context.getBean("hacker", User.class); output(hacker);
User developer = context.getBean("developer", User.class); output(developer);
Assert.assertEquals(robert, hacker); Assert.assertEquals(robert, developer); Assert.assertEquals(hacker, developer); }
@Test public void testBeanByAlias() { User alice = context.getBean("alice", User.class); output(alice);
User userX = context.getBean("user-x", User.class); output(userX);
Assert.assertEquals(alice, userX); }
@Test public void testBeanByGeneratedIdentifier() { User user = context.getBean("com.xtuer.beans.User", User.class); output(user);
User user0 = context.getBean("com.xtuer.beans.User#0", User.class); output(user0);
User user1 = context.getBean("com.xtuer.beans.User#1", User.class); output(user1);
User user2 = context.getBean("com.xtuer.beans.User#2", User.class); output(user2);
Assert.assertEquals(user, user0); Assert.assertNotEquals(user0, user1); Assert.assertNotEquals(user0, user2); Assert.assertNotEquals(user1, user2); }
private void output(Object obj) { try { System.out.println(mapper.writeValueAsString(obj)); } catch (JsonProcessingException e) { e.printStackTrace(); } } }
|