1 | package com.ether; |
---|
2 | |
---|
3 | import org.apache.commons.lang.ArrayUtils; |
---|
4 | import org.junit.Assert; |
---|
5 | import org.junit.Test; |
---|
6 | |
---|
7 | import java.text.DateFormat; |
---|
8 | import java.text.SimpleDateFormat; |
---|
9 | import java.util.ArrayList; |
---|
10 | import java.util.Calendar; |
---|
11 | import java.util.Date; |
---|
12 | import java.util.HashSet; |
---|
13 | import java.util.List; |
---|
14 | import java.util.Set; |
---|
15 | |
---|
16 | /** |
---|
17 | * @author vmipsl |
---|
18 | * @date 2011 |
---|
19 | */ |
---|
20 | public class OtherTest |
---|
21 | { |
---|
22 | @Test |
---|
23 | public void testDatesMoisPlusUn() |
---|
24 | throws Exception |
---|
25 | { |
---|
26 | final DateFormat shortDateFormat = new SimpleDateFormat( "MMyyyy" ); |
---|
27 | final DateFormat completDateFormat = new SimpleDateFormat( "dd-MM-yyyy" ); |
---|
28 | final Date firstMois = shortDateFormat.parse( "102008" ); |
---|
29 | final String firstMoisString = completDateFormat.format( firstMois ); |
---|
30 | Assert.assertEquals( firstMoisString, "01-10-2008" ); |
---|
31 | |
---|
32 | final Calendar calendar = Calendar.getInstance(); |
---|
33 | calendar.setTime( firstMois ); |
---|
34 | calendar.add( Calendar.MONTH, 1 ); |
---|
35 | final Date secondMois = calendar.getTime(); |
---|
36 | final String secondMoisString = completDateFormat.format( secondMois ); |
---|
37 | Assert.assertEquals( secondMoisString, "01-11-2008" ); |
---|
38 | } |
---|
39 | |
---|
40 | @Test |
---|
41 | public void testDates() |
---|
42 | throws Exception |
---|
43 | { |
---|
44 | List<Date> dates = new ArrayList<Date>(); |
---|
45 | final DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" ); |
---|
46 | |
---|
47 | dates.add( dateFormat.parse( "2007-01-01" ) ); |
---|
48 | dates.add( dateFormat.parse( "2007-01-02" ) ); |
---|
49 | |
---|
50 | final DateFormat shortFormat = new SimpleDateFormat( "yyyy" ); |
---|
51 | Set<String> shortDates = new HashSet<String>(); |
---|
52 | for( final Date aDate : dates ) |
---|
53 | { |
---|
54 | shortDates.add( shortFormat.format( aDate ) ); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Test transform List<Pair> en Pair<List, List> |
---|
60 | * Method used in getListsByPlateformByParameterByPeriodForTimeSerie |
---|
61 | */ |
---|
62 | @Test |
---|
63 | public void testListPairToPairLists() |
---|
64 | { |
---|
65 | final List<Object[]> objects = new ArrayList<Object[]>( 3 ); |
---|
66 | final Object[] obj1 = new Object[2]; |
---|
67 | obj1[0] = 2.2; |
---|
68 | obj1[1] = new Date(); |
---|
69 | objects.add( obj1 ); |
---|
70 | |
---|
71 | final Object[] obj2 = new Object[2]; |
---|
72 | obj2[0] = 2.5; |
---|
73 | obj2[1] = new Date(); |
---|
74 | objects.add( obj2 ); |
---|
75 | |
---|
76 | final Object[] obj3 = new Object[2]; |
---|
77 | obj3[0] = 2.7; |
---|
78 | obj3[1] = new Date(); |
---|
79 | objects.add( obj3 ); |
---|
80 | |
---|
81 | if( objects.get( 0 )[0] instanceof Double && objects.get( 0 )[1] instanceof Date ) |
---|
82 | { |
---|
83 | final double[] firstValues = new double[objects.size()]; |
---|
84 | final Date[] secondValues = new Date[objects.size()]; |
---|
85 | int i = 0; |
---|
86 | for( final Object[] value : objects ) |
---|
87 | { |
---|
88 | secondValues[i] = (Date) value[1]; |
---|
89 | firstValues[i] = (Double) value[0]; |
---|
90 | i++; |
---|
91 | } |
---|
92 | final Pair<double[], Date[]> values = new Pair( firstValues, secondValues ); |
---|
93 | |
---|
94 | Assert.assertEquals( values.getFirstValue().length, objects.size() ); |
---|
95 | Assert.assertEquals( values.getSecondValue().length, objects.size() ); |
---|
96 | Assert.assertEquals( values.getFirstValue()[0], obj1[0] ); |
---|
97 | Assert.assertEquals( values.getSecondValue()[0], obj1[1] ); |
---|
98 | Assert.assertEquals( values.getFirstValue()[1], obj2[0] ); |
---|
99 | Assert.assertEquals( values.getSecondValue()[1], obj2[1] ); |
---|
100 | Assert.assertEquals( values.getFirstValue()[2], obj3[0] ); |
---|
101 | Assert.assertEquals( values.getSecondValue()[2], obj3[1] ); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | @Test |
---|
106 | public void testConversionListToArray() |
---|
107 | { |
---|
108 | final List<Double> doubleList = new ArrayList<Double>( 2 ); |
---|
109 | doubleList.add( 1.2 ); |
---|
110 | doubleList.add( 5.6 ); |
---|
111 | |
---|
112 | Double[] doubleArray = new Double[doubleList.size()]; |
---|
113 | doubleArray = doubleList.toArray( doubleArray ); |
---|
114 | |
---|
115 | final double[] doubles = ArrayUtils.toPrimitive( doubleArray ); |
---|
116 | Assert.assertEquals( doubles[0], (double) doubleList.get( 0 ), 0 ); |
---|
117 | Assert.assertEquals( doubles[1], (double) doubleList.get( 1 ), 0 ); |
---|
118 | |
---|
119 | double[] doubles1 = EtherHelper.convertListDoubleToDoubleArray( doubleList ); |
---|
120 | Assert.assertEquals( doubles1[0], (double) doubleList.get( 0 ), 0 ); |
---|
121 | Assert.assertEquals( doubles1[1], (double) doubleList.get( 1 ), 0 ); |
---|
122 | } |
---|
123 | } |
---|