4
4
import me .jahnen .libaums .core .driver .ByteBlockDevice ;
5
5
import me .jahnen .libaums .core .driver .file .FileBlockDeviceDriver ;
6
6
import me .jahnen .libaums .core .fs .fat32 .Fat32FileSystem ;
7
+ import me .jahnen .libaums .core .fs .fat32 .Fat32FileSystemCreator ;
7
8
import me .jahnen .libaums .core .partition .PartitionTableEntry ;
8
9
import me .jahnen .libaums .core .partition .PartitionTypes ;
9
10
10
11
import org .junit .Test ;
11
-
12
12
import java .net .URL ;
13
+ import java .util .ArrayList ;
14
+ import java .util .Arrays ;
13
15
16
+ import static org .junit .Assert .assertEquals ;
14
17
import static org .junit .Assert .assertTrue ;
15
18
16
19
/**
19
22
public class FileSystemFactoryTest {
20
23
@ Test
21
24
public void createFat32FileSystem () throws Exception {
25
+ BlockDeviceDriver blockDevice = createDevice ();
26
+ PartitionTableEntry entry = createPartitionTable ();
27
+ FileSystem fs = FileSystemFactory .INSTANCE .createFileSystem (entry , blockDevice );
28
+
29
+ assertTrue (fs instanceof Fat32FileSystem );
30
+ }
31
+
32
+ @ Test
33
+ public void fileSystemPriority () throws Exception {
34
+ ArrayList <String > orderTracker = new ArrayList <>();
35
+
36
+ // Clear and register with varying priorities to verify creators are invoked in expected order
37
+ FileSystemFactory .clearFileSystems ();
38
+ FileSystemFactory .registerFileSystem (mockCreator (orderTracker ,"not called" ), FileSystemFactory .DEFAULT_PRIORITY + 4 );
39
+ FileSystemFactory .registerFileSystem (mockCreator (orderTracker ,"third" ), FileSystemFactory .DEFAULT_PRIORITY + 1 );
40
+ FileSystemFactory .registerFileSystem (mockCreator (orderTracker ,"first" ));
41
+ FileSystemFactory .registerFileSystem (mockCreator (orderTracker ,"fourth" ), FileSystemFactory .DEFAULT_PRIORITY + 2 );
42
+ FileSystemFactory .registerFileSystem (mockCreator (orderTracker ,"second" ));
43
+ FileSystemFactory .registerFileSystem (new Fat32FileSystemCreator (), FileSystemFactory .DEFAULT_PRIORITY + 3 );
44
+ FileSystemFactory .registerFileSystem (mockCreator (orderTracker ,"not called" ), FileSystemFactory .DEFAULT_PRIORITY + 5 );
45
+
46
+ BlockDeviceDriver blockDevice = createDevice ();
47
+ PartitionTableEntry entry = createPartitionTable ();
48
+ FileSystem fs = FileSystemFactory .INSTANCE .createFileSystem (entry , blockDevice );
49
+
50
+ assertEquals (orderTracker , Arrays .asList ("first" , "second" , "third" , "fourth" ));
51
+ assertTrue (fs instanceof Fat32FileSystem );
52
+
53
+ // Since this is a singleton try to return it to its original state for other tests
54
+ FileSystemFactory .clearFileSystems ();
55
+ FileSystemFactory .registerFileSystem (new Fat32FileSystemCreator (), FileSystemFactory .DEFAULT_PRIORITY + 1 );
56
+ }
57
+
58
+ private FileSystemCreator mockCreator (ArrayList <String > orderTracker , String name ) {
59
+ return (entry , blockDevice ) -> {
60
+ orderTracker .add (name );
61
+ return null ;
62
+ };
63
+ }
64
+
65
+ private BlockDeviceDriver createDevice () throws Exception {
22
66
BlockDeviceDriver blockDevice = new ByteBlockDevice (new FileBlockDeviceDriver (
23
67
new URL ("https://www.dropbox.com/s/3bxngiqmwitlucd/mbr_fat32.img?dl=1" ),
24
68
2 * 512 ));
25
69
blockDevice .init ();
26
70
27
- PartitionTableEntry entry = new PartitionTableEntry (PartitionTypes .FAT32 , 2 * 512 , 1337 );
28
- FileSystem fs = FileSystemFactory .INSTANCE .createFileSystem (entry , blockDevice );
29
-
30
- assertTrue (fs instanceof Fat32FileSystem );
71
+ return blockDevice ;
31
72
}
32
73
74
+ private PartitionTableEntry createPartitionTable () {
75
+ return new PartitionTableEntry (PartitionTypes .FAT32 , 2 * 512 , 1337 );
76
+ }
33
77
}
0 commit comments