Alexander Wels has uploaded a new change for review.

Change subject: userportal,webadmin: messages locale unit test
......................................................................

userportal,webadmin: messages locale unit test

- Added unit tests for all the locale messages which check
  the following:
  - The number of parameters on the interface match the parameters
    on each locales translation. This takes @Optional into account,
    so if there are 3 parameters and one optional, then 2 or 3
    parameters are acceptable.
  - There are no out of bounds indexes in the translation, for
    instance if there are 2 parameters, there is no {2} or higher
    in the translated messages (the index starts at 0).
  - Checks if there is a default translation for missing translations
- If any of these conditions occur the unit test will fail and report
  all errors to the user.

Change-Id: Icda389f2ca08e9d3b7515bb4a0dc1213bc91aeef
Signed-off-by: Alexander Wels <aw...@redhat.com>
---
M frontend/webadmin/modules/gwt-common/pom.xml
A 
frontend/webadmin/modules/gwt-common/src/test/java/org/ovirt/engine/ui/common/CommonApplicationMessagesTest.java
M frontend/webadmin/modules/pom.xml
M frontend/webadmin/modules/userportal-gwtp/pom.xml
A 
frontend/webadmin/modules/userportal-gwtp/src/test/java/org/ovirt/engine/ui/userportal/ApplicationMessagesTest.java
M frontend/webadmin/modules/webadmin/pom.xml
A 
frontend/webadmin/modules/webadmin/src/test/java/org/ovirt/engine/ui/webadmin/ApplicationMessagesTest.java
7 files changed, 257 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/95/32995/1

diff --git a/frontend/webadmin/modules/gwt-common/pom.xml 
b/frontend/webadmin/modules/gwt-common/pom.xml
index 526a41b..05fe03d 100644
--- a/frontend/webadmin/modules/gwt-common/pom.xml
+++ b/frontend/webadmin/modules/gwt-common/pom.xml
@@ -84,6 +84,14 @@
         <directory>src/main/resources</directory>
       </resource>
     </resources>
+    <testResources>
+      <testResource>
+        <directory>${project.basedir}/src/main/resources</directory>
+          <includes>
+            <include>**/*Messages*.properties</include>
+          </includes>
+      </testResource>
+    </testResources>
     <plugins>
       <plugin>
         <groupId>org.codehaus.mojo</groupId>
diff --git 
a/frontend/webadmin/modules/gwt-common/src/test/java/org/ovirt/engine/ui/common/CommonApplicationMessagesTest.java
 
b/frontend/webadmin/modules/gwt-common/src/test/java/org/ovirt/engine/ui/common/CommonApplicationMessagesTest.java
new file mode 100644
index 0000000..fcf7ad1
--- /dev/null
+++ 
b/frontend/webadmin/modules/gwt-common/src/test/java/org/ovirt/engine/ui/common/CommonApplicationMessagesTest.java
@@ -0,0 +1,153 @@
+package org.ovirt.engine.ui.common;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.gwt.i18n.client.Messages.DefaultMessage;
+import com.google.gwt.i18n.client.Messages.Optional;
+
+public class CommonApplicationMessagesTest {
+    private static final String PLACE_HOLDER_STRING = "\\{(\\d+)\\}";
+    private static final Pattern placeHolderPattern = 
Pattern.compile(PLACE_HOLDER_STRING);
+
+    /**
+     * Array of {@code Method} objects from the interface.
+     */
+    protected Method[] messagesMethods;
+
+    /**
+     * List of errors encountered during test as {@code String}.
+     */
+    List<String> errors = new ArrayList<String>();
+
+    @Before
+    public void setUp() throws Exception {
+        messagesMethods = CommonApplicationMessages.class.getDeclaredMethods();
+    }
+
+    @Test
+    public void doTest() throws URISyntaxException, IOException {
+        for (File localeFile: getMessagesPropertiesFiles(".")) {
+            Properties properties = loadProperties(localeFile);
+            compareMethodsToProperties(messagesMethods, properties, 
localeFile);
+        }
+        assertTrue(format(errors), errors.isEmpty());
+    }
+
+    private String format(List<String> errors) {
+        StringBuilder builder = new StringBuilder();
+        for (String error: errors) {
+            builder.append(error);
+            builder.append("\n");
+        }
+        return builder.toString();
+    }
+
+    protected void compareMethodsToProperties(Method[] messagesMethods, 
Properties properties, File propertiesFile) {
+        for (Method method: messagesMethods) {
+            int count = 0;
+            checkForMissingDefault(properties, propertiesFile, method);
+            checkPlaceHolders(properties, propertiesFile, method, count);
+        }
+    }
+
+    private void checkPlaceHolders(Properties properties, File propertiesFile, 
Method method, int count) {
+        if (properties.getProperty(method.getName()) != null) {
+            Set<Integer> foundIndex = new HashSet<Integer>();
+            // Check to make sure the number of parameters is inside the range 
defined.
+            Matcher matcher = 
placeHolderPattern.matcher(properties.getProperty(method.getName()));
+            while (matcher.find()) {
+                int placeHolderIndex = -1;
+                try {
+                    placeHolderIndex = Integer.parseInt(matcher.group(1));
+                    if (!foundIndex.contains(placeHolderIndex)) {
+                        count++;
+                        foundIndex.add(placeHolderIndex);
+                    }
+                    if (placeHolderIndex < 0
+                            || placeHolderIndex >= 
getMaxMethodParameter(method.getParameterAnnotations())) {
+                        errors.add(method.getName() + " contains out of bound 
index " + placeHolderIndex + " in "
+                                + propertiesFile.getName());
+                    }
+                } catch (NumberFormatException nfe) {
+                    errors.add(method.getName() + " contains invalid key " + 
matcher.group(0) + " in "
+                            + propertiesFile.getName());
+                }
+            }
+            if (count < getMinMethodParameter(method.getParameterAnnotations())
+                    || count > 
getMaxMethodParameter(method.getParameterAnnotations())) {
+                errors.add(method.getName() + " does not match the number of 
parameters in "
+                        + propertiesFile.getName());
+            }
+        }
+    }
+
+    private int getMaxMethodParameter(Annotation[][] typeAnnotations) {
+        return typeAnnotations.length;
+    }
+
+    private int getMinMethodParameter(Annotation[][] typeAnnotations) {
+        int minLength = typeAnnotations.length;
+        for (Annotation[] typeAnnotation: typeAnnotations) {
+            if (typeAnnotation.length > 0) {
+                for (Annotation annotation: typeAnnotation) {
+                    if (annotation.annotationType().equals(Optional.class)) {
+                        minLength--;
+                    }
+                }
+            }
+        }
+        return minLength;
+    }
+
+    private void checkForMissingDefault(Properties properties, File 
propertiesFile, Method method) {
+        //Make sure that the properties file has the key, as there is no 
default message.
+        if (method.getAnnotation(DefaultMessage.class) == null) {
+            if (!properties.contains(method.getName())) {
+                errors.add("Key: " + method.getName() + " not found in 
properties file: "
+                        + propertiesFile.getName() + " and no default 
defined");
+            }
+        }
+    }
+
+    private Properties loadProperties(File localeFile) throws IOException {
+        Properties properties = new Properties();
+        properties.load(getClass().getResourceAsStream(localeFile.getName()));
+        return properties;
+    }
+
+    /**
+     * Locate the properties files.
+     * @param path The path to the directory.
+     * @return An {@code Array} of {@code File} objects.
+     * @throws URISyntaxException If path doesn't exist
+     */
+    private File[] getMessagesPropertiesFiles(final String path) throws 
URISyntaxException {
+        File currentDir = new File(
+                
this.getClass().getResource(path).toURI().toASCIIString().replaceAll("file:", 
""));
+        return currentDir.listFiles(new FilenameFilter() {
+
+            @Override
+            public boolean accept(File dir, String name) {
+                return name.contains("Messages") && 
name.endsWith(".properties");
+            }
+        });
+    }
+
+}
diff --git a/frontend/webadmin/modules/pom.xml 
b/frontend/webadmin/modules/pom.xml
index 12b63c1..59f0fb0 100644
--- a/frontend/webadmin/modules/pom.xml
+++ b/frontend/webadmin/modules/pom.xml
@@ -199,6 +199,36 @@
             </sources>
           </configuration>
         </plugin>
+        <plugin>
+          <artifactId>maven-checkstyle-plugin</artifactId>
+          <dependencies>
+            <dependency>
+              <groupId>org.ovirt.engine</groupId>
+              <artifactId>checkstyles</artifactId>
+              <version>${engine.version}</version>
+            </dependency>
+            <dependency>
+              <groupId>org.ovirt.engine</groupId>
+              <artifactId>ovirt-checkstyle-extension</artifactId>
+              <version>${engine.version}</version>
+            </dependency>
+          </dependencies>
+          <configuration>
+            <consoleOutput>true</consoleOutput>
+            <includeTestSourceDirectory>false</includeTestSourceDirectory>
+            <configLocation>checkstyle.xml</configLocation>
+            
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
+            <propertyExpansion>runNlsCheck=false</propertyExpansion>
+          </configuration>
+          <executions>
+            <execution>
+              <phase>compile</phase>
+              <goals>
+                <goal>check</goal>
+              </goals>
+            </execution>
+          </executions>
+        </plugin>
       </plugins>
     </pluginManagement>
   </build>
diff --git a/frontend/webadmin/modules/userportal-gwtp/pom.xml 
b/frontend/webadmin/modules/userportal-gwtp/pom.xml
index 8dfef94..cae09c0 100644
--- a/frontend/webadmin/modules/userportal-gwtp/pom.xml
+++ b/frontend/webadmin/modules/userportal-gwtp/pom.xml
@@ -78,6 +78,13 @@
     </dependency>
     <dependency>
       <groupId>${engine.groupId}</groupId>
+      <artifactId>gwt-common</artifactId>
+      <version>${engine.version}</version>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>${engine.groupId}</groupId>
       <artifactId>frontend</artifactId>
       <version>${engine.version}</version>
     </dependency>
diff --git 
a/frontend/webadmin/modules/userportal-gwtp/src/test/java/org/ovirt/engine/ui/userportal/ApplicationMessagesTest.java
 
b/frontend/webadmin/modules/userportal-gwtp/src/test/java/org/ovirt/engine/ui/userportal/ApplicationMessagesTest.java
new file mode 100644
index 0000000..7c815cc
--- /dev/null
+++ 
b/frontend/webadmin/modules/userportal-gwtp/src/test/java/org/ovirt/engine/ui/userportal/ApplicationMessagesTest.java
@@ -0,0 +1,22 @@
+package org.ovirt.engine.ui.userportal;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Before;
+import org.ovirt.engine.ui.common.CommonApplicationMessagesTest;
+
+public class ApplicationMessagesTest extends CommonApplicationMessagesTest {
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        List<Method> allMessageMethods = new ArrayList<Method>();
+        allMessageMethods.addAll(Arrays.asList(messagesMethods));
+        
allMessageMethods.addAll(Arrays.asList(ApplicationMessages.class.getDeclaredMethods()));
+        messagesMethods = allMessageMethods.toArray(new 
Method[allMessageMethods.size()]);
+    }
+}
diff --git a/frontend/webadmin/modules/webadmin/pom.xml 
b/frontend/webadmin/modules/webadmin/pom.xml
index 71c708c..ee0425e 100644
--- a/frontend/webadmin/modules/webadmin/pom.xml
+++ b/frontend/webadmin/modules/webadmin/pom.xml
@@ -78,6 +78,13 @@
     </dependency>
     <dependency>
       <groupId>${engine.groupId}</groupId>
+      <artifactId>gwt-common</artifactId>
+      <version>${engine.version}</version>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>${engine.groupId}</groupId>
       <artifactId>frontend</artifactId>
       <version>${engine.version}</version>
     </dependency>
@@ -153,6 +160,14 @@
         </includes>
       </resource>
     </resources>
+    <testResources>
+      <testResource>
+        <directory>${project.basedir}/src/main/resources</directory>
+          <includes>
+            <include>**/*Messages*.properties</include>
+          </includes>
+      </testResource>
+    </testResources>
     <plugins>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
diff --git 
a/frontend/webadmin/modules/webadmin/src/test/java/org/ovirt/engine/ui/webadmin/ApplicationMessagesTest.java
 
b/frontend/webadmin/modules/webadmin/src/test/java/org/ovirt/engine/ui/webadmin/ApplicationMessagesTest.java
new file mode 100644
index 0000000..cd642b9
--- /dev/null
+++ 
b/frontend/webadmin/modules/webadmin/src/test/java/org/ovirt/engine/ui/webadmin/ApplicationMessagesTest.java
@@ -0,0 +1,22 @@
+package org.ovirt.engine.ui.webadmin;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Before;
+import org.ovirt.engine.ui.common.CommonApplicationMessagesTest;
+
+public class ApplicationMessagesTest extends CommonApplicationMessagesTest {
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        List<Method> allMessageMethods = new ArrayList<Method>();
+        allMessageMethods.addAll(Arrays.asList(messagesMethods));
+        
allMessageMethods.addAll(Arrays.asList(ApplicationMessages.class.getDeclaredMethods()));
+        messagesMethods = allMessageMethods.toArray(new 
Method[allMessageMethods.size()]);
+    }
+}


-- 
To view, visit http://gerrit.ovirt.org/32995
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icda389f2ca08e9d3b7515bb4a0dc1213bc91aeef
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Alexander Wels <aw...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to