View Javadoc
1   package org.djutils.metadata;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertNotEquals;
5   import static org.junit.Assert.assertTrue;
6   import static org.junit.Assert.fail;
7   
8   import org.junit.Test;
9   
10  /**
11   * MetaDataTest.java. <br>
12   * <br>
13   * Copyright (c) 2020-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
14   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
15   * distributed under a three-clause BSD-style license, which can be found at
16   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. <br>
17   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19   */
20  public class MetaDataTest
21  {
22  
23      /**
24       * Test the ObjectDescriptor class.
25       */
26      @Test
27      public void testObjectDescriptor()
28      {
29          ObjectDescriptor objectDescriptor = new ObjectDescriptor("name", "description", Integer.class);
30          assertEquals("name", "name", objectDescriptor.getName());
31          assertEquals("description", "description", objectDescriptor.getDescription());
32          assertEquals("class", Integer.class, objectDescriptor.getObjectClass());
33          assertEquals(objectDescriptor, objectDescriptor);
34          assertEquals(objectDescriptor.hashCode(), objectDescriptor.hashCode());
35          assertEquals(objectDescriptor, new ObjectDescriptor("name", "description", Integer.class));
36          assertNotEquals(objectDescriptor, null);
37          assertNotEquals(objectDescriptor, new Object());
38          assertNotEquals(objectDescriptor, new ObjectDescriptor("x", "description", Integer.class));
39          assertNotEquals(objectDescriptor, new ObjectDescriptor("name", "x", Integer.class));
40          assertNotEquals(objectDescriptor, new ObjectDescriptor("name", "description", Double.class));
41          try
42          {
43              new ObjectDescriptor(null, "description", Integer.class);
44              fail("null name should have thrown a NullPointerException");
45          }
46          catch (NullPointerException npe)
47          {
48              // Ignore expected exception
49          }
50  
51          try
52          {
53              new ObjectDescriptor("", "description", Integer.class);
54              fail("empty name should have thrown an IllegalArgumentException");
55          }
56          catch (IllegalArgumentException npe)
57          {
58              // Ignore expected exception
59          }
60  
61          try
62          {
63              new ObjectDescriptor("name", null, Integer.class);
64              fail("null description should have thrown a NullPointerException");
65          }
66          catch (NullPointerException npe)
67          {
68              // Ignore expected exception
69          }
70  
71          try
72          {
73              new ObjectDescriptor("name", "description", null);
74              fail("null class should have thrown a NullPointerException");
75          }
76          catch (NullPointerException npe)
77          {
78              // Ignore expected exception
79          }
80  
81          assertTrue("toString returns something descriptive", objectDescriptor.toString().startsWith("ObjectDescriptor"));
82      }
83  
84      /**
85       * Test the MetaData class.
86       */
87      @Test
88      @SuppressWarnings("checkstyle:methodlength")
89      public void testMetaData()
90      {
91          // Construct a MetaData object that specifies a String and a Double.
92          MetaData metaData = new MetaData("meta data name", "meta data description",
93                  new ObjectDescriptor("string", "the string", String.class),
94                  new ObjectDescriptor("length", "the length", Double.class));
95          assertEquals("name", "meta data name", metaData.getName());
96          assertEquals("description", "meta data description", metaData.getDescription());
97          assertEquals("size", 2, metaData.size());
98          assertEquals("name of element 0", "string", metaData.getFieldName(0));
99          assertEquals("name of element 1", "length", metaData.getFieldName(1));
100         assertEquals("description of element 0", "the string", metaData.getObjectDescription(0));
101         assertEquals("description of element 1", "the length", metaData.getObjectDescription(1));
102         assertEquals("class of element 0", String.class, metaData.getObjectClass(0));
103         assertEquals("class of element 1", Double.class, metaData.getObjectClass(1));
104 
105         ObjectDescriptor[] descriptors = metaData.getObjectDescriptors();
106         assertEquals(2, descriptors.length);
107         assertEquals(new ObjectDescriptor("string", "the string", String.class), descriptors[0]);
108         MetaData metaData2 = new MetaData("meta data name", "meta data description",
109                 new ObjectDescriptor[] {new ObjectDescriptor("string", "the string", String.class),
110                         new ObjectDescriptor("length", "the length", Double.class)});
111         assertEquals(metaData, metaData2);
112         assertEquals(metaData.hashCode(), metaData2.hashCode());
113         assertNotEquals(metaData, null);
114         assertNotEquals(metaData, new Object());
115         assertNotEquals(metaData, new MetaData("x", "x", new ObjectDescriptor[0]));
116         assertNotEquals(metaData, new MetaData("meta data name", "x", new ObjectDescriptor[0]));
117         assertNotEquals(metaData, new MetaData("meta data name", "meta data description", new ObjectDescriptor[0]));
118 
119         assertTrue("toString returns something descriptive", metaData.toString().startsWith("MetaData"));
120         metaData.verifyComposition(new Object[] {"TestString", 123.456});
121         metaData.verifyComposition(new Object[] {null, 123.456});
122         try
123         {
124             metaData.verifyComposition(new Object[] {"TestString"}); // too short
125             fail("Too short array should have thrown an IndexOutOfBoundsException");
126         }
127         catch (IndexOutOfBoundsException ioobe)
128         {
129             // Ignore expected exception
130         }
131 
132         try
133         {
134             metaData.verifyComposition(new Object[] {"TestString", 123.456, "too many"}); // too long
135             fail("Too long array should have thrown an IndexOutOfBoundsException");
136         }
137         catch (IndexOutOfBoundsException ioobe)
138         {
139             // Ignore expected exception
140         }
141 
142         try
143         {
144             metaData.verifyComposition(new Object[] {234.678, 123.456}); // element 0 not a String
145             fail("Wrong class should have thrown an ClassCastException");
146         }
147         catch (ClassCastException cce)
148         {
149             // Ignore expected exception
150         }
151 
152         try
153         {
154             metaData.verifyComposition(new Object[] {"TestString", "wrong class"}); // element 1 not a Double
155             fail("Wrong class should have thrown an IndexOutOfBoundsException");
156         }
157         catch (ClassCastException cce)
158         {
159             // Ignore expected exception
160         }
161 
162         try
163         {
164             metaData.getFieldName(-1);
165             fail("Bad index should have thrown an IndexOutOfBoundsException");
166         }
167         catch (IndexOutOfBoundsException ioobe)
168         {
169             // Ignore expected exception
170         }
171 
172         try
173         {
174             metaData.getFieldName(2);
175             fail("Bad index should have thrown an IndexOutOfBoundsException");
176         }
177         catch (IndexOutOfBoundsException ioobe)
178         {
179             // Ignore expected exception
180         }
181 
182         try
183         {
184             metaData.getObjectDescription(-1);
185             fail("Bad index should have thrown an IndexOutOfBoundsException");
186         }
187         catch (IndexOutOfBoundsException ioobe)
188         {
189             // Ignore expected exception
190         }
191 
192         try
193         {
194             metaData.getObjectDescription(2);
195             fail("Bad index should have thrown an IndexOutOfBoundsException");
196         }
197         catch (IndexOutOfBoundsException ioobe)
198         {
199             // Ignore expected exception
200         }
201 
202         try
203         {
204             metaData.getObjectClass(-1);
205             fail("Bad index should have thrown an IndexOutOfBoundsException");
206         }
207         catch (IndexOutOfBoundsException ioobe)
208         {
209             // Ignore expected exception
210         }
211 
212         try
213         {
214             metaData.getObjectClass(2);
215             fail("Bad index should have thrown an IndexOutOfBoundsException");
216         }
217         catch (IndexOutOfBoundsException ioobe)
218         {
219             // Ignore expected exception
220         }
221 
222         try
223         {
224             new MetaData(null, "description", new ObjectDescriptor[] {});
225             fail("null name should have thrown a NullPointerException");
226         }
227         catch (NullPointerException npe)
228         {
229             // Ignore expected exception
230         }
231 
232         try
233         {
234             new MetaData("name", null, new ObjectDescriptor[] {});
235             fail("null description should have thrown a NullPointerException");
236         }
237         catch (NullPointerException npe)
238         {
239             // Ignore expected exception
240         }
241 
242         try
243         {
244             new MetaData("name", "description", (ObjectDescriptor[]) null);
245             fail("null objectDescriptors should have thrown a NullPointerException");
246         }
247         catch (NullPointerException npe)
248         {
249             // Ignore expected exception
250         }
251 
252         try
253         {
254             new MetaData("name", "description", (ObjectDescriptor) null);
255             fail("null objectDescriptors should have thrown a NullPointerException");
256         }
257         catch (NullPointerException npe)
258         {
259             // Ignore expected exception
260         }
261 
262         try
263         {
264             new MetaData("name", "description", new ObjectDescriptor("string", "the string", String.class),
265                     new ObjectDescriptor("length", "the length", Double.class), (ObjectDescriptor) null);
266             fail("null objectDescriptor as part of array should have thrown a NullPointerException");
267         }
268         catch (NullPointerException npe)
269         {
270             // Ignore expected exception
271         }
272 
273         metaData = new MetaData("name", "description", new ObjectDescriptor("integer", "integers only please", Integer.class));
274         assertEquals("name", "name", metaData.getName());
275         assertEquals("description", "description", metaData.getDescription());
276         assertEquals("check name of object descriptor", "integer", metaData.getFieldName(0));
277         assertEquals("check description of object descriptor", "integers only please", metaData.getObjectDescription(0));
278         assertEquals("check class of object descriptor", Integer.class, metaData.getObjectClass(0));
279         assertEquals("size should be 1", 1, metaData.size());
280         try
281         {
282             metaData.getFieldName(-1);
283             fail("Bad index should have thrown an IndexOutOfBoundsException");
284         }
285         catch (IndexOutOfBoundsException ioobe)
286         {
287             // Ignore expected exception
288         }
289 
290         try
291         {
292             metaData.getFieldName(1);
293             fail("Bad index should have thrown an IndexOutOfBoundsException");
294         }
295         catch (IndexOutOfBoundsException ioobe)
296         {
297             // Ignore expected exception
298         }
299 
300         try
301         {
302             metaData.getObjectDescription(-1);
303             fail("Bad index should have thrown an IndexOutOfBoundsException");
304         }
305         catch (IndexOutOfBoundsException ioobe)
306         {
307             // Ignore expected exception
308         }
309 
310         try
311         {
312             metaData.getObjectDescription(1);
313             fail("Bad index should have thrown an IndexOutOfBoundsException");
314         }
315         catch (IndexOutOfBoundsException ioobe)
316         {
317             // Ignore expected exception
318         }
319 
320         try
321         {
322             metaData.getObjectClass(-1);
323             fail("Bad index should have thrown an IndexOutOfBoundsException");
324         }
325         catch (IndexOutOfBoundsException ioobe)
326         {
327             // Ignore expected exception
328         }
329 
330         try
331         {
332             metaData.getObjectClass(1);
333             fail("Bad index should have thrown an IndexOutOfBoundsException");
334         }
335         catch (IndexOutOfBoundsException ioobe)
336         {
337             // Ignore expected exception
338         }
339 
340         metaData = new MetaData("name", "description", new ObjectDescriptor[] {});
341         metaData.verifyComposition(null); // this null is allowed
342         metaData.verifyComposition(new Object[] {}); // empty Object array is too
343 
344         metaData = new MetaData("name", "description", new ObjectDescriptor("n", "d", Integer.class));
345         assertEquals("name", "name", metaData.getName());
346         assertEquals("description", "description", metaData.getDescription());
347         assertEquals("class", Integer.class, metaData.getObjectClass(0));
348         metaData.verifyComposition(123);
349         try
350         {
351             metaData.verifyComposition("wrong");
352             fail("Wrong class should have thrown a ClassCastException");
353         }
354         catch (ClassCastException cce)
355         {
356             // Ignore expected exception
357         }
358 
359         try
360         {
361             new MetaData("", "description", new ObjectDescriptor[] {});
362             fail("Empty name should have thrown an IllegalArgumentException");
363         }
364         catch (IllegalArgumentException iae)
365         {
366             // Ignore expected exception
367         }
368 
369         try
370         {
371             new MetaData("", "description", new ObjectDescriptor("name", "desc", String.class));
372             fail("Empty name should have thrown an IllegalArgumentException");
373         }
374         catch (IllegalArgumentException iae)
375         {
376             // Ignore expected exception
377         }
378 
379         MetaData.EMPTY.verifyComposition(null);
380         MetaData.EMPTY.verifyComposition(new Object[0]);
381         try
382         {
383             MetaData.EMPTY.verifyComposition("wrong");
384             fail("Should have thrown an IndexOutOfBoundsException");
385         }
386         catch (IndexOutOfBoundsException ioobe)
387         {
388             // Ignore expected exception
389         }
390 
391         MetaData.NO_META_DATA.verifyComposition(null);
392         MetaData.NO_META_DATA.verifyComposition(new Object[0]);
393         MetaData.NO_META_DATA.verifyComposition("OK");
394         MetaData.NO_META_DATA.verifyComposition(new Object[] {"OK", "Anything goes", 123f});
395 
396     }
397 
398 }