Author: hboutemy Date: Sat Jan 4 16:45:37 2014 New Revision: 1555368 URL: http://svn.apache.org/r1555368 Log: code simplification
Added: maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/AbstractJarSignerTest.java (with props) Modified: maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/JarSignerUtilTest.java maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/SimpleJarSignTest.java Added: maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/AbstractJarSignerTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/AbstractJarSignerTest.java?rev=1555368&view=auto ============================================================================== --- maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/AbstractJarSignerTest.java (added) +++ maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/AbstractJarSignerTest.java Sat Jan 4 16:45:37 2014 @@ -0,0 +1,48 @@ +package org.apache.maven.shared.jarsigner; + +/* + * 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.File; +import java.io.IOException; + +import org.apache.maven.shared.utils.io.FileUtils; +import org.codehaus.plexus.PlexusTestCase; + +public abstract class AbstractJarSignerTest + extends PlexusTestCase +{ + + protected File prepareTestJar( String filename ) + throws IOException + { + File file = new File( "src/test", filename ); + File target = new File( "target", filename ); + + if ( target.exists() ) + { + FileUtils.forceDelete( target ); + } + + FileUtils.copyFile( file, target ); + + return target; + } + +} Propchange: maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/AbstractJarSignerTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/AbstractJarSignerTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/AbstractJarSignerTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/JarSignerUtilTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/JarSignerUtilTest.java?rev=1555368&r1=1555367&r2=1555368&view=diff ============================================================================== --- maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/JarSignerUtilTest.java (original) +++ maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/JarSignerUtilTest.java Sat Jan 4 16:45:37 2014 @@ -19,10 +19,8 @@ package org.apache.maven.shared.jarsigne * under the License. */ -import junit.framework.TestCase; -import org.apache.maven.shared.utils.io.FileUtils; - import java.io.File; +import java.io.IOException; import java.util.jar.JarFile; import java.util.jar.Manifest; @@ -34,7 +32,7 @@ import java.util.jar.Manifest; * @since 1.1 */ public class JarSignerUtilTest - extends TestCase + extends AbstractJarSignerTest { // Fix MSHARED-277 @@ -42,24 +40,15 @@ public class JarSignerUtilTest throws Exception { - File file = new File( "src/test/javax.persistence_2.0.5.v201212031355.jar" ); - File target = new File( "target/", file.getName() ); - - if ( target.exists() ) - { - FileUtils.forceDelete( target ); - } - - FileUtils.copyFile( file, target ); + File target = prepareTestJar( "javax.persistence_2.0.5.v201212031355.jar" ); assertTrue( JarSignerUtil.isArchiveSigned( target ) ); // check that manifest contains some digest attributes - JarFile originalJarFile = new JarFile( file ); - Manifest originalManifest = originalJarFile.getManifest(); - originalJarFile.close(); + Manifest originalManifest = readManifest( target ); Manifest originalCleanManifest = JarSignerUtil.buildUnsignedManifest( originalManifest ); + assertFalse( originalManifest.equals( originalCleanManifest ) ); assertTrue( originalCleanManifest.equals( JarSignerUtil.buildUnsignedManifest( originalCleanManifest ) ) ); @@ -69,13 +58,24 @@ public class JarSignerUtilTest // check that manifest has no digest entry // see https://jira.codehaus.org/browse/MSHARED-314 - JarFile jarFile = new JarFile( target ); - Manifest manifest = jarFile.getManifest(); - jarFile.close(); + Manifest manifest = readManifest( target ); + Manifest cleanManifest = JarSignerUtil.buildUnsignedManifest( manifest ); + assertTrue( manifest.equals( cleanManifest ) ); assertTrue( manifest.equals( originalCleanManifest ) ); + } + + private Manifest readManifest( File file ) + throws IOException + { + JarFile jarFile = new JarFile( file ); + + Manifest manifest = jarFile.getManifest(); + + jarFile.close(); + return manifest; } } Modified: maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/SimpleJarSignTest.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/SimpleJarSignTest.java?rev=1555368&r1=1555367&r2=1555368&view=diff ============================================================================== --- maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/SimpleJarSignTest.java (original) +++ maven/shared/trunk/maven-jarsigner/src/test/java/org/apache/maven/shared/jarsigner/SimpleJarSignTest.java Sat Jan 4 16:45:37 2014 @@ -20,8 +20,6 @@ package org.apache.maven.shared.jarsigne */ import org.apache.maven.shared.utils.cli.javatool.JavaToolResult; -import org.codehaus.plexus.PlexusTestCase; -import org.apache.maven.shared.utils.io.FileUtils; import java.io.File; @@ -29,52 +27,41 @@ import java.io.File; * @author Olivier Lamy */ public class SimpleJarSignTest - extends PlexusTestCase + extends AbstractJarSignerTest { + private JarSigner jarSigner; - public void testSimpleSign() + public void setUp() throws Exception { - File file = new File( "src/test/simple.jar" ); - File target = new File( "target/simple.jar" ); - - if ( target.exists() ) - { - FileUtils.forceDelete( target ); - } - - FileUtils.copyFile( file, target ); - - JarSignerSignRequest jarSignerRequest = buildJarSignerRequest( target ); - - JarSigner jarSigner = (JarSigner) lookup( JarSigner.class.getName() ); - - JavaToolResult jarSignerResult = jarSigner.execute( jarSignerRequest ); - assertEquals( "not exit code 0 " + jarSignerResult.getExecutionException(), 0, jarSignerResult.getExitCode() ); - + super.setUp(); + jarSigner = (JarSigner) lookup( JarSigner.class.getName() ); } - public void testSimpleSignAdVerify() + public void testSimpleSign() throws Exception { - File file = new File( "src/test/simple.jar" ); - File target = new File( "target/simple.jar" ); + File target = prepareTestJar( "simple.jar" ); - if ( target.exists() ) - { - FileUtils.forceDelete( target ); - } - - FileUtils.copyFile( file, target ); - - JarSignerSignRequest jarSignerRequest = buildJarSignerRequest( target ); - - JarSigner jarSigner = (JarSigner) lookup( JarSigner.class.getName() ); + JarSignerSignRequest jarSignerRequest = new JarSignerSignRequest(); + jarSignerRequest.setArchive( target ); + jarSignerRequest.setKeystore( "src/test/keystore" ); + jarSignerRequest.setVerbose( true ); + jarSignerRequest.setAlias( "foo_alias" ); + jarSignerRequest.setKeypass( "key-passwd" ); + jarSignerRequest.setStorepass( "changeit" ); + jarSignerRequest.setSignedjar( new File( "target/ssimple.jar" ) ); JavaToolResult jarSignerResult = jarSigner.execute( jarSignerRequest ); assertEquals( "not exit code 0 " + jarSignerResult.getExecutionException(), 0, jarSignerResult.getExitCode() ); + } + + public void testSimpleSignAndVerify() + throws Exception + { + testSimpleSign(); JarSignerVerifyRequest request = new JarSignerVerifyRequest(); request.setCerts( true ); @@ -83,21 +70,8 @@ public class SimpleJarSignTest request.setKeystore( "src/test/keystore" ); request.setAlias( "foo_alias" ); - jarSignerResult = jarSigner.execute( request ); - assertEquals( "not exit code 0 " + jarSignerResult.getExecutionException(), 0, jarSignerResult.getExitCode() ); + JavaToolResult jarSignerResult = jarSigner.execute( request ); - } - - protected JarSignerSignRequest buildJarSignerRequest( File target ) - { - JarSignerSignRequest jarSignerRequest = new JarSignerSignRequest(); - jarSignerRequest.setArchive( target ); - jarSignerRequest.setKeystore( "src/test/keystore" ); - jarSignerRequest.setVerbose( true ); - jarSignerRequest.setAlias( "foo_alias" ); - jarSignerRequest.setKeypass( "key-passwd" ); - jarSignerRequest.setStorepass( "changeit" ); - jarSignerRequest.setSignedjar( new File( "target/ssimple.jar" ) ); - return jarSignerRequest; + assertEquals( "not exit code 0 " + jarSignerResult.getExecutionException(), 0, jarSignerResult.getExitCode() ); } }