Skip to content

Commit

Permalink
Provided integration test for NewApp command
Browse files Browse the repository at this point in the history
  • Loading branch information
fcamblor committed Aug 29, 2013
1 parent b9cd78c commit c1b38e0
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
18 changes: 18 additions & 0 deletions restx-core-shell/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,23 @@
<artifactId>ivy</artifactId>
<version>${ivy.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-verifier</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
117 changes: 117 additions & 0 deletions restx-core-shell/src/test/java/restx/core/shell/NewAppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package restx.core.shell;

import com.google.common.base.Charsets;
import jline.console.ConsoleReader;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.junit.*;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import restx.factory.Factory;
import restx.shell.RestxShell;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.mockito.Mockito.*;

/**
* @author fcamblor
*/
@RunWith(Parameterized.class)
public class NewAppTest {

@Rule
public TemporaryFolder workDirectory = new TemporaryFolder();

private final AppShellCommand.NewAppDescriptor descriptor;
private String initialRestxShellHomeValue;

@Before
public void setup() throws IOException {
// Avoiding "restx.shell.home_IS_UNDEFINED/ dir due to logs

This comment has been minimized.

Copy link
@xhanin

xhanin Aug 29, 2013

Nice!

this.initialRestxShellHomeValue = System.getProperty("restx.shell.home");
if(this.initialRestxShellHomeValue == null) {
System.setProperty("restx.shell.home", workDirectory.newFolder(".restx").getAbsolutePath());
}
}

@After
public void teardown() {
if(this.initialRestxShellHomeValue == null) {
System.getProperties().remove("restx.shell.home");
}
}

@Parameterized.Parameters(name="{0}")
public static Iterable<Object[]> data() throws IOException {
return Arrays.asList(
new Object[]{ "Simplest app", createDescriptor("test1", false) },
new Object[]{ "App with hello resource", createDescriptor("test2", true) }
);
}

public NewAppTest(String testName, AppShellCommand.NewAppDescriptor descriptor) {
this.descriptor = descriptor;
}

@Test
public void should_app_generated_executes_maven_well() throws IOException, VerificationException {
RestxShell shell = prepareRestxShell();

AppShellCommand.NewAppCommandRunner appCommandRunner = new AppShellCommand().new NewAppCommandRunner();
Path appPath = appCommandRunner.generateApp(descriptor, shell);

Verifier mavenVerifier = new Verifier(appPath.toString());
mavenVerifier.executeGoal("package");
mavenVerifier.verifyErrorFreeLog();
}

private RestxShell prepareRestxShell() throws IOException {
ConsoleReader consoleReader = new ConsoleReader();
RestxShell shell = spy(new RestxShell(consoleReader, Factory.builder().build()));
doNothing().when(shell).println(anyString());
doReturn(this.workDirectory.getRoot().toPath()).when(shell).currentLocation();
return shell;
}

private static AppShellCommand.NewAppDescriptor createDescriptor(String name, boolean generateHelloResource) throws IOException {
AppShellCommand.NewAppDescriptor desc = new AppShellCommand.NewAppDescriptor();
desc.appName = name+"App";
desc.groupId = "com.foo";
desc.artifactId = name;
desc.mainPackage = "com.foo";
desc.version = "0.1-SNAPSHOT";
desc.buildFile = "all";
desc.signatureKey = "blah blah blah";
desc.adminPassword = "pwd";
desc.defaultPort = "8080";
desc.basePath = "/api";
desc.restxVersion = resolveCurrentPOMVersion();
desc.generateHelloResource = generateHelloResource;
return desc;
}

private static String resolveCurrentPOMVersion() throws IOException {

This comment has been minimized.

Copy link
@xhanin

xhanin Aug 29, 2013

IIUC here you need to know the version of RESTX against which the generated app should be tested. Rather than getting them from the POM, since restx uses restx-build you could also use it to get the current version with something like:

new RestxJsonSupport().parse(new FileInputStream(new File("md.restx.json"))).getGAV().getVersion()

This comment has been minimized.

Copy link
@fcamblor

fcamblor Aug 29, 2013

Author Owner

nice, prefer this :-)

Path pom = Paths.get(".").resolve("pom.xml");
if(Files.notExists(pom)){
throw new FileNotFoundException("pom.xml not found !");
}

// Crappy way to retrieve current artefact's version
// But seems like implementing the *good* way would take a lot LoCs
// http://stackoverflow.com/questions/11525318/how-do-i-obtain-a-fully-resolved-model-of-a-pom-file
String pomContent = com.google.common.io.Files.toString(pom.toFile(), Charsets.UTF_8);
Matcher versionMatcher = Pattern.compile(".*<version>(.*)</version>.*", Pattern.MULTILINE).matcher(pomContent);
if(versionMatcher.find()){
return versionMatcher.group(1);
}
throw new IllegalStateException("restx version not found !");
}
}

0 comments on commit c1b38e0

Please sign in to comment.