View Javadoc
1   package fr.ifremer.dali.dto;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.ImmutableList;
27  import fr.ifremer.dali.dao.technical.Daos;
28  import fr.ifremer.quadrige3.core.dao.referential.UnitId;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  import java.math.BigDecimal;
33  import java.util.ArrayList;
34  import java.util.LinkedHashMap;
35  import java.util.Map;
36  
37  /**
38   * @author ludovic.pecquot@e-is.pro on 13/04/2017.
39   */
40  public class DaliBeansTest {
41  
42      /**
43       * Test de conversion d'unité de longueur
44       */
45      @Test
46      public void testConvertLengthValue() {
47  
48          BigDecimal result = DaliBeans.convertLengthValue(BigDecimal.valueOf(5), UnitId.METER.getValue(), UnitId.CENTIMENTER.getValue());
49          Assert.assertEquals(500, result.intValue());
50  
51          result = DaliBeans.convertLengthValue(BigDecimal.valueOf(2), UnitId.METER.getValue(), UnitId.MILLIMETER.getValue());
52          Assert.assertEquals(2000, result.intValue());
53  
54          result = DaliBeans.convertLengthValue(BigDecimal.valueOf(6000), UnitId.CENTIMENTER.getValue(), UnitId.METER.getValue());
55          Assert.assertEquals(60, result.intValue());
56  
57      }
58  
59      @Test
60      public void testStringJoiner() {
61          Assert.assertEquals("'A','B','C'", Daos.getInStatementFromStringCollection(ImmutableList.of("A","B","A","C")));
62          Assert.assertEquals("", Daos.getInStatementFromStringCollection(new ArrayList<>()));
63          Assert.assertEquals("", Daos.getInStatementFromStringCollection(null));
64      }
65  
66      @Test
67      public void testSplit() {
68          Assert.assertArrayEquals(new String[] {"a","a1","b","b123"}, DaliBeans.split("a,a1,b,b123", ",").toArray());
69          Assert.assertArrayEquals(new String[] {"a","a1","b","b123"}, DaliBeans.split("a|a1|b|b123", "|").toArray());
70      }
71  
72      @Test
73      public void testSplitAndMap() {
74          Map<String,String> expectedMap = new LinkedHashMap<>();
75          expectedMap.put("key1", "value1");
76          expectedMap.put("key2", "value2");
77          expectedMap.put("key3", "value3");
78  
79          Assert.assertEquals(expectedMap, DaliBeans.splitAndMap("key1:value1;key2:value2;key3:value3", ";", ":"));
80          Assert.assertEquals(expectedMap, DaliBeans.splitAndMap("key1|value1;key2|value2;key3|value3", ";", "|"));
81          Assert.assertEquals(expectedMap, DaliBeans.splitAndMap("key1=value1|key2=value2|key3=value3", "|", "="));
82  
83          try {
84              DaliBeans.splitAndMap("key1:value1;key1:value2;key3:value3", ";", ":");
85              Assert.fail("should throw IllegalStateException");
86          } catch (IllegalStateException e) {
87              Assert.assertNotNull(e);
88          }
89      }
90  }