Permalink
Browse files

update validators to use localisable messages

  • Loading branch information...
1 parent 2099604 commit f8eaaa0666357c6fe284d6cf4618650ec3513fc0 @davidmason davidmason committed Jan 19, 2012
View
80 server/zanata-war/src/main/java/org/zanata/webtrans/client/resources/ValidationMessages.java
@@ -0,0 +1,80 @@
+package org.zanata.webtrans.client.resources;
+
+import java.util.List;
+
+import com.google.gwt.i18n.client.LocalizableResource.DefaultLocale;
+import com.google.gwt.i18n.client.LocalizableResource.Generate;
+import com.google.gwt.i18n.client.Messages;
+
+/**
+ * @author David Mason, damason@redhat.com
+ *
+ */
+@DefaultLocale
+@Generate(format = "com.google.gwt.i18n.rebind.format.PropertiesFormat")
+public interface ValidationMessages extends Messages
+{
+ // Newline validator
+
+ @DefaultMessage("Newline lead/trail")
+ String newlineValidatorName();
+
+ @DefaultMessage("Newline lead/trail validation")
+ String newlineValidatorDescription();
+
+ @DefaultMessage("Leading newline missing from target")
+ String leadingNewlineMissing();
+
+ @DefaultMessage("Leading newline added to target")
+ String leadingNewlineAdded();
+
+ @DefaultMessage("Trailing newline missing from target")
+ String trailingNewlineMissing();
+
+ @DefaultMessage("Trailing newline added to target")
+ String trailingNewlineAdded();
+
+
+ // Variables validator
+
+ @DefaultMessage("Variables check")
+ String variablesValidatorName();
+
+ @DefaultMessage("Variables check validation")
+ String variablesValidatorDescription();
+
+ @Description("Lists the variables that are in the original string but have not been included in the target")
+ @DefaultMessage("Variables [ {0,list,string} ] missing in target")
+ @AlternateMessage({ "one", "Variable [ {0,list,string} ] missing in target" })
+ String varsMissing(@PluralCount
+ List<String> vars);
+
+ @Description("Lists the variables that are in the target but are not in the original string")
+ @DefaultMessage("Variables [ {0,list,string} ] added in target")
+ @AlternateMessage({ "one", "Variable [ {0,list,string} ] added in target" })
+ String varsAdded(@PluralCount
+ List<String> vars);
+
+
+ // XHM/HTML tag validator
+
+ @DefaultMessage("HTML/XML tag")
+ String xmlHtmlValidatorName();
+
+ @DefaultMessage("Matching HTML/XML tag validation")
+ String xmlHtmlValidatorDescription();
+
+ @Description("Lists the xml or html tags that are in the target but are not in the original string")
+ @DefaultMessage("Tags [ {0,list,string} ] added in target")
+ @AlternateMessage({ "one", "Tag [ {0,list,string} ] added in target" })
+ String tagsAdded(@PluralCount List<String> tags);
+
+ @Description("Lists the xml or html tags that are in the original string but have not been included in the target")
+ @DefaultMessage("Tags [ {0,list,string} ] missing in target")
+ @AlternateMessage({ "one", "Tag [ {0,list,string} ] missing in target" })
+ String tagsMissing(@PluralCount List<String> tags);
+
+ @DefaultMessage("Tags [ {0,list,string} ] are in a different order in target and source")
+ @AlternateMessage({ "one", "Tag [ {0,list,string} ] is in a different location in target and source" })
+ String tagsWrongOrder(@PluralCount List<String> tags);
+}
View
10 server/zanata-war/src/main/java/org/zanata/webtrans/client/validation/ValidationService.java
@@ -31,6 +31,7 @@
import org.zanata.webtrans.client.events.NotificationEvent.Severity;
import org.zanata.webtrans.client.events.UpdateValidationErrorEvent;
import org.zanata.webtrans.client.resources.TableEditorMessages;
+import org.zanata.webtrans.client.resources.ValidationMessages;
import org.zanata.webtrans.shared.model.TransUnitId;
import org.zanata.webtrans.shared.validation.ValidationObject;
import org.zanata.webtrans.shared.validation.action.HtmlXmlTagValidation;
@@ -53,15 +54,14 @@
private final TableEditorMessages messages;
@Inject
- public ValidationService(final EventBus eventBus, final TableEditorMessages messages)
+ public ValidationService(final EventBus eventBus, final TableEditorMessages messages, final ValidationMessages valMessages)
{
this.eventBus = eventBus;
this.messages = messages;
-
- HtmlXmlTagValidation htmlxmlValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
- NewlineLeadTrailValidation newlineLeadTrailValidation = new NewlineLeadTrailValidation("Newline lead/trail", "Newline lead/trail validation");
- VariablesValidation variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
+ HtmlXmlTagValidation htmlxmlValidation = new HtmlXmlTagValidation(valMessages);
+ NewlineLeadTrailValidation newlineLeadTrailValidation = new NewlineLeadTrailValidation(valMessages);
+ VariablesValidation variablesValidation = new VariablesValidation(valMessages);
validationMap.put(htmlxmlValidation.getId(), htmlxmlValidation);
validationMap.put(newlineLeadTrailValidation.getId(), newlineLeadTrailValidation);
View
41 ...-war/src/main/java/org/zanata/webtrans/shared/validation/action/HtmlXmlTagValidation.java
@@ -22,8 +22,8 @@
import java.util.ArrayList;
import java.util.List;
-import java.util.Stack;
+import org.zanata.webtrans.client.resources.ValidationMessages;
import org.zanata.webtrans.shared.validation.ValidationUtils;
import com.allen_sauer.gwt.log.client.Log;
@@ -37,9 +37,9 @@
**/
public class HtmlXmlTagValidation extends ValidationAction
{
- public HtmlXmlTagValidation(String id, String description)
+ public HtmlXmlTagValidation(final ValidationMessages messages)
{
- super(id, description);
+ super(messages.xmlHtmlValidatorName(), messages.xmlHtmlValidatorDescription(), messages);
}
// private final static String tagRegex = "<[^>]+>[^<]*</[^>]+>";
@@ -52,16 +52,17 @@ public void validate(String source, String target)
{
if (!ValidationUtils.isEmpty(target))
{
- String error = runValidation(source, target);
- if (error.length() > 0)
+ List<String> error = runValidation(source, target);
+ if (!error.isEmpty())
{
- addError("Tag [" + error + "] missing in target");
+
+ addError(getMessages().tagsMissing(error));
}
error = runValidation(target, source);
- if (error.length() > 0)
+ if (!error.isEmpty())
{
- addError("Tag [" + error + "] missing in source");
+ addError(getMessages().tagsAdded(error));
}
if (getError().isEmpty())
@@ -73,25 +74,25 @@ public void validate(String source, String target)
private void orderValidation(String source, String target)
{
+ // TODO improve for cases such as first node moved to end and last node
+ // moved to start. Currently reports every node in these cases, should
+ // only report the one moved node.
List<String> from = getTagList(source);
List<String> to = getTagList(target);
- StringBuilder sb = new StringBuilder();
+ List<String> outOfOrder = new ArrayList<String>();
for (int i = 0; i < from.size(); i++)
{
if (!to.get(i).equals(from.get(i)))
{
- sb.append(" ");
- sb.append(from.get(i));
- sb.append(" ");
+ outOfOrder.add(from.get(i));
}
}
- if (sb.length() > 0)
+ if (!outOfOrder.isEmpty())
{
- addError("Tag [" + sb.toString() + "] are wrong in order");
+ addError(getMessages().tagsWrongOrder(outOfOrder));
}
-
}
private List<String> getTagList(String src)
@@ -107,10 +108,10 @@ private void orderValidation(String source, String target)
return list;
}
- private String runValidation(String compareFrom, String compareTo)
+ private List<String> runValidation(String compareFrom, String compareTo)
{
String tmp = compareTo;
- StringBuilder sb = new StringBuilder();
+ List<String> unmatched = new ArrayList<String>();
MatchResult result = regExp.exec(compareFrom);
while (result != null)
@@ -119,16 +120,14 @@ private String runValidation(String compareFrom, String compareTo)
Log.debug("Found Node:" + node);
if (!tmp.contains(node))
{
- sb.append(" ");
- sb.append(node);
- sb.append(" ");
+ unmatched.add(node);
}
else
{
tmp = tmp.replaceFirst(node, ""); // remove matched node from
}
result = regExp.exec(compareFrom);
}
- return sb.toString();
+ return unmatched;
}
}
View
71 ...rc/main/java/org/zanata/webtrans/shared/validation/action/NewlineLeadTrailValidation.java
@@ -20,10 +20,10 @@
*/
package org.zanata.webtrans.shared.validation.action;
+import org.zanata.webtrans.client.resources.ValidationMessages;
import org.zanata.webtrans.shared.validation.ValidationUtils;
import com.allen_sauer.gwt.log.client.Log;
-import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;
/**
@@ -33,9 +33,9 @@
**/
public class NewlineLeadTrailValidation extends ValidationAction
{
- public NewlineLeadTrailValidation(String id, String description)
+ public NewlineLeadTrailValidation(final ValidationMessages messages)
{
- super(id, description);
+ super(messages.newlineValidatorName(), messages.newlineValidatorDescription(), messages);
}
private final static String leadNewlineRegex = "^\n";
@@ -49,50 +49,47 @@ public void validate(String source, String target)
{
if (!ValidationUtils.isEmpty(target))
{
- String error = runValidation(source, target);
- if (error.length() > 0)
- {
- addError(error + " newline missing in target");
- }
- error = runValidation(target, source);
- if (error.length() > 0)
- {
- addError(error + " newline missing in source");
- }
+ if (!shareLeading(source, target))
+ addError(getMessages().leadingNewlineMissing());
+
+ if (!shareLeading(target, source))
+ addError(getMessages().leadingNewlineAdded());
+
+ if (!shareTrailing(source, target))
+ addError(getMessages().trailingNewlineMissing());
+
+ if (!shareTrailing(target, source))
+ addError(getMessages().trailingNewlineAdded());
}
}
- private String runValidation(String compareFrom, String compareTo)
+ /**
+ * @return false if base has a leading newline and test does not, true
+ * otherwise
+ */
+ private boolean shareLeading(String base, String test)
{
- MatchResult sourceResult = leadRegExp.exec(compareFrom);
- StringBuilder sb = new StringBuilder();
- if (sourceResult != null)
+ if (leadRegExp.test(base))
{
Log.debug("Found leading newline");
- MatchResult targetResult = leadRegExp.exec(compareTo);
- if (targetResult == null)
- {
- sb.append("Leading");
- }
+ return leadRegExp.test(test);
}
+ // no newline so can't fail
+ return true;
+ }
- sourceResult = endRegExp.exec(compareFrom);
- if (sourceResult != null)
+ /**
+ * @return false if base has a trailing newline and test does not, true
+ * otherwise
+ */
+ private boolean shareTrailing(String base, String test)
+ {
+ if (endRegExp.test(base))
{
Log.debug("Found trailing newline");
- MatchResult targetResult = endRegExp.exec(compareTo);
- if (targetResult == null)
- {
- if (sb.length() > 0)
- {
- sb.append("/Trailing");
- }
- else
- {
- sb.append("Trailing");
- }
- }
+ return endRegExp.test(test);
}
- return sb.toString();
+ // no newline so can't fail
+ return true;
}
}
View
11 ...nata-war/src/main/java/org/zanata/webtrans/shared/validation/action/ValidationAction.java
@@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.List;
+import org.zanata.webtrans.client.resources.ValidationMessages;
import org.zanata.webtrans.shared.validation.ValidationObject;
/**
@@ -37,14 +38,22 @@
private final String description;
+ private ValidationMessages messages;
+
+ protected ValidationMessages getMessages()
+ {
+ return messages;
+ }
+
private List<String> errorList = new ArrayList<String>();
public abstract void validate(String source, String target);
- public ValidationAction(String id, String description)
+ public ValidationAction(String id, String description, final ValidationMessages messages)
{
this.id = id;
this.description = description;
+ this.messages = messages;
}
@Override
View
68 ...a-war/src/main/java/org/zanata/webtrans/shared/validation/action/VariablesValidation.java
@@ -20,9 +20,12 @@
*/
package org.zanata.webtrans.shared.validation.action;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.zanata.webtrans.client.resources.ValidationMessages;
import org.zanata.webtrans.shared.validation.ValidationUtils;
-import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;
@@ -33,56 +36,61 @@
**/
public class VariablesValidation extends ValidationAction
{
- public VariablesValidation(String id, String description)
+ public VariablesValidation(final ValidationMessages messages)
{
- super(id, description);
+ super(messages.variablesValidatorName(), messages.variablesValidatorDescription(), messages);
}
- private final static String varRegex = "%[\\w]+";
- private RegExp varRegExp = RegExp.compile(varRegex, "g");
+ // private final static String varRegex = "%[\\w]+";
+ // private final static String varRegex = "%[^\\s,$]+(?![^\\s,$])";
+ private final static String varRegex = "%[^\\s,$]*";
@Override
public void validate(String source, String target)
{
if (!ValidationUtils.isEmpty(target))
{
- String error = runValidation(source, target);
- if (error.length() > 0)
+ ArrayList<String> sourceVars = findVars(source);
+ ArrayList<String> targetVars = findVars(target);
+
+ List<String> missing = listMissing(sourceVars, targetVars);
+ if (!missing.isEmpty())
{
- addError("Variable [" + error + "] not found in target");
+ addError(getMessages().varsMissing(missing));
}
- error = runValidation(target, source);
- if (error.length() > 0)
+ // missing from source = added
+ missing = listMissing(targetVars, sourceVars);
+ if (!missing.isEmpty())
{
- addError("Variable [" + error + "] not found in source");
+ addError(getMessages().varsAdded(missing));
}
}
}
- private String runValidation(String compareFrom, String compareTo)
+ private List<String> listMissing(ArrayList<String> baseVars, ArrayList<String> testVars)
{
- String tmp = compareTo;
- StringBuilder sb = new StringBuilder();
- MatchResult result = varRegExp.exec(compareFrom);
+ ArrayList<String> remainingVars = new ArrayList<String>();
+ remainingVars.addAll(testVars);
+ ArrayList<String> unmatched = new ArrayList<String>();
+ for (String var : baseVars)
+ if (!remainingVars.remove(var))
+ unmatched.add(var);
+ return unmatched;
+ }
+
+ private ArrayList<String> findVars(String inString)
+ {
+ ArrayList<String> vars = new ArrayList<String>();
+ // compile each time to reset index
+ RegExp varRegExp = RegExp.compile(varRegex, "g");
+ MatchResult result = varRegExp.exec(inString);
while (result != null)
{
- String var = result.getGroup(0);
- Log.debug("Found var:" + var);
- if (!tmp.contains(var))
- {
- sb.append(" ");
- sb.append(var);
- sb.append(" ");
- }
- else
- {
- tmp = tmp.replaceFirst(var, ""); // remove matched var
- }
- result = varRegExp.exec(compareFrom);
+ vars.add(result.getGroup(0));
+ result = varRegExp.exec(inString);
}
-
- return sb.toString();
+ return vars;
}
}
View
196 ...ta-war/src/test/java/org/zanata/webtrans/shared/validation/HtmlXmlTagValidationTests.java
@@ -20,121 +20,205 @@
*/
package org.zanata.webtrans.shared.validation;
-import org.junit.Before;
-import org.testng.Assert;
+import static org.easymock.EasyMock.capture;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.anyOf;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasItems;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+
+import java.util.List;
+
+import org.easymock.Capture;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import org.zanata.webtrans.client.resources.ValidationMessages;
import org.zanata.webtrans.shared.validation.action.HtmlXmlTagValidation;
/**
*
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
**/
+@Test(groups = { "unit-tests" })
public class HtmlXmlTagValidationTests
{
+ // mock message strings
+ private static final String MOCK_XML_HTML_VALIDATOR_DESCRIPTION = "test xml html validator description";
+ private static final String MOCK_XML_HTML_VALIDATOR_NAME = "test xml html validator name";
+ private static final String MOCK_TAGS_OUT_OF_ORDER_MESSAGE = "mock tags out of order message";
+ private static final String MOCK_TAGS_MISSING_MESSAGE = "mock tags missing message";
+ private static final String MOCK_TAGS_ADDED_MESSAGE = "mock tags added message";
+
private HtmlXmlTagValidation htmlXmlTagValidation;
- @Before
+ private ValidationMessages mockMessages;
+
+ // captured tag lists sent to messages
+ private Capture<List<String>> capturedTagsAdded;
+ private Capture<List<String>> capturedTagsMissing;
+ private Capture<List<String>> capturedTagsOutOfOrder;
+
+
+ @BeforeClass
+ public void mockMessages()
+ {
+ mockMessages = createMock(ValidationMessages.class);
+
+ capturedTagsAdded = new Capture<List<String>>();
+ capturedTagsMissing = new Capture<List<String>>();
+ capturedTagsOutOfOrder = new Capture<List<String>>();
+
+ expect(mockMessages.tagsAdded(capture(capturedTagsAdded))).andReturn(MOCK_TAGS_ADDED_MESSAGE).anyTimes();
+ expect(mockMessages.tagsMissing(capture(capturedTagsMissing))).andReturn(MOCK_TAGS_MISSING_MESSAGE).anyTimes();
+ expect(mockMessages.tagsWrongOrder(capture(capturedTagsOutOfOrder))).andReturn(MOCK_TAGS_OUT_OF_ORDER_MESSAGE).anyTimes();
+ expect(mockMessages.xmlHtmlValidatorName()).andReturn(MOCK_XML_HTML_VALIDATOR_NAME).anyTimes();
+ expect(mockMessages.xmlHtmlValidatorDescription()).andReturn(MOCK_XML_HTML_VALIDATOR_DESCRIPTION).anyTimes();
+ replay(mockMessages);
+ }
+
+ @BeforeMethod
public void init()
{
htmlXmlTagValidation = null;
+
+ capturedTagsAdded.reset();
+ capturedTagsMissing.reset();
+ capturedTagsOutOfOrder.reset();
}
@Test
- public void HTMLTagTestTagMissing()
+ public void idIsSet()
{
- htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
-
- String source = "<html><title>HTML TAG Test</title><table><tr><td>column 1 row 1</td><td>column 2 row 1</td></tr></table></html>";
- String target = "<html><title>HTML TAG Test</title><table><tr><td>column 1 row 1</td></tr></table></html>";
- htmlXmlTagValidation.validate(source, target);
- Assert.assertTrue(htmlXmlTagValidation.hasError());
- Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
+ htmlXmlTagValidation = new HtmlXmlTagValidation(mockMessages);
+ assertThat(htmlXmlTagValidation.getId(), is(MOCK_XML_HTML_VALIDATOR_NAME));
}
@Test
- public void HTMLTagTestTagMissing2()
+ public void descriptionIsSet()
{
- htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
-
- String source = "<html><title>HTML TAG Test</title><table><tr><td>column 1 row 1</td><td>column 2 row 1</td></tr></table></html>";
- String target = "<html><title>HTML TAG Test</title><table><tr></tr></table></html>";
- htmlXmlTagValidation.validate(source, target);
- Assert.assertTrue(htmlXmlTagValidation.hasError());
- Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
+ htmlXmlTagValidation = new HtmlXmlTagValidation(mockMessages);
+ assertThat(htmlXmlTagValidation.getDescription(), is(MOCK_XML_HTML_VALIDATOR_DESCRIPTION));
}
@Test
- public void HTMLTagTestMatching()
+ public void matchingHtmlNoError()
{
- htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
-
+ htmlXmlTagValidation = new HtmlXmlTagValidation(mockMessages);
String source = "<html><title>HTML TAG Test</title><table><tr><td>column 1 row 1</td><td>column 2 row 1</td></tr></table></html>";
String target = "<html><title>HTML TAG Test</title><table><tr><td>column 1 row 1</td><td>column 2 row 1</td></tr></table></html>";
htmlXmlTagValidation.validate(source, target);
- Assert.assertFalse(htmlXmlTagValidation.hasError());
- Assert.assertEquals(htmlXmlTagValidation.getError().size(), 0);
+
+ assertThat(htmlXmlTagValidation.hasError(), is(false));
+ assertThat(htmlXmlTagValidation.getError().size(), is(0));
}
@Test
- public void XMLTagTestTagMissing()
+ public void matchingXmlNoError()
{
- htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
-
- String source = "<group><users><user>1</user><user>2</user></users></group>";
- String target = "<group><users></users></group>";
+ htmlXmlTagValidation = new HtmlXmlTagValidation(mockMessages);
+ String source = "<group><users><user>name</user></users></group>";
+ String target = "<group><users><user>nombre</user></users></group>";
htmlXmlTagValidation.validate(source, target);
- Assert.assertTrue(htmlXmlTagValidation.hasError());
- Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
+
+ assertThat(htmlXmlTagValidation.hasError(), is(false));
+ assertThat(htmlXmlTagValidation.getError().size(), is(0));
}
@Test
- public void XMLTagTestTagMissing2()
+ public void addedTagError()
{
- htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
-
+ htmlXmlTagValidation = new HtmlXmlTagValidation(mockMessages);
String source = "<group><users><user>1</user></users></group>";
- String target = "<group><users></users></group>";
+ String target = "<group><users><user>1</user></users><foo></group>";
htmlXmlTagValidation.validate(source, target);
- Assert.assertTrue(htmlXmlTagValidation.hasError());
- Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
+
+ assertThat(htmlXmlTagValidation.hasError(), is(true));
+ assertThat(htmlXmlTagValidation.getError(), hasItem(MOCK_TAGS_ADDED_MESSAGE));
+ assertThat(htmlXmlTagValidation.getError().size(), is(1));
+
+ assertThat(capturedTagsAdded.getValue(), hasItem("<foo>"));
+ assertThat(capturedTagsAdded.getValue().size(), is(1));
}
@Test
- public void XMLTagTestTagMissing3()
+ public void addedTagsError()
{
- htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
-
+ htmlXmlTagValidation = new HtmlXmlTagValidation(mockMessages);
String source = "<group><users><user>1</user></users></group>";
- String target = "<group><users><user>1</user></users><users></users></group>";
+ String target = "<foo><group><users><bar><user>1</user></users></group><moo>";
htmlXmlTagValidation.validate(source, target);
- Assert.assertTrue(htmlXmlTagValidation.hasError());
- Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
+
+ assertThat(htmlXmlTagValidation.hasError(), is(true));
+ assertThat(htmlXmlTagValidation.getError(), hasItem(MOCK_TAGS_ADDED_MESSAGE));
+ assertThat(htmlXmlTagValidation.getError().size(), is(1));
+
+ assertThat(capturedTagsAdded.getValue(), hasItems("<foo>", "<bar>", "<moo>"));
+ assertThat(capturedTagsAdded.getValue().size(), is(3));
}
@Test
- public void XMLTagTestTagWrongOrder()
+ public void missingTagError()
{
- htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
-
- String source = "<group><users><user></user></users><users></users></group>";
- String target = "<group><users></users><users></user><user></users></group>";
+ htmlXmlTagValidation = new HtmlXmlTagValidation(mockMessages);
+ String source = "<html><title>HTML TAG Test</title><foo><table><tr><td>column 1 row 1</td><td>column 2 row 1</td></tr></table></html>";
+ String target = "<html><title>HTML TAG Test</title><table><tr><td>column 1 row 1</td><td>column 2 row 1</td></tr></table></html>";
htmlXmlTagValidation.validate(source, target);
- Assert.assertTrue(htmlXmlTagValidation.hasError());
- Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
+
+ assertThat(htmlXmlTagValidation.hasError(), is(true));
+ assertThat(htmlXmlTagValidation.getError(), hasItem(MOCK_TAGS_MISSING_MESSAGE));
+ assertThat(htmlXmlTagValidation.getError().size(), is(1));
+
+ assertThat(capturedTagsMissing.getValue(), hasItem("<foo>"));
+ assertThat(capturedTagsMissing.getValue().size(), is(1));
}
@Test
- public void XMLTagTestMatching()
+ public void missingTagsError()
{
- htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
+ htmlXmlTagValidation = new HtmlXmlTagValidation(mockMessages);
+ String source = "<html><title>HTML TAG Test</title><p><table><tr><td>column 1 row 1</td></tr></table></html>";
+ String target = "<title>HTML TAG Test</title><table><tr><td>column 1 row 1</td></tr></table>";
+ htmlXmlTagValidation.validate(source, target);
- String source = "<group><users><user>1</user></users></group>";
- String target = "<group><users><user>1</user></users></group>";
+ assertThat(htmlXmlTagValidation.hasError(), is(true));
+ assertThat(htmlXmlTagValidation.getError(), hasItem(MOCK_TAGS_MISSING_MESSAGE));
+ assertThat(htmlXmlTagValidation.getError().size(), is(1));
+
+ assertThat(capturedTagsMissing.getValue(), hasItems("<html>", "<p>", "</html>"));
+ assertThat(capturedTagsMissing.getValue().size(), is(3));
+ }
+
+ // TODO update algorithm, this test will fail so will need updating
+ @SuppressWarnings("unchecked")
+ @Test
+ public void wrongOrderTagError()
+ {
+ htmlXmlTagValidation = new HtmlXmlTagValidation(mockMessages);
+ String source = "<one><two><three></three></two><four></four></one>";
+ String target = "<one><two></two><four></three><three></four></one>";
htmlXmlTagValidation.validate(source, target);
- Assert.assertFalse(htmlXmlTagValidation.hasError());
- Assert.assertEquals(htmlXmlTagValidation.getError().size(), 0);
+
+ assertThat(htmlXmlTagValidation.hasError(), is(true));
+ assertThat(htmlXmlTagValidation.getError(), hasItem(MOCK_TAGS_OUT_OF_ORDER_MESSAGE));
+ assertThat(htmlXmlTagValidation.getError().size(), is(1));
+
+ // Note: only <three> and </three> have been moved in this string, but the
+ // algorithm just looks for changed index at the moment.
+ assertThat(capturedTagsOutOfOrder.getValue(), hasItems("<three>", "</three>", "</two>", "<four>"));
+ assertThat(capturedTagsOutOfOrder.getValue(), not(anyOf(hasItem("<one>"), hasItem("<two>"), hasItem("</four>"), hasItem("</one>"))));
+ assertThat(capturedTagsOutOfOrder.getValue().size(), is(4));
}
+
+ // TODO test cases for moving one tag a long way (when the algorithm
+ // handles that better). e.g.
+ // String source = "<one><two><three></three></two><four></four></one>";
+ // String target = "</one><one><two><three></three></two><four></four>";
}
View
198 .../src/test/java/org/zanata/webtrans/shared/validation/NewlineLeadTrailValidationTests.java
@@ -20,84 +20,216 @@
*/
package org.zanata.webtrans.shared.validation;
-import org.junit.Before;
-import org.testng.Assert;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasItems;
+import static org.hamcrest.Matchers.is;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import org.zanata.webtrans.client.resources.ValidationMessages;
import org.zanata.webtrans.shared.validation.action.NewlineLeadTrailValidation;
/**
*
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
**/
+@Test(groups = { "unit-tests" })
public class NewlineLeadTrailValidationTests
{
+ // mock message strings
+ private static final String MOCK_NEWLINE_VALIDATOR_NAME = "test newline validator name";
+ private static final String MOCK_NEWLINE_VALIDATOR_DESCRIPTION = "test xml html validator description";
+
+ private static final String MOCK_TRAILING_NEWLINE_MISSING_MESSAGE = "mock trailing newline missing message";
+ private static final String MOCK_TRAILING_NEWLINE_ADDED_MESSAGE = "mock trailing newline added message";
+ private static final String MOCK_LEADING_NEWLINE_MISSING_MESSAGE = "mock leading newline missing message";
+ private static final String MOCK_LEADING_NEWLINE_ADDED_MESSAGE = "mock leading newline added message";
+
+
private NewlineLeadTrailValidation newlineLeadTrailValidation;
- @Before
+ private ValidationMessages mockMessages;
+
+ @BeforeClass
+ public void mockMessages()
+ {
+ mockMessages = createMock(ValidationMessages.class);
+
+ expect(mockMessages.newlineValidatorName()).andReturn(MOCK_NEWLINE_VALIDATOR_NAME).anyTimes();
+ expect(mockMessages.newlineValidatorDescription()).andReturn(MOCK_NEWLINE_VALIDATOR_DESCRIPTION).anyTimes();
+
+ expect(mockMessages.leadingNewlineAdded()).andReturn(MOCK_LEADING_NEWLINE_ADDED_MESSAGE).anyTimes();
+ expect(mockMessages.leadingNewlineMissing()).andReturn(MOCK_LEADING_NEWLINE_MISSING_MESSAGE).anyTimes();
+ expect(mockMessages.trailingNewlineAdded()).andReturn(MOCK_TRAILING_NEWLINE_ADDED_MESSAGE).anyTimes();
+ expect(mockMessages.trailingNewlineMissing()).andReturn(MOCK_TRAILING_NEWLINE_MISSING_MESSAGE).anyTimes();
+
+ replay(mockMessages);
+ }
+
+ @BeforeMethod
public void init()
{
newlineLeadTrailValidation = null;
}
@Test
- public void NewlineLeadTrailTestWithMissingLead()
+ public void idIsSet()
{
- newlineLeadTrailValidation = new NewlineLeadTrailValidation("Newline lead/trail", "Newline lead/trail validation");
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ assertThat(newlineLeadTrailValidation.getId(), is(MOCK_NEWLINE_VALIDATOR_NAME));
+ }
- String source = "\nTesting string with leading new line";
- String target = "Testing string with leading new line\n";
+ @Test
+ public void descriptionIsSet()
+ {
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ assertThat(newlineLeadTrailValidation.getDescription(), is(MOCK_NEWLINE_VALIDATOR_DESCRIPTION));
+ }
+
+ @Test
+ public void noNewlinesBothMatch()
+ {
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "String without newlines";
+ String target = "Different newline-devoid string";
+ newlineLeadTrailValidation.validate(source, target);
+
+ assertThat(newlineLeadTrailValidation.hasError(), is(false));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(0));
+ }
+
+ @Test
+ public void bothNewlinesBothMatch()
+ {
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "\nString with both newlines\n";
+ String target = "\nDifferent newline-infested string\n";
+ newlineLeadTrailValidation.validate(source, target);
+
+ assertThat(newlineLeadTrailValidation.hasError(), is(false));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(0));
+ }
+
+ @Test
+ public void internalNewlinesDontCount()
+ {
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "String with an \n internal newline.";
+ String target = "Different string lacking the newline";
newlineLeadTrailValidation.validate(source, target);
- Assert.assertTrue(newlineLeadTrailValidation.hasError());
- Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 2);
+
+ assertThat(newlineLeadTrailValidation.hasError(), is(false));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(0));
}
@Test
- public void NewlineLeadTrailTestWithMissingTrail()
+ public void missingLeadingNewline()
{
- newlineLeadTrailValidation = new NewlineLeadTrailValidation("Newline lead/trail", "Newline lead/trail validation");
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "\nTesting string with leading new line";
+ String target = "Different string with the newline removed";
+ newlineLeadTrailValidation.validate(source, target);
+
+ assertThat(newlineLeadTrailValidation.hasError(), is(true));
+ assertThat(newlineLeadTrailValidation.getError(), hasItem(MOCK_LEADING_NEWLINE_MISSING_MESSAGE));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(1));
+ }
- String source = "Testing string with leading new line\n";
- String target = "\nTesting string with leading new line";
+ @Test
+ public void addedLeadingNewline()
+ {
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "Testing string without a leading new line";
+ String target = "\nDifferent string with a leading newline added";
newlineLeadTrailValidation.validate(source, target);
- Assert.assertTrue(newlineLeadTrailValidation.hasError());
- Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 2);
+
+ assertThat(newlineLeadTrailValidation.hasError(), is(true));
+ assertThat(newlineLeadTrailValidation.getError(), hasItem(MOCK_LEADING_NEWLINE_ADDED_MESSAGE));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(1));
}
@Test
- public void NewlineLeadTrailTestWithMissingBoth()
+ public void missingTrailingNewline()
{
- newlineLeadTrailValidation = new NewlineLeadTrailValidation("Newline lead/trail", "Newline lead/trail validation");
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "Testing string with trailing new line\n";
+ String target = "Different string with the newline removed";
+ newlineLeadTrailValidation.validate(source, target);
- String source = "\nTesting string with leading new line\n";
- String target = "Testing string with leading new line";
+ assertThat(newlineLeadTrailValidation.hasError(), is(true));
+ assertThat(newlineLeadTrailValidation.getError(), hasItem(MOCK_TRAILING_NEWLINE_MISSING_MESSAGE));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(1));
+ }
+
+ @Test
+ public void addedTrailingNewline()
+ {
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "Testing string without a trailing new line";
+ String target = "Different string with a trailing newline added\n";
newlineLeadTrailValidation.validate(source, target);
- Assert.assertTrue(newlineLeadTrailValidation.hasError());
- Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 1);
+
+ assertThat(newlineLeadTrailValidation.hasError(), is(true));
+ assertThat(newlineLeadTrailValidation.getError(), hasItem(MOCK_TRAILING_NEWLINE_ADDED_MESSAGE));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(1));
}
@Test
- public void NewlineLeadTrailTestWithMissingBoth2()
+ public void addedBothNewlines()
{
- newlineLeadTrailValidation = new NewlineLeadTrailValidation("Newline lead/trail", "Newline lead/trail validation");
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "Testing string with no newlines";
+ String target = "\nDifferent string with both added\n";
+ newlineLeadTrailValidation.validate(source, target);
- String source = "\nTesting string with leading new line";
- String target = "\nTesting string with leading new line\n";
+ assertThat(newlineLeadTrailValidation.hasError(), is(true));
+ assertThat(newlineLeadTrailValidation.getError(), hasItems(MOCK_TRAILING_NEWLINE_ADDED_MESSAGE, MOCK_TRAILING_NEWLINE_ADDED_MESSAGE));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(2));
+ }
+
+ @Test
+ public void missingBothNewlines()
+ {
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "\nString with both newlines\n";
+ String target = "Other string with no newlines";
newlineLeadTrailValidation.validate(source, target);
- Assert.assertTrue(newlineLeadTrailValidation.hasError());
- Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 1);
+
+ assertThat(newlineLeadTrailValidation.hasError(), is(true));
+ assertThat(newlineLeadTrailValidation.getError(), hasItems(MOCK_TRAILING_NEWLINE_MISSING_MESSAGE, MOCK_TRAILING_NEWLINE_MISSING_MESSAGE));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(2));
}
@Test
- public void NewlineLeadTrailTestMatch()
+ public void addedAndMissing1()
{
- newlineLeadTrailValidation = new NewlineLeadTrailValidation("Newline lead/trail", "Newline lead/trail validation");
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "\nString with only leading newline";
+ String target = "Other string with newline trailing\n";
+ newlineLeadTrailValidation.validate(source, target);
+
+ assertThat(newlineLeadTrailValidation.hasError(), is(true));
+ assertThat(newlineLeadTrailValidation.getError(), hasItems(MOCK_LEADING_NEWLINE_MISSING_MESSAGE, MOCK_TRAILING_NEWLINE_ADDED_MESSAGE));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(2));
+ }
- String source = "\nTesting string with leading new line\n";
- String target = "\nTesting string with leading new line\n";
+ @Test
+ public void addedAndMissing2()
+ {
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation(mockMessages);
+ String source = "String with trailing newline\n";
+ String target = "\nOther string with newline leading";
newlineLeadTrailValidation.validate(source, target);
- Assert.assertFalse(newlineLeadTrailValidation.hasError());
- Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 0);
+
+ assertThat(newlineLeadTrailValidation.hasError(), is(true));
+ assertThat(newlineLeadTrailValidation.getError(), hasItems(MOCK_LEADING_NEWLINE_ADDED_MESSAGE, MOCK_TRAILING_NEWLINE_MISSING_MESSAGE));
+ assertThat(newlineLeadTrailValidation.getError().size(), is(2));
}
}
View
215 ...ata-war/src/test/java/org/zanata/webtrans/shared/validation/VariablesValidationTests.java
@@ -20,85 +20,236 @@
*/
package org.zanata.webtrans.shared.validation;
-import org.junit.Before;
-import org.testng.Assert;
+import static org.easymock.EasyMock.capture;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasItems;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+
+import java.util.List;
+
+import org.easymock.Capture;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
-import org.zanata.webtrans.shared.validation.action.NewlineLeadTrailValidation;
+import org.zanata.webtrans.client.resources.ValidationMessages;
import org.zanata.webtrans.shared.validation.action.VariablesValidation;
/**
*
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
**/
+@Test(groups = { "unit-tests" })
public class VariablesValidationTests
{
+ private static final String MOCK_VARIABLES_VALIDATOR_NAME = "test variable validator name";
+ private static final String MOCK_VARIABLES_VALIDATOR_DESCRIPTION = "test variable validator description";
+ private static final String MOCK_VARIABLES_ADDED_MESSAGE = "test variables added message";
+ private static final String MOCK_VARIABLES_MISSING_MESSAGE = "test variables missing message";
+
private VariablesValidation variablesValidation;
- @Before
+ private ValidationMessages mockMessages;
+ private Capture<List<String>> capturedVarsAdded;
+ private Capture<List<String>> capturedVarsMissing;
+
+ @BeforeClass
+ public void mockMessages()
+ {
+ mockMessages = createMock(ValidationMessages.class);
+
+ capturedVarsAdded = new Capture<List<String>>();
+ capturedVarsMissing = new Capture<List<String>>();
+
+ expect(mockMessages.varsAdded(capture(capturedVarsAdded))).andReturn(MOCK_VARIABLES_ADDED_MESSAGE).anyTimes();
+ expect(mockMessages.varsMissing(capture(capturedVarsMissing))).andReturn(MOCK_VARIABLES_MISSING_MESSAGE).anyTimes();
+ expect(mockMessages.variablesValidatorName()).andReturn(MOCK_VARIABLES_VALIDATOR_NAME).anyTimes();
+ expect(mockMessages.variablesValidatorDescription()).andReturn(MOCK_VARIABLES_VALIDATOR_DESCRIPTION).anyTimes();
+ replay(mockMessages);
+ }
+
+ @BeforeMethod
public void init()
{
variablesValidation = null;
+
+ capturedVarsAdded.reset();
+ capturedVarsMissing.reset();
+ }
+
+ @Test
+ public void idIsSet()
+ {
+ variablesValidation = new VariablesValidation(mockMessages);
+ assertThat(variablesValidation.getId(), is(MOCK_VARIABLES_VALIDATOR_NAME));
+ }
+
+ @Test
+ public void descriptionIsSet()
+ {
+ variablesValidation = new VariablesValidation(mockMessages);
+ assertThat(variablesValidation.getDescription(), is(MOCK_VARIABLES_VALIDATOR_DESCRIPTION));
}
@Test
- public void VariablesTestWithMismatch()
+ public void noErrorForMatchingVars()
{
- variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
+ variablesValidation = new VariablesValidation(mockMessages);
+ String source = "Testing string with variable %var1 and %var2";
+ String target = "%var2 and %var1 included, order not relevant";
+ variablesValidation.validate(source, target);
+
+ assertThat(variablesValidation.hasError(), is(false));
+ assertThat(variablesValidation.getError().size(), is(0));
+
+ assertThat(capturedVarsAdded.hasCaptured(), is(false));
+ assertThat(capturedVarsMissing.hasCaptured(), is(false));
+ }
+ @Test
+ public void missingVarInTarget()
+ {
+ variablesValidation = new VariablesValidation(mockMessages);
String source = "Testing string with variable %var1";
String target = "Testing string with no variables";
variablesValidation.validate(source, target);
- Assert.assertTrue(variablesValidation.hasError());
- Assert.assertEquals(variablesValidation.getError().size(), 1);
+
+ assertThat(variablesValidation.hasError(), is(true));
+ assertThat(variablesValidation.getError(), hasItem(MOCK_VARIABLES_MISSING_MESSAGE));
+ assertThat(variablesValidation.getError().size(), is(1));
+
+ assertThat(capturedVarsMissing.getValue(), hasItem("%var1"));
+ assertThat(capturedVarsMissing.getValue().size(), is(1));
+ assertThat(capturedVarsAdded.hasCaptured(), is(false));
}
@Test
- public void VariablesTestWithMismatch2()
+ public void missingVarsThroughoutTarget()
{
- variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
-
- String source = "Testing string with variable %var1 and %var2";
+ variablesValidation = new VariablesValidation(mockMessages);
+ String source = "%var1 variables in all parts %var2 of the string %var3";
String target = "Testing string with no variables";
variablesValidation.validate(source, target);
- Assert.assertTrue(variablesValidation.hasError());
- Assert.assertEquals(variablesValidation.getError().size(), 1);
+
+ assertThat(variablesValidation.hasError(), is(true));
+ assertThat(variablesValidation.getError(), hasItem(MOCK_VARIABLES_MISSING_MESSAGE));
+ assertThat(variablesValidation.getError().size(), is(1));
+
+ assertThat(capturedVarsMissing.getValue(), hasItems("%var1", "%var2", "%var3"));
+ assertThat(capturedVarsMissing.getValue().size(), is(3));
+ assertThat(capturedVarsAdded.hasCaptured(), is(false));
}
@Test
- public void VariablesTestWithMismatch3()
+ public void addedVarInTarget()
{
- variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
+ variablesValidation = new VariablesValidation(mockMessages);
+ String source = "Testing string with no variables";
+ String target = "Testing string with variable %var1";
+ variablesValidation.validate(source, target);
- String source = "Testing string with variable %var1 and %var2 and %var3";
- String target = "Testing string with no variables";
+ assertThat(variablesValidation.hasError(), is(true));
+ assertThat(variablesValidation.getError(), hasItem(MOCK_VARIABLES_ADDED_MESSAGE));
+ assertThat(variablesValidation.getError().size(), is(1));
+
+ assertThat(capturedVarsAdded.getValue(), hasItem("%var1"));
+ assertThat(capturedVarsAdded.getValue().size(), is(1));
+ assertThat(capturedVarsMissing.hasCaptured(), is(false));
+ }
+
+ @Test
+ public void addedVarsThroughoutTarget()
+ {
+ variablesValidation = new VariablesValidation(mockMessages);
+ String source = "Testing string with no variables";
+ String target = "%var1 variables in all parts %var2 of the string %var3";
variablesValidation.validate(source, target);
- Assert.assertTrue(variablesValidation.hasError());
- Assert.assertEquals(variablesValidation.getError().size(), 1);
+
+ assertThat(variablesValidation.hasError(), is(true));
+ assertThat(variablesValidation.getError(), hasItem(MOCK_VARIABLES_ADDED_MESSAGE));
+ assertThat(variablesValidation.getError().size(), is(1));
+
+ assertThat(capturedVarsAdded.getValue(), hasItems("%var1", "%var2", "%var3"));
+ assertThat(capturedVarsAdded.getValue().size(), is(3));
+ assertThat(capturedVarsMissing.hasCaptured(), is(false));
}
@Test
- public void VariablesTestWithMismatch4()
+ public void bothAddedAndMissingVars()
{
- variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
+ variablesValidation = new VariablesValidation(mockMessages);
+ String source = "String with %var1 and %var2 only, not 3";
+ String target = "String with %var2 and %var3, not 1";
+ variablesValidation.validate(source, target);
+
+ assertThat(variablesValidation.hasError(), is(true));
+ assertThat(variablesValidation.getError(), hasItems(MOCK_VARIABLES_ADDED_MESSAGE, MOCK_VARIABLES_MISSING_MESSAGE));
+ assertThat(variablesValidation.getError().size(), is(2));
- String source = "Testing string with variable %var1 and %var2 and %var3";
- String target = "Testing string with variable %var1 and %var2 and %var3 and %var4";
+ assertThat(capturedVarsAdded.getValue(), hasItem("%var3"));
+ assertThat(capturedVarsAdded.getValue().size(), is(1));
+ assertThat(capturedVarsMissing.getValue(), hasItem("%var1"));
+ assertThat(capturedVarsMissing.getValue().size(), is(1));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void substringVariablesDontMatch()
+ {
+ variablesValidation = new VariablesValidation(mockMessages);
+ String source = "%testing";
+ String target = "%test %testing";
variablesValidation.validate(source, target);
- Assert.assertTrue(variablesValidation.hasError());
- Assert.assertEquals(variablesValidation.getError().size(), 1);
+
+ assertThat(variablesValidation.hasError(), is(true));
+ assertThat(variablesValidation.getError(), hasItem(MOCK_VARIABLES_ADDED_MESSAGE));
+ assertThat(variablesValidation.getError().size(), is(1));
+
+ assertThat(capturedVarsAdded.getValue(), allOf(hasItem("%test"), not(hasItem("%testing"))));
+ assertThat(capturedVarsAdded.getValue().size(), is(1));
+ assertThat(capturedVarsMissing.hasCaptured(), is(false));
}
+ @SuppressWarnings("unchecked")
@Test
- public void VariablesTestWithMatch()
+ public void superstringVariablesDontMatch()
{
- variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
+ variablesValidation = new VariablesValidation(mockMessages);
- String source = "Testing string with variable %var1 and %var2";
- String target = "Testing string with variable %var1 and %var2";
+ String source = "%what %whatever";
+ String target = "%whatever";
+ variablesValidation.validate(source, target);
+
+ assertThat(variablesValidation.hasError(), is(true));
+ assertThat(variablesValidation.getError(), hasItem(MOCK_VARIABLES_MISSING_MESSAGE));
+ assertThat(variablesValidation.getError().size(), is(1));
+
+ assertThat(capturedVarsMissing.getValue(), allOf(hasItem("%what"), not(hasItem("%whatever"))));
+ assertThat(capturedVarsAdded.hasCaptured(), is(false));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void superstringVariablesDontMatch2()
+ {
+ variablesValidation = new VariablesValidation(mockMessages);
+
+ String source = "%test";
+ String target = "%testing";
variablesValidation.validate(source, target);
- Assert.assertFalse(variablesValidation.hasError());
- Assert.assertEquals(variablesValidation.getError().size(), 0);
+
+ assertThat(variablesValidation.hasError(), is(true));
+ assertThat(variablesValidation.getError(), hasItems(MOCK_VARIABLES_MISSING_MESSAGE, MOCK_VARIABLES_ADDED_MESSAGE));
+ assertThat(variablesValidation.getError().size(), is(2));
+
+ assertThat(capturedVarsMissing.getValue(), allOf(hasItem("%test"), not(hasItem("%testing"))));
+ assertThat(capturedVarsAdded.getValue(), allOf(hasItem("%testing"), not(hasItem("%test"))));
}
}

0 comments on commit f8eaaa0

Please sign in to comment.