1+ package niwer .nebulae .icons ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
4+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5+ import static org .junit .jupiter .api .Assertions .assertTrue ;
6+
7+ import java .lang .reflect .Field ;
8+ import java .lang .reflect .InvocationTargetException ;
9+ import java .nio .file .Files ;
10+ import java .nio .file .Path ;
11+
12+ import org .junit .jupiter .api .AfterEach ;
13+ import org .junit .jupiter .api .Test ;
14+ import org .junit .jupiter .api .io .TempDir ;
15+
16+ class FileIconRegistryManagerTest {
17+
18+ @ TempDir
19+ Path tempDir ;
20+
21+ private final String originalOsName = System .getProperty ("os.name" );
22+ private final String originalUserHome = System .getProperty ("user.home" );
23+
24+ @ AfterEach
25+ void restoreOsName () {
26+ if (originalOsName != null ) {
27+ System .setProperty ("os.name" , originalOsName );
28+ }
29+ if (originalUserHome != null ) {
30+ System .setProperty ("user.home" , originalUserHome );
31+ }
32+ }
33+
34+ @ Test
35+ void registerUsesTheCurrentOperatingSystemHandler () {
36+ System .setProperty ("os.name" , "Mac OS X" );
37+
38+ assertThrows (UnsupportedOperationException .class , () -> FileIconRegistryManager .register (".txt" , NebulaeIcons .DATA ));
39+ }
40+
41+ @ Test
42+ void registerDelegatesToLinuxHandler () throws Exception {
43+ System .setProperty ("os.name" , "Linux" );
44+ System .setProperty ("user.home" , tempDir .toString ());
45+ resetIconStream (NebulaeIcons .IMAGE );
46+
47+ FileIconRegistryManager .register (".neb" , NebulaeIcons .IMAGE );
48+
49+ assertTrue (Files .exists (tempDir .resolve (".nebulae_register/icons/niwer.nebulae.neb.png" )));
50+ assertTrue (Files .exists (tempDir .resolve (".local/share/mime/packages/niwer.nebulae.neb.xml" )));
51+ assertTrue (Files .exists (tempDir .resolve (".local/share/icons/hicolor/256x256/mimetypes/application-x-niwer.nebulae.neb.png" )));
52+ }
53+
54+ @ Test
55+ void selectsWindowsHandlerWhenOsContainsWin () throws Exception {
56+ assertInstanceOf (WindowsFileIconHandler .class , createHandler ("Windows 11" ));
57+ }
58+
59+ @ Test
60+ void selectsLinuxHandlerWhenOsContainsLinux () throws Exception {
61+ assertInstanceOf (LinuxFileIconHandler .class , createHandler ("Linux" ));
62+ }
63+
64+ @ Test
65+ void selectsMacHandlerWhenOsContainsMac () throws Exception {
66+ assertInstanceOf (MacOSFileIconHandler .class , createHandler ("Mac OS X" ));
67+ }
68+
69+ @ Test
70+ void rejectsUnsupportedOperatingSystem () {
71+ InvocationTargetException exception = assertThrows (InvocationTargetException .class , () -> createHandler ("Plan9" ));
72+
73+ assertInstanceOf (UnsupportedOperationException .class , exception .getCause ());
74+ }
75+
76+ private static FileIconHandler createHandler (String osName ) throws Exception {
77+ System .setProperty ("os.name" , osName );
78+ return invokeCreateHandler ();
79+ }
80+
81+ private static FileIconHandler invokeCreateHandler () throws Exception {
82+ var method = FileIconRegistryManager .class .getDeclaredMethod ("createHandler" );
83+ method .setAccessible (true );
84+ return (FileIconHandler ) method .invoke (null );
85+ }
86+
87+ private static void resetIconStream (NebulaeIcons icon ) throws Exception {
88+ Field field = NebulaeIcons .class .getDeclaredField ("stream" );
89+ field .setAccessible (true );
90+ field .set (icon , null );
91+ }
92+ }
0 commit comments