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
19
20
21
22
23
24
25 public class ClassFileDescriptorTest
26 {
27
28
29
30
31
32
33 public void classFileDescriptorTest() throws ParseException, MalformedURLException
34 {
35 URL cfdClassURL = URLResource.getResource("/org/djutils-test-resources/test/Test.class");
36
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
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
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
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
77
78
79
80
81
82 public void classFileDescriptorTestWithSpaces() throws ParseException, MalformedURLException, URISyntaxException
83 {
84 URL cfdClassURL = URLResource.getResource("/org/djutils-test-resources/test folder/Test.class");
85
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
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
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
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 }