Package org.eclipse.egit.core.internal.util

Source Code of org.eclipse.egit.core.internal.util.ResourceUtilTest

/*******************************************************************************
* Copyright (C) 2012, 2013 Robin Stocker <robin@nibor.org>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.egit.core.internal.util;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.egit.core.op.ConnectProviderOperation;
import org.eclipse.egit.core.test.GitTestCase;
import org.eclipse.egit.core.test.TestProject;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ResourceUtilTest extends GitTestCase {

  private Repository repository;

  @Before
  public void before() throws Exception {
    repository = FileRepositoryBuilder.create(gitDir);
    repository.create();
    connect(project.getProject());
  }

  @After
  public void after() {
    repository.close();
  }

  @Test
  public void getResourceForLocationShouldReturnFile() throws Exception {
    IFile file = project.createFile("file", new byte[] {});
    IResource resource = ResourceUtil.getResourceForLocation(file.getLocation());
    assertThat(resource, instanceOf(IFile.class));
  }

  @Test
  public void getResourceForLocationShouldReturnFolder() throws Exception {
    IFolder folder = project.createFolder("folder");
    IResource resource = ResourceUtil.getResourceForLocation(folder.getLocation());
    assertThat(resource, instanceOf(IFolder.class));
  }

  @Test
  public void getResourceForLocationShouldReturnNullForInexistentFile() throws Exception {
    IPath location = project.getProject().getLocation().append("inexistent");
    IResource resource = ResourceUtil.getResourceForLocation(location);
    assertThat(resource, nullValue());
  }

  @Test
  public void getFileForLocationShouldReturnExistingFileInCaseOfNestedProject()
      throws Exception {
    TestProject nested = new TestProject(true, "Project-1/Project-0");
    connect(nested.getProject());
    IFile file = nested.createFile("a.txt", new byte[] {});
    IPath location = file.getLocation();

    IFile result = ResourceUtil.getFileForLocation(location);
    assertThat(result, notNullValue());
    assertTrue("Returned IFile should exist", result.exists());
    assertThat(result.getProject(), is(nested.getProject()));
  }

  @Test
  public void getFileForLocationShouldReturnExistingFileInCaseOfNestedNotClosedProject()
      throws Exception {
    TestProject nested = new TestProject(true, "Project-1/Project-0");
    connect(nested.getProject());
    TestProject nested2 = new TestProject(true,
        "Project-1/Project-0/Project");
    connect(nested2.getProject());
    IFile file = nested2.createFile("a.txt", new byte[] {});
    IPath location = file.getLocation();
    nested2.project.close(new NullProgressMonitor());
    IFile result = ResourceUtil.getFileForLocation(location);
    assertThat(result, notNullValue());
    assertTrue("Returned IFile should exist", result.exists());
    assertThat(result.getProject(), is(nested.getProject()));
  }

  @Test
  public void getFileForLocationShouldNotUseFilesWithoutRepositoryMapping()
      throws Exception {
    TestProject nested = new TestProject(true, "Project-1/Project-0");
    IFile file = nested.createFile("a.txt", new byte[] {});
    IPath location = file.getLocation();

    IFile result = ResourceUtil.getFileForLocation(location);
    assertThat(result, notNullValue());
    assertTrue("Returned IFile should exist", result.exists());
    assertThat(result.getProject(), is(project.getProject()));
  }

  private void connect(IProject p) throws CoreException {
    ConnectProviderOperation operation = new ConnectProviderOperation(p,
        gitDir);
    operation.execute(null);
  }
}
TOP

Related Classes of org.eclipse.egit.core.internal.util.ResourceUtilTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.