001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.platform.test; 020 021import java.io.FileInputStream; 022import java.io.IOException; 023import java.io.InputStream; 024import java.util.Arrays; 025import java.util.List; 026import java.util.Properties; 027import java.util.Set; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.nuxeo.common.utils.FileUtils; 032 033import static org.junit.Assert.assertEquals; 034import static org.junit.Assert.assertNotNull; 035 036/** 037 * Base test class with helpers for translation files. 038 * 039 * @since 7.3 040 */ 041public abstract class AbstractTranslationTestCase { 042 043 private static final Log log = LogFactory.getLog(AbstractTranslationTestCase.class); 044 045 protected InputStream getFromContext(String path) throws IOException { 046 String file = FileUtils.getResourcePathFromContext(path); 047 return new FileInputStream(file); 048 049 } 050 051 protected TranslationProperties extractProps(String path) throws IOException { 052 InputStream in = getFromContext(path); 053 return extractProps(in); 054 } 055 056 protected TranslationProperties extractProps(InputStream in) throws IOException { 057 TranslationProperties props = new TranslationProperties(); 058 props.load(in); 059 return props; 060 } 061 062 protected void checkFormat(String path) throws IOException { 063 TranslationProperties p = extractProps(path); 064 assertNotNull(p); 065 checkSingleLabels(path, p); 066 // TODO: check encoding? 067 } 068 069 protected void checkSingleLabels(String path, TranslationProperties p) throws IOException { 070 // maybe refine when dealing with long labels, just warn in case labels are not well chosen 071 Set<String> single = p.getSingleLabels(); 072 if (single.size() > 0) { 073 log.warn(String.format("%s single translation keys in file at '%s'.", single.size(), path)); 074 if (log.isDebugEnabled()) { 075 log.debug(String.format("Single keys: '%s'", single)); 076 } 077 } 078 } 079 080 protected void checkDuplicates(String path, String... allowed) throws IOException { 081 TranslationProperties r = extractProps(path); 082 if (allowed != null && allowed.length > 0) { 083 assertEquals(String.format("Unexpected duplicates in file at '%s'", path), Arrays.asList(allowed), 084 r.getDuplicates()); 085 } else { 086 assertEquals(String.format("Duplicates in file at '%s': %s", path, r.getDuplicates().toString()), 0, 087 r.getDuplicates().size()); 088 } 089 } 090 091 protected void checkDiff(String path1, String path2) throws IOException { 092 Properties p1 = extractProps(path1); 093 Properties p2 = extractProps(path2); 094 TranslationMessagesDiffer diff = new TranslationMessagesDiffer(p1, p2); 095 List<String> missing = diff.getMissingDestKeys(); 096 assertEquals( 097 String.format("Missing translation keys in file at '%s' compared to '%s': %s", path2, path1, missing), 098 0, missing.size()); 099 } 100 101}