Skip to content

Commit 29101d3

Browse files
author
Robert Scholte
committed
#1 Reduce debug logging when hitting a MalformedURL
Don't pass exception, is causes very weird RNFE-messages.
1 parent 06344f5 commit 29101d3

File tree

3 files changed

+73
-27
lines changed

3 files changed

+73
-27
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,13 @@
3030
<groupId>org.codehaus.plexus</groupId>
3131
<artifactId>plexus-container-default</artifactId>
3232
</dependency>
33+
34+
<!-- TEST -->
35+
<dependency>
36+
<groupId>org.mockito</groupId>
37+
<artifactId>mockito-core</artifactId>
38+
<version>1.9.5</version>
39+
<scope>test</scope>
40+
</dependency>
3341
</dependencies>
3442
</project>

src/main/java/org/codehaus/plexus/resource/loader/URLResourceLoader.java

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.IOException;
2828
import java.io.InputStream;
29+
import java.net.MalformedURLException;
2930
import java.net.URL;
3031
import java.util.HashMap;
3132
import java.util.Iterator;
@@ -60,8 +61,6 @@ public PlexusResource getResource( String name )
6061
throw new ResourceNotFoundException( "URLResourceLoader : No template name provided" );
6162
}
6263

63-
Exception exception = null;
64-
6564
for ( Iterator i = paths.iterator(); i.hasNext(); )
6665
{
6766
String path = (String) i.next();
@@ -99,6 +98,13 @@ public synchronized InputStream getInputStream()
9998
};
10099
}
101100
}
101+
catch( MalformedURLException mue )
102+
{
103+
if ( getLogger().isDebugEnabled() )
104+
{
105+
getLogger().debug( "URLResourceLoader: No valid URL '" + path + name + '\'' );
106+
}
107+
}
102108
catch ( IOException ioe )
103109
{
104110
if ( getLogger().isDebugEnabled() )
@@ -107,17 +113,10 @@ public synchronized InputStream getInputStream()
107113
"URLResourceLoader: Exception when looking for '" + name + "' at '" + path + "'",
108114
ioe );
109115
}
110-
111-
// only save the first one for later throwing
112-
if ( exception == null )
113-
{
114-
exception = ioe;
115-
}
116116
}
117117
}
118118

119119
// here we try to download without any path just the name which can be an url
120-
121120
try
122121
{
123122
URL u = new URL( name );
@@ -143,31 +142,22 @@ public synchronized InputStream getInputStream()
143142
};
144143
}
145144
}
146-
catch ( IOException ioe )
145+
catch( MalformedURLException mue )
147146
{
148147
if ( getLogger().isDebugEnabled() )
149148
{
150-
getLogger().debug( "URLResourceLoader: Exception when looking for '" + name, ioe );
149+
getLogger().debug( "URLResourceLoader: No valid URL '" + name + '\'' );
151150
}
152-
153-
// only save the first one for later throwing
154-
if ( exception == null )
151+
}
152+
catch ( IOException ioe )
153+
{
154+
if ( getLogger().isDebugEnabled() )
155155
{
156-
exception = ioe;
156+
getLogger().debug( "URLResourceLoader: Exception when looking for '" + name + '\'', ioe );
157157
}
158-
}
158+
}
159159

160-
// if we never found the template
161-
String msg;
162-
if ( exception == null )
163-
{
164-
msg = "URLResourceLoader : Resource '" + name + "' not found.";
165-
}
166-
else
167-
{
168-
msg = exception.getMessage();
169-
}
170160
// convert to a general Velocity ResourceNotFoundException
171-
throw new ResourceNotFoundException( msg );
161+
throw new ResourceNotFoundException( name );
172162
}
173163
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.codehaus.plexus.resource.loader;
2+
3+
import static org.mockito.Mockito.when;
4+
import static org.mockito.Mockito.verify;
5+
import static org.mockito.Mockito.verifyNoMoreInteractions;
6+
import junit.framework.TestCase;
7+
8+
import org.codehaus.plexus.logging.Logger;
9+
import org.mockito.InjectMocks;
10+
import org.mockito.Mock;
11+
import org.mockito.MockitoAnnotations;
12+
13+
public class URLResourceLoaderTest
14+
extends TestCase
15+
{
16+
@Mock
17+
private Logger logger;
18+
19+
@InjectMocks
20+
private ResourceLoader resourceLoader = new URLResourceLoader();
21+
22+
@Override
23+
protected void setUp()
24+
throws Exception
25+
{
26+
MockitoAnnotations.initMocks( this );
27+
}
28+
29+
public void testMalformedURL()
30+
throws Exception
31+
{
32+
when( logger.isDebugEnabled() ).thenReturn( true );
33+
34+
try
35+
{
36+
resourceLoader.getResource( "LICENSE.txt" );
37+
fail();
38+
}
39+
catch ( ResourceNotFoundException e )
40+
{
41+
verify( logger ).isDebugEnabled();
42+
verify( logger ).debug( "URLResourceLoader: No valid URL 'LICENSE.txt'" );
43+
verifyNoMoreInteractions( logger );
44+
assertEquals( "Could not find resource 'LICENSE.txt'.", e.getMessage() );
45+
}
46+
47+
}
48+
}

0 commit comments

Comments
 (0)