Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 8c5e2aee6e66704623c9a1dc18701786d600ff58 1 parent fa9a84d
Alex Eng authored January 06, 2012
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)
48 48
    {
49 49
       if (!ValidationUtils.isEmpty(target))
50 50
       {
51  
-         String tmp = target;
52  
-         StringBuilder sb = new StringBuilder();
53  
-         MatchResult result = regExp.exec(source);
  51
+         String error = runValidation(source, target);
  52
+         if (error.length() > 0)
  53
+         {
  54
+            addError("Tag [" + error + "] missing in target");
  55
+         }
54 56
 
55  
-         while (result != null)
  57
+         error = runValidation(target, source);
  58
+         if (error.length() > 0)
56 59
          {
57  
-            String node = result.getGroup(0);
58  
-            Log.debug("Found Node:" + node);
59  
-            if (!tmp.contains(node))
60  
-            {
61  
-               sb.append(" ");
62  
-               sb.append(node);
63  
-               sb.append(" ");
64  
-            }
65  
-            else
66  
-            {
67  
-               tmp = tmp.replaceFirst(node, ""); // remove matched node from
68  
-            }
69  
-            result = regExp.exec(source);
  60
+            addError("Tag [" + error + "] missing in source");
70 61
          }
  62
+      }
  63
+   }
71 64
 
72  
-         if (sb.length() > 0)
  65
+   private String runValidation(String compareFrom, String compareTo)
  66
+   {
  67
+      String tmp = compareTo;
  68
+      StringBuilder sb = new StringBuilder();
  69
+      MatchResult result = regExp.exec(compareFrom);
  70
+
  71
+      while (result != null)
  72
+      {
  73
+         String node = result.getGroup(0);
  74
+         Log.debug("Found Node:" + node);
  75
+         if (!tmp.contains(node))
  76
+         {
  77
+            sb.append(" ");
  78
+            sb.append(node);
  79
+            sb.append(" ");
  80
+         }
  81
+         else
73 82
          {
74  
-            addError("Tag [" + sb.toString() + "] missing in target");
  83
+            tmp = tmp.replaceFirst(node, ""); // remove matched node from
75 84
          }
  85
+         result = regExp.exec(compareFrom);
76 86
       }
  87
+      return sb.toString();
77 88
    }
78 89
 }
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)
49 49
    {
50 50
       if (!ValidationUtils.isEmpty(target))
51 51
       {
52  
-         MatchResult sourceResult = leadRegExp.exec(source);
53  
-         StringBuilder sb = new StringBuilder();
54  
-         if (sourceResult != null)
  52
+         String error = runValidation(source, target);
  53
+         if (error.length() > 0)
55 54
          {
56  
-            Log.debug("Found leading newline in source");
57  
-            MatchResult targetResult = leadRegExp.exec(target);
58  
-            if (targetResult == null)
59  
-            {
60  
-               sb.append("Leading");
61  
-            }
  55
+            addError(error + " newline missing in target");
  56
+         }
  57
+         error = runValidation(target, source);
  58
+         if (error.length() > 0)
  59
+         {
  60
+            addError(error + " newline missing in source");
62 61
          }
  62
+      }
  63
+   }
63 64
 
64  
-         sourceResult = endRegExp.exec(source);
65  
-         if (sourceResult != null)
  65
+   private String runValidation(String compareFrom, String compareTo)
  66
+   {
  67
+      MatchResult sourceResult = leadRegExp.exec(compareFrom);
  68
+      StringBuilder sb = new StringBuilder();
  69
+      if (sourceResult != null)
  70
+      {
  71
+         Log.debug("Found leading newline");
  72
+         MatchResult targetResult = leadRegExp.exec(compareTo);
  73
+         if (targetResult == null)
66 74
          {
67  
-            Log.debug("Found trailing newline in source");
68  
-            MatchResult targetResult = endRegExp.exec(target);
69  
-            if (targetResult == null)
70  
-            {
71  
-               if (sb.length() > 0)
72  
-               {
73  
-                  sb.append("/Trailing");
74  
-               }
75  
-               else
76  
-               {
77  
-                  sb.append("Trailing");
78  
-               }
79  
-            }
  75
+            sb.append("Leading");
80 76
          }
  77
+      }
81 78
 
82  
-         if (sb.length() > 0)
  79
+      sourceResult = endRegExp.exec(compareFrom);
  80
+      if (sourceResult != null)
  81
+      {
  82
+         Log.debug("Found trailing newline");
  83
+         MatchResult targetResult = endRegExp.exec(compareTo);
  84
+         if (targetResult == null)
83 85
          {
84  
-            addError(sb.toString() + " newline missing in target");
  86
+            if (sb.length() > 0)
  87
+            {
  88
+               sb.append("/Trailing");
  89
+            }
  90
+            else
  91
+            {
  92
+               sb.append("Trailing");
  93
+            }
85 94
          }
86 95
       }
  96
+      return sb.toString();
87 97
    }
88 98
 }
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)
39 39
    }
40 40
 
41 41
    private final static String varRegex = "%[\\w]+";
42  
-   private final static RegExp varRegExp = RegExp.compile(varRegex, "g");
  42
+   private RegExp varRegExp = RegExp.compile(varRegex, "g");
43 43
 
44 44
    @Override
45 45
    public void validate(String source, String target)
46 46
    {
47 47
       if (!ValidationUtils.isEmpty(target))
48 48
       {
49  
-         String tmp = target;
50  
-         StringBuilder sb = new StringBuilder();
51  
-         MatchResult result = varRegExp.exec(source);
  49
+         String error = runValidation(source, target);
  50
+         if (error.length() > 0)
  51
+         {
  52
+            addError("Variable [" + error + "] not found in target");
  53
+         }
52 54
 
53  
-         while (result != null)
  55
+         error = runValidation(target, source);
  56
+         if (error.length() > 0)
54 57
          {
55  
-            String var = result.getGroup(0);
56  
-            Log.debug("Found var:" + var);
57  
-            if (!tmp.contains(var))
58  
-            {
59  
-               sb.append(" ");
60  
-               sb.append(var);
61  
-               sb.append(" ");
62  
-            }
63  
-            else
64  
-            {
65  
-               tmp = tmp.replaceFirst(var, ""); // remove matched var
66  
-            }
67  
-            result = varRegExp.exec(source);
  58
+            addError("Variable [" + error + "] not found in source");
68 59
          }
  60
+      }
  61
+   }
  62
+
  63
+   private String runValidation(String compareFrom, String compareTo)
  64
+   {
  65
+      String tmp = compareTo;
  66
+      StringBuilder sb = new StringBuilder();
  67
+      MatchResult result = varRegExp.exec(compareFrom);
69 68
 
70  
-         if (sb.length() > 0)
  69
+      while (result != null)
  70
+      {
  71
+         String var = result.getGroup(0);
  72
+         Log.debug("Found var:" + var);
  73
+         if (!tmp.contains(var))
71 74
          {
72  
-            addError("Variable [" + sb.toString() + "] missing in target");
  75
+            sb.append(" ");
  76
+            sb.append(var);
  77
+            sb.append(" ");
73 78
          }
  79
+         else
  80
+         {
  81
+            tmp = tmp.replaceFirst(var, ""); // remove matched var
  82
+         }
  83
+         result = varRegExp.exec(compareFrom);
74 84
       }
  85
+
  86
+      return sb.toString();
75 87
    }
76 88
 }
12  server/zanata-war/src/test/java/org/zanata/webtrans/shared/validation/HtmlXmlTagValidationTests.java
@@ -101,6 +101,18 @@ public void XMLTagTestTagMissing2()
101 101
    }
102 102
 
103 103
    @Test
  104
+   public void XMLTagTestTagMissing3()
  105
+   {
  106
+      htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
  107
+
  108
+      String source = "<group><users><user>1</user></users></group>";
  109
+      String target = "<group><users><user>1</user></users><users></users></group>";
  110
+      htmlXmlTagValidation.validate(source, target);
  111
+      Assert.assertTrue(htmlXmlTagValidation.hasError());
  112
+      Assert.assertEquals(htmlXmlTagValidation.getError().size(), 1);
  113
+   }
  114
+
  115
+   @Test
104 116
    public void XMLTagTestMatching()
105 117
    {
106 118
       htmlXmlTagValidation = new HtmlXmlTagValidation("HTML/XML tag", "Matching HTML/XML tag validation");
16  server/zanata-war/src/test/java/org/zanata/webtrans/shared/validation/NewlineLeadTrailValidationTests.java
@@ -49,7 +49,7 @@ public void NewlineLeadTrailTestWithMissingLead()
49 49
       String target = "Testing string with leading new line\n";
50 50
       newlineLeadTrailValidation.validate(source, target);
51 51
       Assert.assertTrue(newlineLeadTrailValidation.hasError());
52  
-      Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 1);
  52
+      Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 2);
53 53
    }
54 54
 
55 55
    @Test
@@ -61,7 +61,7 @@ public void NewlineLeadTrailTestWithMissingTrail()
61 61
       String target = "\nTesting string with leading new line";
62 62
       newlineLeadTrailValidation.validate(source, target);
63 63
       Assert.assertTrue(newlineLeadTrailValidation.hasError());
64  
-      Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 1);
  64
+      Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 2);
65 65
    }
66 66
 
67 67
    @Test
@@ -77,6 +77,18 @@ public void NewlineLeadTrailTestWithMissingBoth()
77 77
    }
78 78
 
79 79
    @Test
  80
+   public void NewlineLeadTrailTestWithMissingBoth2()
  81
+   {
  82
+      newlineLeadTrailValidation = new NewlineLeadTrailValidation("Newline lead/trail", "Newline lead/trail validation");
  83
+
  84
+      String source = "\nTesting string with leading new line";
  85
+      String target = "\nTesting string with leading new line\n";
  86
+      newlineLeadTrailValidation.validate(source, target);
  87
+      Assert.assertTrue(newlineLeadTrailValidation.hasError());
  88
+      Assert.assertEquals(newlineLeadTrailValidation.getError().size(), 1);
  89
+   }
  90
+
  91
+   @Test
80 92
    public void NewlineLeadTrailTestMatch()
81 93
    {
82 94
       newlineLeadTrailValidation = new NewlineLeadTrailValidation("Newline lead/trail", "Newline lead/trail validation");
12  server/zanata-war/src/test/java/org/zanata/webtrans/shared/validation/VariablesValidationTests.java
@@ -78,6 +78,18 @@ public void VariablesTestWithMismatch3()
78 78
    }
79 79
 
80 80
    @Test
  81
+   public void VariablesTestWithMismatch4()
  82
+   {
  83
+      variablesValidation = new VariablesValidation("Variables check", "Variables check validation");
  84
+
  85
+      String source = "Testing string with variable %var1 and %var2 and %var3";
  86
+      String target = "Testing string with variable %var1 and %var2 and %var3 and %var4";
  87
+      variablesValidation.validate(source, target);
  88
+      Assert.assertTrue(variablesValidation.hasError());
  89
+      Assert.assertEquals(variablesValidation.getError().size(), 1);
  90
+   }
  91
+
  92
+   @Test
81 93
    public void VariablesTestWithMatch()
82 94
    {
83 95
       variablesValidation = new VariablesValidation("Variables check", "Variables check validation");

0 notes on commit 8c5e2ae

Please sign in to comment.
Something went wrong with that request. Please try again.