提交 55e601c3 编写于 作者: J Juergen Hoeller

Revise system property replacement tests

See gh-22959
上级 515d627a
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -34,7 +34,7 @@ import static org.junit.Assert.*; ...@@ -34,7 +34,7 @@ import static org.junit.Assert.*;
public class ResourceEditorTests { public class ResourceEditorTests {
@Test @Test
public void sunnyDay() throws Exception { public void sunnyDay() {
PropertyEditor editor = new ResourceEditor(); PropertyEditor editor = new ResourceEditor();
editor.setAsText("classpath:org/springframework/core/io/ResourceEditorTests.class"); editor.setAsText("classpath:org/springframework/core/io/ResourceEditorTests.class");
Resource resource = (Resource) editor.getValue(); Resource resource = (Resource) editor.getValue();
...@@ -43,19 +43,19 @@ public class ResourceEditorTests { ...@@ -43,19 +43,19 @@ public class ResourceEditorTests {
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void ctorWithNullCtorArgs() throws Exception { public void ctorWithNullCtorArgs() {
new ResourceEditor(null, null); new ResourceEditor(null, null);
} }
@Test @Test
public void setAndGetAsTextWithNull() throws Exception { public void setAndGetAsTextWithNull() {
PropertyEditor editor = new ResourceEditor(); PropertyEditor editor = new ResourceEditor();
editor.setAsText(null); editor.setAsText(null);
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
} }
@Test @Test
public void setAndGetAsTextWithWhitespaceResource() throws Exception { public void setAndGetAsTextWithWhitespaceResource() {
PropertyEditor editor = new ResourceEditor(); PropertyEditor editor = new ResourceEditor();
editor.setAsText(" "); editor.setAsText(" ");
assertEquals("", editor.getAsText()); assertEquals("", editor.getAsText());
...@@ -63,6 +63,20 @@ public class ResourceEditorTests { ...@@ -63,6 +63,20 @@ public class ResourceEditorTests {
@Test @Test
public void testSystemPropertyReplacement() { public void testSystemPropertyReplacement() {
PropertyEditor editor = new ResourceEditor();
System.setProperty("test.prop", "foo");
try {
editor.setAsText("${test.prop}");
Resource resolved = (Resource) editor.getValue();
assertEquals("foo", resolved.getFilename());
}
finally {
System.getProperties().remove("test.prop");
}
}
@Test
public void testSystemPropertyReplacementWithUnresolvablePlaceholder() {
PropertyEditor editor = new ResourceEditor(); PropertyEditor editor = new ResourceEditor();
System.setProperty("test.prop", "foo"); System.setProperty("test.prop", "foo");
try { try {
...@@ -76,13 +90,11 @@ public class ResourceEditorTests { ...@@ -76,13 +90,11 @@ public class ResourceEditorTests {
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testStrictSystemPropertyReplacement() { public void testStrictSystemPropertyReplacementWithUnresolvablePlaceholder() {
PropertyEditor editor = new ResourceEditor(new DefaultResourceLoader(), new StandardEnvironment(), false); PropertyEditor editor = new ResourceEditor(new DefaultResourceLoader(), new StandardEnvironment(), false);
System.setProperty("test.prop", "foo"); System.setProperty("test.prop", "foo");
try { try {
editor.setAsText("${test.prop}-${bar}"); editor.setAsText("${test.prop}-${bar}");
Resource resolved = (Resource) editor.getValue();
assertEquals("foo-${bar}", resolved.getFilename());
} }
finally { finally {
System.getProperties().remove("test.prop"); System.getProperties().remove("test.prop");
......
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -32,7 +32,7 @@ import static org.junit.Assert.*; ...@@ -32,7 +32,7 @@ import static org.junit.Assert.*;
public class ResourceArrayPropertyEditorTests { public class ResourceArrayPropertyEditorTests {
@Test @Test
public void testVanillaResource() throws Exception { public void testVanillaResource() {
PropertyEditor editor = new ResourceArrayPropertyEditor(); PropertyEditor editor = new ResourceArrayPropertyEditor();
editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class"); editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class");
Resource[] resources = (Resource[]) editor.getValue(); Resource[] resources = (Resource[]) editor.getValue();
...@@ -41,7 +41,7 @@ public class ResourceArrayPropertyEditorTests { ...@@ -41,7 +41,7 @@ public class ResourceArrayPropertyEditorTests {
} }
@Test @Test
public void testPatternResource() throws Exception { public void testPatternResource() {
// N.B. this will sometimes fail if you use classpath: instead of classpath*:. // N.B. this will sometimes fail if you use classpath: instead of classpath*:.
// The result depends on the classpath - if test-classes are segregated from classes // The result depends on the classpath - if test-classes are segregated from classes
// and they come first on the classpath (like in Maven) then it breaks, if classes // and they come first on the classpath (like in Maven) then it breaks, if classes
...@@ -58,9 +58,9 @@ public class ResourceArrayPropertyEditorTests { ...@@ -58,9 +58,9 @@ public class ResourceArrayPropertyEditorTests {
PropertyEditor editor = new ResourceArrayPropertyEditor(); PropertyEditor editor = new ResourceArrayPropertyEditor();
System.setProperty("test.prop", "foo"); System.setProperty("test.prop", "foo");
try { try {
editor.setAsText("${test.prop}-${bar}"); editor.setAsText("${test.prop}");
Resource[] resources = (Resource[]) editor.getValue(); Resource[] resources = (Resource[]) editor.getValue();
assertEquals("foo-${bar}", resources[0].getFilename()); assertEquals("foo", resources[0].getFilename());
} }
finally { finally {
System.getProperties().remove("test.prop"); System.getProperties().remove("test.prop");
...@@ -68,15 +68,13 @@ public class ResourceArrayPropertyEditorTests { ...@@ -68,15 +68,13 @@ public class ResourceArrayPropertyEditorTests {
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testStrictSystemPropertyReplacement() { public void testStrictSystemPropertyReplacementWithUnresolvablePlaceholder() {
PropertyEditor editor = new ResourceArrayPropertyEditor( PropertyEditor editor = new ResourceArrayPropertyEditor(
new PathMatchingResourcePatternResolver(), new StandardEnvironment(), new PathMatchingResourcePatternResolver(), new StandardEnvironment(),
false); false);
System.setProperty("test.prop", "foo"); System.setProperty("test.prop", "foo");
try { try {
editor.setAsText("${test.prop}-${bar}"); editor.setAsText("${test.prop}-${bar}");
Resource[] resources = (Resource[]) editor.getValue();
assertEquals("foo-${bar}", resources[0].getFilename());
} }
finally { finally {
System.getProperties().remove("test.prop"); System.getProperties().remove("test.prop");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册