From 3b59f12eb9a49109a2143b6f2c7bae048e8deaeb Mon Sep 17 00:00:00 2001 From: Roman Vais Date: Aug 25 2016 13:26:16 +0000 Subject: Add new test cases and new resources for them Edit and expand test cases: xmvn-api * DefaultArtifactTest.java xmvn-connector-aether * ModelValidatorTest.java Add new test cases: xmvn-api * DeploymentRequestTest.java * ResolutionRequestTest.java xmvn-connector-aether * MavenPluginValidatorTest.java * MavenLifecycleParticipantTest.java * MojoExecutionListenerTest.java * PluginVersionResolverTest.java xmvn-connector-ivy * DeployTest.java Add new resources: xmvn-connector-aether * test.pom.xml * testEmptyCfg.pom.xml * testInvalid1.pom.xml * testInvalid2.pom.xml --- diff --git a/xmvn-api/src/test/java/org/fedoraproject/xmvn/artifact/DefaultArtifactTest.java b/xmvn-api/src/test/java/org/fedoraproject/xmvn/artifact/DefaultArtifactTest.java index 54d96be..4bb4f44 100644 --- a/xmvn-api/src/test/java/org/fedoraproject/xmvn/artifact/DefaultArtifactTest.java +++ b/xmvn-api/src/test/java/org/fedoraproject/xmvn/artifact/DefaultArtifactTest.java @@ -88,6 +88,46 @@ public class DefaultArtifactTest } /** + * Test two-argument constructor with groupId as null pointer. + */ + @Test( expected = IllegalArgumentException.class ) + public void testGroupIdNull() + throws Exception + { + new DefaultArtifact( null, "" ); + } + + /** + * Test two-argument constructor with artifactId as null pointer. + */ + @Test( expected = IllegalArgumentException.class ) + public void testArtifactIdNull() + throws Exception + { + new DefaultArtifact( "gid", null ); + } + + /** + * Test two-argument constructor with groupId as null pointer. + */ + @Test( expected = IllegalArgumentException.class ) + public void testGroupIdEmpty() + throws Exception + { + new DefaultArtifact( "", "aid" ); + } + + /** + * Test two-argument constructor with artifactId as null pointer. + */ + @Test( expected = IllegalArgumentException.class ) + public void testArtifactIdEmpty() + throws Exception + { + new DefaultArtifact( "gid", "" ); + } + + /** * Test two-argument constructor. */ @Test diff --git a/xmvn-api/src/test/java/org/fedoraproject/xmvn/deployer/DeploymentRequestTest.java b/xmvn-api/src/test/java/org/fedoraproject/xmvn/deployer/DeploymentRequestTest.java new file mode 100644 index 0000000..7949b55 --- /dev/null +++ b/xmvn-api/src/test/java/org/fedoraproject/xmvn/deployer/DeploymentRequestTest.java @@ -0,0 +1,191 @@ +package org.fedoraproject.xmvn.deployer; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import org.easymock.EasyMock; +import org.easymock.EasyMockRunner; +import org.easymock.TestSubject; +import org.fedoraproject.xmvn.artifact.Artifact; +import org.fedoraproject.xmvn.deployer.DeploymentRequest; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Roman Vais + */ + +@RunWith( EasyMockRunner.class ) +public class DeploymentRequestTest +{ + private ArrayList depExcl; + + private Artifact artifact, dependencyA, dependencyB, dependencyC, dependencyD; + + @TestSubject + private final DeploymentRequest deployRq = new DeploymentRequest(); + + @Before + public void setUp() + { + artifact = EasyMock.createMock( Artifact.class ); + dependencyA = EasyMock.createMock( Artifact.class ); + dependencyB = EasyMock.createMock( Artifact.class ); + dependencyC = EasyMock.createMock( Artifact.class ); + dependencyD = EasyMock.createMock( Artifact.class ); + EasyMock.replay( artifact, dependencyA, dependencyB, dependencyC, dependencyD ); + + depExcl = new ArrayList<>(); + depExcl.add( dependencyC ); + depExcl.add( dependencyD ); + } + + /** + * Test of get and set artifact methods together with equality method + */ + @Test + public void getSetEqualeTest() + throws Exception + { + // tests setting an artifact + deployRq.setArtifact( artifact ); + Artifact gained = deployRq.getArtifact(); + Assert.assertSame( artifact, gained ); + + deployRq.setArtifact( null ); + gained = deployRq.getArtifact(); + Assert.assertSame( gained, null ); + + // tests equality of empty DeploymentRequests + DeploymentRequest extraRq = new DeploymentRequest(); + Assert.assertTrue( deployRq.equals( extraRq ) ); + + // tests inequality if one of the requests doesn't have artifact + extraRq.setArtifact( artifact ); + Assert.assertFalse( deployRq.equals( extraRq ) ); + + extraRq.setArtifact( null ); + deployRq.setArtifact( artifact ); + Assert.assertFalse( deployRq.equals( extraRq ) ); + + // tests if requests with the same artifact are considered equal + extraRq.setArtifact( artifact ); + Assert.assertTrue( deployRq.equals( extraRq ) ); + + // test remaining cases + Assert.assertFalse( deployRq.equals( null ) ); + Assert.assertFalse( deployRq.equals( new Object() ) ); + Assert.assertTrue( deployRq.equals( deployRq ) ); + } + + /** + * Test of adding, getting and removing dependencies + */ + @Test + public void addAndGetDependenciesTest() + throws Exception + { + // tests basic add and get dependency + deployRq.setArtifact( artifact ); + deployRq.addDependency( dependencyA ); + List listedDependencies; + listedDependencies = deployRq.getDependencies(); + + DependencyDescriptor dsc = listedDependencies.get( 0 ); + + if ( dsc.getDependencyArtifact() != ( dependencyA ) || listedDependencies.size() != 1 ) + { + Assert.fail( "Added dependenci is not in list or there are more items." ); + } + + // tests removal of dependency + deployRq.addDependency( dependencyB ); + deployRq.removeDependency( dependencyA ); + listedDependencies = deployRq.getDependencies(); + + if ( listedDependencies.size() > 1 ) + { + Assert.fail( "Dependency was not removed from the list." ); + } + + // tests adding dependency with exclusions + deployRq.addDependency( dependencyB, depExcl ); + listedDependencies = deployRq.getDependencies(); + Assert.assertTrue( listedDependencies.get( 1 ).getExclusions().equals( depExcl ) ); + deployRq.removeDependency( dependencyB ); + + deployRq.addDependency( dependencyB, dependencyC, dependencyD ); + listedDependencies = deployRq.getDependencies(); + Assert.assertTrue( listedDependencies.get( 1 ).getExclusions().equals( depExcl ) ); + deployRq.removeDependency( dependencyB ); + + // tests adding optional dependency without exclusions + deployRq.addDependency( dependencyB, true, new ArrayList() ); + listedDependencies = deployRq.getDependencies(); + dsc = listedDependencies.get( 1 ); + Assert.assertTrue( dsc.isOptional() ); + Assert.assertTrue( dsc.getExclusions().isEmpty() ); + + } + + /** + * Test of property manipulation + */ + @Test + public void propertiesTest() + throws Exception + { + // no properties should be present + Assert.assertTrue( "Hash map of properties is not empty before adding first one.", + deployRq.getProperties().isEmpty() ); + + // tests adding and getting new properties + deployRq.addProperty( "key", null ); + Assert.assertTrue( "Property has been added even thou it's value is a null.", + deployRq.getProperties().isEmpty() ); + + deployRq.addProperty( "key", "value" ); + Assert.assertTrue( deployRq.getProperty( "key" ).equals( "value" ) ); + + // tests removing property + deployRq.removeProperty( "key" ); + Assert.assertFalse( "Poperty was not removed.", deployRq.getProperty( "key" ) != null ); + + } + + /** + * Test of hash method + */ + @Test + public void hashTest() + throws Exception + { + // tests hash + DeploymentRequest extraRq = new DeploymentRequest(); + Assert.assertTrue( "Hash of deployment request is what ?", extraRq.hashCode() != 0 || extraRq.hashCode() == 0 ); + + extraRq.setArtifact( dependencyB ); + deployRq.setArtifact( dependencyA ); + + Assert.assertTrue( extraRq.hashCode() != deployRq.hashCode() ); + + } + + /** + * Test of get and set methods for plan path + */ + @Test + public void planPathTest() + throws Exception + { + // tests set and get planPath + Path planPath = Paths.get( "/tmp/foo/bar/plan" ); + deployRq.setPlanPath( planPath ); + Assert.assertTrue( deployRq.getPlanPath().equals( planPath ) ); + + } + +} diff --git a/xmvn-api/src/test/java/org/fedoraproject/xmvn/resolver/ResolutionRequestTest.java b/xmvn-api/src/test/java/org/fedoraproject/xmvn/resolver/ResolutionRequestTest.java new file mode 100644 index 0000000..17c3aa6 --- /dev/null +++ b/xmvn-api/src/test/java/org/fedoraproject/xmvn/resolver/ResolutionRequestTest.java @@ -0,0 +1,101 @@ +package org.fedoraproject.xmvn.resolver; + +import org.easymock.EasyMock; +import org.easymock.EasyMockRunner; +import org.easymock.TestSubject; +import org.fedoraproject.xmvn.artifact.Artifact; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Roman Vais + */ + +@RunWith( EasyMockRunner.class ) +public class ResolutionRequestTest +{ + private Artifact artifact; + + @TestSubject + private final ResolutionRequest rrq = new ResolutionRequest(); + + @Before + public void setUp() + { + artifact = EasyMock.createMock( Artifact.class ); + rrq.setArtifact( null ); + rrq.setProviderNeeded( false ); + rrq.setPersistentFileNeeded( false ); + } + + /** + * Test of get and set methods + */ + @Test + public void getSetTest() + throws Exception + { + // tests set and get artifact + rrq.setArtifact( artifact ); + Assert.assertTrue( artifact == rrq.getArtifact() ); + + // tests set and get 'ProviderNeeded' + rrq.setProviderNeeded( true ); + Assert.assertTrue( rrq.isProviderNeeded() ); + rrq.setProviderNeeded( false ); + Assert.assertFalse( rrq.isProviderNeeded() ); + + // tests set and get 'PersistentFileNeeded' + rrq.setPersistentFileNeeded( true ); + Assert.assertTrue( rrq.isPersistentFileNeeded() ); + rrq.setPersistentFileNeeded( false ); + Assert.assertFalse( rrq.isPersistentFileNeeded() ); + } + + /** + * Test of constructor wit artifact argument + */ + @Test + public void extraConstructorTest() + throws Exception + { + ResolutionRequest extraRq = new ResolutionRequest( artifact ); + Assert.assertTrue( artifact == extraRq.getArtifact() ); + } + + /** + * Test of equality method + */ + @Test + public void equalityTest() + throws Exception + { + + Assert.assertTrue( rrq.equals( rrq ) ); + Assert.assertFalse( rrq.equals( null ) ); + Assert.assertFalse( rrq.equals( new Object() ) ); + + ResolutionRequest extraRq = new ResolutionRequest(); + Assert.assertTrue( rrq.equals( extraRq ) ); + + extraRq.setArtifact( artifact ); + Assert.assertFalse( rrq.equals( extraRq ) ); + + rrq.setArtifact( artifact ); + Assert.assertTrue( rrq.equals( extraRq ) ); + + rrq.setProviderNeeded( true ); + Assert.assertFalse( rrq.equals( extraRq ) ); + + rrq.setProviderNeeded( false ); + rrq.setPersistentFileNeeded( true ); + Assert.assertFalse( rrq.equals( extraRq ) ); + + extraRq.setArtifact( EasyMock.createMock( Artifact.class ) ); + Assert.assertFalse( rrq.equals( extraRq ) ); + + } + +} diff --git a/xmvn-connector-aether/src/test/java/org/apache/maven/plugin/MavenPluginValidatorTest.java b/xmvn-connector-aether/src/test/java/org/apache/maven/plugin/MavenPluginValidatorTest.java new file mode 100644 index 0000000..c938eb8 --- /dev/null +++ b/xmvn-connector-aether/src/test/java/org/apache/maven/plugin/MavenPluginValidatorTest.java @@ -0,0 +1,67 @@ +/*- + * Copyright (c) 2015 Red Hat, Inc. + * + * Licensed 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. + */ +package org.apache.maven.plugin; + +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.easymock.EasyMock; +import org.easymock.EasyMockRunner; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Roman Vais + */ + +@RunWith( EasyMockRunner.class ) +public class MavenPluginValidatorTest +{ + + private MavenPluginValidator validator; + + private PluginDescriptor desc; + + @Before + public void setUp() + throws Exception + { + validator = new MavenPluginValidator( null ); + desc = EasyMock.createMock( PluginDescriptor.class ); + } + + @Test + public void testMavenPluginValidator() + throws Exception + { + EasyMock.expect( desc.getVersion() ).andReturn( null ); + desc.setVersion( EasyMock.anyObject( String.class ) ); + EasyMock.expectLastCall(); + EasyMock.replay( desc ); + + validator.validate( desc ); + EasyMock.verify( desc ); + + EasyMock.reset( desc ); + EasyMock.expect( desc.getVersion() ).andReturn( "1.2.3" ); + EasyMock.replay( desc ); + validator.validate( desc ); + + Assert.assertFalse( validator.hasErrors() ); + Assert.assertTrue( validator.getErrors().isEmpty() ); + } + +} \ No newline at end of file diff --git a/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/MavenLifecycleParticipantTest.java b/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/MavenLifecycleParticipantTest.java new file mode 100644 index 0000000..041d548 --- /dev/null +++ b/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/MavenLifecycleParticipantTest.java @@ -0,0 +1,58 @@ +package org.fedoraproject.xmvn.connector.aether; + +import javax.inject.Inject; + +import org.apache.maven.model.validation.ModelValidator; +import org.apache.maven.plugin.MavenPluginValidator; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.easymock.EasyMock; +import org.easymock.Mock; +import org.easymock.MockType; +import org.easymock.TestSubject; +import org.eclipse.sisu.launch.InjectedTest; +import org.junit.Before; +import org.junit.Test; + +import com.google.inject.Binder; + +/** + * @author Roman Vais + */ + +public class MavenLifecycleParticipantTest + extends InjectedTest +{ + + @Inject + @TestSubject + private XMvnMavenLifecycleParticipant participant; + + @Override + public void configure( Binder binder ) + { + /* + * XMvnWorkspaceReader workspaceReader; binder.bind( XMvnMavenLifecycleParticipant.class ).to( + * XMvnModelValidator.class ); + */ + } + + @Before + @Override + public void setUp() + throws Exception + { + super.setUp(); + } + + /* + * TODO: needs to be created/finished + */ + + @Test + public void testMavenPluginValidator() + throws Exception + { + + } + +} diff --git a/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/ModelValidatorTest.java b/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/ModelValidatorTest.java index 5ae894a..8cda234 100644 --- a/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/ModelValidatorTest.java +++ b/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/ModelValidatorTest.java @@ -17,33 +17,194 @@ package org.fedoraproject.xmvn.connector.aether; import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.FileReader; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; + import javax.inject.Inject; +import org.apache.maven.model.Build; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.Extension; import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginExecution; import org.apache.maven.model.validation.ModelValidator; +import org.easymock.EasyMock; +import org.easymock.EasyMockRunner; +import org.easymock.Mock; import org.eclipse.sisu.launch.InjectedTest; +import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; +import org.junit.runner.RunWith; +import com.google.inject.Binder; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.Xpp3DomBuilder; /** * @author Mikolaj Izdebski + * @author Roman Vais */ + +@RunWith( EasyMockRunner.class ) public class ModelValidatorTest extends InjectedTest { + @Inject private ModelValidator validator; + @Mock + private Model model; + + @Mock + private Build build; + + private ArrayList dl; + + private ArrayList el; + + private ArrayList pl; + + @Override + public void configure( Binder binder ) + { + binder.bind( ModelValidator.class ).to( XMvnModelValidator.class ); + } + + private Path getResource( String resource ) + { + if ( resource == null ) + return null; + return Paths.get( "src/test/resources" ).resolve( resource ).toAbsolutePath(); + } + + @Before + @Override + public void setUp() + throws Exception + { + super.setUp(); + + Xpp3Dom glCfg, locCfg; + glCfg = Xpp3DomBuilder.build( new FileReader( getResource( "test.pom.xml" ).toFile() ) ); + + dl = new ArrayList<>(); + Dependency dep = new Dependency(); + dl.add( dep ); + + el = new ArrayList<>(); + Extension ext = new Extension(); + el.add( ext ); + + ArrayList pel; + pel = new ArrayList<>(); + + PluginExecution exec; + + locCfg = Xpp3DomBuilder.build( new FileReader( getResource( "testInvalid1.pom.xml" ).toFile() ) ); + exec = new PluginExecution(); + exec.setConfiguration( locCfg ); + pel.add( exec ); + + locCfg = Xpp3DomBuilder.build( new FileReader( getResource( "testInvalid2.pom.xml" ).toFile() ) ); + exec = new PluginExecution(); + exec.setConfiguration( locCfg ); + pel.add( exec ); + + locCfg = Xpp3DomBuilder.build( new FileReader( getResource( "testEmptyCfg.pom.xml" ).toFile() ) ); + exec = new PluginExecution(); + exec.setConfiguration( locCfg ); + pel.add( exec ); + + pl = new ArrayList<>(); + Plugin plg; + + plg = new Plugin(); + plg.setGroupId( "org.apache.maven.plugins" ); + plg.setArtifactId( "maven-compiler-plugin" ); + plg.setConfiguration( glCfg ); + plg.addDependency( dep ); + plg.setExecutions( pel ); + pl.add( plg ); + + plg = new Plugin(); + plg.setGroupId( "org.apache.maven.plugins" ); + plg.setArtifactId( "foo" ); + plg.setConfiguration( glCfg ); + plg.addDependency( dep ); + plg.setVersion( "starter edition" ); + pl.add( plg ); + + plg = new Plugin(); + plg.setGroupId( "foofoo" ); + plg.setArtifactId( "maven-compiler-plugin" ); + plg.setConfiguration( glCfg ); + plg.addDependency( dep ); + plg.setVersion( "[1.0]" ); + pl.add( plg ); + + plg = new Plugin(); + plg.setGroupId( "foobar" ); + plg.setArtifactId( "bar" ); + plg.setConfiguration( glCfg ); + plg.addDependency( dep ); + plg.setVersion( "(" ); + pl.add( plg ); + + build = EasyMock.createMock( Build.class ); + model = EasyMock.createMock( Model.class ); + + } + @Test public void testMinimalModelValidation() throws Exception { - Model model = new Model(); - model.setModelVersion( "4.0.0" ); - model.setGroupId( "foo" ); - model.setArtifactId( "bar" ); - model.setVersion( "1.2.3" ); + EasyMock.expect( model.getBuild() ).andReturn( null ).atLeastOnce(); + EasyMock.expect( model.getDependencies() ).andReturn( new ArrayList<>() ).atLeastOnce(); + + EasyMock.replay( model ); assertTrue( validator instanceof XMvnModelValidator ); ( (XMvnModelValidator) validator ).customizeModel( model ); + EasyMock.verify( model ); + } + + @Test + public void testAdvancedModelValidation() + throws Exception + { + EasyMock.expect( build.getExtensions() ).andReturn( el ).atLeastOnce(); + EasyMock.expect( build.getPlugins() ).andReturn( new ArrayList<>() ).atLeastOnce(); + + EasyMock.expect( model.getBuild() ).andReturn( build ).atLeastOnce(); + EasyMock.expect( model.getDependencies() ).andReturn( dl ).atLeastOnce(); + + EasyMock.replay( model, build ); + + ( (XMvnModelValidator) validator ).customizeModel( model ); + EasyMock.verify( model, build ); } + + @Test + public void testFullModelValidation() + throws Exception + { + // TODO: Debug and finish + EasyMock.expect( build.getExtensions() ).andReturn( el ).atLeastOnce(); + EasyMock.expect( build.getPlugins() ).andReturn( pl ).atLeastOnce(); + + EasyMock.expect( model.getBuild() ).andReturn( build ).atLeastOnce(); + EasyMock.expect( model.getDependencies() ).andReturn( dl ).atLeastOnce(); + + EasyMock.replay( model, build ); + + ( (XMvnModelValidator) validator ).customizeModel( model ); + EasyMock.verify( model, build ); + } + } diff --git a/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/MojoExecutionListenerTest.java b/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/MojoExecutionListenerTest.java new file mode 100644 index 0000000..3a17dba --- /dev/null +++ b/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/MojoExecutionListenerTest.java @@ -0,0 +1,151 @@ +package org.fedoraproject.xmvn.connector.aether; + +import static org.junit.Assert.assertTrue; +import javax.inject.Inject; +import org.apache.maven.execution.MojoExecutionEvent; +import org.apache.maven.execution.MojoExecutionListener; +import org.apache.maven.plugin.Mojo; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.project.MavenProject; +import org.easymock.EasyMock; +import org.easymock.EasyMockRunner; +import org.easymock.Mock; +import org.easymock.MockType; +import org.eclipse.sisu.launch.InjectedTest; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import com.google.inject.Binder; + +/** + * @author Roman Vais + */ + +@RunWith( EasyMockRunner.class ) +public class MojoExecutionListenerTest + extends InjectedTest +{ + + @Inject + private MojoExecutionListener listener; + + @Mock( type = MockType.STRICT ) + private MojoExecutionEvent event; + + @Mock( type = MockType.STRICT ) + private Mojo mojo; + + @Mock( type = MockType.STRICT ) + private MojoExecution exec; + + @Mock( type = MockType.STRICT ) + private MavenProject project; + + @Override + public void configure( Binder binder ) + { + binder.bind( MojoExecutionListener.class ).to( XMvnMojoExecutionListener.class ); + } + + @Before + @Override + public void setUp() + throws Exception + { + super.setUp(); + + event = EasyMock.createMock( MojoExecutionEvent.class ); + mojo = EasyMock.createMock( Mojo.class ); + exec = EasyMock.createMock( MojoExecution.class ); + project = EasyMock.createMock( MavenProject.class ); + + EasyMock.expect( event.getMojo() ).andReturn( mojo ).atLeastOnce(); + EasyMock.expect( event.getExecution() ).andReturn( exec ).atLeastOnce(); + EasyMock.replay( event, mojo, exec, project ); + } + + @Test + public void testListenerInjection() + throws Exception + { + assertTrue( listener instanceof XMvnMojoExecutionListener ); + } + + @Test + public void testListenerBeforeExecution() + throws Exception + { + // tests javadoc aggregate + EasyMock.reset( exec ); + EasyMock.expect( exec.getGroupId() ).andReturn( "org.apache.maven.plugins" ).once(); + EasyMock.expect( exec.getArtifactId() ).andReturn( "maven-javadoc-plugin" ).once(); + EasyMock.expect( exec.getGoal() ).andReturn( "aggregate" ).once(); + EasyMock.replay( exec ); + + listener.beforeMojoExecution( event ); + EasyMock.verify( exec ); + + // tests xmvn build deep + EasyMock.reset( exec ); + EasyMock.expect( exec.getGroupId() ).andReturn( "org.fedoraproject.xmvn" ).times( 2 ); + EasyMock.expect( exec.getArtifactId() ).andReturn( "xmvn-mojo" ).once(); + EasyMock.expect( exec.getGoal() ).andReturn( "builddep" ).once(); + EasyMock.replay( exec ); + + listener.beforeMojoExecution( event ); + EasyMock.verify( exec ); + + // tests nonexistent + EasyMock.reset( exec ); + EasyMock.expect( exec.getGroupId() ).andReturn( "org.example" ).times( 2 ); + EasyMock.expect( exec.getArtifactId() ).andReturn( "nonexistent" ).anyTimes(); + EasyMock.expect( exec.getGoal() ).andReturn( "builddep" ).anyTimes(); + EasyMock.replay( exec ); + + listener.beforeMojoExecution( event ); + EasyMock.verify( event, mojo, exec ); + + } + + @Test // TODO: debug and finish (bean interface problem -> mojo mock) + @Ignore + public void testListenerAfterSuccess() + throws Exception + { + // tests javadoc aggregate + EasyMock.reset( exec, event ); + EasyMock.expect( exec.getGroupId() ).andReturn( "org.apache.maven.plugins" ).once(); + EasyMock.expect( exec.getArtifactId() ).andReturn( "maven-javadoc-plugin" ).once(); + EasyMock.expect( exec.getGoal() ).andReturn( "aggregate" ).once(); + EasyMock.expect( event.getMojo() ).andReturn( mojo ).atLeastOnce(); + EasyMock.expect( event.getExecution() ).andReturn( exec ).atLeastOnce(); + EasyMock.expect( event.getProject() ).andReturn( project ); + EasyMock.replay( exec, event ); + + listener.afterMojoExecutionSuccess( event ); + EasyMock.verify( exec ); + + // tests xmvn build deep + EasyMock.reset( exec ); + EasyMock.expect( exec.getGroupId() ).andReturn( "org.fedoraproject.xmvn" ).times( 2 ); + EasyMock.expect( exec.getArtifactId() ).andReturn( "xmvn-mojo" ).once(); + EasyMock.expect( exec.getGoal() ).andReturn( "builddep" ).once(); + EasyMock.replay( exec ); + + listener.afterMojoExecutionSuccess( event ); + EasyMock.verify( exec ); + + // tests nonexistent + EasyMock.reset( exec ); + EasyMock.expect( exec.getGroupId() ).andReturn( "org.example" ).times( 2 ); + EasyMock.expect( exec.getArtifactId() ).andReturn( "nonexistent" ).anyTimes(); + EasyMock.expect( exec.getGoal() ).andReturn( "builddep" ).anyTimes(); + EasyMock.replay( exec ); + + listener.afterMojoExecutionSuccess( event ); + EasyMock.verify( event, mojo, exec ); + + } + +} diff --git a/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/PluginVersionResolverTest.java b/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/PluginVersionResolverTest.java new file mode 100644 index 0000000..18de728 --- /dev/null +++ b/xmvn-connector-aether/src/test/java/org/fedoraproject/xmvn/connector/aether/PluginVersionResolverTest.java @@ -0,0 +1,111 @@ +package org.fedoraproject.xmvn.connector.aether; + +import java.util.ArrayList; +import java.util.Collections; + +import javax.inject.Inject; + +import org.apache.maven.plugin.version.PluginVersionRequest; +import org.apache.maven.plugin.version.PluginVersionResolver; +import org.apache.maven.plugin.version.PluginVersionResult; +import org.easymock.EasyMock; +import org.easymock.Mock; +import org.easymock.MockType; +import org.easymock.TestSubject; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.repository.WorkspaceReader; +import org.eclipse.aether.repository.WorkspaceRepository; +import org.eclipse.sisu.launch.InjectedTest; +import org.fedoraproject.xmvn.artifact.Artifact; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.google.inject.Binder; + +/** + * @author Roman Vais + */ + +public class PluginVersionResolverTest + extends InjectedTest +{ + + @Inject + @TestSubject + private PluginVersionResolver resolver; + + @Override + public void configure( Binder binder ) + { + binder.bind( PluginVersionResolver.class ).to( XMvnPluginVersionResolver.class ); + } + + @Mock (type = MockType.STRICT) + private PluginVersionRequest rq; + + @Mock (type = MockType.STRICT) + private RepositorySystemSession session; + + @Mock (type = MockType.STRICT) + private WorkspaceReader reader; + + private WorkspaceRepository repo; + + @Before + @Override + public void setUp() + throws Exception + { + super.setUp(); + rq = EasyMock.createMock( PluginVersionRequest.class ); + session = EasyMock.createMock( RepositorySystemSession.class ); + reader = EasyMock.createMock( WorkspaceReader.class ); + repo = new WorkspaceRepository(); + + EasyMock.expect( rq.getRepositorySession() ).andReturn( session ).times( 2 ); + EasyMock.expect( rq.getGroupId() ).andReturn( "test.example" ).times( 2 ); + EasyMock.expect( rq.getArtifactId() ).andReturn( "nonexistent" ).times( 2 ); + + EasyMock.expect( session.getWorkspaceReader() ).andReturn( reader ).times( 2 ); + + EasyMock.expect( reader.findVersions( EasyMock.anyObject( DefaultArtifact.class ) ) ).andReturn( Collections.emptyList() ); + EasyMock.expect( reader.getRepository() ).andReturn( repo ).times( 2 ); + + EasyMock.replay( rq, session, reader); + + } + + @Test + public void testInjection() + throws Exception + { + Assert.assertTrue( this.resolver instanceof XMvnPluginVersionResolver ); + } + + @Test + public void testResolution() + throws Exception + { + PluginVersionResult result; + + result = resolver.resolve( rq ); + Assert.assertTrue( result.getRepository().equals( repo ) ); + Assert.assertTrue( result.getVersion().equals( Artifact.DEFAULT_VERSION ) ); + + ArrayList list = new ArrayList<>(); + list.add( "1.2.3" ); + + EasyMock.reset( reader ); + EasyMock.expect( reader.findVersions( EasyMock.anyObject( DefaultArtifact.class ) ) ).andReturn( list ); + EasyMock.expect( reader.getRepository() ).andReturn( repo ); + EasyMock.replay( reader); + + + result = resolver.resolve( rq ); + Assert.assertTrue( result.getRepository().equals( repo ) ); + Assert.assertTrue( result.getVersion().equals( "1.2.3" ) ); + } + +} diff --git a/xmvn-connector-aether/src/test/resources/test.pom.xml b/xmvn-connector-aether/src/test/resources/test.pom.xml new file mode 100644 index 0000000..f5087ae --- /dev/null +++ b/xmvn-connector-aether/src/test/resources/test.pom.xml @@ -0,0 +1,6 @@ + + + 1.6 + 1.6 + + diff --git a/xmvn-connector-aether/src/test/resources/testEmptyCfg.pom.xml b/xmvn-connector-aether/src/test/resources/testEmptyCfg.pom.xml new file mode 100644 index 0000000..d939c22 --- /dev/null +++ b/xmvn-connector-aether/src/test/resources/testEmptyCfg.pom.xml @@ -0,0 +1,4 @@ + + + + diff --git a/xmvn-connector-aether/src/test/resources/testInvalid1.pom.xml b/xmvn-connector-aether/src/test/resources/testInvalid1.pom.xml new file mode 100644 index 0000000..97f6f83 --- /dev/null +++ b/xmvn-connector-aether/src/test/resources/testInvalid1.pom.xml @@ -0,0 +1,5 @@ + + + 1.6 + 1.2 + diff --git a/xmvn-connector-aether/src/test/resources/testInvalid2.pom.xml b/xmvn-connector-aether/src/test/resources/testInvalid2.pom.xml new file mode 100644 index 0000000..431d42e --- /dev/null +++ b/xmvn-connector-aether/src/test/resources/testInvalid2.pom.xml @@ -0,0 +1,6 @@ + + + 1.0 + 1.0 + + diff --git a/xmvn-connector-ivy/src/test/java/org/fedoraproject/xmvn/connector/ivy/DeployTest.java b/xmvn-connector-ivy/src/test/java/org/fedoraproject/xmvn/connector/ivy/DeployTest.java new file mode 100644 index 0000000..1411314 --- /dev/null +++ b/xmvn-connector-ivy/src/test/java/org/fedoraproject/xmvn/connector/ivy/DeployTest.java @@ -0,0 +1,288 @@ +/*- + * Copyright (c) 2014-2016 Red Hat, Inc. + * + * Licensed 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. + */ + +package org.fedoraproject.xmvn.connector.ivy; + +import java.io.File; +import java.io.IOException; + +import org.apache.ivy.core.module.descriptor.Artifact; +import org.apache.ivy.core.module.id.ModuleRevisionId; +import org.apache.ivy.core.settings.IvySettings; +import org.easymock.EasyMock; +import org.easymock.EasyMockRunner; +import org.easymock.Mock; +import org.easymock.MockType; +import org.easymock.TestSubject; +import org.fedoraproject.xmvn.deployer.Deployer; +import org.fedoraproject.xmvn.deployer.DeploymentRequest; +import org.fedoraproject.xmvn.deployer.DeploymentResult; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Mikolaj Izdebski + * @author Roman Vais + */ + +@RunWith( EasyMockRunner.class ) +public class DeployTest +{ + private DeploymentResult deploied, deployFail; + + private File fileValid, fileBroken; + + private Artifact artifact; + + private ModuleRevisionId mri; + + @TestSubject + private final IvyResolver ivyResolver = new IvyResolver(); + + @Mock( type = MockType.STRICT ) + private Deployer deployer; + + @Before + public void setUp() + { + // deployment results + deploied = new DeploymentResult() + { + @Override + public Exception getException() + { + return null; + } + }; + + deployFail = new DeploymentResult() + { + @Override + public Exception getException() + { + return new Exception( "Tested Exception" ); + } + }; + + // prepare all the resources needed for testing; ivy itself + ivyResolver.setSettings( new IvySettings() ); + + // artifact xml files + fileValid = new File( "src/test/resources/bz1127804.ivy" ); + fileBroken = new File( "/tmp/foo/bar/foobar" ); + + artifact = EasyMock.createMock( Artifact.class ); + mri = EasyMock.createMock( ModuleRevisionId.class ); + + } + + /** + * Test of basic functionality publish and deploy methods + */ + @Test + public void publishDeployTest() + throws Exception + { + // prepare arguments for tested function + EasyMock.expect( artifact.getType() ).andReturn( "jar" ).atLeastOnce(); + EasyMock.expect( artifact.getExt() ).andReturn( "jar" ).atLeastOnce(); + EasyMock.expect( artifact.getName() ).andReturn( "artifact" ); + EasyMock.expect( artifact.getExtraAttribute( "classifier" ) ).andReturn( null ); + EasyMock.expect( artifact.getModuleRevisionId() ).andReturn( mri ); + + EasyMock.expect( mri.getOrganisation() ).andReturn( "organization" ); + EasyMock.expect( mri.getRevision() ).andReturn( "1.2.3" ); + + EasyMock.expect( deployer.deploy( EasyMock.isA( DeploymentRequest.class ) ) ).andReturn( deploied ); + EasyMock.replay( artifact, mri, deployer ); + + // run the basic functionality of publish method + ivyResolver.publish( artifact, fileValid, true ); + EasyMock.verify( artifact, mri, deployer ); + } + + /** + * Test of deploy method with use of publish method call and valid xml file, ivy type + */ + @Test + public void publishDeployIvyTest() + throws Exception + { + // prepare arguments for tested function + EasyMock.expect( artifact.getType() ).andReturn( "ivy" ).atLeastOnce(); + EasyMock.expect( artifact.getExt() ).andReturn( "jar" ).atLeastOnce(); + EasyMock.expect( artifact.getName() ).andReturn( "artifact" ); + EasyMock.expect( artifact.getExtraAttribute( "classifier" ) ).andReturn( null ); + EasyMock.expect( artifact.getModuleRevisionId() ).andReturn( mri ); + + EasyMock.expect( mri.getOrganisation() ).andReturn( "organization" ); + EasyMock.expect( mri.getRevision() ).andReturn( "1.2.3" ); + + EasyMock.expect( deployer.deploy( EasyMock.isA( DeploymentRequest.class ) ) ).andReturn( deploied ); + EasyMock.replay( artifact, mri, deployer ); + + // run the basic functionality of publish method + ivyResolver.publish( artifact, fileValid, true ); + EasyMock.verify( artifact, mri, deployer ); + } + + /** + * Test of deploy method with use of publish method call and valid xml file, xml extension + */ + @Test + public void publishDeployXmlTest() + throws Exception + { + // prepare arguments for tested function + EasyMock.expect( artifact.getType() ).andReturn( "jar" ).atLeastOnce(); + EasyMock.expect( artifact.getExt() ).andReturn( "xml" ).atLeastOnce(); + EasyMock.expect( artifact.getName() ).andReturn( "artifact" ); + EasyMock.expect( artifact.getExtraAttribute( "classifier" ) ).andReturn( null ); + EasyMock.expect( artifact.getModuleRevisionId() ).andReturn( mri ); + + EasyMock.expect( mri.getOrganisation() ).andReturn( "organization" ); + EasyMock.expect( mri.getRevision() ).andReturn( "1.2.3" ); + + EasyMock.expect( deployer.deploy( EasyMock.isA( DeploymentRequest.class ) ) ).andReturn( deploied ); + EasyMock.replay( artifact, mri, deployer ); + + // run the basic functionality of publish method + ivyResolver.publish( artifact, fileValid, true ); + EasyMock.verify( artifact, mri, deployer ); + } + + /** + * Test for IOException thrown by deploy method with use of publish method call + */ + @Test + public void publishDeployExceptionTest() + throws Exception + { + // prepare arguments for tested function + EasyMock.expect( artifact.getType() ).andReturn( "jar" ).atLeastOnce(); + EasyMock.expect( artifact.getExt() ).andReturn( "jar" ).atLeastOnce(); + EasyMock.expect( artifact.getName() ).andReturn( "artifact" ); + EasyMock.expect( artifact.getExtraAttribute( "classifier" ) ).andReturn( null ); + EasyMock.expect( artifact.getModuleRevisionId() ).andReturn( mri ); + + EasyMock.expect( mri.getOrganisation() ).andReturn( "organization" ); + EasyMock.expect( mri.getRevision() ).andReturn( "1.2.3" ); + + EasyMock.expect( deployer.deploy( EasyMock.isA( DeploymentRequest.class ) ) ).andReturn( deployFail ); + EasyMock.replay( artifact, mri, deployer ); + + // run the basic functionality of publish method + try + { + ivyResolver.publish( artifact, fileBroken, true ); + Assert.fail(); + } + catch ( Exception e ) + { + + } + EasyMock.verify( artifact, mri, deployer ); + } + + /** + * Test of deployEffectivePom method with use of publish method call and valid xml file + */ + @Test + public void publishDeployEffectivePomTest() + throws Exception + { + // prepare arguments for tested function + EasyMock.expect( artifact.getType() ).andReturn( "ivy" ).atLeastOnce(); + EasyMock.expect( artifact.getExt() ).andReturn( "xml" ).atLeastOnce(); + EasyMock.expect( artifact.getName() ).andReturn( "artifact" ).atLeastOnce(); + EasyMock.expect( artifact.getExtraAttribute( "classifier" ) ).andReturn( null ); + EasyMock.expect( artifact.getModuleRevisionId() ).andReturn( mri ).atLeastOnce(); + + EasyMock.expect( mri.getOrganisation() ).andReturn( "organization" ).atLeastOnce(); + EasyMock.expect( mri.getName() ).andReturn( "FooRevision" ); + EasyMock.expect( mri.getExtraAttribute( "classifier" ) ).andReturn( null ); + EasyMock.expect( mri.getRevision() ).andReturn( "1.2.3" ).atLeastOnce(); + + EasyMock.expect( deployer.deploy( EasyMock.isA( DeploymentRequest.class ) ) ).andReturn( deploied ).times( 2 ); + EasyMock.replay( artifact, mri, deployer ); + + // run the test of the function + ivyResolver.publish( artifact, fileValid, true ); + EasyMock.verify( artifact, mri, deployer ); + } + + /** + * Test for IOException thrown by deployEffectivePom method with use of publish method call if given file doesn't + * exist + */ + @Test + public void publishDeployEffectivePomExeptionTest() + throws Exception + { + // prepare arguments for tested function + EasyMock.expect( artifact.getType() ).andReturn( "ivy" ).atLeastOnce(); + EasyMock.expect( artifact.getExt() ).andReturn( "xml" ).atLeastOnce(); + EasyMock.expect( artifact.getModuleRevisionId() ).andReturn( mri ).atLeastOnce(); + EasyMock.replay( artifact, deployer ); + + // run the test + try + { + ivyResolver.publish( artifact, fileBroken, true ); + Assert.fail(); + } + catch ( IOException e ) + { + + } + EasyMock.verify( artifact, deployer ); + } + + /** + * Test for IOException thrown by deployEffectivePom method with use of publish method call caused by deployment + * failure + */ + @Test + public void publishDeployEffectivePomFailTest() + throws Exception + { + // prepare arguments for tested function + EasyMock.expect( artifact.getType() ).andReturn( "ivy" ).atLeastOnce(); + EasyMock.expect( artifact.getExt() ).andReturn( "xml" ).atLeastOnce(); + EasyMock.expect( artifact.getModuleRevisionId() ).andReturn( mri ).atLeastOnce(); + + EasyMock.expect( mri.getOrganisation() ).andReturn( "organization" ).atLeastOnce(); + EasyMock.expect( mri.getName() ).andReturn( "FooRevision" ); + EasyMock.expect( mri.getExtraAttribute( "classifier" ) ).andReturn( null ); + EasyMock.expect( mri.getRevision() ).andReturn( "1.2.3" ).atLeastOnce(); + + EasyMock.expect( deployer.deploy( EasyMock.isA( DeploymentRequest.class ) ) ).andReturn( deployFail ); + EasyMock.replay( artifact, mri, deployer ); + // run the test + try + { + ivyResolver.publish( artifact, fileValid, true ); + Assert.fail(); + } + catch ( IOException e ) + { + + } + EasyMock.verify( artifact, deployer ); + } +}