📄 servletcontextsupporttests.java
字号:
TestBean tb2 = (TestBean) wac.getBean("tb2");
assertEquals(98, tb1.getAge());
assertEquals("yourkey4namemyvarmyvar${", tb1.getName());
assertEquals(tb2, tb1.getSpouse());
}
public void testServletContextPropertyPlaceholderConfigurerWithContextOverride() {
MockServletContext sc = new MockServletContext();
sc.addInitParameter("key4", "mykey4");
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(sc);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("age", "${age}");
pvs.addPropertyValue("name", "${key4}name${var}${var}${");
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
wac.registerSingleton("tb1", TestBean.class, pvs);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, null);
wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my\nkey4=yourkey4");
pvs.addPropertyValue("contextOverride", Boolean.TRUE);
wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
wac.refresh();
TestBean tb1 = (TestBean) wac.getBean("tb1");
TestBean tb2 = (TestBean) wac.getBean("tb2");
assertEquals(98, tb1.getAge());
assertEquals("mykey4namemyvarmyvar${", tb1.getName());
assertEquals(tb2, tb1.getSpouse());
}
public void testServletContextPropertyPlaceholderConfigurerWithContextOverrideAndAttributes() {
MockServletContext sc = new MockServletContext();
sc.addInitParameter("key4", "mykey4");
sc.setAttribute("key4", "attrkey4");
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(sc);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("age", "${age}");
pvs.addPropertyValue("name", "${key4}name${var}${var}${");
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
wac.registerSingleton("tb1", TestBean.class, pvs);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, null);
wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("properties", "age=98\nvar=${m}var\nref=tb2\nm=my\nkey4=yourkey4");
pvs.addPropertyValue("contextOverride", Boolean.TRUE);
pvs.addPropertyValue("searchContextAttributes", Boolean.TRUE);
wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
wac.refresh();
TestBean tb1 = (TestBean) wac.getBean("tb1");
TestBean tb2 = (TestBean) wac.getBean("tb2");
assertEquals(98, tb1.getAge());
assertEquals("attrkey4namemyvarmyvar${", tb1.getName());
assertEquals(tb2, tb1.getSpouse());
}
public void testServletContextPropertyPlaceholderConfigurerWithAttributes() {
MockServletContext sc = new MockServletContext();
sc.addInitParameter("key4", "mykey4");
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(sc);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("age", "${age}");
pvs.addPropertyValue("name", "name${var}${var}${");
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
wac.registerSingleton("tb1", TestBean.class, pvs);
ConstructorArgumentValues cas = new ConstructorArgumentValues();
cas.addIndexedArgumentValue(1, "${age}");
cas.addGenericArgumentValue("${var}name${age}");
pvs = new MutablePropertyValues();
List friends = new ManagedList();
friends.add("na${age}me");
friends.add(new RuntimeBeanReference("${ref}"));
pvs.addPropertyValue("friends", friends);
Set someSet = new ManagedSet();
someSet.add("na${age}me");
someSet.add(new RuntimeBeanReference("${ref}"));
pvs.addPropertyValue("someSet", someSet);
Map someMap = new ManagedMap();
someMap.put("key1", new RuntimeBeanReference("${ref}"));
someMap.put("key2", "${age}name");
MutablePropertyValues innerPvs = new MutablePropertyValues();
innerPvs.addPropertyValue("touchy", "${os.name}");
someMap.put("key3", new RootBeanDefinition(TestBean.class, innerPvs));
MutablePropertyValues innerPvs2 = new MutablePropertyValues(innerPvs);
someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child"));
pvs.addPropertyValue("someMap", someMap);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, cas, pvs);
wac.getDefaultListableBeanFactory().registerBeanDefinition("tb2", bd);
pvs = new MutablePropertyValues();
pvs.addPropertyValue("properties", "var=${m}var\nref=tb2\nm=my");
pvs.addPropertyValue("searchContextAttributes", Boolean.TRUE);
wac.registerSingleton("configurer", ServletContextPropertyPlaceholderConfigurer.class, pvs);
sc.setAttribute("age", new Integer(98));
wac.refresh();
TestBean tb1 = (TestBean) wac.getBean("tb1");
TestBean tb2 = (TestBean) wac.getBean("tb2");
assertEquals(98, tb1.getAge());
assertEquals(98, tb2.getAge());
assertEquals("namemyvarmyvar${", tb1.getName());
assertEquals("myvarname98", tb2.getName());
assertEquals(tb2, tb1.getSpouse());
assertEquals(2, tb2.getFriends().size());
assertEquals("na98me", tb2.getFriends().iterator().next());
assertEquals(tb2, tb2.getFriends().toArray()[1]);
assertEquals(2, tb2.getSomeSet().size());
assertTrue(tb2.getSomeSet().contains("na98me"));
assertTrue(tb2.getSomeSet().contains(tb2));
assertEquals(4, tb2.getSomeMap().size());
assertEquals(tb2, tb2.getSomeMap().get("key1"));
assertEquals("98name", tb2.getSomeMap().get("key2"));
TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3");
TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4");
assertEquals(0, inner1.getAge());
assertEquals(null, inner1.getName());
assertEquals(System.getProperty("os.name"), inner1.getTouchy());
assertEquals(98, inner2.getAge());
assertEquals("namemyvarmyvar${", inner2.getName());
assertEquals(System.getProperty("os.name"), inner2.getTouchy());
}
public void testServletContextResourcePatternResolver() throws IOException {
final Set paths = new HashSet();
paths.add("/WEB-INF/context1.xml");
paths.add("/WEB-INF/context2.xml");
MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {
public Set getResourcePaths(String path) {
if ("/WEB-INF/".equals(path)) {
return paths;
}
return null;
}
};
ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc);
Resource[] found = rpr.getResources("/WEB-INF/*.xml");
Set foundPaths = new HashSet();
for (int i = 0; i < found.length; i++) {
foundPaths.add(((ServletContextResource) found[i]).getPath());
}
assertEquals(2, foundPaths.size());
assertTrue(foundPaths.contains("/WEB-INF/context1.xml"));
assertTrue(foundPaths.contains("/WEB-INF/context2.xml"));
}
public void testServletContextResourcePatternResolverWithAbsolutePaths() throws IOException {
final Set paths = new HashSet();
paths.add("C:/webroot/WEB-INF/context1.xml");
paths.add("C:/webroot/WEB-INF/context2.xml");
paths.add("C:/webroot/someOtherDirThatDoesntContainPath");
MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {
public Set getResourcePaths(String path) {
if ("/WEB-INF/".equals(path)) {
return paths;
}
return null;
}
};
ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc);
Resource[] found = rpr.getResources("/WEB-INF/*.xml");
Set foundPaths = new HashSet();
for (int i = 0; i < found.length; i++) {
foundPaths.add(((ServletContextResource) found[i]).getPath());
}
assertEquals(2, foundPaths.size());
assertTrue(foundPaths.contains("/WEB-INF/context1.xml"));
assertTrue(foundPaths.contains("/WEB-INF/context2.xml"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -