11/*
2- * Copyright 2002-2012 the original author or authors.
2+ * Copyright 2002-2017 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -47,40 +47,19 @@ public class ClassPathResourceTests {
4747 private static final Pattern DESCRIPTION_PATTERN = Pattern .compile ("^class path resource \\ [(.+?)\\ ]$" );
4848
4949
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-
7050 @ Test
7151 public void stringConstructorRaisesExceptionWithFullyQualifiedPath () {
7252 assertExceptionContainsFullyQualifiedPath (new ClassPathResource (FQ_RESOURCE_PATH ));
7353 }
7454
7555 @ Test
7656 public void classLiteralConstructorRaisesExceptionWithFullyQualifiedPath () {
77- assertExceptionContainsFullyQualifiedPath (new ClassPathResource (NONEXISTENT_RESOURCE_NAME , this . getClass ()));
57+ assertExceptionContainsFullyQualifiedPath (new ClassPathResource (NONEXISTENT_RESOURCE_NAME , getClass ()));
7858 }
7959
8060 @ Test
8161 public void classLoaderConstructorRaisesExceptionWithFullyQualifiedPath () {
82- assertExceptionContainsFullyQualifiedPath (new ClassPathResource (FQ_RESOURCE_PATH ,
83- this .getClass ().getClassLoader ()));
62+ assertExceptionContainsFullyQualifiedPath (new ClassPathResource (FQ_RESOURCE_PATH , getClass ().getClassLoader ()));
8463 }
8564
8665 @ Test
@@ -91,31 +70,64 @@ public void getDescriptionWithStringConstructor() {
9170 @ Test
9271 public void getDescriptionWithStringConstructorAndLeadingSlash () {
9372 assertDescriptionContainsExpectedPath (new ClassPathResource (FQ_RESOURCE_PATH_WITH_LEADING_SLASH ),
94- FQ_RESOURCE_PATH );
73+ FQ_RESOURCE_PATH );
9574 }
9675
9776 @ Test
9877 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 );
10180 }
10281
10382 @ Test
10483 public void getDescriptionWithClassLiteralConstructorAndLeadingSlash () {
10584 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 );
10786 }
10887
10988 @ Test
11089 public void getDescriptionWithClassLoaderConstructor () {
11190 assertDescriptionContainsExpectedPath (
112- new ClassPathResource (FQ_RESOURCE_PATH , this . getClass ().getClassLoader ()), FQ_RESOURCE_PATH );
91+ new ClassPathResource (FQ_RESOURCE_PATH , getClass ().getClassLoader ()), FQ_RESOURCE_PATH );
11392 }
11493
11594 @ Test
11695 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+ }
119131 }
120132
121133}
0 commit comments