Skip to content

Commit d8e86d4

Browse files
committed
AbstractJFile test coverage
1 parent 0c086ab commit d8e86d4

File tree

2 files changed

+196
-6
lines changed

2 files changed

+196
-6
lines changed

fj-core-jvfs/src/main/java/org/fugerit/java/core/jvfs/helpers/AbstractJFile.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.Arrays;
1111
import java.util.List;
1212

13-
import org.fugerit.java.core.cfg.ConfigRuntimeException;
13+
import org.fugerit.java.core.function.SafeFunction;
1414
import org.fugerit.java.core.jvfs.JFile;
1515
import org.fugerit.java.core.jvfs.JVFS;
1616

@@ -130,11 +130,8 @@ public List<JFile> lsFiles() throws IOException {
130130

131131
@Override
132132
public String describe() {
133-
try {
134-
return this.getClass().getSimpleName()+"[path:"+this.getPath()+",isDirectory:"+this.isDirectory()+",isFile:"+this.isFile()+"]";
135-
} catch (IOException e) {
136-
throw new ConfigRuntimeException( e );
137-
}
133+
// see SafeFunction.get() [if any Exception is thrown, it will be wrapped by a ConfigRuntimeException]
134+
return SafeFunction.get( () -> this.getClass().getSimpleName()+"[path:"+this.getPath()+",isDirectory:"+this.isDirectory()+",isFile:"+this.isFile()+"]" );
138135
}
139136

140137
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package test.org.fugerit.java.core.jvfs.db.helper;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
import java.io.Reader;
10+
import java.io.StringReader;
11+
import java.io.Writer;
12+
import java.util.Arrays;
13+
import java.util.stream.Collectors;
14+
15+
import org.fugerit.java.core.cfg.ConfigRuntimeException;
16+
import org.fugerit.java.core.function.SafeFunction;
17+
import org.fugerit.java.core.io.StreamIO;
18+
import org.fugerit.java.core.jvfs.JFile;
19+
import org.fugerit.java.core.jvfs.JMount;
20+
import org.fugerit.java.core.jvfs.JVFS;
21+
import org.fugerit.java.core.jvfs.JVFSImpl;
22+
import org.fugerit.java.core.jvfs.helpers.AbstractJFile;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
import lombok.extern.slf4j.Slf4j;
27+
28+
@Slf4j
29+
public class TestAbstractJFile {
30+
31+
@Test
32+
public void test1() throws IOException {
33+
File baseFolder = new File( "target/test_abstract_j_file" );
34+
if ( !baseFolder.exists() ) {
35+
baseFolder.mkdirs();
36+
}
37+
JVFS jvfs = TestJMount.createJVFS( baseFolder );
38+
JFile file = jvfs.getJFile( "/abstrat.txt" );
39+
String test = "test";
40+
try ( Reader reader = new StringReader( test );
41+
Writer writer = file.getWriter() ) {
42+
StreamIO.pipeChar( reader , writer, StreamIO.MODE_CLOSE_NONE );
43+
}
44+
try ( Reader reader = file.getReader() ) {
45+
String testRead = StreamIO.readString( reader );
46+
Assert.assertEquals( test , testRead );
47+
}
48+
log.info( "describe : {}", file.describe() );
49+
Assert.assertEquals( test.length() , file.getSupposedSize() );
50+
Arrays.asList( jvfs.getRoot().list() ).stream().forEach( f -> log.info( "kid : {}", f ) );
51+
log.info( "normalized path : {}", AbstractJFile.normalizePath( file.getPath() ) );
52+
log.info( "normalized path : {}", AbstractJFile.normalizePath( jvfs.getRoot().getPath() ) );
53+
Assert.assertTrue( file.isFile() );
54+
Assert.assertTrue( jvfs.getRoot().isDirectory() );
55+
}
56+
57+
}
58+
59+
class TestJMount implements JMount {
60+
61+
public static JVFS createDefaultJVFS() {
62+
JVFS jvfs = null;
63+
try {
64+
jvfs = createJVFS(new File("/"));
65+
} catch (IOException e) {
66+
throw ConfigRuntimeException.convertExMethod( "createDefaultJVFS", e );
67+
}
68+
return jvfs;
69+
}
70+
71+
public static JVFS createJVFS(File root) throws IOException {
72+
return JVFSImpl.getInstance(new TestJMount(root));
73+
}
74+
75+
public String toString() {
76+
return this.getClass().getSimpleName() + "[base:" + this.base + "]";
77+
}
78+
79+
private File base; // the base path for the JMount
80+
81+
public TestJMount(File base) {
82+
this.base = base;
83+
}
84+
85+
@Override
86+
public JFile getJFile(JVFS jvfs, String point, String relPath) {
87+
File file = new File(base, relPath);
88+
if ( point.endsWith( JFile.SEPARATOR ) ) {
89+
point = point.substring( 0, point.length()-JFile.SEPARATOR.length() );
90+
}
91+
String path = point+relPath;
92+
if ( file.isDirectory() && !path.endsWith( JFile.SEPARATOR ) ) {
93+
path+=JFile.SEPARATOR;
94+
}
95+
return (new TestJFile(path, jvfs, file));
96+
}
97+
98+
}
99+
100+
101+
class TestJFile extends AbstractJFile {
102+
103+
private File file;
104+
105+
protected TestJFile(String path, JVFS jvfs, File file) {
106+
super(path, jvfs);
107+
this.file = file;
108+
}
109+
110+
@Override
111+
public JFile[] listFiles() throws IOException {
112+
return Arrays.asList( this.file.listFiles() ).stream().map( f ->
113+
SafeFunction.get( () -> this.getChild( f.getName() ) )
114+
).collect( Collectors.toList() ).toArray( new JFile[0] );
115+
}
116+
117+
@Override
118+
public boolean delete() throws IOException {
119+
// will be implemented if needed for tests
120+
return false;
121+
}
122+
123+
@Override
124+
public boolean create() throws IOException {
125+
// will be implemented if needed for tests
126+
return false;
127+
}
128+
129+
@Override
130+
public boolean exists() throws IOException {
131+
// will be implemented if needed for tests
132+
return false;
133+
}
134+
135+
@Override
136+
public boolean mkdir() throws IOException {
137+
// will be implemented if needed for tests
138+
return false;
139+
}
140+
141+
@Override
142+
public boolean mkdirs() throws IOException {
143+
// will be implemented if needed for tests
144+
return false;
145+
}
146+
147+
@Override
148+
public InputStream getInputStream() throws IOException {
149+
return new FileInputStream(this.file);
150+
}
151+
152+
@Override
153+
public OutputStream getOutputStream() throws IOException {
154+
return new FileOutputStream(this.file);
155+
}
156+
157+
@Override
158+
public boolean isCanRead() throws IOException {
159+
// will be implemented if needed for tests
160+
return false;
161+
}
162+
163+
@Override
164+
public boolean isCanWrite() throws IOException {
165+
// will be implemented if needed for tests
166+
return false;
167+
}
168+
169+
@Override
170+
public void setReadOnly(boolean readOnly) throws IOException {
171+
// will be implemented if needed for tests
172+
173+
}
174+
175+
@Override
176+
public long getLastModified() throws IOException {
177+
// will be implemented if needed for tests
178+
return 0;
179+
}
180+
181+
@Override
182+
public void setLastModified(long time) throws IOException {
183+
// will be implemented if needed for tests
184+
185+
}
186+
187+
@Override
188+
public boolean rename(JFile newFile) throws IOException {
189+
// will be implemented if needed for tests
190+
return false;
191+
}
192+
193+
}

0 commit comments

Comments
 (0)