3

我正在尝试测试一些使用 Blobstore API 的代码,但我并没有真正了解如何将一些文件放入 Blobstore。以下不起作用:

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;
}

我的配置:

new LocalServiceTestHelper(
    new LocalBlobstoreServiceTestConfig()
        //.setNoStorage(true)
        .setBackingStoreLocation("war/WEB-INF/appengine-generated"),
    new LocalFileServiceTestConfig()
).setUp();

有任何想法吗?

4

1 回答 1

3

以下测试运行成功

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;
  }
}

两个修改:

  • 用户 LocalBlobstoreServiceTestConfig 而不是 LocalFileServiceTestConfig
  • writeChannel.closeFinally(); 而不是 writeChannel.close()

希望这有帮助。

于 2012-09-12T04:48:03.357 回答