Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
ZANATA-28 - convert testng to junit
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Aug 23, 2013
1 parent 5a7c79e commit 93cdc6a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 67 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Expand Up @@ -155,12 +155,18 @@
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
15 changes: 10 additions & 5 deletions zanata-cli/pom.xml
Expand Up @@ -164,11 +164,16 @@
<artifactId>powermock-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
60 changes: 60 additions & 0 deletions zanata-cli/src/test/java/org/zanata/client/ParameterRule.java
@@ -0,0 +1,60 @@
package org.zanata.client;

import java.util.List;

import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;

/**
* This rule is a workaround to use PowerMockRunner or any other JUnit runner but wanted JUnit parameterized runner feature.
* PowerMockRule can not be used due to lack of dependency (powermock-classloading-xstream) in Fedora.
* The alternative dependency (powermock-classloading-objenesis) doesn't work in java 7.
*
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
public class ParameterRule<T> implements MethodRule
{
private static final Logger log = LoggerFactory.getLogger(ParameterRule.class);

private int parameterIndex = 0;
private List<T> parameters;

public ParameterRule(T... parameters)
{
this.parameters = ImmutableList.copyOf(parameters);
}

public T getParameter()
{
return parameters.get(parameterIndex);
}

@Override
public Statement apply(final Statement base, FrameworkMethod method, Object target)
{
return new Statement()
{
public void evaluate()
{
for (int i = 0; i < parameters.size(); i++)
{
parameterIndex = i;
try
{
log.debug("running with parameter: {}", parameters.get(parameterIndex));
base.evaluate();
}
catch (Throwable throwable)
{
throw Throwables.propagate(throwable);
}
}
}
};
}
}
91 changes: 32 additions & 59 deletions zanata-cli/src/test/java/org/zanata/client/ZanataClientTest.java
@@ -1,79 +1,52 @@
package org.zanata.client;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kohsuke.args4j.spi.SubCommand;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.IObjectFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;
import org.powermock.modules.junit4.PowerMockRunner;
import org.zanata.client.commands.NullAbortStrategy;
import org.zanata.client.commands.ZanataCommand;
import org.zanata.client.commands.stats.GetStatisticsOptionsImpl;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
@PrepareForTest(SubCommandHandler2.class)
@PowerMockIgnore("org.apache.log4j.*")
@RunWith(PowerMockRunner.class)
public class ZanataClientTest
{
ByteArrayOutputStream out;
ByteArrayOutputStream err;
ZanataClient client;
@Rule
public ParameterRule<String> rule = new ParameterRule<String>("list-remote", "pull", "push", "put-project", "put-user", "put-version", "stats");

@ObjectFactory
public IObjectFactory getObjectFactory()
{
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
private ByteArrayOutputStream out;
private ByteArrayOutputStream err;
private ZanataClient client;

@BeforeMethod
void before()
@Before
public void before()
{
out = new ByteArrayOutputStream();
err = new ByteArrayOutputStream();
client = new ZanataClient(new NullAbortStrategy(), new PrintStream(out), new PrintStream(err));
}

@DataProvider(name = "commands")
public static Object[][] createCommandNames() throws Exception
{
return toGrid(Arrays.asList(/*"help",*/ "list-remote", "pull",
"push", "put-project", "put-user", "put-version", "stats"));
}

/**
* Useful for TestNG DataProvider methods.
*
* @param collection
* @return
* @throws Exception
*/
private static Object[][] toGrid(Collection<?> collection) throws Exception
{
Object[][] result = new Object[collection.size()][1];
int i = 0;
for (Object obj : collection)
{
result[i++] = new Object[] { obj };
}
return result;
}

/**
* The top-level command, and each subcommand, should output a Usage
* message which includes a --help option.
Expand Down Expand Up @@ -122,10 +95,10 @@ public void testHelpOption() throws Exception
* zanata-cli help sub-command
* @throws Exception
*/
@Test(dataProvider = "commands")
public void testHelpSubCommand(String cmdName) throws Exception
@Test
public void testHelpSubCommand() throws Exception
{
client.processArgs("help", cmdName);
client.processArgs("help", rule.getParameter());
assertOutputIncludesUsage(out);
assertThat("Usage should not list subcommands", !outputListsCommands(out));
}
Expand All @@ -134,10 +107,10 @@ public void testHelpSubCommand(String cmdName) throws Exception
* zanata-cli sub-command --help
* @throws Exception
*/
@Test(dataProvider = "commands")
public void testHelpSubOption(String cmdName) throws Exception
@Test
public void testHelpSubOption() throws Exception
{
client.processArgs(cmdName, "--help");
client.processArgs(rule.getParameter(), "--help");
assertOutputIncludesUsage(out);
assertThat("Usage should not list subcommands", !outputListsCommands(out));
}
Expand All @@ -153,7 +126,7 @@ public void testInvalidCommand() throws Exception
assertOutputIncludesUsage(err);
assertThat("Usage should list subcommands", outputListsCommands(err));
}

/**
* zanata-cli --nosuchoption
* @throws Exception
Expand All @@ -165,15 +138,15 @@ public void testInvalidOption() throws Exception
assertOutputIncludesUsage(err);
assertThat("Usage should list subcommands", outputListsCommands(err));
}

/**
* zanata-cli sub-command --nosuchoption
* @throws Exception
*/
@Test(dataProvider = "commands")
public void testInvalidSubOption(String cmdName) throws Exception
@Test
public void testInvalidSubOption() throws Exception
{
client.processArgs(cmdName, "--nosuchoption");
client.processArgs(rule.getParameter(), "--nosuchoption");
assertOutputIncludesUsage(err);
// ideally, we would get the subcommand's help, but args4j doesn't return the subcommand name when something goes wrong:
// assertThat("Usage should not list subcommands", !outputListsCommands(err));
Expand Down

0 comments on commit 93cdc6a

Please sign in to comment.