Skip to content

Commit 7e3fb4a

Browse files
slawekjaranowskimichael-o
authored andcommitted
Replace Plexus Logging by SLF4J
This closes #9
1 parent f8466cc commit 7e3fb4a

File tree

6 files changed

+49
-62
lines changed

6 files changed

+49
-62
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
<artifactId>plexus-component-annotations</artifactId>
5050
<optional>true</optional>
5151
</dependency>
52+
<dependency>
53+
<groupId>org.slf4j</groupId>
54+
<artifactId>slf4j-api</artifactId>
55+
<version>1.7.30</version>
56+
</dependency>
5257

5358
<!-- TEST -->
5459
<dependency>
@@ -57,6 +62,12 @@
5762
<version>3.9.0</version>
5863
<scope>test</scope>
5964
</dependency>
65+
<dependency>
66+
<groupId>org.simplify4u</groupId>
67+
<artifactId>slf4j-mock</artifactId>
68+
<version>2.1.0</version>
69+
<scope>test</scope>
70+
</dependency>
6071
</dependencies>
6172

6273
<build>

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,20 @@
2424
* SOFTWARE.
2525
*/
2626

27-
import org.codehaus.plexus.logging.AbstractLogEnabled;
2827
import org.codehaus.plexus.resource.PlexusResource;
2928

3029
import java.io.IOException;
3130
import java.io.InputStream;
32-
import java.util.List;
3331
import java.util.ArrayList;
32+
import java.util.List;
3433

3534
/**
3635
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
3736
* @version $Id$
3837
*/
39-
public abstract class AbstractResourceLoader
40-
extends AbstractLogEnabled
41-
implements ResourceLoader
38+
public abstract class AbstractResourceLoader implements ResourceLoader
4239
{
43-
protected List<String> paths = new ArrayList<String>();
40+
protected List<String> paths = new ArrayList<>();
4441

4542
public void addSearchPath( String path )
4643
{

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
3030
import org.codehaus.plexus.resource.PlexusResource;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
3133

3234
import java.util.LinkedHashMap;
3335
import java.util.Map;
@@ -39,6 +41,8 @@
3941
public class JarResourceLoader
4042
extends AbstractResourceLoader
4143
{
44+
private static final Logger LOGGER = LoggerFactory.getLogger( ResourceLoader.class );
45+
4246
public static final String ID = "jar";
4347

4448
/**
@@ -50,14 +54,14 @@ public class JarResourceLoader
5054
* Maps JAR URLs to the actual JAR (key = the JAR URL, value = the JAR).
5155
*/
5256
private Map<String, JarHolder> jarfiles = new LinkedHashMap<String, JarHolder>( 89 );
53-
57+
5458
private boolean initializeCalled;
5559

5660
public void initialize()
5761
throws InitializationException
5862
{
5963
initializeCalled = true;
60-
64+
6165
if ( paths != null )
6266
{
6367
for ( int i = 0; i < paths.size(); i++ )
@@ -69,21 +73,17 @@ public void initialize()
6973

7074
private void loadJar( String path )
7175
{
72-
if ( getLogger().isDebugEnabled() )
73-
{
74-
getLogger().debug( "JarResourceLoader : trying to load \"" + path + "\"" );
75-
}
76+
LOGGER.debug( "JarResourceLoader : trying to load '{}'", path );
7677

7778
// Check path information
7879
if ( path == null )
7980
{
80-
getLogger().error( "JarResourceLoader : can not load JAR - JAR path is null" );
81+
LOGGER.error( "JarResourceLoader : can not load JAR - JAR path is null" );
8182
return;
8283
}
8384
if ( !path.startsWith( "jar:" ) )
8485
{
85-
getLogger().error(
86-
"JarResourceLoader : JAR path must start with jar: -> "
86+
LOGGER.error( "JarResourceLoader : JAR path must start with jar: -> "
8787
+ "see java.net.JarURLConnection for information" );
8888
return;
8989
}
@@ -128,7 +128,7 @@ private void addEntries( Map entries )
128128

129129
/**
130130
* Get an InputStream so that the Runtime can build a template with it.
131-
*
131+
*
132132
* @param source name of template to get
133133
* @return InputStream containing the template
134134
* @throws ResourceNotFoundException if template not found in the file template path.
@@ -147,7 +147,7 @@ public PlexusResource getResource( String source )
147147
throw new ResourceNotFoundException( e.getMessage(), e );
148148
}
149149
}
150-
150+
151151
if ( source == null || source.length() == 0 )
152152
{
153153
throw new ResourceNotFoundException( "Need to have a resource!" );

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

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@
2424
* SOFTWARE.
2525
*/
2626

27+
import org.codehaus.plexus.component.annotations.Component;
28+
import org.codehaus.plexus.resource.PlexusResource;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
2732
import java.io.IOException;
2833
import java.io.InputStream;
2934
import java.net.MalformedURLException;
3035
import java.net.URL;
3136
import java.util.HashMap;
3237
import java.util.Map;
3338

34-
import org.codehaus.plexus.component.annotations.Component;
35-
import org.codehaus.plexus.resource.PlexusResource;
36-
3739
/**
3840
* @author Jason van Zyl
3941
*/
@@ -42,9 +44,11 @@ public class URLResourceLoader
4244
extends AbstractResourceLoader
4345
{
4446

47+
private static final Logger LOGGER = LoggerFactory.getLogger( URLResourceLoader.class );
48+
4549
public static final String ID = "url";
4650

47-
protected Map<String, String> templateRoots = new HashMap<String, String>();
51+
protected Map<String, String> templateRoots = new HashMap<>();
4852

4953
/**
5054
* Get an InputStream so that the Runtime can build a template with it.
@@ -71,10 +75,7 @@ public PlexusResource getResource( String name )
7175

7276
if ( inputStream != null )
7377
{
74-
if ( getLogger().isDebugEnabled() )
75-
{
76-
getLogger().debug( "URLResourceLoader: Found '" + name + "' at '" + path + "'" );
77-
}
78+
LOGGER.debug( "URLResourceLoader: Found '{}' at '{}'", name, path );
7879

7980
// save this root for later re-use
8081
templateRoots.put( name, path );
@@ -98,19 +99,11 @@ public synchronized InputStream getInputStream()
9899
}
99100
catch( MalformedURLException mue )
100101
{
101-
if ( getLogger().isDebugEnabled() )
102-
{
103-
getLogger().debug( "URLResourceLoader: No valid URL '" + path + name + '\'' );
104-
}
102+
LOGGER.debug( "URLResourceLoader: No valid URL '{}{}'", path, name );
105103
}
106104
catch ( IOException ioe )
107105
{
108-
if ( getLogger().isDebugEnabled() )
109-
{
110-
getLogger().debug(
111-
"URLResourceLoader: Exception when looking for '" + name + "' at '" + path + "'",
112-
ioe );
113-
}
106+
LOGGER.debug( "URLResourceLoader: Exception when looking for '{}' at '{}'", name, path, ioe );
114107
}
115108
}
116109

@@ -142,18 +135,12 @@ public synchronized InputStream getInputStream()
142135
}
143136
catch( MalformedURLException mue )
144137
{
145-
if ( getLogger().isDebugEnabled() )
146-
{
147-
getLogger().debug( "URLResourceLoader: No valid URL '" + name + '\'' );
148-
}
138+
LOGGER.debug( "URLResourceLoader: No valid URL '{}'", name );
149139
}
150140
catch ( IOException ioe )
151141
{
152-
if ( getLogger().isDebugEnabled() )
153-
{
154-
getLogger().debug( "URLResourceLoader: Exception when looking for '" + name + '\'', ioe );
155-
}
156-
}
142+
LOGGER.debug( "URLResourceLoader: Exception when looking for '{}'", name, ioe );
143+
}
157144

158145
// convert to a general Velocity ResourceNotFoundException
159146
throw new ResourceNotFoundException( name );

src/test/java/org/codehaus/plexus/resource/loader/FileResourceLoaderTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.io.File;
2828

2929
import org.codehaus.plexus.resource.PlexusResource;
30-
import org.codehaus.plexus.resource.loader.AbstractResourceLoaderTest;
3130

3231
/**
3332
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>

src/test/java/org/codehaus/plexus/resource/loader/URLResourceLoaderTest.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,38 @@
11
package org.codehaus.plexus.resource.loader;
22

3-
import static org.mockito.Mockito.when;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.fail;
45
import static org.mockito.Mockito.verify;
56
import static org.mockito.Mockito.verifyNoMoreInteractions;
6-
import junit.framework.TestCase;
77

8-
import org.codehaus.plexus.logging.Logger;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
910
import org.mockito.InjectMocks;
1011
import org.mockito.Mock;
11-
import org.mockito.MockitoAnnotations;
12+
import org.mockito.junit.MockitoJUnitRunner;
13+
import org.slf4j.Logger;
1214

15+
16+
@RunWith( MockitoJUnitRunner.class )
1317
public class URLResourceLoaderTest
14-
extends TestCase
1518
{
1619
@Mock
1720
private Logger logger;
1821

1922
@InjectMocks
2023
private ResourceLoader resourceLoader = new URLResourceLoader();
2124

22-
@Override
23-
protected void setUp()
24-
throws Exception
25-
{
26-
MockitoAnnotations.initMocks( this );
27-
}
28-
25+
@Test
2926
public void testMalformedURL()
30-
throws Exception
3127
{
32-
when( logger.isDebugEnabled() ).thenReturn( true );
33-
3428
try
3529
{
3630
resourceLoader.getResource( "LICENSE.txt" );
3731
fail();
3832
}
3933
catch ( ResourceNotFoundException e )
4034
{
41-
verify( logger ).isDebugEnabled();
42-
verify( logger ).debug( "URLResourceLoader: No valid URL 'LICENSE.txt'" );
35+
verify( logger ).debug( "URLResourceLoader: No valid URL '{}'", "LICENSE.txt" );
4336
verifyNoMoreInteractions( logger );
4437
assertEquals( "Could not find resource 'LICENSE.txt'.", e.getMessage() );
4538
}

0 commit comments

Comments
 (0)