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