File tree Expand file tree Collapse file tree
main/java/com/baeldung/filesystem/jndi
java/com/baeldung/filesystem/jndi/test Expand file tree Collapse file tree Original file line number Diff line number Diff line change 4545 <artifactId >commons-math3</artifactId >
4646 <version >${commons-math3.version} </version >
4747 </dependency >
48-
48+
4949 <dependency >
50- <groupId >org.decimal4j</groupId >
51- <artifactId >decimal4j</artifactId >
52- <version >${decimal4j.version} </version >
50+ <groupId >org.decimal4j</groupId >
51+ <artifactId >decimal4j</artifactId >
52+ <version >${decimal4j.version} </version >
5353 </dependency >
5454
5555 <dependency >
177177 <version >2.1.0.1</version >
178178 </dependency >
179179
180+ <dependency >
181+ <groupId >com.sun.messaging.mq</groupId >
182+ <artifactId >fscontext</artifactId >
183+ <version >${fscontext.version} </version >
184+ </dependency >
180185 </dependencies >
181186
182187 <build >
382387 <unix4j .version>0.4</unix4j .version>
383388 <grep4j .version>1.8.7</grep4j .version>
384389 <lombok .version>1.16.12</lombok .version>
390+ <fscontext .version>4.6-b01</fscontext .version>
385391
386392 <!-- testing -->
387393 <org .hamcrest.version>1.3</org .hamcrest.version>
Original file line number Diff line number Diff line change 1+ package com .baeldung .filesystem .jndi ;
2+
3+ import java .io .File ;
4+ import java .util .Hashtable ;
5+
6+ import javax .naming .Context ;
7+ import javax .naming .InitialContext ;
8+ import javax .naming .NamingException ;
9+
10+ public class LookupFSJNDI {
11+ InitialContext ctx = null ;
12+
13+ public LookupFSJNDI () throws NamingException {
14+ super ();
15+ init ();
16+ }
17+
18+ private void init () throws NamingException {
19+ Hashtable <String , String > env = new Hashtable <String , String >();
20+
21+ env .put (Context .INITIAL_CONTEXT_FACTORY ,
22+ "com.sun.jndi.fscontext.RefFSContextFactory" );
23+ // URI to namespace (actual directory)
24+ env .put (Context .PROVIDER_URL , "file:./src/test/resources" );
25+
26+ ctx = new InitialContext (env );
27+ }
28+
29+ public InitialContext getCtx () {
30+ return ctx ;
31+ }
32+
33+ public File getFile (String fileName ) {
34+ File file ;
35+ try {
36+ file = (File )getCtx ().lookup (fileName );
37+ } catch (NamingException e ) {
38+ file = null ;
39+ }
40+ return file ;
41+ }
42+
43+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .filesystem .jndi .test ;
2+
3+ import static org .junit .Assert .assertNotNull ;
4+
5+ import java .io .File ;
6+
7+ import javax .naming .InitialContext ;
8+ import javax .naming .NamingException ;
9+
10+ import org .junit .Test ;
11+
12+ import com .baeldung .filesystem .jndi .LookupFSJNDI ;
13+
14+ public class LookupFSJNDITest {
15+ LookupFSJNDI fsjndi ;
16+ File file ;
17+ InitialContext ctx = null ;
18+ final String FILENAME = "test.find" ;
19+
20+ public LookupFSJNDITest () {
21+ try {
22+ fsjndi = new LookupFSJNDI ();
23+ } catch (NamingException e ) {
24+ fsjndi = null ;
25+ }
26+ }
27+
28+ @ Test
29+ public void whenInitializationLookupFSJNDIIsNotNull_thenSuccess () {
30+ assertNotNull ("Class LookupFSJNDI has instance" , fsjndi );
31+ }
32+
33+ @ Test
34+ public void givenLookupFSJNDI_whengetInitialContextIsNotNull_thenSuccess () {
35+ ctx = fsjndi .getCtx ();
36+ assertNotNull ("Context exists" , ctx );
37+ }
38+
39+ @ Test
40+ public void givenInitialContext_whenLokupFileExists_thenSuccess () {
41+ File file = fsjndi .getFile (FILENAME );
42+ assertNotNull ("File exists" , file );
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ Test of JNDI on file.
You can’t perform that action at this time.
0 commit comments