Author: adietish
Date: 2012-01-19 08:51:31 -0500 (Thu, 19 Jan 2012)
New Revision: 37966
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/GitIgnore.java
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/GitIgnoreTest.java
Log:
[JBIDE-10479] switched GitIgnore to acces .gitignore-file by the resource API (was:
java.io.File)
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/GitIgnore.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/GitIgnore.java 2012-01-19
11:05:53 UTC (rev 37965)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/GitIgnore.java 2012-01-19
13:51:31 UTC (rev 37966)
@@ -11,16 +11,18 @@
package org.jboss.tools.openshift.egit.core;
import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashSet;
import java.util.Set;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jgit.lib.Constants;
/**
@@ -29,28 +31,29 @@
public class GitIgnore {
public static final String NL = System.getProperty("line.separator");
-
+
private Set<String> entries;
- private File file;
+ private IFile file;
- public GitIgnore(IProject project) throws IOException {
- this(new File(project.getLocation().toFile(), Constants.GITIGNORE_FILENAME));
+ public GitIgnore(IProject project) throws IOException, CoreException {
+ this(project.getFile(Constants.GITIGNORE_FILENAME));
}
- public GitIgnore(File gitIgnoreFile) throws IOException {
+ public GitIgnore(IFile gitIgnoreFile) throws IOException, CoreException {
this.file = gitIgnoreFile;
initEntries(gitIgnoreFile);
}
- private void initEntries(File gitIgnore) throws IOException {
+ private void initEntries(IFile gitIgnore) throws IOException, CoreException {
this.entries = new HashSet<String>();
if (gitIgnore == null
- || !gitIgnore.canRead()) {
+ || !gitIgnore.isAccessible()) {
return;
}
BufferedReader reader = null;
try {
- reader = new BufferedReader(new FileReader(gitIgnore));
+
+ reader = new BufferedReader(new InputStreamReader(gitIgnore.getContents()));
for (String line = null; (line = reader.readLine()) != null;) {
if (line != null) {
entries.add(line);
@@ -69,33 +72,26 @@
public boolean contains(String entry) {
return entries.contains(entry);
}
-
+
public int size() {
return entries.size();
}
/**
* Writes the entries in this instance to the .gitignore file. Overwrites
- * and existing file if the given overwrite is set to <code>true</code>,
- * appends otherwise.
+ * and existing file
*
- * @param overwrite
- * overwrites an existing file if <code>true</code>, appends
- * otherwise
* @throws IOException
+ * @throws CoreException
*/
- public File write(boolean overwrite) throws IOException {
- BufferedWriter writer = new BufferedWriter(new FileWriter(file, !overwrite));
- try {
- for (String entry : entries) {
- writer.write(entry);
- writer.write(NL);
- }
- writer.flush();
- return file;
- } finally {
- writer.close();
+ public IFile write(IProgressMonitor monitor) throws CoreException {
+ StringBuilder builder = new StringBuilder();
+ for (String entry : entries) {
+ builder.append(entry);
+ builder.append(NL);
}
+ file.setContents(new ByteArrayInputStream(builder.toString().getBytes()),
IResource.FORCE, monitor);
+ return file;
}
public boolean exists() {
Modified:
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/GitIgnoreTest.java
===================================================================
---
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/GitIgnoreTest.java 2012-01-19
11:05:53 UTC (rev 37965)
+++
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/GitIgnoreTest.java 2012-01-19
13:51:31 UTC (rev 37966)
@@ -12,11 +12,14 @@
import static org.junit.Assert.assertTrue;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.jgit.lib.Constants;
import org.jboss.tools.openshift.egit.core.GitIgnore;
import org.junit.Test;
@@ -27,19 +30,22 @@
public class GitIgnoreTest {
public static final String NL = System.getProperty("line.separator");
-
- private File gitIgnoreFile;
- public void setUp() throws IOException {
- this.gitIgnoreFile = createGitFile("");
+ private IFile gitIgnoreFile;
+
+ private IProject project;
+
+ public void setUp() throws CoreException {
+ this.project = createRandomProject();
+ this.gitIgnoreFile = createGitFile(project, "");
}
public void tearDown() {
- gitIgnoreFile.delete();
+ silentlyDelete(project);
}
@Test
- public void canAddEntries() throws IOException {
+ public void canAddEntries() throws CoreException, IOException {
GitIgnore gitIgnore = new GitIgnore(gitIgnoreFile);
assertTrue(gitIgnore.size() == 0);
String entry = "dummy";
@@ -49,62 +55,68 @@
}
@Test
- public void canParseExistingEntries() throws IOException {
- File gitIgnoreFile = null;
+ public void canParseExistingEntries() throws CoreException, IOException {
+ IFile gitIgnoreFile = null;
+ IProject project = null;
try {
- gitIgnoreFile = createGitFile("redhat", "jboss",
"tools");
+ project = createRandomProject();
+ gitIgnoreFile = createGitFile(project, "redhat", "jboss",
"tools");
GitIgnore gitIgnore = new GitIgnore(gitIgnoreFile);
assertTrue(gitIgnore.size() == 3);
assertTrue(gitIgnore.contains("redhat"));
assertTrue(gitIgnore.contains("jboss"));
assertTrue(gitIgnore.contains("tools"));
} finally {
- if (gitIgnoreFile != null) {
- gitIgnoreFile.delete();
- }
+ silentlyDelete(project);
}
}
@Test
- public void entryIsNotAddedTwice() throws IOException {
- File gitIgnoreFile = null;
+ public void entryIsNotAddedTwice() throws CoreException, IOException {
+ IFile gitIgnoreFile = null;
+ IProject project = null;
try {
- gitIgnoreFile = createGitFile("redhat");
+ project = createRandomProject();
+ gitIgnoreFile = createGitFile(project, "redhat");
GitIgnore gitIgnore = new GitIgnore(gitIgnoreFile);
assertTrue(gitIgnore.size() == 1);
assertTrue(gitIgnore.contains("redhat"));
gitIgnore.add("redhat");
assertTrue(gitIgnore.size() == 1);
} finally {
- gitIgnoreFile.delete();
+ silentlyDelete(project);
}
}
- private File createTmpFolder() throws IOException {
- String tmpFolder = System.getProperty("java.io.tmpdir");
- String currentTime = String.valueOf(System.currentTimeMillis());
- File randomTmpFolder = new File(tmpFolder, currentTime);
- randomTmpFolder.mkdir();
- return randomTmpFolder;
+ private IFile createGitFile(IProject project, String... gitIgnoreEntries) throws
CoreException {
+ IFile gitFile = project.getFile(Constants.GITIGNORE_FILENAME);
+ StringBuilder builder = new StringBuilder();
+ for (String entry : gitIgnoreEntries) {
+ builder.append(entry);
+ builder.append(NL);
+ }
+ gitFile.create(new ByteArrayInputStream(builder.toString().getBytes()),
IResource.FORCE, null);
+ return gitFile;
+ }
+ private IProject createRandomProject() throws CoreException {
+ String projectName = String.valueOf(System.currentTimeMillis());
+ IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
+ project.create(null);
+ project.open(null);
+ return project;
}
- private File createGitFile(String... gitIgnoreEntries) throws IOException {
- File gitFile = new File(createTmpFolder(), Constants.GITIGNORE_FILENAME);
- BufferedWriter writer = null;
+ private void silentlyDelete(IProject project) {
+ if (project == null
+ || !project.isAccessible()) {
+ return;
+ }
try {
- writer = new BufferedWriter(new FileWriter(gitFile));
- for (String entry : gitIgnoreEntries) {
- writer.write(entry);
- writer.write(NL);
- }
- writer.flush();
- } finally {
- if (writer != null) {
- writer.close();
- }
+ project.close(null);
+ project.delete(true, null);
+ } catch (CoreException e) {
+ e.printStackTrace();
}
-
- return gitFile;
}
}
Show replies by date