Exemple #1
0
  public void testDeletion() {
    File f = null;
    try {
      f = File.createTempFile("com.google.gwt.dev.javac.impl.FileResourceTest", ".tmp");
      f.deleteOnExit();
      Util.writeStringAsFile(f, "contents 1");
    } catch (IOException e) {
      fail("Failed to create test file");
    }

    File dir = f.getParentFile();
    DirectoryClassPathEntry cpe = new DirectoryClassPathEntry(dir);
    FileResource r = new FileResource(cpe, f.getName(), f);
    assertEquals(f.getAbsoluteFile().toURI().toString(), r.getLocation());

    /*
     * In this case, there's no subdirectory, so the path should match the
     * simple filename.
     */
    assertEquals(f.getName(), r.getPath());

    // Delete the file.
    f.delete();

    /*
     *  The resource is no longer available.  Check to make sure we can't access its contents
     *  through the API.
     */
    InputStream in = null;
    try {
      in = r.openContents();
      fail("Open contents unexpectedly succeeded.");
    } catch (IOException expected) {
    }
  }
Exemple #2
0
 private void onNewFile(Path file) {
   FileResource resource = toFileResource(file);
   ResourceResolution resourceResolution = getPathPrefixSet().includesResource(resource.getPath());
   if (resourceResolution != null) {
     resolutionsByResource.put(resource, resourceResolution);
   }
 }
Exemple #3
0
 @Test
 @SuppressWarnings("unchecked")
 public void testFileResourceGetChildReturnsNull() throws IOException {
   File file = File.createTempFile("fileresourcetest", ".tmp");
   file.deleteOnExit();
   FileResource<?> fileResource = resourceFactory.create(FileResource.class, file);
   Assert.assertNull(fileResource.getChild("foo"));
 }
Exemple #4
0
 @Test
 @SuppressWarnings("unchecked")
 public void testDirectoryResourceReifyShouldRetunNullForFiles() throws IOException {
   File file = File.createTempFile("fileresourcetest", ".tmp");
   file.deleteOnExit();
   FileResource<?> fileResource = resourceFactory.create(FileResource.class, file);
   Assert.assertNotNull(fileResource);
   Assert.assertNull(fileResource.reify(DirectoryResource.class));
 }
  @Override
  public Response<List<BlameInfo>> apply(FileResource resource)
      throws RestApiException, OrmException, IOException, InvalidChangeOperationException {
    if (!allowBlame) {
      throw new BadRequestException("blame is disabled");
    }

    Project.NameKey project = resource.getRevision().getChange().getProject();
    try (Repository repository = repoManager.openRepository(project);
        ObjectInserter ins = repository.newObjectInserter();
        RevWalk revWalk = new RevWalk(ins.newReader())) {
      String refName =
          resource.getRevision().getEdit().isPresent()
              ? resource.getRevision().getEdit().get().getRefName()
              : resource.getRevision().getPatchSet().getRefName();

      Ref ref = repository.findRef(refName);
      if (ref == null) {
        throw new ResourceNotFoundException("unknown ref " + refName);
      }
      ObjectId objectId = ref.getObjectId();
      RevCommit revCommit = revWalk.parseCommit(objectId);
      RevCommit[] parents = revCommit.getParents();

      String path = resource.getPatchKey().getFileName();

      List<BlameInfo> result;
      if (!base) {
        result = blame(revCommit, path, repository, revWalk);

      } else if (parents.length == 0) {
        throw new ResourceNotFoundException("Initial commit doesn't have base");

      } else if (parents.length == 1) {
        result = blame(parents[0], path, repository, revWalk);

      } else if (parents.length == 2) {
        ObjectId automerge = autoMerger.merge(repository, revWalk, ins, revCommit, mergeStrategy);
        result = blame(automerge, path, repository, revWalk);

      } else {
        throw new ResourceNotFoundException(
            "Cannot generate blame for merge commit with more than 2 parents");
      }

      Response<List<BlameInfo>> r = Response.ok(result);
      if (resource.isCacheable()) {
        r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
      }
      return r;
    }
  }
Exemple #6
0
 @Test
 @SuppressWarnings("unchecked")
 public void testResourceOutputStream() throws IOException {
   File file = File.createTempFile("fileresourcetest", ".tmp");
   file.deleteOnExit();
   FileResource<?> fileResource = resourceFactory.create(FileResource.class, file);
   try (OutputStream os = fileResource.getResourceOutputStream()) {
     os.write("CONTENT".getBytes());
     os.flush();
   }
   List<String> fileContent = Files.readAllLines(file.toPath(), Charset.defaultCharset());
   Assert.assertEquals("CONTENT", fileContent.get(0));
 }
 @Override
 public Object apply(FileResource resource, Input input) throws OrmException {
   ReviewDb db = dbProvider.get();
   AccountPatchReview apr = getExisting(db, resource);
   if (apr == null) {
     db.accountPatchReviews()
         .insert(
             Collections.singleton(
                 new AccountPatchReview(resource.getPatchKey(), resource.getAccountId())));
     return Response.created("");
   } else {
     return Response.ok("");
   }
 }
Exemple #8
0
 @Test
 @SuppressWarnings("unchecked")
 public void testDirectoryResourceResolveShouldBeTheSameForChild() throws IOException {
   File file = File.createTempFile("fileresourcetest", ".tmp");
   file.deleteOnExit();
   FileResource<?> fileResource = resourceFactory.create(FileResource.class, file);
   FileResource<?> parentResource =
       resourceFactory.create(FileResource.class, file.getParentFile());
   Assert.assertNotNull(fileResource);
   Assert.assertNotNull(parentResource);
   List<Resource<?>> children = parentResource.resolveChildren(file.getName());
   Assert.assertEquals(1, children.size());
   Assert.assertEquals(fileResource, children.get(0));
 }
Exemple #9
0
  @Test
  @SuppressWarnings("unchecked")
  public void testMoveFileResource() throws IOException {
    File file = File.createTempFile("fileresourcetest", ".tmp");
    file.deleteOnExit();

    File dest = File.createTempFile("newFileResourcetest", ".tmp");
    dest.deleteOnExit();

    FileResource<?> src = resourceFactory.create(FileResource.class, file);
    FileResource<?> newFile = resourceFactory.create(FileResource.class, dest);

    src.moveTo(newFile);
    Assert.assertNotNull(dest);
    Assert.assertTrue(dest.isFile());
    Assert.assertEquals(
        src.getUnderlyingResourceObject().getAbsolutePath(), dest.getAbsolutePath());
    Assert.assertTrue(src.exists());
  }
Exemple #10
0
  @Test
  public void testMoveFileResourceToDirectory() throws IOException {
    File file = File.createTempFile("fileresourcetest", ".tmp");
    file.deleteOnExit();
    file.createNewFile();

    File folder = OperatingSystemUtils.createTempDir();
    folder.deleteOnExit();
    folder.mkdir();

    FileResource<?> src = resourceFactory.create(file).reify(FileResource.class);
    DirectoryResource folderResource = resourceFactory.create(DirectoryResource.class, folder);
    src.moveTo(folderResource);

    Assert.assertNotNull(src);
    Assert.assertTrue(
        src.getUnderlyingResourceObject().getAbsolutePath().contains(folder.getName()));
    Assert.assertTrue(src.exists());
    Assert.assertFalse(src.isDirectory());
  }
Exemple #11
0
  public void testBasic() {
    File f = null;
    try {
      f = File.createTempFile("com.google.gwt.dev.javac.impl.FileResourceTest", ".tmp");
      f.deleteOnExit();
      Util.writeStringAsFile(f, "contents 1");
    } catch (IOException e) {
      fail("Failed to create test file");
    }

    File dir = f.getParentFile();
    DirectoryClassPathEntry cpe = new DirectoryClassPathEntry(dir);
    FileResource r = new FileResource(cpe, f.getName(), f);
    assertEquals(f.getAbsoluteFile().toURI().toString(), r.getLocation());

    /*
     * In this case, there's no subdirectory, so the path should match the
     * simple filename.
     */
    assertEquals(f.getName(), r.getPath());
  }
 public void executeTransactionOperation(
     TransactionContext txnContext, FileResource.CounterOperation operation) throws ACIDException {
   // lockManager.lock(txnContext, resourceId, 0);
   ILogManager logManager = transactionProvider.getLogManager();
   int currentValue = ((FileResource) resource).getMemoryCounter();
   int finalValue;
   switch (operation) {
     case INCREMENT:
       finalValue = currentValue + 1;
       int logRecordLength =
           ((FileLogger) logger).generateLogRecordContent(currentValue, finalValue);
       logManager.log(
           LogType.UPDATE, txnContext, 1, -1, 1, ResourceType.LSM_BTREE, 0, null, logger, memLSN);
       ((FileResource) resource).increment();
       break;
     case DECREMENT:
       finalValue = currentValue - 1;
       logRecordLength = ((FileLogger) logger).generateLogRecordContent(currentValue, finalValue);
       logManager.log(
           LogType.UPDATE, txnContext, 1, -1, 1, ResourceType.LSM_BTREE, 0, null, logger, memLSN);
       ((FileResource) resource).decrement();
       break;
   }
 }
Exemple #13
0
 @Test
 public void testAppendableOutputStream() throws IOException {
   File file = File.createTempFile("fileresourcetest", ".tmp");
   file.deleteOnExit();
   FileResource<?> fileResource = resourceFactory.create(file).reify(FileResource.class);
   Assert.assertNotNull(fileResource);
   Assert.assertNull(fileResource.reify(DirectoryResource.class));
   try (OutputStream os = fileResource.getResourceOutputStream(false)) {
     os.write("HELLO".getBytes());
     os.flush();
   }
   Assert.assertEquals("HELLO", fileResource.getContents());
   try (OutputStream os = fileResource.getResourceOutputStream(true)) {
     os.write(" WORLD".getBytes());
     os.flush();
   }
   Assert.assertEquals("HELLO WORLD", fileResource.getContents());
   try (OutputStream os = fileResource.getResourceOutputStream()) {
     os.write("GOODBYE".getBytes());
     os.flush();
   }
   Assert.assertEquals("GOODBYE", fileResource.getContents());
 }
Exemple #14
0
  @Test
  public void testDeleteDirectoryRecursive() throws IOException {
    File folder = OperatingSystemUtils.createTempDir();
    folder.deleteOnExit();
    folder.mkdir();

    File folder2 = OperatingSystemUtils.createTempDir();
    folder2.deleteOnExit();
    folder2.mkdir();

    File file = File.createTempFile("fileresourcetest", ".tmp");
    file.deleteOnExit();
    file.createNewFile();

    File file2 = File.createTempFile("fileresourcetest2", ".tmp");
    file2.deleteOnExit();
    file2.createNewFile();

    DirectoryResource folderResource = resourceFactory.create(DirectoryResource.class, folder);
    FileResource<?> src = resourceFactory.create(file).reify(FileResource.class);
    src.moveTo(folderResource);

    DirectoryResource folderResource2 = resourceFactory.create(DirectoryResource.class, folder2);
    FileResource<?> src2 = resourceFactory.create(file2).reify(FileResource.class);

    folderResource2.moveTo(folderResource);
    src2.moveTo(folderResource2);

    Assert.assertTrue(folderResource.isDirectory());
    Assert.assertTrue(folderResource.exists());
    Assert.assertTrue(src.exists());

    Assert.assertTrue(folderResource2.isDirectory());
    Assert.assertTrue(folderResource2.exists());
    Assert.assertTrue(src2.exists());

    folderResource.delete(true);

    Assert.assertTrue(!folderResource.exists());
    Assert.assertTrue(!file.exists());
    Assert.assertTrue(!folderResource2.exists());
    Assert.assertTrue(!file2.exists());
  }
 public void recover() throws ACIDException, IOException {
   recoveryManager.startRecovery(true);
   ((FileResource) resource).sync();
 }
Exemple #16
0
 private FileResource toFileResource(Path path) {
   String relativePath = getRelativePath(path);
   return FileResource.of(relativePath, path.toFile());
 }
 @NotNull
 public File getFile() {
   return fileResource.getFile();
 }
 private static AccountPatchReview getExisting(ReviewDb db, FileResource resource)
     throws OrmException {
   AccountPatchReview.Key key =
       new AccountPatchReview.Key(resource.getPatchKey(), resource.getAccountId());
   return db.accountPatchReviews().get(key);
 }