1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2012 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.
17
17
package org .springframework .core .io ;
18
18
19
19
import static org .hamcrest .CoreMatchers .instanceOf ;
20
+ import static org .junit .Assert .assertEquals ;
20
21
import static org .junit .Assert .assertThat ;
22
+ import static org .junit .Assert .assertTrue ;
21
23
import static org .junit .Assert .fail ;
22
24
import static org .junit .internal .matchers .StringContains .containsString ;
23
25
24
26
import java .io .FileNotFoundException ;
25
27
import java .io .IOException ;
28
+ import java .util .regex .Matcher ;
29
+ import java .util .regex .Pattern ;
26
30
27
31
import org .junit .Test ;
28
32
29
33
/**
30
- * Unit tests cornering bug SPR-6888.
34
+ * Unit tests that serve as regression tests for the bugs described in SPR-6888
35
+ * and SPR-9413.
31
36
*
32
37
* @author Chris Beams
38
+ * @author Sam Brannen
33
39
*/
34
40
public class ClassPathResourceTests {
41
+
35
42
private static final String PACKAGE_PATH = "org/springframework/core/io" ;
36
- private static final String RESOURCE_NAME = "notexist.xml" ;
37
- private static final String FQ_RESOURCE_PATH = PACKAGE_PATH + '/' + RESOURCE_NAME ;
43
+ private static final String NONEXISTENT_RESOURCE_NAME = "nonexistent.xml" ;
44
+ private static final String FQ_RESOURCE_PATH = PACKAGE_PATH + '/' + NONEXISTENT_RESOURCE_NAME ;
45
+
46
+ /**
47
+ * Absolute path version of {@link #FQ_RESOURCE_PATH}.
48
+ */
49
+ private static final String FQ_RESOURCE_PATH_WITH_LEADING_SLASH = '/' + FQ_RESOURCE_PATH ;
50
+
51
+ private static final Pattern DESCRIPTION_PATTERN = Pattern .compile ("^class path resource \\ [(.+?)\\ ]$" );
52
+
53
+
54
+ private void assertDescriptionContainsExpectedPath (ClassPathResource resource , String expectedPath ) {
55
+ Matcher matcher = DESCRIPTION_PATTERN .matcher (resource .getDescription ());
56
+ assertTrue (matcher .matches ());
57
+ assertEquals (1 , matcher .groupCount ());
58
+ String match = matcher .group (1 );
59
+
60
+ assertEquals (expectedPath , match );
61
+ }
62
+
63
+ private void assertExceptionContainsFullyQualifiedPath (ClassPathResource resource ) {
64
+ try {
65
+ resource .getInputStream ();
66
+ fail ("FileNotFoundException expected for resource: " + resource );
67
+ }
68
+ catch (IOException ex ) {
69
+ assertThat (ex , instanceOf (FileNotFoundException .class ));
70
+ assertThat (ex .getMessage (), containsString (FQ_RESOURCE_PATH ));
71
+ }
72
+ }
38
73
39
74
@ Test
40
75
public void stringConstructorRaisesExceptionWithFullyQualifiedPath () {
@@ -43,21 +78,48 @@ public void stringConstructorRaisesExceptionWithFullyQualifiedPath() {
43
78
44
79
@ Test
45
80
public void classLiteralConstructorRaisesExceptionWithFullyQualifiedPath () {
46
- assertExceptionContainsFullyQualifiedPath (new ClassPathResource (RESOURCE_NAME , this .getClass ()));
81
+ assertExceptionContainsFullyQualifiedPath (new ClassPathResource (NONEXISTENT_RESOURCE_NAME , this .getClass ()));
47
82
}
48
83
49
84
@ Test
50
85
public void classLoaderConstructorRaisesExceptionWithFullyQualifiedPath () {
51
- assertExceptionContainsFullyQualifiedPath (new ClassPathResource (FQ_RESOURCE_PATH , this .getClass ().getClassLoader ()));
86
+ assertExceptionContainsFullyQualifiedPath (new ClassPathResource (FQ_RESOURCE_PATH ,
87
+ this .getClass ().getClassLoader ()));
52
88
}
53
89
54
- private void assertExceptionContainsFullyQualifiedPath (ClassPathResource resource ) {
55
- try {
56
- resource .getInputStream ();
57
- fail ("FileNotFoundException expected for resource: " + resource );
58
- } catch (IOException ex ) {
59
- assertThat (ex , instanceOf (FileNotFoundException .class ));
60
- assertThat (ex .getMessage (), containsString (FQ_RESOURCE_PATH ));
61
- }
90
+ @ Test
91
+ public void getDescriptionWithStringConstructor () {
92
+ assertDescriptionContainsExpectedPath (new ClassPathResource (FQ_RESOURCE_PATH ), FQ_RESOURCE_PATH );
93
+ }
94
+
95
+ @ Test
96
+ public void getDescriptionWithStringConstructorAndLeadingSlash () {
97
+ assertDescriptionContainsExpectedPath (new ClassPathResource (FQ_RESOURCE_PATH_WITH_LEADING_SLASH ),
98
+ FQ_RESOURCE_PATH );
99
+ }
100
+
101
+ @ Test
102
+ public void getDescriptionWithClassLiteralConstructor () {
103
+ assertDescriptionContainsExpectedPath (new ClassPathResource (NONEXISTENT_RESOURCE_NAME , this .getClass ()),
104
+ FQ_RESOURCE_PATH );
62
105
}
106
+
107
+ @ Test
108
+ public void getDescriptionWithClassLiteralConstructorAndLeadingSlash () {
109
+ assertDescriptionContainsExpectedPath (
110
+ new ClassPathResource (FQ_RESOURCE_PATH_WITH_LEADING_SLASH , this .getClass ()), FQ_RESOURCE_PATH );
111
+ }
112
+
113
+ @ Test
114
+ public void getDescriptionWithClassLoaderConstructor () {
115
+ assertDescriptionContainsExpectedPath (
116
+ new ClassPathResource (FQ_RESOURCE_PATH , this .getClass ().getClassLoader ()), FQ_RESOURCE_PATH );
117
+ }
118
+
119
+ @ Test
120
+ public void getDescriptionWithClassLoaderConstructorAndLeadingSlash () {
121
+ assertDescriptionContainsExpectedPath (new ClassPathResource (FQ_RESOURCE_PATH_WITH_LEADING_SLASH ,
122
+ this .getClass ().getClassLoader ()), FQ_RESOURCE_PATH );
123
+ }
124
+
63
125
}
0 commit comments