nc.noumea.mairie.appock.core.utility.AppockUtilTest.java Source code

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.core.utility.AppockUtilTest.java

Source

package nc.noumea.mairie.appock.core.utility;

/*-
 * #%L
 * Logiciel de Gestion des approvisionnements et des stocks des fournitures administratives de la Mairie de Nouma
 * %%
 * Copyright (C) 2017 Mairie de Nouma, Nouvelle-Caldonie
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import org.postgresql.util.Base64;

import nc.noumea.mairie.appock.core.security.AppUser;
import nc.noumea.mairie.appock.enums.Role;

public class AppockUtilTest {

    @Test
    public void sameIdNotNull() {
        Assert.assertTrue(AppockUtil.sameIdAndNotNull(new Long(0), new Long(0)));
        Assert.assertTrue(AppockUtil.sameIdAndNotNull(new Long(100), new Long(100)));

        Assert.assertFalse(AppockUtil.sameIdAndNotNull(new Long(100), new Long(10000)));
        Assert.assertFalse(AppockUtil.sameIdAndNotNull(new Long(100), null));
        Assert.assertFalse(AppockUtil.sameIdAndNotNull(null, new Long(100)));
        Assert.assertFalse(AppockUtil.sameIdAndNotNull(null, new Long(0)));
    }

    @Test
    public void majusculeSansAccentTrim() {
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim(null), "");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim(""), "");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim(" "), "");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim(" spR  "), "SUPER");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim(" spR!"), "SUPER!");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim("e"), "EEEE");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim("a!"), "AAAA!");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim(" iI"), "IIII");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim(" a"), "CA");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim("AZERTYUIOPQSDFGHJKLMWXCVBN"),
                "AZERTYUIOPQSDFGHJKLMWXCVBN");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim("azertyuiopqsdfghjklmwxcvbn"),
                "AZERTYUIOPQSDFGHJKLMWXCVBN");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrim("-()[]+*/"), "-()[]+*/");
    }

    @Test
    public void majusculeSansAccentTrimToNull() {
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull(null), null);
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull(""), null);
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull(" "), null);
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull(" spR  "), "SUPER");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull(" spR!"), "SUPER!");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull("e"), "EEEE");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull("a!"), "AAAA!");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull(" iI"), "IIII");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull(" a"), "CA");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull("AZERTYUIOPQSDFGHJKLMWXCVBN"),
                "AZERTYUIOPQSDFGHJKLMWXCVBN");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull("azertyuiopqsdfghjklmwxcvbn"),
                "AZERTYUIOPQSDFGHJKLMWXCVBN");
        Assert.assertEquals(AppockUtil.majusculeSansAccentTrimToNull("-()[]+*/"), "-()[]+*/");
    }

    @Test
    public void paddingZeroAGaucheSaufSiVide() {
        Assert.assertNull(AppockUtil.paddingZeroAGaucheSaufSiVide(null, 4));
        Assert.assertNull(AppockUtil.paddingZeroAGaucheSaufSiVide("  ", 4));
        Assert.assertNull(AppockUtil.paddingZeroAGaucheSaufSiVide("  ", 0));
        //
        Assert.assertEquals(AppockUtil.paddingZeroAGaucheSaufSiVide("AA", 4), "00AA");
        Assert.assertEquals(AppockUtil.paddingZeroAGaucheSaufSiVide("AA", 2), "AA");
        Assert.assertEquals(AppockUtil.paddingZeroAGaucheSaufSiVide("AA", 1), "AA");
        Assert.assertEquals(AppockUtil.paddingZeroAGaucheSaufSiVide("AA", 0), "AA");
    }

    private void assertEqualsAvecPointOuVirgule(String texte1, String texte2) {
        String texte1Modifie = StringUtils.replace(texte1, ",", ".");
        String texte2Modifie = StringUtils.replace(texte2, ",", ".");
        Assert.assertEquals(texte1Modifie, texte2Modifie);
    }

    @Test
    public void getLastOrNull() {
        List<String> liste = null;
        Assert.assertNull(AppockUtil.getLastOrNull(liste));

        liste = new ArrayList<>();
        Assert.assertNull(AppockUtil.getLastOrNull(liste));

        liste.add("test");
        Assert.assertEquals("test", AppockUtil.getLastOrNull(liste));

        liste.add("dernier");
        Assert.assertEquals("dernier", AppockUtil.getLastOrNull(liste));
    }

    @Test
    public void construitTexteTronque() {
        Assert.assertNull(AppockUtil.construitTexteTronque(null, 10, "[...]"));
        Assert.assertEquals(AppockUtil.construitTexteTronque("", 3, "[...]"), "");
        Assert.assertEquals(AppockUtil.construitTexteTronque(" ", 3, "[...]"), "");
        Assert.assertEquals(AppockUtil.construitTexteTronque("       ", 3, "[...]"), "");

        Assert.assertEquals(AppockUtil.construitTexteTronque("Long texte", 3, "[...]"), "Lon[...]");
        Assert.assertEquals(AppockUtil.construitTexteTronque("Long texte", 3, " [...]"), "Lon [...]");
        Assert.assertEquals(AppockUtil.construitTexteTronque("Long texte", 100, "[...]"), "Long texte");
        Assert.assertEquals(AppockUtil.construitTexteTronque("Long texte", 9, "[...]"), "Long text[...]");
        Assert.assertEquals(AppockUtil.construitTexteTronque("Long texte", 10, "[...]"), "Long texte");
        Assert.assertEquals(AppockUtil.construitTexteTronque("Long texte", 0, "[...]"), "[...]");
        Assert.assertEquals(AppockUtil.construitTexteTronque("Long texte", -1, "[...]"), "[...]");

        Assert.assertEquals(AppockUtil.construitTexteTronque("Long texte", 3, ""), "Lon");
        Assert.assertEquals(AppockUtil.construitTexteTronque("Long texte", 3, " "), "Lon ");
        Assert.assertEquals(AppockUtil.construitTexteTronque("  Long texte  ", 3, " "), "Lon ");

        Assert.assertEquals(AppockUtil.construitTexteTronque(" Long texte  ", 3, null), "Lon"); // suffixe null interprt comme vide
    }

    @Test
    public void chargeElement() {
        // on s'assure juste qu'il n'y a pas d'exception :
        AppockUtil.chargeElement(null);
        AppockUtil.chargeElement("test");
    }

    @Test
    public void chargeCollection() {
        // on s'assure juste qu'il n'y a pas d'exception :
        AppockUtil.chargeCollection(null);
        AppockUtil.chargeCollection(new ArrayList<String>());
        AppockUtil.chargeCollection(Arrays.asList("test1"));
        AppockUtil.chargeCollection(Arrays.asList("test1", "test2"));
    }

    @Test
    public void createList() {
        // cas limites
        Assert.assertTrue(AppockUtil.createList(null, 0).isEmpty());
        Assert.assertTrue(AppockUtil.createList(null, 1).isEmpty());

        List<Object> liste = new ArrayList<>();

        // liste vide
        Assert.assertTrue(AppockUtil.createList(liste, 0).isEmpty());

        // liste remplie
        liste.add(new Object[] { "A1", "A2", "A3" });
        liste.add(new Object[] { "B1", "B2" });
        Assert.assertEquals(AppockUtil.createList(liste, 0), Arrays.asList("A1", "B1"));
        Assert.assertEquals(AppockUtil.createList(liste, 1), Arrays.asList("A2", "B2"));
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void createListOutOfBound() {
        List<Object> liste = new ArrayList<>();
        liste.add(new Object[] { "A1", "A2" });
        AppockUtil.createList(liste, 2); // hors limite
    }

    @Test
    public void getSimpleNameOfClass() {
        // cas limite
        Assert.assertNull(AppockUtil.getSimpleNameOfClass(null));

        // cas standard
        Assert.assertEquals(AppockUtil.getSimpleNameOfClass(Integer.class), "Integer"); // classe standard Java
        Assert.assertEquals(AppockUtil.getSimpleNameOfClass(AppUser.class), "AppUser"); // classe
    }

    @Test
    public void getSimpleClassNameOfObject() {
        // cas limite
        Assert.assertNull(AppockUtil.getSimpleClassNameOfObject(null));

        // cas standard
        Assert.assertEquals(AppockUtil.getSimpleClassNameOfObject("test"), "String");
        Assert.assertEquals(AppockUtil.getSimpleClassNameOfObject(123), "Integer");
        Assert.assertEquals(AppockUtil.getSimpleClassNameOfObject(Role.ROLE_ADMIN), "Role"); // enum
        Assert.assertEquals(AppockUtil.getSimpleClassNameOfObject(new AppUser()), "AppUser"); // classe
    }

    @Test
    public void getMaxLength() throws Exception {
        // cas limite
        Assert.assertNull(AppockUtil.getMaxLength(null, "nomPropriete"));

        // cas standard
        AppUser appUser = new AppUser();
        Assert.assertEquals(AppockUtil.getMaxLength(appUser, "login"), (Integer) 50);
        Assert.assertEquals(AppockUtil.getMaxLength(appUser, "nom"), (Integer) 100);
    }

    @Test
    public void getMaxLengthClassProperty() throws Exception {
        Assert.assertEquals(
                AppockUtil.getMaxLengthClassProperty("nc.noumea.mairie.appock.core.security.AppUser", "email"),
                (Integer) 200);
    }

    @Test
    public void isValidEmailAddress() {
        // cas ko
        Assert.assertFalse(AppockUtil.isValidEmailAddress(""));
        Assert.assertFalse(AppockUtil.isValidEmailAddress(null));
        Assert.assertFalse(AppockUtil.isValidEmailAddress("  "));
        Assert.assertFalse(AppockUtil.isValidEmailAddress("pas bien"));
        Assert.assertFalse(AppockUtil.isValidEmailAddress("pas_bien"));
        Assert.assertFalse(AppockUtil.isValidEmailAddress("test@test.nc@test.nc"));
        Assert.assertFalse(AppockUtil.isValidEmailAddress("jean.dupont@gmail.com!"));

        // cas ok
        Assert.assertTrue(AppockUtil.isValidEmailAddress("test@test.nc"));
        Assert.assertTrue(AppockUtil.isValidEmailAddress("jean.dupont@gmail.com"));
        Assert.assertTrue(AppockUtil.isValidEmailAddress(" test@test.nc "));
    }

    @Test
    public void capitalizeFullyFrench() {
        Assert.assertNull(AppockUtil.capitalizeFullyFrench(null));
        Assert.assertNull(AppockUtil.capitalizeFullyFrench(""));
        Assert.assertNull(AppockUtil.capitalizeFullyFrench("  "));
        Assert.assertEquals(AppockUtil.capitalizeFullyFrench("jean-charles"), "Jean-Charles");
        Assert.assertEquals(AppockUtil.capitalizeFullyFrench(" jean-charles "), " Jean-Charles ");
    }

    @Test
    public void representationCfp() {
        Assert.assertEquals(AppockUtil.representationCfp(100), "100 FCFP");
        Assert.assertEquals(AppockUtil.representationCfp(0), "0 FCFP");
        Assert.assertEquals(AppockUtil.representationCfp(-90), "-90 FCFP");
        Assert.assertEquals(AppockUtil.representationCfp(1000), "1 000 FCFP");
        Assert.assertEquals(AppockUtil.representationCfp(1234567), "1 234 567 FCFP");
        Assert.assertEquals(AppockUtil.representationCfp(-1234567), "-1 234 567 FCFP");
    }

    @Test
    public void getMimeType() throws IOException {
        // texte simple
        Assert.assertEquals(AppockUtil.getMimeType("texte simple".getBytes()), "text/plain");

        // note, flux base64 gnr avec https://www.base64-image.de/
        String pngEncodeBase64 = "iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwwAADsMBx2+oZAAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC41ZYUyZQAAAF5JREFUSEvtjEEOgDAMw/bvPj6wA5WgSaRJ24kimUO9eAA4Aj3ugB5XiAh8mXf6eIUOJx1OfhZWY+dKhKHGzpUIQ42dKxGGGjtXIgw1di7H9zd/Lx6nxs51ON2hMMYFe3siNLTl2zMAAAAASUVORK5CYII=";
        Assert.assertEquals(AppockUtil.getMimeType(Base64.decode(pngEncodeBase64)), "image/png");

        // cas null
        Assert.assertNull(AppockUtil.getMimeType(null));
    }

    @Test
    public void formatteAvec2ChiffreApresVirgule() {
        assertEqualsAvecPointOuVirgule("1.12", "1.12");
        assertEqualsAvecPointOuVirgule("1,12", "1,12");
        assertEqualsAvecPointOuVirgule("1,12", "1.12");
        assertEqualsAvecPointOuVirgule("1.12", "1,12");

        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(1.12345, false), "1,12");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(1.12345, false), "1.12");

        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(-1385.99, false), "-1385,99");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(-1385.999, false), "-1386,00");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(null, false), "");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(1.0, false), "1,00");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(0.0, false), "0,00");

        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(1.12345, true), "1,12");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(-10385.99, true), "-10 385,99");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(-10385.999, true), "-10 386,00");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(null, true), "");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(1.0, true), "1,00");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(0.0, true), "0,00");
        assertEqualsAvecPointOuVirgule(AppockUtil.formatteAvec2ChiffreApresVirgule(12345678.88, true),
                "12 345 678,88");
    }

    @Test
    public void arrondiDizaine() {
        Assert.assertEquals(AppockUtil.arrondiDizaine(0.1), 10);
        Assert.assertEquals(AppockUtil.arrondiDizaine(0.11), 20);
        Assert.assertEquals(AppockUtil.arrondiDizaine(0.158), 20);
        Assert.assertEquals(AppockUtil.arrondiDizaine(0.2), 20);
        Assert.assertEquals(AppockUtil.arrondiDizaine(0.21), 30);
        Assert.assertEquals(AppockUtil.arrondiDizaine(0.9), 90);
        Assert.assertEquals(AppockUtil.arrondiDizaine(0.9123), 100);
    }

    @Test
    public void joinListeStringNotBlankTest() {
        List<String> liste = new ArrayList<>();
        liste.add("Elt1");
        liste.add(" Elt2 ");
        liste.add(null);
        liste.add("Elt 3");
        liste.add("");
        liste.add(" Elt4");
        liste.add("Elt5 ");
        Assert.assertEquals("Elt1, Elt2, Elt 3, Elt4, Elt5", AppockUtil.joinListeStringNotBlank(liste, ", ", 10));
        Assert.assertEquals("Elt1, Elt2, Elt 3, Elt4, Elt5", AppockUtil.joinListeStringNotBlank(liste, ", ", null));
        Assert.assertEquals("Elt1, Elt2, Elt 3, Elt4, Elt5", AppockUtil.joinListeStringNotBlank(liste, ", ", -1));
        Assert.assertEquals("Elt1, Elt2, Elt 3, ...", AppockUtil.joinListeStringNotBlank(liste, ", ", 3));
    }
}