View Javadoc
1   package org.djutils.reflection;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertTrue;
5   
6   import java.io.File;
7   import java.net.MalformedURLException;
8   import java.net.URISyntaxException;
9   import java.net.URL;
10  import java.text.ParseException;
11  import java.text.SimpleDateFormat;
12  import java.util.Date;
13  
14  import org.djutils.io.URLResource;
15  import org.djutils.reflection.ClassUtil.ClassFileDescriptor;
16  
17  /**
18   * ClassFileDescriptorTest.java. <br>
19   * <br>
20   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
21   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
22   * source code and binary code of this software is proprietary information of Delft University of Technology.
23   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
24   */
25  public class ClassFileDescriptorTest
26  {
27      /**
28       * Test the classFileDescriptor method in ClassUtil.
29       * @throws ParseException on error
30       * @throws MalformedURLException on error
31       */
32      // TODO: @Test test fails under Ubuntu
33      public void classFileDescriptorTest() throws ParseException, MalformedURLException
34      {
35          URL cfdClassURL = URLResource.getResource("/org/djutils-test-resources/test/Test.class");
36          // change the last accessed date
37          SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
38          File fileClass = new File(cfdClassURL.getPath());
39          fileClass.setLastModified(formatter.parse("2000-01-01 01:02:03").getTime());
40  
41          ClassFileDescriptor cfdClass = ClassUtil.classFileDescriptor(cfdClassURL);
42          assertEquals("Test.class", cfdClass.getName());
43          Date cfdClassDate = new Date(cfdClass.getLastChangedDate());
44          assertEquals("2000-01-01 01:02:03", formatter.format(cfdClassDate));
45  
46          assertTrue(
47                  cfdClass.getPath().toString().replaceAll("\\\\", "/").endsWith("/org/djutils-test-resources/test/Test.class"));
48          assertTrue(cfdClass.toString().startsWith("ClassFileDescriptor ["));
49  
50          URL cfdJarURL = URLResource.getResource("/org/djutils-test-resources/test/Test.jar");
51          // change the last accessed date of the jar -- not of the file in the jar
52          File fileJar = new File(cfdJarURL.getPath());
53          fileJar.setLastModified(formatter.parse("2010-11-12 01:02:03").getTime());
54  
55          ClassFileDescriptor cfdJar = ClassUtil.classFileDescriptor(cfdJarURL);
56          assertEquals("Test.jar", cfdJar.getName());
57          Date cfdJarDate = new Date(cfdJar.getLastChangedDate());
58          assertEquals("2010-11-12 01:02:03", formatter.format(cfdJarDate));
59  
60          // find the file in the jar
61          URL cfdJarFileURL = new URL("jar:file:" + cfdJarURL.getPath() + "!/Test.class");
62          ClassFileDescriptor cfdJarFile = ClassUtil.classFileDescriptor(cfdJarFileURL);
63          assertEquals("Test.class", cfdJarFile.getName());
64          Date cfdJarFileDate = new Date(cfdJarFile.getLastChangedDate());
65          assertEquals("2000-01-01 01:02:03", formatter.format(cfdJarFileDate));
66  
67          // if the file in the jar cannot be found, we should get the file descriptor of the jar itself
68          URL cfdJarFileURL2 = new URL("jar:file:" + cfdJarURL.getPath() + "!/TestXYZ.class");
69          ClassFileDescriptor cfdJarFile2 = ClassUtil.classFileDescriptor(cfdJarFileURL2);
70          assertEquals("Test.jar", cfdJarFile2.getName());
71          Date cfdJarFileDate2 = new Date(cfdJarFile2.getLastChangedDate());
72          assertEquals("2010-11-12 01:02:03", formatter.format(cfdJarFileDate2));
73      }
74  
75      /**
76       * Test the classFileDescriptor method in ClassUtil.
77       * @throws ParseException on error
78       * @throws MalformedURLException on error
79       * @throws URISyntaxException on error
80       */
81      // TODO: @Test test fails under Ubuntu
82      public void classFileDescriptorTestWithSpaces() throws ParseException, MalformedURLException, URISyntaxException
83      {
84          URL cfdClassURL = URLResource.getResource("/org/djutils-test-resources/test folder/Test.class");
85          // change the last accessed date
86          SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
87          File fileClass = new File(cfdClassURL.toURI());
88          assertTrue(fileClass.exists());
89          fileClass.setLastModified(formatter.parse("2000-01-01 01:02:03").getTime());
90  
91          ClassFileDescriptor cfdClass = ClassUtil.classFileDescriptor(cfdClassURL);
92          assertEquals("Test.class", cfdClass.getName());
93          Date cfdClassDate = new Date(cfdClass.getLastChangedDate());
94          assertEquals("2000-01-01 01:02:03", formatter.format(cfdClassDate));
95  
96          URL cfdJarURL = URLResource.getResource("/org/djutils-test-resources/test folder/Test.jar");
97          // change the last accessed date of the jar -- not of the file in the jar
98          File fileJar = new File(cfdJarURL.toURI());
99          fileJar.setLastModified(formatter.parse("2010-11-12 01:02:03").getTime());
100 
101         ClassFileDescriptor cfdJar = ClassUtil.classFileDescriptor(cfdJarURL);
102         assertEquals("Test.jar", cfdJar.getName());
103         Date cfdJarDate = new Date(cfdJar.getLastChangedDate());
104         assertEquals("2010-11-12 01:02:03", formatter.format(cfdJarDate));
105 
106         // find the file in the jar
107         URL cfdJarFileURL = new URL("jar:file:" + cfdJarURL.getPath() + "!/Test.class");
108         ClassFileDescriptor cfdJarFile = ClassUtil.classFileDescriptor(cfdJarFileURL);
109         assertEquals("Test.class", cfdJarFile.getName());
110         Date cfdJarFileDate = new Date(cfdJarFile.getLastChangedDate());
111         assertEquals("2000-01-01 01:02:03", formatter.format(cfdJarFileDate));
112 
113         // if the file in the jar cannot be found, we should get the file descriptor of the jar itself
114         URL cfdJarFileURL2 = new URL("jar:file:" + cfdJarURL.getPath() + "!/TestXYZ.class");
115         ClassFileDescriptor cfdJarFile2 = ClassUtil.classFileDescriptor(cfdJarFileURL2);
116         assertEquals("Test.jar", cfdJarFile2.getName());
117         Date cfdJarFileDate2 = new Date(cfdJarFile2.getLastChangedDate());
118         assertEquals("2010-11-12 01:02:03", formatter.format(cfdJarFileDate2));
119     }
120 
121 }