Skip to content

Commit

Permalink
Add Test
Browse files Browse the repository at this point in the history
Add first (somewhat silly) unit tests to `docker:watch` (#187)
  • Loading branch information
rhuss committed Jul 3, 2015
1 parent 6d8614d commit a780ec6
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ private Entry(File srcFile, File destFile) {
if (!srcFile.exists()) {
throw new IllegalArgumentException("Source " + srcFile + " does not exist");
}
if (srcFile.isDirectory()) {
throw new IllegalArgumentException("Can only watch files, not directories: " + srcFile);
}
if (destFile.isAbsolute()) {
throw new IllegalArgumentException("Destination " + destFile + " must not be absolute");
}
Expand All @@ -86,7 +89,7 @@ public File getDestFile() {
return destFile;
}

public boolean isUpdated() {
boolean isUpdated() {
if (srcFile.lastModified() > lastModified) {
// Update last modified as a side effect
lastModified = srcFile.lastModified();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/
public class MappingTrackArchiver extends TrackingArchiver {


public void clear() {
added.clear();
}
Expand All @@ -44,7 +43,7 @@ public AssemblyFiles getAssemblyFiles() {
AssemblyFiles ret = new AssemblyFiles();
for (Addition addition : added) {
Object resource = addition.resource;
if (resource instanceof File) {
if (resource instanceof File && addition.destination != null) {
ret.addEntry((File) resource,new File(addition.destination));
} else if (resource instanceof FileSet) {
FileSet fs = (FileSet) resource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package org.jolokia.docker.maven.service;/*
package org.jolokia.docker.maven.service;

/*
*
* Copyright 2014 Roland Huss
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
package org.jolokia.docker.maven.assembly;

import java.util.Arrays;

import mockit.*;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
import org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException;
import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
import org.apache.maven.plugin.assembly.archive.AssemblyArchiver;
import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
import org.apache.maven.plugin.assembly.io.AssemblyReadException;
import org.apache.maven.plugin.assembly.io.AssemblyReader;
import org.apache.maven.plugin.assembly.model.Assembly;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.manager.ArchiverManager;
import org.codehaus.plexus.util.ReflectionUtils;
import org.jolokia.docker.maven.config.AssemblyConfiguration;
import org.jolokia.docker.maven.config.BuildImageConfiguration;
import org.junit.Before;
import org.jolokia.docker.maven.util.MojoParameters;
import org.junit.Test;

import static org.junit.Assert.assertFalse;

public class DockerAssemblyManagerTest {

@Tested
private DockerAssemblyManager assemblyManager;

@Before
public void setup() {
assemblyManager = new DockerAssemblyManager();
}
@Injectable
private AssemblyArchiver assemblyArchiver;

@Injectable
private AssemblyReader assemblyReader;

@Injectable
private ArchiverManager archiverManager;

@Injectable
private MappingTrackArchiver trackArchiver;


@Test
public void testNoAssembly() {
Expand All @@ -27,4 +51,35 @@ public void testNoAssembly() {
assertFalse(content.contains("COPY"));
assertFalse(content.contains("VOLUME"));
}

@Test
public void assemblyFiles(@Injectable final MojoParameters mojoParams,
@Injectable final MavenProject project,
@Injectable final Assembly assembly) throws AssemblyFormattingException, ArchiveCreationException, InvalidAssemblerConfigurationException, MojoExecutionException, AssemblyReadException, IllegalAccessException {

ReflectionUtils.setVariableValueInObject(assemblyManager, "trackArchiver", trackArchiver);

new NonStrictExpectations() {{
mojoParams.getOutputDirectory();
result = "target/"; times = 3;

mojoParams.getProject();
result = project;

project.getBasedir();
result = ".";

assemblyReader.readAssemblies((AssemblerConfigurationSource) any);
result = Arrays.asList(assembly);

}};

BuildImageConfiguration buildConfig =
new BuildImageConfiguration.Builder()
.assembly(new AssemblyConfiguration.Builder()
.descriptorRef("artifact")
.build())
.build();

assemblyManager.getAssemblyFiles("testImage", buildConfig, mojoParams);}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.jolokia.docker.maven.assembly;/*
*
* Copyright 2014 Roland Huss
*
* 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.
*/

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import static org.junit.Assert.*;

/**
* @author roland
* @since 02/07/15
*/
public class MappingTrackArchiverTest {


@Test(expected = IllegalArgumentException.class)
public void noDirectory() throws Exception {
MappingTrackArchiver archiver = new MappingTrackArchiver();
archiver.addDirectory(new File(System.getProperty("user.home")), "tmp");
AssemblyFiles files = archiver.getAssemblyFiles();
}

@Test
public void simple() throws Exception {
MappingTrackArchiver archiver = new MappingTrackArchiver();
File tempFile = File.createTempFile("tracker", "txt");
archiver.addFile(tempFile, "test.txt");
AssemblyFiles files = archiver.getAssemblyFiles();
assertNotNull(files);
List<AssemblyFiles.Entry> entries = files.getUpdatedEntriesAndRefresh();
assertEquals(0, entries.size());
Thread.sleep(1000);
FileUtils.touch(tempFile);
entries = files.getUpdatedEntriesAndRefresh();
assertEquals(1,entries.size());
AssemblyFiles.Entry entry = entries.get(0);
assertEquals(tempFile,entry.getSrcFile());
assertEquals(new File("test.txt"),entry.getDestFile());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package org.jolokia.docker.maven.service;/*
*
* Copyright 2014 Roland Huss
*
* 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.
*/

import java.io.IOException;
import java.io.StringReader;

import mockit.*;
import mockit.integration.junit4.JMockit;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.*;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.RepositorySystemSession;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* @author roland
* @since 01/07/15
*/
@RunWith(JMockit.class)
public class MojoExecutionServiceTest {

@Tested
MojoExecutionService executionService;

@Mocked @Injectable
protected MavenProject project;

@Mocked @Injectable
MavenSession session;

@Mocked @Injectable
BuildPluginManager pluginManager;

@Mocked
RepositorySystemSession repository;

@Mocked
PluginDescriptor pluginDescriptor;

@Test
public void straight() throws MojoFailureException, MojoExecutionException, InvalidPluginDescriptorException, PluginResolutionException, PluginDescriptorParsingException, PluginNotFoundException, PluginConfigurationException, PluginManagerException, IOException, XmlPullParserException {
call("io.fabric8:fabric8-maven-plugin", "delete-pods", null, true);
}

@Test
public void straightWithExecutionId() throws MojoFailureException, MojoExecutionException, InvalidPluginDescriptorException, PluginResolutionException, PluginDescriptorParsingException, PluginNotFoundException, PluginConfigurationException, PluginManagerException, IOException, XmlPullParserException {
call("io.fabric8:fabric8-maven-plugin", "delete-pods", "1", true);
}

@Test(expected = MojoExecutionException.class)
public void noDescriptor() throws MojoFailureException, MojoExecutionException, InvalidPluginDescriptorException, PluginResolutionException, PluginDescriptorParsingException, PluginNotFoundException, PluginConfigurationException, PluginManagerException, IOException, XmlPullParserException {
call("io.fabric8:fabric8-maven-plugin", "delete-pods", null, false);
}

private void call(final String plugin, final String goal, final String executionId, final boolean withDescriptor) throws MojoFailureException, MojoExecutionException, InvalidPluginDescriptorException, PluginResolutionException, PluginDescriptorParsingException, PluginNotFoundException, PluginConfigurationException, PluginManagerException, IOException, XmlPullParserException {
new Expectations() {{
project.getPlugin(plugin);
Plugin plugin = new Plugin();
result = new Plugin();

session.getRepositorySession();
result = repository;

project.getRemotePluginRepositories();
result = null;

pluginManager.loadPlugin(plugin,null,repository);
result = pluginDescriptor;

pluginDescriptor.getMojo(goal);
if (withDescriptor) {
MojoDescriptor descriptor = new MojoDescriptor();
PlexusConfiguration config = new XmlPlexusConfiguration(Xpp3DomBuilder.build(new StringReader("<config name='test'><test>1</test></config>")));
descriptor.setMojoConfiguration(config);
result = descriptor;

pluginManager.executeMojo(session, (MojoExecution) any);
} else {
result = null;
}
}};

executionService.callPluginGoal(plugin + ":" + goal + (executionId != null ? "#" + executionId : "") );

new Verifications() {{
}};
}

@Test(expected = MojoFailureException.class)
public void noPlugin() throws MojoFailureException, MojoExecutionException {
new Expectations() {{
project.getPlugin(anyString);
result = null;
}};

executionService.callPluginGoal("bla:blub:bla");
}

@Test(expected = MojoFailureException.class)
public void wrongFormat() throws MojoFailureException, MojoExecutionException {
executionService.callPluginGoal("blubber");
}
}

0 comments on commit a780ec6

Please sign in to comment.