Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement validation to check from target to source/source to target

  • Loading branch information...
commit 8c5e2aee6e66704623c9a1dc18701786d600ff58 1 parent fa9a84d
@aeng aeng authored
View
49 server/zanata-war/src/main/java/org/zanata/webtrans/shared/validation/action/HtmlXmlTagValidation.java
@@ -48,31 +48,42 @@ public void validate(String source, String target)
{
if (!ValidationUtils.isEmpty(target))
{
- String tmp = target;
- StringBuilder sb = new StringBuilder();
- MatchResult result = regExp.exec(source);
+ String error = runValidation(source, target);
+ if (error.length() > 0)
+ {
+ addError("Tag [" + error + "] missing in target");
+ }
- while (result != null)
+ error = runValidation(target, source);
+ if (error.length() > 0)
{
- String node = result.getGroup(0);
- Log.debug("Found Node:" + node);
- if (!tmp.contains(node))
- {
- sb.append(" ");
- sb.append(node);
- sb.append(" ");
- }
- else
- {
- tmp = tmp.replaceFirst(node, ""); // remove matched node from
- }
- result = regExp.exec(source);
+ addError("Tag [" + error + "] missing in source");
}
+ }
+ }
- if (sb.length() > 0)
+ private String runValidation(String compareFrom, String compareTo)
+ {
+ String tmp = compareTo;
+ StringBuilder sb = new StringBuilder();
+ MatchResult result = regExp.exec(compareFrom);
+
+ while (result != null)
+ {
+ String node = result.getGroup(0);
+ Log.debug("Found Node:" + node);
+ if (!tmp.contains(node))
+ {
+ sb.append(" ");
+ sb.append(node);
+ sb.append(" ");
+ }
+ else
{
- addError("Tag [" + sb.toString() + "] missing in target");
+ tmp = tmp.replaceFirst(node, ""); // remove matched node from
}
+ result = regExp.exec(compareFrom);
}
+ return sb.toString();
}
}
View
62 ...er/zanata-war/src/main/java/org/zanata/webtrans/shared/validation/action/NewlineLeadTrailValidation.java
@@ -49,40 +49,50 @@ public void validate(String source, String target)
{
if (!ValidationUtils.isEmpty(target))
{
- MatchResult sourceResult = leadRegExp.exec(source);
- StringBuilder sb = new StringBuilder();
- if (sourceResult != null)
+ String error = runValidation(source, target);
+ if (error.length() > 0)
{
- Log.debug("Found leading newline in source");
- MatchResult targetResult = leadRegExp.exec(target);
- if (targetResult == null)
- {
- sb.append("Leading");
- }
+ addError(error + " newline missing in target");
+ }
+ error = runValidation(target, source);
+ if (error.length() > 0)
+ {
+ addError(error + " newline missing in source");
}
+ }
+ }
- sourceResult = endRegExp.exec(source);
- if (sourceResult != null)
+ private String runValidation(String compareFrom, String compareTo)
+ {
+ MatchResult sourceResult = leadRegExp.exec(compareFrom);
+ StringBuilder sb = new StringBuilder();
+ if (sourceResult != null)
+ {
+ Log.debug("Found leading newline");
+ MatchResult targetResult = leadRegExp.exec(compareTo);
+ if (targetResult == null)
{
- Log.debug("Found trailing newline in source");
- MatchResult targetResult = endRegExp.exec(target);
- if (targetResult == null)
- {
- if (sb.length() > 0)
- {
- sb.append("/Trailing");
- }
- else
- {
- sb.append("Trailing");
- }
- }
+ sb.append("Leading");
}
+ }
- if (sb.length() > 0)
+ sourceResult = endRegExp.exec(compareFrom);
+ if (sourceResult != null)
+ {
+ Log.debug("Found trailing newline");
+ MatchResult targetResult = endRegExp.exec(compareTo);
+ if (targetResult == null)
{
- addError(sb.toString() + " newline missing in target");
+ if (sb.length() > 0)
+ {
+ sb.append("/Trailing");
+ }
+ else
+ {
+ sb.append("Trailing");
+ }
}
}
+ return sb.toString();
}
}
View
52 server/zanata-war/src/main/java/org/zanata/webtrans/shared/validation/action/VariablesValidation.java
@@ -39,38 +39,50 @@ public VariablesValidation(String id, String description)
}
private final static String varRegex = "%[\\w]+";
- private final static RegExp varRegExp = RegExp.compile(varRegex, "g");
+ private RegExp varRegExp = RegExp.compile(varRegex, "g");
@Override
public void validate(String source, String target)
{
if (!ValidationUtils.isEmpty(target))
{
- String tmp = target;
- StringBuilder sb = new StringBuilder();
- MatchResult result = varRegExp.exec(source);
+ String error = runValidation(source, target);
+ if (error.length() > 0)
+ {
+ addError("Variable [" + error + "] not found in target");
+ }
- while (result != null)
+ error = runValidation(target, source);
+ if (error.length() > 0)
{
- 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(source);
+ addError("Variable [" + error + "] not found in source");
}
+ }
+ }
+
+ private String runValidation(String compareFrom, String compareTo)
+ {
+ String tmp = compareTo;
+ StringBuilder sb = new StringBuilder();
+ MatchResult result = varRegExp.exec(compareFrom);
- if (sb.length() > 0)
+ while (result != null)
+ {
+ String var = result.getGroup(0);
+ Log.debug("Found var:" + var);
+ if (!tmp.contains(var))
{
- addError("Variable [" + sb.toString() + "] missing in target");
+ sb.append(" ");
+ sb.append(var);
+ sb.append(" ");
}
+ else
+ {
+ tmp = tmp.replaceFirst(var, ""); // remove matched var
+ }
+ result = varRegExp.exec(compareFrom);
}
+
+ return sb.toString();
}
}
View
12 server/zanata-war/src/test/java/org/zanata/webtrans/shared/validation/HtmlXmlTagValidationTests.java
@@ -101,6 +101,18 @@ public void XMLTagTestTagMissing2()
}
@Test
+ public void XMLTagTestTagMissing3()
+ {
+ htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
+
+ String source = "<group><users><user>1</user></users></group>";
+ String target = "<group><users><user>1</user></users><users></users></group>";
+ htmlXmlTagValidation.validate(source, target);
+ Assert.assertTrue(htmlXmlTagValidation.hasError());
+ Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
+ }
+
+ @Test
public void XMLTagTestMatching()
{
htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
View
16 server/zanata-war/src/test/java/org/zanata/webtrans/shared/validation/NewlineLeadTrailValidationTests.java
@@ -49,7 +49,7 @@ public void NewlineLeadTrailTestWithMissingLead()
String target = "Testing string with leading new line\n";
newlineLeadTrailValidation.validate(source, target);
Assert.assertTrue(newlineLeadTrailValidation.hasError());
- Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 1);
+ Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 2);
}
@Test
@@ -61,7 +61,7 @@ public void NewlineLeadTrailTestWithMissingTrail()
String target = "\nTesting string with leading new line";
newlineLeadTrailValidation.validate(source, target);
Assert.assertTrue(newlineLeadTrailValidation.hasError());
- Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 1);
+ Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 2);
}
@Test
@@ -77,6 +77,18 @@ public void NewlineLeadTrailTestWithMissingBoth()
}
@Test
+ public void NewlineLeadTrailTestWithMissingBoth2()
+ {
+ newlineLeadTrailValidation = new NewlineLeadTrailValidation("Newline lead/trail", "Newline lead/trail validation");
+
+ String source = "\nTesting string with leading new line";
+ String target = "\nTesting string with leading new line\n";
+ newlineLeadTrailValidation.validate(source, target);
+ Assert.assertTrue(newlineLeadTrailValidation.hasError());
+ Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 1);
+ }
+
+ @Test
public void NewlineLeadTrailTestMatch()
{
newlineLeadTrailValidation = new NewlineLeadTrailValidation("Newline lead/trail", "Newline lead/trail validation");
View
12 server/zanata-war/src/test/java/org/zanata/webtrans/shared/validation/VariablesValidationTests.java
@@ -78,6 +78,18 @@ public void VariablesTestWithMismatch3()
}
@Test
+ public void VariablesTestWithMismatch4()
+ {
+ variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
+
+ String source = "Testing string with variable %var1 and %var2 and %var3";
+ String target = "Testing string with variable %var1 and %var2 and %var3 and %var4";
+ variablesValidation.validate(source, target);
+ Assert.assertTrue(variablesValidation.hasError());
+ Assert.assertEquals(variablesValidation.getError().size(), 1);
+ }
+
+ @Test
public void VariablesTestWithMatch()
{
variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
Please sign in to comment.
Something went wrong with that request. Please try again.