use of org.apache.commons.vfs2.FileContent in project pentaho-platform by pentaho.
the class FileObjectTestHelper method mockFile.
public static FileObject mockFile(final String contents, final boolean exists) throws FileSystemException {
FileObject fileObject = mock(FileObject.class);
when(fileObject.exists()).thenReturn(exists);
FileContent fileContent = mock(FileContent.class);
when(fileObject.getContent()).thenReturn(fileContent);
when(fileContent.getInputStream()).thenReturn(IOUtils.toInputStream(contents));
final FileObject parent = mock(FileObject.class);
when(fileObject.getParent()).thenReturn(parent);
final FileName fileName = mock(FileName.class);
when(parent.getName()).thenReturn(fileName);
when(fileName.getURI()).thenReturn("mondrian:/catalog");
return fileObject;
}
use of org.apache.commons.vfs2.FileContent in project zeppelin by apache.
the class VFSNotebookRepo method getNote.
private Note getNote(FileObject noteDir) throws IOException {
if (!isDirectory(noteDir)) {
throw new IOException(noteDir.getName().toString() + " is not a directory");
}
FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
if (!noteJson.exists()) {
throw new IOException(noteJson.getName().toString() + " not found");
}
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
FileContent content = noteJson.getContent();
InputStream ins = content.getInputStream();
String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
ins.close();
Note note = gson.fromJson(json, Note.class);
for (Paragraph p : note.getParagraphs()) {
if (p.getStatus() == Status.PENDING || p.getStatus() == Status.RUNNING) {
p.setStatus(Status.ABORT);
}
List<ApplicationState> appStates = p.getAllApplicationStates();
if (appStates != null) {
for (ApplicationState app : appStates) {
if (app.getStatus() != ApplicationState.Status.ERROR) {
app.setStatus(ApplicationState.Status.UNLOADED);
}
}
}
}
return note;
}
use of org.apache.commons.vfs2.FileContent in project pentaho-kettle by pentaho.
the class Log4jFileAppenderTest method before.
@Before
public void before() throws IOException {
outputStream = mock(OutputStream.class);
FileContent fileContent = mock(FileContent.class);
when(fileContent.getOutputStream(anyBoolean())).thenReturn(outputStream);
FileObject file = mock(FileObject.class);
when(file.getContent()).thenReturn(fileContent);
log4jFileAppender = new Log4jFileAppender(file);
}
use of org.apache.commons.vfs2.FileContent in project pentaho-platform by pentaho.
the class SolutionRepositoryVfsFileObjectTest method testContentRelatedMethods.
@Test
@SuppressWarnings({ "checkstyle:onestatementperline", "multiple statements help understand the mock definition" })
public void testContentRelatedMethods(@mockit.Mocked final RepositoryFile mockRepoFile, @mockit.Mocked final IAclNodeHelper mockAclHelper, @mockit.Mocked final IUnifiedRepository mockUnifiedRepository, @mockit.Mocked final SimpleRepositoryFileData mockFileData) throws FileSystemException {
String fileRef = "/etc/mondrian/SteelWheels/schema.xml";
new mockit.NonStrictExpectations() {
// CHECKSTYLE IGNORE check FOR NEXT 6 LINES
{
mockUnifiedRepository.getFile(anyString);
result = mockRepoFile;
mockUnifiedRepository.getDataForRead((Serializable) any, (Class) any);
result = mockFileData;
mockAclHelper.canAccess(mockRepoFile, EnumSet.of(RepositoryFilePermission.READ));
result = true;
mockFileData.getStream();
result = new ByteArrayInputStream("some string".getBytes());
}
};
Deencapsulation.setField(SolutionRepositoryVfsFileObject.class, "repository", mockUnifiedRepository);
SolutionRepositoryVfsFileObject solutionRepositoryVfsFileObject = new SolutionRepositoryVfsFileObject(fileRef);
Deencapsulation.setField(solutionRepositoryVfsFileObject, "aclHelper", mockAclHelper);
FileContent someFileContent = solutionRepositoryVfsFileObject.getContent();
assertThat(someFileContent, is(notNullValue()));
assertThat(solutionRepositoryVfsFileObject.isContentOpen(), is(false));
someFileContent.getInputStream();
assertThat(solutionRepositoryVfsFileObject.isContentOpen(), is(true));
someFileContent.close();
assertThat(solutionRepositoryVfsFileObject.isContentOpen(), is(false));
someFileContent = solutionRepositoryVfsFileObject.getContent();
someFileContent.getInputStream();
assertThat(solutionRepositoryVfsFileObject.isContentOpen(), is(true));
solutionRepositoryVfsFileObject.close();
assertThat(solutionRepositoryVfsFileObject.isContentOpen(), is(false));
}
use of org.apache.commons.vfs2.FileContent in project javautils by jiadongpo.
the class FtpVFS method testftp2.
public void testftp2() throws Exception {
FileSystemManager fsManager = VFS.getManager();
FileObject fo = fsManager.resolveFile("ftp://ci:Zj4xyBkgjd@10.151.30.10:21/apps/tomcat7-40-tomcat-air-ticket-merchant/logs");
// 得到远程文件列表
FileObject[] children = fo.getChildren();
for (int i = 0; i < children.length; i++) {
FileObject f = children[i];
FileContent c = f.getContent();
File localFile = new File(f.getName().getBaseName());
FileOutputStream out = new FileOutputStream(localFile);
// 写入本地
org.apache.commons.io.IOUtils.copy(c.getInputStream(), out);
// 或使用写入
FileObject obj = fsManager.resolveFile(this.getTargetResourceURL() + f.getName().getBaseName());
if (!obj.exists()) {
obj.createFile();
obj.copyFrom(f, Selectors.SELECT_SELF);
}
final long size = (f.getType() == FileType.FILE) ? c.getSize() : -1;
final long date = (f.getType() == FileType.FILE) ? c.getLastModifiedTime() : -1;
System.out.println(f.getName().getPath() + " date:" + date + " Size:" + size);
}
}
Aggregations