1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -47,40 +47,19 @@ public class ClassPathResourceTests {
47
47
private static final Pattern DESCRIPTION_PATTERN = Pattern .compile ("^class path resource \\ [(.+?)\\ ]$" );
48
48
49
49
50
- private void assertDescriptionContainsExpectedPath (ClassPathResource resource , String expectedPath ) {
51
- Matcher matcher = DESCRIPTION_PATTERN .matcher (resource .getDescription ());
52
- assertTrue (matcher .matches ());
53
- assertEquals (1 , matcher .groupCount ());
54
- String match = matcher .group (1 );
55
-
56
- assertEquals (expectedPath , match );
57
- }
58
-
59
- private void assertExceptionContainsFullyQualifiedPath (ClassPathResource resource ) {
60
- try {
61
- resource .getInputStream ();
62
- fail ("FileNotFoundException expected for resource: " + resource );
63
- }
64
- catch (IOException ex ) {
65
- assertThat (ex , instanceOf (FileNotFoundException .class ));
66
- assertThat (ex .getMessage (), containsString (FQ_RESOURCE_PATH ));
67
- }
68
- }
69
-
70
50
@ Test
71
51
public void stringConstructorRaisesExceptionWithFullyQualifiedPath () {
72
52
assertExceptionContainsFullyQualifiedPath (new ClassPathResource (FQ_RESOURCE_PATH ));
73
53
}
74
54
75
55
@ Test
76
56
public void classLiteralConstructorRaisesExceptionWithFullyQualifiedPath () {
77
- assertExceptionContainsFullyQualifiedPath (new ClassPathResource (NONEXISTENT_RESOURCE_NAME , this . getClass ()));
57
+ assertExceptionContainsFullyQualifiedPath (new ClassPathResource (NONEXISTENT_RESOURCE_NAME , getClass ()));
78
58
}
79
59
80
60
@ Test
81
61
public void classLoaderConstructorRaisesExceptionWithFullyQualifiedPath () {
82
- assertExceptionContainsFullyQualifiedPath (new ClassPathResource (FQ_RESOURCE_PATH ,
83
- this .getClass ().getClassLoader ()));
62
+ assertExceptionContainsFullyQualifiedPath (new ClassPathResource (FQ_RESOURCE_PATH , getClass ().getClassLoader ()));
84
63
}
85
64
86
65
@ Test
@@ -91,31 +70,64 @@ public void getDescriptionWithStringConstructor() {
91
70
@ Test
92
71
public void getDescriptionWithStringConstructorAndLeadingSlash () {
93
72
assertDescriptionContainsExpectedPath (new ClassPathResource (FQ_RESOURCE_PATH_WITH_LEADING_SLASH ),
94
- FQ_RESOURCE_PATH );
73
+ FQ_RESOURCE_PATH );
95
74
}
96
75
97
76
@ Test
98
77
public void getDescriptionWithClassLiteralConstructor () {
99
- assertDescriptionContainsExpectedPath (new ClassPathResource (NONEXISTENT_RESOURCE_NAME , this . getClass ()),
100
- FQ_RESOURCE_PATH );
78
+ assertDescriptionContainsExpectedPath (new ClassPathResource (NONEXISTENT_RESOURCE_NAME , getClass ()),
79
+ FQ_RESOURCE_PATH );
101
80
}
102
81
103
82
@ Test
104
83
public void getDescriptionWithClassLiteralConstructorAndLeadingSlash () {
105
84
assertDescriptionContainsExpectedPath (
106
- new ClassPathResource (FQ_RESOURCE_PATH_WITH_LEADING_SLASH , this . getClass ()), FQ_RESOURCE_PATH );
85
+ new ClassPathResource (FQ_RESOURCE_PATH_WITH_LEADING_SLASH , getClass ()), FQ_RESOURCE_PATH );
107
86
}
108
87
109
88
@ Test
110
89
public void getDescriptionWithClassLoaderConstructor () {
111
90
assertDescriptionContainsExpectedPath (
112
- new ClassPathResource (FQ_RESOURCE_PATH , this . getClass ().getClassLoader ()), FQ_RESOURCE_PATH );
91
+ new ClassPathResource (FQ_RESOURCE_PATH , getClass ().getClassLoader ()), FQ_RESOURCE_PATH );
113
92
}
114
93
115
94
@ Test
116
95
public void getDescriptionWithClassLoaderConstructorAndLeadingSlash () {
117
- assertDescriptionContainsExpectedPath (new ClassPathResource (FQ_RESOURCE_PATH_WITH_LEADING_SLASH ,
118
- this .getClass ().getClassLoader ()), FQ_RESOURCE_PATH );
96
+ assertDescriptionContainsExpectedPath (
97
+ new ClassPathResource (FQ_RESOURCE_PATH_WITH_LEADING_SLASH , getClass ().getClassLoader ()), FQ_RESOURCE_PATH );
98
+ }
99
+
100
+ @ Test
101
+ public void dropLeadingSlashForClassLoaderAccess () {
102
+ assertEquals ("test.html" , new ClassPathResource ("/test.html" ).getPath ());
103
+ assertEquals ("test.html" , ((ClassPathResource ) new ClassPathResource ("" ).createRelative ("/test.html" )).getPath ());
104
+ }
105
+
106
+ @ Test
107
+ public void preserveLeadingSlashForClassRelativeAccess () {
108
+ assertEquals ("/test.html" , new ClassPathResource ("/test.html" , getClass ()).getPath ());
109
+ assertEquals ("/test.html" , ((ClassPathResource ) new ClassPathResource ("" , getClass ()).createRelative ("/test.html" )).getPath ());
110
+ }
111
+
112
+
113
+ private void assertDescriptionContainsExpectedPath (ClassPathResource resource , String expectedPath ) {
114
+ Matcher matcher = DESCRIPTION_PATTERN .matcher (resource .getDescription ());
115
+ assertTrue (matcher .matches ());
116
+ assertEquals (1 , matcher .groupCount ());
117
+ String match = matcher .group (1 );
118
+
119
+ assertEquals (expectedPath , match );
120
+ }
121
+
122
+ private void assertExceptionContainsFullyQualifiedPath (ClassPathResource resource ) {
123
+ try {
124
+ resource .getInputStream ();
125
+ fail ("FileNotFoundException expected for resource: " + resource );
126
+ }
127
+ catch (IOException ex ) {
128
+ assertThat (ex , instanceOf (FileNotFoundException .class ));
129
+ assertThat (ex .getMessage (), containsString (FQ_RESOURCE_PATH ));
130
+ }
119
131
}
120
132
121
133
}
0 commit comments