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

Commit

Permalink
Avoid mutation of test data; use DTOUtil.toXML instead of toString
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Sep 24, 2013
1 parent cbce91f commit 5417de9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
36 changes: 26 additions & 10 deletions zanata-war/src/test/java/org/zanata/rest/service/RestUtilsTest.java
Expand Up @@ -15,6 +15,7 @@
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.zanata.rest.dto.DTOUtil;
import org.zanata.rest.dto.VersionInfo;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.ResourceMeta;
Expand Down Expand Up @@ -50,27 +51,42 @@ public void prepareSeam()
@DataProvider(name = "ResourceTestData")
public Object[][] getResourceTestData()
{
return new Object[][] { new Object[] { resourceTestFactory.getPoHeaderTest() }, new Object[] { resourceTestFactory.getPotEntryHeaderTest() }, new Object[] { resourceTestFactory.getTextFlowCommentTest() }
, new Object[] { resourceTestFactory.getTextFlowTest2() }
// @formatter:off
return new Object[][] {
new Object[] { "getPoHeaderTest", resourceTestFactory.getPoHeaderTest() },
new Object[] { "getPotEntryHeaderTest", resourceTestFactory.getPotEntryHeaderTest() },
new Object[] { "getTextFlowCommentTest", resourceTestFactory.getTextFlowCommentTest() },
new Object[] { "getTextFlowTest2", resourceTestFactory.getTextFlowTest2() }
};
// @formatter:on
}

@DataProvider(name = "ResourceMetaTestData")
public Object[][] getResourceMetaTestData()
{
return new Object[][] { new Object[] { resourceTestFactory.getResourceMeta() }, new Object[] { resourceTestFactory.getPoHeaderResourceMeta() }
// @formatter:off
return new Object[][] {
new Object[] { "getResourceMeta", resourceTestFactory.getResourceMeta() },
new Object[] { "getPoHeaderResourceMeta", resourceTestFactory.getPoHeaderResourceMeta() }
};
// @formatter:on
}

@DataProvider(name = "TranslationTestData")
public Object[][] getTranslationTestData()
{
return new Object[][] { new Object[] { transTestFactory.getPoTargetHeaderTextFlowTargetTest() }, new Object[] { transTestFactory.getTestObject() }, new Object[] { transTestFactory.getTestObject2() }, new Object[] { transTestFactory.getTextFlowTargetCommentTest() }
// @formatter:off
return new Object[][] {
new Object[] { "getPoTargetHeaderTextFlowTargetTest", transTestFactory.getPoTargetHeaderTextFlowTargetTest() },
new Object[] { "getTestObject", transTestFactory.getTestObject() },
new Object[] { "getTestObject2", transTestFactory.getTestObject2() },
new Object[] { "getTextFlowTargetCommentTest", transTestFactory.getTextFlowTargetCommentTest() }
};
// @formatter:on
}

@Test(dataProvider = "ResourceTestData")
public void testUnmarshallResource(Resource res) throws UnsupportedEncodingException
public void testUnmarshallResource(String desc, Resource res) throws UnsupportedEncodingException
{
// SeamMockClientExecutor test = new SeamMockClientExecutor();
// ClientRequest client = test.createRequest("http://example.com/");
Expand All @@ -83,13 +99,13 @@ private <T extends Serializable> void testRestUtilUnmarshall(T entity, Class<T>
InputStream messageBody = null;
try
{
String testStr = entity.toString();
String testStr = DTOUtil.toXML(entity);
log.info("expect:" + testStr);

messageBody = new ByteArrayInputStream(testStr.getBytes("UTF-8"));
T unmarshall = restUtils.unmarshall(type, messageBody, MediaType.APPLICATION_XML_TYPE, new Headers<String>());
Log.info("got:" + unmarshall.toString());
assertThat(entity.toString(), is(testStr));
Log.info("got:" + DTOUtil.toXML(unmarshall));
assertThat(DTOUtil.toXML(entity), is(testStr));
}
finally
{
Expand Down Expand Up @@ -142,13 +158,13 @@ public void testUnmarshallJasonTranslationsResource()


@Test(dataProvider = "TranslationTestData")
public void testUnmarshallTranslation(TranslationsResource res) throws UnsupportedEncodingException
public void testUnmarshallTranslation(String desc, TranslationsResource res) throws UnsupportedEncodingException
{
testRestUtilUnmarshall(res, TranslationsResource.class);
}

@Test(dataProvider = "ResourceMetaTestData")
public void testUnmarshallResourceMeta(ResourceMeta res) throws UnsupportedEncodingException
public void testUnmarshallResourceMeta(String desc, ResourceMeta res) throws UnsupportedEncodingException
{
testRestUtilUnmarshall(res, ResourceMeta.class);
}
Expand Down
@@ -1,6 +1,7 @@
package org.zanata.rest.service;

import javax.ws.rs.core.Response.Status;
import javax.xml.bind.JAXBException;

import org.jboss.resteasy.client.ClientResponse;
import org.mockito.Mockito;
Expand All @@ -11,6 +12,7 @@
import org.testng.annotations.Test;
import org.zanata.common.LocaleId;
import org.zanata.rest.StringSet;
import org.zanata.rest.dto.DTOUtil;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TranslationsResource;
import org.zanata.seam.SeamAutowire;
Expand All @@ -36,12 +38,12 @@ public Object[][] getTestData()
{
// @formatter:off
return new Object[][]
{
new Object[] { transTestFactory.getTestObject() },
new Object[] { transTestFactory.getPoTargetHeaderTextFlowTargetTest() },
new Object[] { transTestFactory.getTextFlowTargetCommentTest() },
new Object[] { transTestFactory.getAllExtension() }
};
{
new Object[] { "getTestObject", transTestFactory.getTestObject() },
new Object[] { "getPoTargetHeaderTextFlowTargetTest", transTestFactory.getPoTargetHeaderTextFlowTargetTest() },
new Object[] { "getTextFlowTargetCommentTest", transTestFactory.getTextFlowTargetCommentTest() },
new Object[] { "getAllExtension", transTestFactory.getAllExtension() }
};
// @formatter:on
}

Expand Down Expand Up @@ -81,8 +83,9 @@ public void testDeleteTranslation()
}

@Test(dataProvider = "TranslationTestData")
public void testPutGetTranslation(TranslationsResource sr)
public void testPutGetTranslation(String desc, TranslationsResource sr)
{
sr = cloneDTO(sr);
Resource res = resourceTestFactory.getTextFlowTest();
sourceDocResource.putResource(res.getName(), res, new StringSet("gettext;comment"));
log.debug("successful put resource:" + res.getName());
Expand All @@ -97,8 +100,9 @@ public void testPutGetTranslation(TranslationsResource sr)
}

@Test(dataProvider = "TranslationTestData")
public void testPutGetTranslationNoExtension(TranslationsResource sr)
public void testPutGetTranslationNoExtension(String desc, TranslationsResource sr)
{
sr = cloneDTO(sr);
Resource res = resourceTestFactory.getTextFlowTest();
sourceDocResource.putResource(res.getName(), res, new StringSet("gettext;comment"));
log.debug("successful put resource:" + res.getName());
Expand All @@ -113,9 +117,10 @@ public void testPutGetTranslationNoExtension(TranslationsResource sr)
assertThat(base.toString(), is(get.toString()));
}

@Test(dataProvider = "TranslationTestData")
public void testPutNoExtensionGetTranslation(TranslationsResource sr)
@Test(dataProvider = "TranslationTestData", dependsOnMethods = { "testPutGetTranslation" })
public void testPutNoExtensionGetTranslation(String desc, TranslationsResource sr)
{
sr = cloneDTO(sr);
Resource res = resourceTestFactory.getTextFlowTest();
sourceDocResource.putResource(res.getName(), res, new StringSet("gettext;comment"));
log.debug("successful put resource:" + res.getName());
Expand All @@ -131,8 +136,9 @@ public void testPutNoExtensionGetTranslation(TranslationsResource sr)
}

@Test(dataProvider = "TranslationTestData")
public void testPutGetNoExtensionTranslation(TranslationsResource sr)
public void testPutGetNoExtensionTranslation(String desc, TranslationsResource sr)
{
sr = cloneDTO(sr);
Resource res = resourceTestFactory.getTextFlowTest();
sourceDocResource.putResource(res.getName(), res, new StringSet("gettext;comment"));
log.debug("successful put resource:" + res.getName());
Expand All @@ -147,4 +153,16 @@ public void testPutGetNoExtensionTranslation(TranslationsResource sr)
assertThat(base.toString(), is(get.toString()));
}

private <T> T cloneDTO(T dto)
{
try
{
return (T) DTOUtil.toObject(DTOUtil.toXML(dto), dto.getClass());
}
catch (JAXBException e)
{
throw new RuntimeException(e);
}
}

}

0 comments on commit 5417de9

Please sign in to comment.