You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2020/07/29 15:58:50 UTC

[maven-dependency-analyzer] branch master updated: [MSHARED-932] remove dependency on jmock (#19)

This is an automated email from the ASF dual-hosted git repository.

elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-analyzer.git


The following commit(s) were added to refs/heads/master by this push:
     new fed7bde  [MSHARED-932] remove dependency on jmock (#19)
fed7bde is described below

commit fed7bde20ce3ba2637aea3276aa68406fb73b262
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Wed Jul 29 15:58:43 2020 +0000

    [MSHARED-932] remove dependency on jmock (#19)
    
    * remove dependency on jmock
---
 pom.xml                                            |   6 --
 .../dependency/analyzer/AbstractFileTest.java      |  19 ++--
 .../analyzer/ClassFileVisitorUtilsTest.java        |  91 ++++++++++---------
 .../dependency/analyzer/InputStreamConstraint.java | 100 ---------------------
 4 files changed, 53 insertions(+), 163 deletions(-)

diff --git a/pom.xml b/pom.xml
index b0206e6..a209d5a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -123,12 +123,6 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>jmock</groupId>
-      <artifactId>jmock</artifactId>
-      <version>1.1.0</version>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.13</version>
diff --git a/src/test/java/org/apache/maven/shared/dependency/analyzer/AbstractFileTest.java b/src/test/java/org/apache/maven/shared/dependency/analyzer/AbstractFileTest.java
index 9acfcb6..1860b02 100644
--- a/src/test/java/org/apache/maven/shared/dependency/analyzer/AbstractFileTest.java
+++ b/src/test/java/org/apache/maven/shared/dependency/analyzer/AbstractFileTest.java
@@ -20,23 +20,20 @@ package org.apache.maven.shared.dependency.analyzer;
  */
 
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.jar.JarOutputStream;
 import java.util.zip.ZipEntry;
 
-import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
-import org.jmock.MockObjectTestCase;
+import org.apache.commons.io.FileUtils;
+
+import junit.framework.TestCase;
 
 /**
- * 
- * 
  * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
  * @version $Id$
  */
-public abstract class AbstractFileTest extends MockObjectTestCase
+public abstract class AbstractFileTest extends TestCase
 {
     // protected methods ------------------------------------------------------
 
@@ -62,11 +59,7 @@ public abstract class AbstractFileTest extends MockObjectTestCase
     protected File createFile( File parent, String child, String data ) throws IOException
     {
         File file = new File( parent, child );
-
-        OutputStream out = new FileOutputStream( file );
-        IOUtil.copy( data, out );
-        out.close();
-
+        FileUtils.write( file, data, StandardCharsets.UTF_8 );
         return file;
     }
 
diff --git a/src/test/java/org/apache/maven/shared/dependency/analyzer/ClassFileVisitorUtilsTest.java b/src/test/java/org/apache/maven/shared/dependency/analyzer/ClassFileVisitorUtilsTest.java
index c705f6d..cdc7141 100644
--- a/src/test/java/org/apache/maven/shared/dependency/analyzer/ClassFileVisitorUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/dependency/analyzer/ClassFileVisitorUtilsTest.java
@@ -22,11 +22,15 @@ package org.apache.maven.shared.dependency.analyzer;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.jar.JarOutputStream;
 
-import org.codehaus.plexus.util.FileUtils;
-import org.jmock.Mock;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 
 /**
  * Tests <code>ClassFileVisitorUtils</code>.
@@ -38,7 +42,28 @@ import org.jmock.Mock;
 public class ClassFileVisitorUtilsTest
     extends AbstractFileTest
 {
-    // tests ------------------------------------------------------------------
+    
+    private MockVisitor visitor = new MockVisitor();
+
+    private static class MockVisitor implements ClassFileVisitor
+    {
+        
+        ArrayList<String> classNames = new ArrayList<>();
+        ArrayList<String> data = new ArrayList<>();
+
+        @Override
+        public void visitClass( String className, InputStream in )
+        {
+            classNames.add( className );
+            try {
+                List<String> lines = IOUtils.readLines( in, StandardCharsets.UTF_8 );
+                data.addAll( lines );
+            } catch (IOException ex) {
+                throw new RuntimeException( ex );
+            }
+        }
+
+    }
 
     public void testAcceptJar()
         throws IOException
@@ -50,13 +75,12 @@ public class ClassFileVisitorUtilsTest
             writeEntry( out, "x/y/z.class", "class x.y.z" );
         }
 
-        Mock mock = mock( ClassFileVisitor.class );
-        expectVisitClass( mock, "a.b.c", "class a.b.c" );
-        expectVisitClass( mock, "x.y.z", "class x.y.z" );
-
-        ClassFileVisitorUtils.accept( file.toURI().toURL(), (ClassFileVisitor) mock.proxy() );
-
-        mock.verify();
+        ClassFileVisitorUtils.accept( file.toURI().toURL(), visitor );
+        
+        assertTrue(visitor.classNames.contains( "a.b.c" ));
+        assertTrue(visitor.classNames.contains( "x.y.z" ));
+        assertTrue(visitor.data.contains( "class a.b.c" ));
+        assertTrue(visitor.data.contains( "class x.y.z" ));
     }
 
     public void testAcceptJarWithNonClassEntry()
@@ -68,11 +92,9 @@ public class ClassFileVisitorUtilsTest
             writeEntry( out, "a/b/c.jpg", "jpeg a.b.c" );
         }
 
-        Mock mock = mock( ClassFileVisitor.class );
+        ClassFileVisitorUtils.accept( file.toURI().toURL(), visitor );
 
-        ClassFileVisitorUtils.accept( file.toURI().toURL(), (ClassFileVisitor) mock.proxy() );
-
-        mock.verify();
+        assertTrue(visitor.classNames.isEmpty());
     }
 
     public void testAcceptDir()
@@ -86,15 +108,14 @@ public class ClassFileVisitorUtilsTest
         File xyDir = mkdirs( dir, "x/y" );
         createFile( xyDir, "z.class", "class x.y.z" );
 
-        Mock mock = mock( ClassFileVisitor.class );
-        expectVisitClass( mock, "a.b.c", "class a.b.c" );
-        expectVisitClass( mock, "x.y.z", "class x.y.z" );
-
-        ClassFileVisitorUtils.accept( dir.toURI().toURL(), (ClassFileVisitor) mock.proxy() );
+        ClassFileVisitorUtils.accept( dir.toURI().toURL(), visitor );
 
         FileUtils.deleteDirectory( dir );
 
-        mock.verify();
+        assertTrue(visitor.classNames.contains( "a.b.c" ));
+        assertTrue(visitor.classNames.contains( "x.y.z" ));
+        assertTrue(visitor.data.contains( "class a.b.c" ));
+        assertTrue(visitor.data.contains( "class x.y.z" ));
     }
 
     public void testAcceptDirWithNonClassFile()
@@ -105,13 +126,11 @@ public class ClassFileVisitorUtilsTest
         File abDir = mkdirs( dir, "a/b" );
         createFile( abDir, "c.jpg", "jpeg a.b.c" );
 
-        Mock mock = mock( ClassFileVisitor.class );
-
-        ClassFileVisitorUtils.accept( dir.toURI().toURL(), (ClassFileVisitor) mock.proxy() );
+        ClassFileVisitorUtils.accept( dir.toURI().toURL(), visitor );
 
         FileUtils.deleteDirectory( dir );
 
-        mock.verify();
+        assertTrue(visitor.classNames.isEmpty());
     }
 
     public void testAcceptWithFile()
@@ -120,14 +139,12 @@ public class ClassFileVisitorUtilsTest
         File file = File.createTempFile( "test", ".class" );
         file.deleteOnExit();
 
-        Mock mock = mock( ClassFileVisitor.class );
-
         URL url = file.toURI().toURL();
 
         try
         {
-            ClassFileVisitorUtils.accept( url, (ClassFileVisitor) mock.proxy() );
-            fail("expected IllegalArgumntException");
+            ClassFileVisitorUtils.accept( url, visitor );
+            fail("expected IllegalArgumentException");
         }
         catch ( IllegalArgumentException exception )
         {
@@ -138,30 +155,16 @@ public class ClassFileVisitorUtilsTest
     public void testAcceptWithUnsupportedScheme()
         throws IOException
     {
-        Mock mock = mock( ClassFileVisitor.class );
-
         URL url = new URL( "http://localhost/" );
 
         try
         {
-            ClassFileVisitorUtils.accept( url, (ClassFileVisitor) mock.proxy() );
-            fail("expected IllegalArgumntException");
+            ClassFileVisitorUtils.accept( url, visitor );
+            fail("expected IllegalArgumentException");
         }
         catch ( IllegalArgumentException exception )
         {
             assertEquals( "Cannot accept visitor on URL: " + url, exception.getMessage() );
         }
     }
-
-    // private methods --------------------------------------------------------
-
-    private void expectVisitClass( Mock mock, String className, String data )
-    {
-        mock.expects( atLeastOnce() ).method( "visitClass" ).with( eq( className ), in( data ) );
-    }
-
-    private InputStreamConstraint in( String expected )
-    {
-        return new InputStreamConstraint( expected );
-    }
 }
diff --git a/src/test/java/org/apache/maven/shared/dependency/analyzer/InputStreamConstraint.java b/src/test/java/org/apache/maven/shared/dependency/analyzer/InputStreamConstraint.java
deleted file mode 100644
index 93b273e..0000000
--- a/src/test/java/org/apache/maven/shared/dependency/analyzer/InputStreamConstraint.java
+++ /dev/null
@@ -1,100 +0,0 @@
-package org.apache.maven.shared.dependency.analyzer;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import java.io.IOException;
-import java.io.InputStream;
-
-import org.codehaus.plexus.util.IOUtil;
-import org.jmock.core.Constraint;
-
-/**
- * 
- * 
- * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
- * @version $Id$
- */
-public class InputStreamConstraint
-    implements Constraint
-{
-    // constants --------------------------------------------------------------
-
-    private static final String DEFAULT_CHARSET_NAME = "UTF-8";
-
-    // fields -----------------------------------------------------------------
-
-    private final String expected;
-
-    private final String charsetName;
-
-    // constructors -----------------------------------------------------------
-
-    public InputStreamConstraint( String expected )
-    {
-        this( expected, DEFAULT_CHARSET_NAME );
-    }
-
-    public InputStreamConstraint( String expected, String charsetName )
-    {
-        this.expected = expected;
-        this.charsetName = charsetName;
-    }
-
-    // Constraint methods -----------------------------------------------------
-
-    /*
-     * @see org.jmock.core.Constraint#eval(java.lang.Object)
-     */
-    public boolean eval( Object object )
-    {
-        if ( !( object instanceof InputStream ) )
-        {
-            return false;
-        }
-
-        InputStream in = (InputStream) object;
-
-        try
-        {
-            String actual = IOUtil.toString( in, charsetName );
-
-            return expected.equals( actual );
-        }
-        catch ( IOException exception )
-        {
-            return false;
-        }
-    }
-
-    // SelfDescribing methods -------------------------------------------------
-
-    /*
-     * @see org.jmock.core.SelfDescribing#describeTo(java.lang.StringBuffer)
-     */
-    public StringBuffer describeTo( StringBuffer buffer )
-    {
-        buffer.append( "in(" );
-        buffer.append( "\"" ).append( expected ).append( "\"" );
-        buffer.append( "," ).append( charsetName );
-        buffer.append( ")" );
-
-        return buffer;
-    }
-}