View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.test.model;
17  
18  import org.fourthline.cling.model.types.Base64Datatype;
19  import org.fourthline.cling.model.types.DLNADoc;
20  import org.fourthline.cling.model.types.Datatype;
21  import org.fourthline.cling.model.types.DateTimeDatatype;
22  import org.fourthline.cling.model.types.DoubleDatatype;
23  import org.fourthline.cling.model.types.FloatDatatype;
24  import org.fourthline.cling.model.types.IntegerDatatype;
25  import org.fourthline.cling.model.types.InvalidValueException;
26  import org.fourthline.cling.model.types.UnsignedIntegerFourBytesDatatype;
27  import org.fourthline.cling.model.types.UnsignedIntegerOneByteDatatype;
28  import org.fourthline.cling.model.types.UnsignedIntegerTwoBytesDatatype;
29  import org.fourthline.cling.model.types.csv.CSVBoolean;
30  import org.fourthline.cling.model.types.csv.CSVString;
31  import org.testng.annotations.Test;
32  
33  import java.util.Arrays;
34  import java.util.Calendar;
35  import java.util.Date;
36  import java.util.List;
37  import java.util.TimeZone;
38  
39  import static org.testng.Assert.assertEquals;
40  
41  
42  public class DatatypesTest {
43  
44      @Test
45      public void upperLowerCase() {
46          // Broken devices do this
47          assertEquals(Datatype.Builtin.getByDescriptorName("String"), Datatype.Builtin.STRING);
48          assertEquals(Datatype.Builtin.getByDescriptorName("strinG"), Datatype.Builtin.STRING);
49          assertEquals(Datatype.Builtin.getByDescriptorName("STRING"), Datatype.Builtin.STRING);
50          assertEquals(Datatype.Builtin.getByDescriptorName("string"), Datatype.Builtin.STRING);
51      }
52  
53      @Test
54      public void validUnsignedIntegers() {
55  
56          UnsignedIntegerOneByteDatatype typeOne = new UnsignedIntegerOneByteDatatype();
57          assertEquals(typeOne.valueOf("123").getValue(), Long.valueOf(123l));
58  
59          UnsignedIntegerTwoBytesDatatype typeTwo = new UnsignedIntegerTwoBytesDatatype();
60          assertEquals(typeTwo.valueOf("257").getValue(), Long.valueOf(257l));
61  
62          UnsignedIntegerFourBytesDatatype typeFour = new UnsignedIntegerFourBytesDatatype();
63          assertEquals(typeFour.valueOf("65536").getValue(), Long.valueOf(65536l));
64          assertEquals(typeFour.valueOf("4294967295").getValue(), Long.valueOf(4294967295l));
65  
66          // Well, no need to write another test for that
67          assertEquals(typeFour.valueOf("4294967295").increment(true).getValue(), Long.valueOf(1));
68  
69      }
70  
71      @Test(expectedExceptions = InvalidValueException.class)
72      public void invalidUnsignedIntegersOne() {
73          UnsignedIntegerOneByteDatatype typeOne = new UnsignedIntegerOneByteDatatype();
74          typeOne.valueOf("256");
75      }
76  
77      @Test(expectedExceptions = InvalidValueException.class)
78      public void invalidUnsignedIntegersTwo() {
79          UnsignedIntegerTwoBytesDatatype typeTwo = new UnsignedIntegerTwoBytesDatatype();
80          typeTwo.valueOf("65536");
81      }
82  
83      @Test
84      public void signedIntegers() {
85  
86          IntegerDatatype type = new IntegerDatatype(1);
87          assert type.isValid(123);
88          assert type.isValid(-124);
89          assert type.valueOf("123") == 123;
90          assert type.valueOf("-124") == -124;
91          assert !type.isValid(256);
92  
93          type = new IntegerDatatype(2);
94          assert type.isValid(257);
95          assert type.isValid(-257);
96          assert type.valueOf("257") == 257;
97          assert type.valueOf("-257") == -257;
98          assert !type.isValid(32768);
99  
100     }
101 
102     @Test
103     public void dateAndTime() {
104         DateTimeDatatype type = (DateTimeDatatype) Datatype.Builtin.DATE.getDatatype();
105 
106         Calendar expexted = Calendar.getInstance();
107         expexted.set(Calendar.YEAR, 2010);
108         expexted.set(Calendar.MONTH, 10);
109         expexted.set(Calendar.DAY_OF_MONTH, 3);
110         expexted.set(Calendar.HOUR_OF_DAY, 8);
111         expexted.set(Calendar.MINUTE, 9);
112         expexted.set(Calendar.SECOND, 10);
113 
114         Calendar parsedDate = type.valueOf("2010-11-03");
115         assertEquals(parsedDate.get(Calendar.YEAR), expexted.get(Calendar.YEAR));
116         assertEquals(parsedDate.get(Calendar.MONTH), expexted.get(Calendar.MONTH));
117         assertEquals(parsedDate.get(Calendar.DAY_OF_MONTH), expexted.get(Calendar.DAY_OF_MONTH));
118         assertEquals(type.getString(expexted), "2010-11-03");
119 
120         type = (DateTimeDatatype) Datatype.Builtin.DATETIME.getDatatype();
121 
122         parsedDate = type.valueOf("2010-11-03");
123         assertEquals(parsedDate.get(Calendar.YEAR), expexted.get(Calendar.YEAR));
124         assertEquals(parsedDate.get(Calendar.MONTH), expexted.get(Calendar.MONTH));
125         assertEquals(parsedDate.get(Calendar.DAY_OF_MONTH), expexted.get(Calendar.DAY_OF_MONTH));
126 
127         parsedDate = type.valueOf("2010-11-03T08:09:10");
128         assertEquals(parsedDate.get(Calendar.YEAR), expexted.get(Calendar.YEAR));
129         assertEquals(parsedDate.get(Calendar.MONTH), expexted.get(Calendar.MONTH));
130         assertEquals(parsedDate.get(Calendar.DAY_OF_MONTH), expexted.get(Calendar.DAY_OF_MONTH));
131         assertEquals(parsedDate.get(Calendar.HOUR_OF_DAY), expexted.get(Calendar.HOUR_OF_DAY));
132         assertEquals(parsedDate.get(Calendar.MINUTE), expexted.get(Calendar.MINUTE));
133         assertEquals(parsedDate.get(Calendar.SECOND), expexted.get(Calendar.SECOND));
134 
135         assertEquals(type.getString(expexted), "2010-11-03T08:09:10");
136     }
137 
138     @Test
139     public void dateAndTimeWithZone() {
140         DateTimeDatatype type =
141                 new DateTimeDatatype(
142                         new String[]{"yyyy-MM-dd", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ssZ"},
143                         "yyyy-MM-dd'T'HH:mm:ssZ"
144                 ) {
145                     @Override
146                     protected TimeZone getTimeZone() {
147                         // Set the "local" timezone to CET for the test
148                         return TimeZone.getTimeZone("CET");
149                     }
150                 };
151 
152         Calendar expected = Calendar.getInstance();
153         expected.setTimeZone(TimeZone.getTimeZone("CET"));
154         expected.set(Calendar.YEAR, 2010);
155         expected.set(Calendar.MONTH, 10);
156         expected.set(Calendar.DAY_OF_MONTH, 3);
157         expected.set(Calendar.HOUR_OF_DAY, 8);
158         expected.set(Calendar.MINUTE, 9);
159         expected.set(Calendar.SECOND, 10);
160 
161         Calendar parsedDate = type.valueOf("2010-11-03T08:09:10");
162         assertEquals(parsedDate.get(Calendar.YEAR), expected.get(Calendar.YEAR));
163         assertEquals(parsedDate.get(Calendar.MONTH), expected.get(Calendar.MONTH));
164         assertEquals(parsedDate.get(Calendar.DAY_OF_MONTH), expected.get(Calendar.DAY_OF_MONTH));
165         assertEquals(parsedDate.get(Calendar.HOUR_OF_DAY), expected.get(Calendar.HOUR_OF_DAY));
166         assertEquals(parsedDate.get(Calendar.MINUTE), expected.get(Calendar.MINUTE));
167         assertEquals(parsedDate.get(Calendar.SECOND), expected.get(Calendar.SECOND));
168 
169         parsedDate = type.valueOf("2010-11-03T08:09:10+0100");
170         assertEquals(parsedDate.get(Calendar.YEAR), expected.get(Calendar.YEAR));
171         assertEquals(parsedDate.get(Calendar.MONTH), expected.get(Calendar.MONTH));
172         assertEquals(parsedDate.get(Calendar.DAY_OF_MONTH), expected.get(Calendar.DAY_OF_MONTH));
173         assertEquals(parsedDate.get(Calendar.HOUR_OF_DAY), expected.get(Calendar.HOUR_OF_DAY));
174         assertEquals(parsedDate.get(Calendar.MINUTE), expected.get(Calendar.MINUTE));
175         assertEquals(parsedDate.get(Calendar.SECOND), expected.get(Calendar.SECOND));
176         assertEquals(parsedDate.getTimeZone(), expected.getTimeZone());
177 
178         assertEquals(type.getString(expected), "2010-11-03T08:09:10+0100");
179     }
180 
181     @Test
182     public void time() {
183         DateTimeDatatype type = (DateTimeDatatype) Datatype.Builtin.TIME.getDatatype();
184 
185         Calendar expected = Calendar.getInstance();
186         expected.setTime(new Date(0));
187         expected.set(Calendar.HOUR_OF_DAY, 8);
188         expected.set(Calendar.MINUTE, 9);
189         expected.set(Calendar.SECOND, 10);
190 
191         Calendar parsedTime = type.valueOf("08:09:10");
192         assertEquals(parsedTime.get(Calendar.HOUR_OF_DAY), expected.get(Calendar.HOUR_OF_DAY));
193         assertEquals(parsedTime.get(Calendar.MINUTE), expected.get(Calendar.MINUTE));
194         assertEquals(parsedTime.get(Calendar.SECOND), expected.get(Calendar.SECOND));
195         assertEquals(type.getString(expected), "08:09:10");
196     }
197 
198     @Test
199     public void timeWithZone() {
200 
201         DateTimeDatatype type = new DateTimeDatatype(new String[]{"HH:mm:ssZ", "HH:mm:ss"}, "HH:mm:ssZ") {
202             @Override
203             protected TimeZone getTimeZone() {
204                 // Set the "local" timezone to CET for the test
205                 return TimeZone.getTimeZone("CET");
206             }
207         };
208 
209         Calendar expected = Calendar.getInstance();
210         expected.setTimeZone(TimeZone.getTimeZone("CET"));
211         expected.setTime(new Date(0));
212         expected.set(Calendar.HOUR_OF_DAY, 8);
213         expected.set(Calendar.MINUTE, 9);
214         expected.set(Calendar.SECOND, 10);
215 
216         assertEquals(type.valueOf("08:09:10").getTimeInMillis(), expected.getTimeInMillis());
217         assertEquals(type.valueOf("08:09:10+0100").getTimeInMillis(), expected.getTimeInMillis());
218         assertEquals(type.getString(expected), "08:09:10+0100");
219 
220     }
221 
222     @Test
223     public void base64() {
224         Base64Datatype type = (Base64Datatype) Datatype.Builtin.BIN_BASE64.getDatatype();
225         assert Arrays.equals(type.valueOf("a1b2"), new byte[]{107, 86, -10});
226         assert type.getString(new byte[]{107, 86, -10}).equals("a1b2");
227     }
228 
229     @Test
230     public void simpleCSV() {
231         List<String> csv = new CSVString("foo,bar,baz");
232         assert csv.size() == 3;
233         assert csv.get(0).equals("foo");
234         assert csv.get(1).equals("bar");
235         assert csv.get(2).equals("baz");
236         assert csv.toString().equals("foo,bar,baz");
237 
238         csv = new CSVString("f\\\\oo,b\\,ar,b\\\\az");
239         assert csv.size() == 3;
240         assertEquals(csv.get(0), "f\\oo");
241         assertEquals(csv.get(1), "b,ar");
242         assertEquals(csv.get(2), "b\\az");
243 
244         List<Boolean> csvBoolean = new CSVBoolean("1,1,0");
245         assert csvBoolean.size() == 3;
246         assertEquals(csvBoolean.get(0), new Boolean(true));
247         assertEquals(csvBoolean.get(1), new Boolean(true));
248         assertEquals(csvBoolean.get(2), new Boolean(false));
249         assertEquals(csvBoolean.toString(), "1,1,0");
250     }
251 
252     @Test
253     public void parseDLNADoc() {
254         DLNADoc doc = DLNADoc.valueOf("DMS-1.50");
255         assertEquals(doc.getDevClass(), "DMS");
256         assertEquals(doc.getVersion(), DLNADoc.Version.V1_5.toString());
257         assertEquals(doc.toString(), "DMS-1.50");
258 
259         doc = DLNADoc.valueOf("M-DMS-1.50");
260         assertEquals(doc.getDevClass(), "M-DMS");
261         assertEquals(doc.getVersion(), DLNADoc.Version.V1_5.toString());
262         assertEquals(doc.toString(), "M-DMS-1.50");
263     }
264 
265     @Test
266     public void caseSensitivity() {
267         Datatype.Builtin dt = Datatype.Builtin.getByDescriptorName("datetime");
268         assert dt != null;
269         dt = Datatype.Builtin.getByDescriptorName("dateTime");
270         assert dt != null;
271         dt = Datatype.Builtin.getByDescriptorName("DATETIME");
272         assert dt != null;
273     }
274 
275     @Test
276     public void valueOfDouble() {
277         DoubleDatatype dt = (DoubleDatatype)Datatype.Builtin.R8.getDatatype();
278         Double d = dt.valueOf("1.23");
279         assertEquals(d, 1.23d);
280     }
281 
282     @Test
283     public void valueOfFloat() {
284         FloatDatatype dt = (FloatDatatype)Datatype.Builtin.R4.getDatatype();
285         Float f = dt.valueOf("1.23456");
286         assertEquals(f, 1.23456f);
287     }
288 
289 }