Skip to content

Commit

Permalink
Solving #31
Browse files Browse the repository at this point in the history
Remove references to WarFile
Move and adjust tests for WarFile into GenericMojoTests
Fix bug, missing "s" on  ".ebextensions"
Delete WarFile and tests
  • Loading branch information
Alan Evans committed Jun 21, 2016
1 parent 5351e64 commit 08ebdd2
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 392 deletions.
4 changes: 0 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ public void execute() throws MojoFailureException {
try {
final ZipFile zipFile = this.createZipFile();
this.validate(zipFile);
final WarFile warFile = this.createWarFile(zipFile);
this.validateWarFile(warFile);
zipFile.close();
} catch (final IOException ex) {
throw new MojoFailureException(
Expand Down Expand Up @@ -213,15 +211,6 @@ public void execute() throws MojoFailureException {
}
}

/**
* Creates a {@link WarFile} out of the {@link ZipFile}.
* @param zipfile The zip file.
* @return WarFile the war file.
*/
protected WarFile createWarFile(final ZipFile zipfile) {
return new WarFile(zipfile);
}

/**
* Creates a {@link ZipFile} out of the war.
* @return ZipFile the zip file
Expand All @@ -231,16 +220,6 @@ protected ZipFile createZipFile() throws IOException {
return new ZipFile(this.war);
}

/**
* Validate {@link WarFile} given.
* @param warfile The war file
* @throws MojoFailureException Throw in case of validation error.
*/
protected void validateWarFile(final WarFile warfile)
throws MojoFailureException {
warfile.checkEbextensionsValidity();
}

/**
* Creates server crecentials.
* @return Server credentials based on settings and server attributes.
Expand Down Expand Up @@ -335,23 +314,26 @@ protected boolean validYaml(final String text) {
}

/**
* Validate the war file.
*
* Verifies that the .ebextensions contains valid configuration file or
* files.
* @param zip Zip file
* @throws org.apache.maven.plugin.MojoFailureException Thrown, if
* validation fails.
* @throws org.apache.maven.plugin.MojoFailureException Thrown, if the
* .ebextensions does not exist in the WAR file, is empty or one of its
* files is neither valid JSON, nor valid YAML.
*/
protected void validate(final ZipFile zip) throws MojoFailureException {
if (zip.getEntry(".ebextension") == null) {
if (zip.getEntry(".ebextensions") == null) {
throw new MojoFailureException(
".ebextensions directory does not exist in the WAR file"
);
}
final Enumeration<? extends ZipEntry> entries = zip.entries();
int files = 0;
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
if (entry.getName().startsWith(".ebextensions/")
&& !entry.isDirectory()) {
files += 1;
final String text = this.readFile(zip, entry);
if (this.validJson(text) || this.validYaml(text)) {
continue;
Expand All @@ -366,6 +348,11 @@ protected void validate(final ZipFile zip) throws MojoFailureException {
);
}
}
if (files < 1) {
throw new MojoFailureException(
".ebextensions contains no config files."
);
}
}

/**
Expand Down
155 changes: 0 additions & 155 deletions src/main/java/com/jcabi/beanstalk/maven/plugin/WarFile.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@
package com.jcabi.beanstalk.maven.plugin;

import java.io.File;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.apache.maven.plugin.MojoFailureException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -72,8 +78,6 @@ public void executesSuccessfully() throws Exception {
mojo.execute();
Mockito.verify(mojo).createZipFile();
Mockito.verify(mojo).validate(mockZipFile);
Mockito.verify(mojo).createWarFile(mockZipFile);
Mockito.verify(mojo).validateWarFile(Mockito.any(WarFile.class));
Mockito.verify(mojo).createServerCredentials();
Mockito.verify(mockZipFile).close();
Mockito.verify(mockFile, Mockito.times(2)).exists();
Expand Down Expand Up @@ -147,4 +151,78 @@ public void validatesGoodYaml() throws Exception {
)
);
}

/**
* Verifies that execute throws an exception, if there is no .ebextensions
* directory in the WAR file.
* @throws Exception Thrown in case of error.
*/
@Test
public void checkEbextensionsValidityThrowsExceptionNoDir()
throws Exception {
// @checkstyle IllegalTypeCheck (2 lines)
final AbstractBeanstalkMojo mojo =
Mockito.mock(AbstractBeanstalkMojo.class);
final File mockFile = Mockito.mock(File.class);
final ZipFile mockZipFile = Mockito.mock(ZipFile.class);
Mockito.when(mockFile.exists()).thenReturn(true);
Mockito.when(mojo.createZipFile()).thenReturn(mockZipFile);
Mockito.doCallRealMethod().when(mojo).execute();
Mockito.doCallRealMethod().when(mojo)
.setWar(Mockito.any(File.class));
Mockito.doCallRealMethod().when(mojo)
.validate(Mockito.any(ZipFile.class));
mojo.setWar(mockFile);
Mockito.when(mockZipFile.getEntry(".ebextensions")).thenReturn(null);
try {
mojo.execute();
Assert.fail("Expect MojoFailureException to be thrown");
} catch (final MojoFailureException exception) {
MatcherAssert.assertThat(
exception.getMessage(),
Matchers.equalTo(
".ebextensions directory does not exist in the WAR file"
)
);
}
}

/**
* Verifies that execute throws an exception, if the .ebextensions directory
* is empty.
* @throws Exception Thrown in case of error.
*/
@Test
@SuppressWarnings("unchecked")
public void checkEbextensionsValidityThrowsExceptionNoConfigFiles()
throws Exception {
// @checkstyle IllegalTypeCheck (2 lines)
final AbstractBeanstalkMojo mojo =
Mockito.mock(AbstractBeanstalkMojo.class);
Mockito.doCallRealMethod().when(mojo).execute();
Mockito.doCallRealMethod().when(mojo).createZipFile();
Mockito.doCallRealMethod().when(mojo)
.setWar(Mockito.any(File.class));
Mockito.doCallRealMethod().when(mojo)
.validate(Mockito.any(ZipFile.class));
final File temp = File.createTempFile("test", ".zip");
final FileOutputStream fos = new FileOutputStream(temp);
final ZipOutputStream out = new ZipOutputStream(fos);
out.putNextEntry(new ZipEntry(".ebextensions/"));
out.flush();
out.close();
fos.flush();
fos.close();
mojo.setWar(temp);
try {
mojo.execute();
} catch (final MojoFailureException exception) {
MatcherAssert.assertThat(
exception.getMessage(),
Matchers.equalTo(
".ebextensions contains no config files."
)
);
}
}
}
Loading

0 comments on commit 08ebdd2

Please sign in to comment.