本文介绍了使用JUnit中的Blobstore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图测试一些使用Blobstore API的代码,但我并没有真正了解我如何将一些文件放入Blobstore。以下不起作用:
私人BlobKey createBlob(字符串路径)抛出异常{
FileService fileService = FileServiceFactory.getFileService ();
AppEngineFile file = fileService.createNewBlobFile(foobar);
FileWriteChannel writeChannel = fileService.openWriteChannel(file,true);
OutputStream输出= Channels.newOutputStream(writeChannel);
//复制文件,番石榴样式
InputStream input = new FileInputStream(path);
assertNotNull(input);
ByteStreams.copy(输入,输出);
input.close();
//以防万一...
output.flush();
output.close();
writeChannel.close();
//不工作!!!
BlobKey blobKey = fileService.getBlobKey(file);
assertNotNull(blobKey);
返回blobKey;
}
我的配置:
new LocalServiceTestHelper(
new LocalBlobstoreServiceTestConfig()
//.setNoStorage(true)
.setBackingStoreLocation(war / WEB-INF / appengine-generated ),
new LocalFileServiceTestConfig()
).setUp();
有什么想法?
解决方案下面的测试运行成功
public class TestBlobstore {
private static final LocalServiceTestHelper helper =
新的LocalServiceTestHelper(新的LocalDatastoreServiceTestConfig(),
新的LocalBlobstoreServiceTestConfig()
);
public TestBlobstore(){
}
@Before
public void setUp(){
helper.setUp();
}
@Test
public void testBlobstore()抛出异常{
System.out.println(createBlob(test.txt));
私有BlobKey createBlob(String path)抛出异常{
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(foobar);
FileWriteChannel writeChannel = fileService.openWriteChannel(file,true);
OutputStream输出= Channels.newOutputStream(writeChannel);
//复制文件,番石榴样式
InputStream input = new FileInputStream(path);
assertNotNull(input);
ByteStreams.copy(输入,输出);
input.close();
//以防万一...
output.flush();
output.close();
writeChannel.closeFinally();
//不工作!!!
BlobKey blobKey = fileService.getBlobKey(file);
assertNotNull(blobKey);
返回blobKey;
$ / code $ / pre
$ b $ p 两个修改:
- 用户LocalBlobstoreServiceTestConfig代替LocalFileServiceTestConfig
- writeChannel.closeFinally();而不是writeChannel.close()
希望得到这个帮助。
I am trying to test some code that uses the Blobstore API, but I don't really get how I am expected to get some files into the blobstore. The following is not working:
private BlobKey createBlob(String path) throws Exception {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile("foobar");
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
OutputStream output = Channels.newOutputStream(writeChannel);
// copy files, guava-style
InputStream input = new FileInputStream(path);
assertNotNull(input);
ByteStreams.copy(input, output);
input.close();
// just in case...
output.flush();
output.close();
writeChannel.close();
// U NO WORK!!!
BlobKey blobKey = fileService.getBlobKey(file);
assertNotNull(blobKey);
return blobKey;
}
My config:
new LocalServiceTestHelper(
new LocalBlobstoreServiceTestConfig()
//.setNoStorage(true)
.setBackingStoreLocation("war/WEB-INF/appengine-generated"),
new LocalFileServiceTestConfig()
).setUp();
Any ideas?
解决方案 The following test run successfully
public class TestBlobstore {
private static final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig(),
new LocalBlobstoreServiceTestConfig()
);
public TestBlobstore() {
}
@Before
public void setUp() {
helper.setUp();
}
@Test
public void testBlobstore() throws Exception {
System.out.println(createBlob("test.txt"));
}
private BlobKey createBlob(String path) throws Exception {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile("foobar");
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
OutputStream output = Channels.newOutputStream(writeChannel);
// copy files, guava-style
InputStream input = new FileInputStream(path);
assertNotNull(input);
ByteStreams.copy(input, output);
input.close();
// just in case...
output.flush();
output.close();
writeChannel.closeFinally();
// U NO WORK!!!
BlobKey blobKey = fileService.getBlobKey(file);
assertNotNull(blobKey);
return blobKey;
}
}
Two modifications:
- User LocalBlobstoreServiceTestConfig instead of LocalFileServiceTestConfig
- writeChannel.closeFinally(); instead of writeChannel.close()
Hope this help.
这篇关于使用JUnit中的Blobstore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-02 01:34