Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement variables validation checks

  • Loading branch information...
commit 942b44ce93e958e02afe03bd0e4b31cd95b91a1b 1 parent bfd538e
@aeng aeng authored
View
12 server/zanata-war/src/main/java/org/zanata/webtrans/shared/validation/action/HtmlXmlTagValidation.java
@@ -49,22 +49,30 @@ public void validate(String source, String target)
if (!ValidationUtils.isEmpty(target))
{
String tmp = target;
+ StringBuilder sb = new StringBuilder();
MatchResult result = regExp.exec(source);
+
while (result != null)
{
String node = result.getGroup(0);
Log.debug("Found Node:" + node);
if (!tmp.contains(node))
{
- addError("Tag " + node + " not found in target");
+ sb.append(" ");
+ sb.append(node);
+ sb.append(" ");
}
else
{
tmp = tmp.replaceFirst(node, ""); // remove matched node from
- // target
}
result = regExp.exec(source);
}
+
+ if (sb.length() > 0)
+ {
+ addError("Tag [" + sb.toString() + "] missing in target");
+ }
}
}
}
View
17 ...er/zanata-war/src/main/java/org/zanata/webtrans/shared/validation/action/NewlineLeadTrailValidation.java
@@ -50,13 +50,14 @@ public void validate(String source, String target)
if (!ValidationUtils.isEmpty(target))
{
MatchResult sourceResult = leadRegExp.exec(source);
+ StringBuilder sb = new StringBuilder();
if (sourceResult != null)
{
Log.debug("Found leading newline in source");
MatchResult targetResult = leadRegExp.exec(target);
if (targetResult == null)
{
- addError("Leading newline not found in target");
+ sb.append("Leading");
}
}
@@ -67,9 +68,21 @@ public void validate(String source, String target)
MatchResult targetResult = endRegExp.exec(target);
if (targetResult == null)
{
- addError("Trailing newline not found in target");
+ if (sb.length() > 0)
+ {
+ sb.append("/Trailing");
+ }
+ else
+ {
+ sb.append("Trailing");
+ }
}
}
+
+ if (sb.length() > 0)
+ {
+ addError(sb.toString() + " newline missing in target");
+ }
}
}
}
View
34 server/zanata-war/src/main/java/org/zanata/webtrans/shared/validation/action/VariablesValidation.java
@@ -23,6 +23,8 @@
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;
/**
*
@@ -36,22 +38,38 @@ public VariablesValidation(String id, String description)
super(id, description);
}
- private final String[] varList = { "%s", "%d" };
-
+ private final static String varRegex = "%[\\w]+";
+ private final static RegExp varRegExp = RegExp.compile(varRegex, "g");
+
@Override
public void validate(String source, String target)
{
if (!ValidationUtils.isEmpty(target))
{
- for (String var : varList)
+ String tmp = target;
+ StringBuilder sb = new StringBuilder();
+ MatchResult result = varRegExp.exec(source);
+
+ while (result != null)
{
- int srcCount = ValidationUtils.countMatches(source, var);
- int tgtCount = ValidationUtils.countMatches(target, var);
- Log.debug("Variable [" + var + "]: src-" + srcCount + " target-" + tgtCount);
- if (srcCount != tgtCount)
+ String var = result.getGroup(0);
+ Log.debug("Found var:" + var);
+ if (!tmp.contains(var))
+ {
+ sb.append(" ");
+ sb.append(var);
+ sb.append(" ");
+ }
+ else
{
- addError("Variable [" + var + "] count mismatch");
+ tmp = tmp.replaceFirst(var, ""); // remove matched var
}
+ result = varRegExp.exec(source);
+ }
+
+ if (sb.length() > 0)
+ {
+ addError("Variable [" + sb.toString() + "] missing in target");
}
}
}
View
8 server/zanata-war/src/test/java/org/zanata/webtrans/shared/validation/HtmlXmlTagValidationTests.java
@@ -49,7 +49,7 @@ public void HTMLTagTestTagMissing()
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(), 2);
+ Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
}
@Test
@@ -61,7 +61,7 @@ public void HTMLTagTestTagMissing2()
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(), 4);
+ Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
}
@Test
@@ -85,7 +85,7 @@ public void XMLTagTestTagMissing()
String target = "<group><users></users></group>";
htmlXmlTagValidation.validate(source, target);
Assert.assertTrue(htmlXmlTagValidation.hasError());
- Assert.assertEquals(htmlXmlTagValidation.getError().size(), 4);
+ Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
}
@Test
@@ -97,7 +97,7 @@ public void XMLTagTestTagMissing2()
String target = "<group><users></users></group>";
htmlXmlTagValidation.validate(source, target);
Assert.assertTrue(htmlXmlTagValidation.hasError());
- Assert.assertEquals(htmlXmlTagValidation.getError().size(), 2);
+ Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
}
@Test
View
2  server/zanata-war/src/test/java/org/zanata/webtrans/shared/validation/NewlineLeadTrailValidationTests.java
@@ -73,7 +73,7 @@ public void NewlineLeadTrailTestWithMissingBoth()
String target = "Testing string with leading new line";
newlineLeadTrailValidation.validate(source, target);
Assert.assertTrue(newlineLeadTrailValidation.hasError());
- Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 2);
+ Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 1);
}
@Test
View
14 server/zanata-war/src/test/java/org/zanata/webtrans/shared/validation/VariablesValidationTests.java
@@ -46,7 +46,7 @@ public void VariablesTestWithMismatch()
{
variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
- String source = "Testing string with variable %s";
+ String source = "Testing string with variable %var1";
String target = "Testing string with no variables";
variablesValidation.validate(source, target);
Assert.assertTrue(variablesValidation.hasError());
@@ -58,11 +58,11 @@ public void VariablesTestWithMismatch2()
{
variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
- String source = "Testing string with variable %s and %d";
+ String source = "Testing string with variable %var1 and %var2";
String target = "Testing string with no variables";
variablesValidation.validate(source, target);
Assert.assertTrue(variablesValidation.hasError());
- Assert.assertEquals(variablesValidation.getError().size(), 2);
+ Assert.assertEquals(variablesValidation.getError().size(), 1);
}
@Test
@@ -70,11 +70,11 @@ public void VariablesTestWithMismatch3()
{
variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
- String source = "Testing string with variable %s and %s and %d";
+ String source = "Testing string with variable %var1 and %var2 and %var3";
String target = "Testing string with no variables";
variablesValidation.validate(source, target);
Assert.assertTrue(variablesValidation.hasError());
- Assert.assertEquals(variablesValidation.getError().size(), 2);
+ Assert.assertEquals(variablesValidation.getError().size(), 1);
}
@Test
@@ -82,8 +82,8 @@ public void VariablesTestWithMatch()
{
variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
- String source = "Testing string with variable %s and %d";
- String target = "Testing string with variable %s and %d";
+ String source = "Testing string with variable %var1 and %var2";
+ String target = "Testing string with variable %var1 and %var2";
variablesValidation.validate(source, target);
Assert.assertFalse(variablesValidation.hasError());
Assert.assertEquals(variablesValidation.getError().size(), 0);
Please sign in to comment.
Something went wrong with that request. Please try again.