File tree 2 files changed +50
-5
lines changed
main/java/org/fulib/scenarios/library
test/java/org/fulib/scenarios/library
2 files changed +50
-5
lines changed Original file line number Diff line number Diff line change @@ -16,21 +16,43 @@ public DirLibrary(File source)
16
16
17
17
// =============== Methods ===============
18
18
19
- private File getFile (String className )
19
+ private File getValidFileOrNull (String className )
20
20
{
21
- return new File (this .getSource ().getPath () + File .separatorChar + className + ".class" );
21
+ final String classFileName = className .replace ('/' , File .separatorChar ) + ".class" ;
22
+ final File file = new File (this .getSource (), classFileName );
23
+ if (!file .exists ())
24
+ {
25
+ return null ;
26
+ }
27
+
28
+ final String canonicalFile ;
29
+ try
30
+ {
31
+ canonicalFile = file .getCanonicalPath ();
32
+ }
33
+ catch (IOException e )
34
+ {
35
+ return null ;
36
+ }
37
+
38
+ // case sensitive check
39
+ if (!canonicalFile .endsWith (classFileName ))
40
+ {
41
+ return null ;
42
+ }
43
+ return file ;
22
44
}
23
45
24
46
@ Override
25
47
public boolean hasClass (String name )
26
48
{
27
- return this .getFile (name ). exists () ;
49
+ return this .getValidFileOrNull (name ) != null ;
28
50
}
29
51
30
52
@ Override
31
53
public InputStream loadClass (String name ) throws IOException
32
54
{
33
- final File file = this .getFile (name );
34
- return file . exists () ? new FileInputStream (file ) : null ;
55
+ final File file = this .getValidFileOrNull (name );
56
+ return file != null ? new FileInputStream (file ) : null ;
35
57
}
36
58
}
Original file line number Diff line number Diff line change
1
+ package org .fulib .scenarios .library ;
2
+
3
+ import org .junit .Test ;
4
+
5
+ import java .io .File ;
6
+
7
+ import static org .junit .Assert .*;
8
+
9
+ public class DirLibraryTest
10
+ {
11
+
12
+ @ Test
13
+ public void hasClass ()
14
+ {
15
+ final File classesDir = new File ("build/classes/java/test" );
16
+ final DirLibrary library = new DirLibrary (classesDir );
17
+
18
+ assertTrue (library .hasClass ("org/fulib/scenarios/library/DirLibraryTest" ));
19
+ assertFalse (library .hasClass ("org/fulib/scenarios/library/ClassThatDoesNotExist" ));
20
+ assertFalse (library .hasClass ("org/fulib/scenarios/library/dirlibrarytest" ));
21
+ assertFalse (library .hasClass ("Org/Fulib/Scenarios/Library/DirLibraryTest" ));
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments