001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.test;
018
019import java.io.FileInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.util.Arrays;
023import java.util.List;
024import java.util.Properties;
025import java.util.Set;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.utils.FileUtils;
030
031import static org.junit.Assert.assertEquals;
032import static org.junit.Assert.assertNotNull;
033
034/**
035 * Base test class with helpers for translation files.
036 *
037 * @since 7.3
038 */
039public abstract class AbstractTranslationTestCase {
040
041    private static final Log log = LogFactory.getLog(AbstractTranslationTestCase.class);
042
043    protected InputStream getFromContext(String path) throws IOException {
044        String file = FileUtils.getResourcePathFromContext(path);
045        return new FileInputStream(file);
046
047    }
048
049    protected TranslationProperties extractProps(String path) throws IOException {
050        InputStream in = getFromContext(path);
051        return extractProps(in);
052    }
053
054    protected TranslationProperties extractProps(InputStream in) throws IOException {
055        TranslationProperties props = new TranslationProperties();
056        props.load(in);
057        return props;
058    }
059
060    protected void checkFormat(String path) throws IOException {
061        TranslationProperties p = extractProps(path);
062        assertNotNull(p);
063        checkSingleLabels(path, p);
064        // TODO: check encoding?
065    }
066
067    protected void checkSingleLabels(String path, TranslationProperties p) throws IOException {
068        // maybe refine when dealing with long labels, just warn in case labels are not well chosen
069        Set<String> single = p.getSingleLabels();
070        if (single.size() > 0) {
071            log.warn(String.format("%s single translation keys in file at '%s'.", single.size(), path));
072            if (log.isDebugEnabled()) {
073                log.debug(String.format("Single keys: '%s'", single));
074            }
075        }
076    }
077
078    protected void checkDuplicates(String path, String... allowed) throws IOException {
079        TranslationProperties r = extractProps(path);
080        if (allowed != null && allowed.length > 0) {
081            assertEquals(String.format("Unexpected duplicates in file at '%s'", path), Arrays.asList(allowed),
082                    r.getDuplicates());
083        } else {
084            assertEquals(String.format("Duplicates in file at '%s': %s", path, r.getDuplicates().toString()), 0,
085                    r.getDuplicates().size());
086        }
087    }
088
089    protected void checkDiff(String path1, String path2) throws IOException {
090        Properties p1 = extractProps(path1);
091        Properties p2 = extractProps(path2);
092        TranslationMessagesDiffer diff = new TranslationMessagesDiffer(p1, p2);
093        List<String> missing = diff.getMissingDestKeys();
094        assertEquals(
095                String.format("Missing translation keys in file at '%s' compared to '%s': %s", path2, path1, missing),
096                0, missing.size());
097    }
098
099}