Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Kurtakov2012-06-12 21:00:29 +0000
committerAlexander Kurtakov2012-06-12 21:00:29 +0000
commitc58f0966b3770e7f03d06cf80a33775ef8712ea9 (patch)
tree80984343ecc1619faa1a057b1c3619a00c94439b
parent5c6a832e0b912442934b29fdb3ddf1e96c00fd4e (diff)
downloadorg.eclipse.linuxtools-c58f0966b3770e7f03d06cf80a33775ef8712ea9.tar.gz
org.eclipse.linuxtools-c58f0966b3770e7f03d06cf80a33775ef8712ea9.tar.xz
org.eclipse.linuxtools-c58f0966b3770e7f03d06cf80a33775ef8712ea9.zip
Add changelog tests.
Forgotten during conversion to git.
-rw-r--r--.gitignore2
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/.classpath7
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/.project28
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/.settings/org.eclipse.jdt.core.prefs8
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/ChangeLog29
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/META-INF/MANIFEST.MF37
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/build.properties4
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/formatters/tests/GNUFormatTest.java443
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/AllTests.java30
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/ChangeLogWriterTest.java297
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/TestsPluginActivator.java50
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/parsers/tests/CParserTest.java431
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/parsers/tests/JavaParserTest.java462
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/CStringStorage.java48
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/CStringStorageInput.java55
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/ChangeLogTestProject.java148
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/TestChangeLogTestProject.java150
-rw-r--r--changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/helpers/EditorHelper.java69
18 files changed, 2298 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000..2941331498
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+bin/
+target/ \ No newline at end of file
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/.classpath b/changelog/org.eclipse.linuxtools.changelog.tests/.classpath
new file mode 100644
index 0000000000..ad32c83a78
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/.project b/changelog/org.eclipse.linuxtools.changelog.tests/.project
new file mode 100644
index 0000000000..7ff14f1a34
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.linuxtools.changelog.tests</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/.settings/org.eclipse.jdt.core.prefs b/changelog/org.eclipse.linuxtools.changelog.tests/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000000..e2efb1f98a
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,8 @@
+#Mon Nov 22 13:51:47 EST 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/ChangeLog b/changelog/org.eclipse.linuxtools.changelog.tests/ChangeLog
new file mode 100644
index 0000000000..b52dbf62f6
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/ChangeLog
@@ -0,0 +1,29 @@
+2010-12-07 Severin Gehwolf <sgehwolf@redhat.com>
+
+ * src/org/eclipse/linuxtools/changelog/core/tests/AllTests.java: Added CParserTest.
+ * src/org/eclipse/linuxtools/changelog/parsers/tests/CParserTest.java: New file.
+ * src/org/eclipse/linuxtools/changelog/tests/fixtures/CStringStorage.java: New file.
+ * src/org/eclipse/linuxtools/changelog/tests/fixtures/CStringStorageInput.java: New file.
+ * META-INF/MANIFEST.MF: Import packages required for CParserTest
+ * src/org/eclipse/linuxtools/changelog/parsers/tests/JavaParserTest.java: Removed unnecessary comment.
+ * src/org/eclipse/linuxtools/changelog/tests/fixtures/ChangeLogTestProject.java (addCNature): New method.
+ (addCCNature): New method.
+ * src/org/eclipse/linuxtools/changelog/tests/fixtures/TestChangeLogTestProject.java (canAddCNature): New test for
+ new method.
+ (canAddCCNature): New test for new method.
+
+2010-12-06 Severin Gehwolf <sgehwolf@redhat.com>
+
+ * src/org/eclipse/linuxtools/changelog/parsers/tests/JavaParserTest.java: New file.
+ * src/org/eclipse/linuxtools/changelog/core/formatters/tests/GNUFormatTest.java: New file.
+ JUnit plug-in tests for GNUFormat.
+ * src/org/eclipse/linuxtools/changelog/core/tests/AllTests.java: New file.
+ * src/org/eclipse/linuxtools/changelog/core/tests/ChangeLogWriterTest.java: New file.
+ JUnit plug-in tests for ChangeLogWriterTest.
+ * src/org/eclipse/linuxtools/changelog/core/tests/TestsPluginActivator.java: New file.
+ * src/org/eclipse/linuxtools/changelog/tests/fixtures/ChangeLogTestProject.java: New file.
+ * src/org/eclipse/linuxtools/changelog/tests/fixtures/TestChangeLogTestProject.java: New file.
+ * src/org/eclipse/linuxtools/changelog/tests/helpers/EditorHelper.java: New File.
+ * build.properties: New file.
+ * META-INF/MANIFEST.MF: New file.
+ * ChangeLog: New file. \ No newline at end of file
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/META-INF/MANIFEST.MF b/changelog/org.eclipse.linuxtools.changelog.tests/META-INF/MANIFEST.MF
new file mode 100644
index 0000000000..946954d8c6
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/META-INF/MANIFEST.MF
@@ -0,0 +1,37 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Eclipse Linuxtools Changelog Tests
+Bundle-SymbolicName: org.eclipse.linuxtools.changelog.tests
+Bundle-Version: 2.7.0.qualifier
+Bundle-Activator: org.eclipse.linuxtools.changelog.core.tests.TestsPluginActivator
+Bundle-Vendor: Eclipse.org
+Require-Bundle: org.eclipse.ui,
+ org.eclipse.core.runtime,
+ org.junit4;bundle-version="4.8.1"
+Bundle-RequiredExecutionEnvironment: J2SE-1.5
+Bundle-ActivationPolicy: lazy
+Import-Package: org.eclipse.cdt.core,
+ org.eclipse.cdt.core.model,
+ org.eclipse.cdt.core.settings.model,
+ org.eclipse.cdt.core.templateengine,
+ org.eclipse.cdt.core.templateengine.process,
+ org.eclipse.cdt.managedbuilder.core,
+ org.eclipse.cdt.managedbuilder.templateengine,
+ org.eclipse.compare,
+ org.eclipse.compare.structuremergeviewer,
+ org.eclipse.core.resources,
+ org.eclipse.jdt.core,
+ org.eclipse.jface.text,
+ org.eclipse.linuxtools.changelog.core,
+ org.eclipse.linuxtools.changelog.core.actions,
+ org.eclipse.linuxtools.changelog.core.editors,
+ org.eclipse.linuxtools.changelog.core.formatters,
+ org.eclipse.linuxtools.changelog.core.ui,
+ org.eclipse.linuxtools.changelog.parsers,
+ org.eclipse.linuxtools.changelog.parsers.java,
+ org.eclipse.team.ui.synchronize,
+ org.eclipse.ui,
+ org.eclipse.ui.editors.text,
+ org.eclipse.ui.ide,
+ org.eclipse.ui.part,
+ org.eclipse.ui.texteditor
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/build.properties b/changelog/org.eclipse.linuxtools.changelog.tests/build.properties
new file mode 100644
index 0000000000..34d2e4d2da
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/build.properties
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/formatters/tests/GNUFormatTest.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/formatters/tests/GNUFormatTest.java
new file mode 100644
index 0000000000..2e3c45931c
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/formatters/tests/GNUFormatTest.java
@@ -0,0 +1,443 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc. and others.
+ * 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.linuxtools.changelog.core.formatters.tests;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.linuxtools.changelog.core.formatters.GNUFormat;
+import org.eclipse.linuxtools.changelog.tests.fixtures.ChangeLogTestProject;
+import static org.eclipse.linuxtools.changelog.tests.helpers.EditorHelper.closeEditor;
+import static org.eclipse.linuxtools.changelog.tests.helpers.EditorHelper.openEditor;
+import static org.eclipse.linuxtools.changelog.tests.helpers.EditorHelper.getContent;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.texteditor.AbstractTextEditor;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.eclipse.jface.text.IDocument;
+
+public class GNUFormatTest {
+
+ // The instance under test
+ private GNUFormat gnuFormatter;
+ // A faked project
+ private ChangeLogTestProject project;
+ // The IEditorPart corresponding to the ChangeLog file
+ private IEditorPart changelogEditorPart = null;
+
+ // Tokens for the GNU format
+ private static final String TWO_SPACES = " ";
+ private static final String SPACE = " ";
+ private static final String TAB = "\t";
+ private static final String LEFT_EMAIL_BRACKET = "<";
+ private static final String RIGHT_EMAIL_BRACKET = ">";
+ private static final String FILE_ENTRY_START_MARKER = "* ";
+ private static final String FILE_ENTRY_END_MARKER = ": ";
+ private static final String FUNCTION_START_MARKER = "(";
+ private static final String FUNCTION_END_MARKER = ")";
+ private static final String NEW_LINE = "\n";
+
+ @Before
+ public void setUp() throws Exception {
+ gnuFormatter = new GNUFormat();
+ project = new ChangeLogTestProject("GNUFormatterTest");
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ // Most tests in this class use changelogEditorPart. In order to avoid
+ // spill-over from previous runs, truncate content (i.e. manually set
+ // content to the empty string).
+ if (changelogEditorPart != null) { // testFormatDateLine does not use it
+ AbstractTextEditor castEditor = (AbstractTextEditor) changelogEditorPart;
+ IDocumentProvider iDocProvider = castEditor.getDocumentProvider();
+ IDocument changelogContentDoc = iDocProvider.getDocument(castEditor.getEditorInput());
+ changelogContentDoc.set(""); // empty content
+ changelogEditorPart.doSave(null);
+ // Also close open editor in order for default content to work.
+ // I.e. avoid spill over from previous test runs
+ closeEditor(changelogEditorPart);
+ }
+ project.getTestProject().delete(true, true, null); // dispose
+ }
+
+ /**
+ * GNU ChangeLog style date lines are of the following format
+ *
+ * YYYY-MM-DD Author Name <author.email@domain.com>
+ */
+ @Test
+ public void testFormatDateLine() {
+ // Today's date in ISO format
+ Calendar c = new GregorianCalendar();
+ String isoDate = String.format("%1$tY-%1$tm-%1$td", c);
+ String authorName = "William Shakespeare";
+ String authorEmail = "william.shakespeare@medieval.com";
+ final String expectedDateFormatting = isoDate +
+ TWO_SPACES +
+ authorName +
+ TWO_SPACES +
+ LEFT_EMAIL_BRACKET +
+ authorEmail +
+ RIGHT_EMAIL_BRACKET +
+ "\n\n";
+ assertEquals(expectedDateFormatting,
+ gnuFormatter.formatDateLine(authorName, authorEmail));
+ }
+
+ /**
+ * Test merge of two separate changes to an existing file within the same changelog entry.
+ * I.e. test for
+ *
+ * * path/to/file (method1): First change.
+ * (method2): Another change in same file but different function/method.
+ */
+ @Test
+ public void twoChangesInSameFileAreProperlyMerged() throws Exception {
+ // set date/author line
+ String authorName = "Test Author";
+ String email = "test@example.com";
+ final String dateLine = gnuFormatter.formatDateLine(authorName, email);
+
+ // full absolute path to ChangeLog file (relative to project root)
+ String changelogPath = "/" + project.getTestProject().getName() + "/path/to";
+ final String changelogFilePath = changelogPath + "/ChangeLog";
+
+ // entry file path (need overlap with changelogPath)
+ String fileEntryRelPath = "eclipse/example/test/NewCoffeeMaker.java";
+ final String entryFilePath = changelogPath + "/" + fileEntryRelPath;
+
+ final String firstMethodName = "main";
+ final String firstChangeComment = "Fix args parsing.";
+ // Setup proper pre-existing content
+ String content = dateLine + NEW_LINE + NEW_LINE + TAB + FILE_ENTRY_START_MARKER +
+ fileEntryRelPath + SPACE + FUNCTION_START_MARKER +
+ firstMethodName + FUNCTION_END_MARKER + FILE_ENTRY_END_MARKER +
+ firstChangeComment + NEW_LINE;
+
+ assertNull(project.getTestProject().findMember(new Path("/path/to/ChangeLog")));
+
+ // add a ChangeLog file to our test project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ content.getBytes());
+ IFile changelogFile = project.addFileToProject( "/path/to", "ChangeLog",
+ newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember(new Path("/path/to/ChangeLog")));
+
+ // Open a document and get the IEditorPart
+ changelogEditorPart = openEditor(changelogFile);
+ // make sure changelog editor content is right before merging
+ assertEquals(content, getContent(changelogEditorPart));
+
+ final String secondMethodName = "toString";
+
+ // Do the merge with the existing content
+ gnuFormatter.mergeChangelog(dateLine, secondMethodName,
+ "" /* no default content */, changelogEditorPart,
+ changelogFilePath, entryFilePath);
+
+ final String actualMergeResult = getContent(changelogEditorPart);
+
+ final String expectedResult = dateLine + NEW_LINE + NEW_LINE + TAB + FILE_ENTRY_START_MARKER +
+ fileEntryRelPath + SPACE + FUNCTION_START_MARKER +
+ firstMethodName + FUNCTION_END_MARKER + FILE_ENTRY_END_MARKER +
+ firstChangeComment + NEW_LINE + TAB + FUNCTION_START_MARKER +
+ secondMethodName + FUNCTION_END_MARKER + FILE_ENTRY_END_MARKER +
+ NEW_LINE;
+
+ assertEquals(expectedResult, actualMergeResult);
+ }
+
+ /**
+ * If two different authors make modifications on the same day, both modifications must
+ * show up as separate changelog entries for that day.
+ */
+ @Test
+ public void newChangeLogEntryForNewAuthorOnSameDay() throws Exception {
+ // first author
+ String authorName = "Test Author";
+ String email = "test@example.com";
+ final String firstDateLine = gnuFormatter.formatDateLine(authorName, email);
+ // second author
+ authorName = "William Shakespeare";
+ email = "will@pear.com";
+ final String secondDateLine = gnuFormatter.formatDateLine(authorName, email);
+
+ // full absolute path to ChangeLog file
+ String changelogPath = "/" + project.getTestProject().getName() + "/path/to";
+ final String changelogFilePath = changelogPath + "/ChangeLog";
+
+ // No existing content in ChangeLog file
+ String content = "";
+
+ assertNull(project.getTestProject().findMember(new Path("/path/to/ChangeLog")));
+
+ // add a ChangeLog file to our test project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ content.getBytes());
+ IFile changelogFile = project.addFileToProject( changelogPath, "ChangeLog",
+ newFileInputStream);
+ // Open a document and get the IEditorPart
+ changelogEditorPart = openEditor(changelogFile);
+
+ // entry file path (needs overlap with changelogPath)
+ String fileEntryRelPath = "eclipse/example/test/NewCoffeeMaker.java";
+ final String entryFilePath = changelogPath + "/" + fileEntryRelPath;
+
+ final String guessedFunctionName = "bazinga";
+
+ // merge first changelog entry with empty content
+ gnuFormatter.mergeChangelog(firstDateLine, guessedFunctionName,
+ "" /* empty default content */, changelogEditorPart,
+ changelogFilePath, entryFilePath);
+
+ String actualMergeResult = getContent(changelogEditorPart);
+
+ String expectedResult = firstDateLine + TAB + FILE_ENTRY_START_MARKER +
+ fileEntryRelPath + SPACE + FUNCTION_START_MARKER +
+ guessedFunctionName + FUNCTION_END_MARKER +
+ FILE_ENTRY_END_MARKER;
+
+ assertEquals(expectedResult, actualMergeResult);
+
+ // add second changelog entry on same date but by different author
+ gnuFormatter.mergeChangelog(secondDateLine, guessedFunctionName,
+ "" /* empty default content */, changelogEditorPart,
+ changelogFilePath, entryFilePath);
+
+ actualMergeResult = getContent(changelogEditorPart);
+
+ expectedResult = secondDateLine + TAB + FILE_ENTRY_START_MARKER +
+ fileEntryRelPath + SPACE + FUNCTION_START_MARKER +
+ guessedFunctionName + FUNCTION_END_MARKER +
+ FILE_ENTRY_END_MARKER + NEW_LINE + NEW_LINE +
+ expectedResult;
+
+ assertEquals(expectedResult, actualMergeResult);
+ }
+
+ /**
+ * Basic format check.
+ */
+ @Test
+ public void mergeChangeLogHasProperFormat() throws Exception {
+ // set date/author line
+ String authorName = "Test Author";
+ String email = "test@example.com";
+ final String dateLine = gnuFormatter.formatDateLine(authorName, email);
+
+ // full absolute path to ChangeLog file (relative to project root)
+ String changelogPath = "/" + project.getTestProject().getName() + "/path/to";
+ final String changelogFilePath = changelogPath + "/ChangeLog";
+
+ // Content to merge into
+ String content = "";
+
+ assertNull(project.getTestProject().findMember(new Path("/path/to/ChangeLog")));
+
+ // add a ChangeLog file to our test project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ content.getBytes());
+ IFile changelogFile = project.addFileToProject( "/path/to", "ChangeLog",
+ newFileInputStream);
+ // Open a document and get the IEditorPart
+ changelogEditorPart = openEditor(changelogFile);
+
+ // make sure changelog editor content is empty
+ assertEquals(content, getContent(changelogEditorPart));
+
+ // entry file path (need overlap with changelogPath)
+ String fileEntryRelPath = "eclipse/example/test/NewCoffeeMaker.java";
+ final String entryFilePath = changelogPath + "/" + fileEntryRelPath;
+
+ // Will show up surrounded by "(" and ")" in ChangeLog
+ final String guessedFunctionName = "bazinga";
+
+ // This always returns an empty String (should probably be changed...)
+ // merge result will be written to editorContent
+ gnuFormatter.mergeChangelog(dateLine, guessedFunctionName,
+ "" /* empty default content */, changelogEditorPart,
+ changelogFilePath, entryFilePath);
+
+ final String actualMergeResult = getContent(changelogEditorPart);
+
+ final String expectedResult = dateLine + TAB + FILE_ENTRY_START_MARKER +
+ fileEntryRelPath + SPACE + FUNCTION_START_MARKER +
+ guessedFunctionName + FUNCTION_END_MARKER +
+ FILE_ENTRY_END_MARKER;
+
+ assertEquals(expectedResult, actualMergeResult);
+ }
+
+ /**
+ * Here's the scenario for this test. It may be that there are only files removed and
+ * new files added for a commit. This should yield to changelog entries for the following
+ * form:
+ *
+ *<code>
+ * 2010-11-26 Some Author <some.author@example.com>
+ *
+ * * path/to/deleted/file.c: Removed.
+ * * path/to/new/file.c: New file.
+ *</code>
+ *
+ * Prior to the fix for Eclipse Bz #331244 the result was:
+ *
+ *<code>
+ * 2010-11-26 Some Author <some.author@example.com>
+ *
+ * * path/to/deleted/file.c: Removed.
+ * * path/to/new/file.c:
+ *</code>
+ *
+ * This regression test should catch this.
+ */
+ @Test
+ public void canHaveEntriesWithDefaultTextOnly() throws Exception {
+ // set date/author line
+ String authorName = "Test Author";
+ String email = "test@example.com";
+ final String dateLine = gnuFormatter.formatDateLine(authorName, email);
+
+ // full absolute path to ChangeLog file (relative to project root)
+ String changelogPath = "/" + project.getTestProject().getName() + "/path/example";
+ final String changelogFilePath = changelogPath + "/ChangeLog";
+
+ // Content to merge into
+ String content = "";
+
+ // add a ChangeLog file to our test project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ content.getBytes());
+ IFile changelogFile = project.addFileToProject( "/path/example", "ChangeLog",
+ newFileInputStream);
+ // Open a document and get the IEditorPart
+ changelogEditorPart = openEditor(changelogFile);
+
+ // make sure changelog editor content is empty
+ assertEquals(content, getContent(changelogEditorPart));
+
+ // entry file path (need overlap with changelogPath)
+ final String firstFileEntryRelPath = "eclipse/example/test/NewCoffeeMaker.java";
+ String entryFilePath = changelogPath + "/" + firstFileEntryRelPath;
+
+ final String firstDefaultContent = "New file.";
+
+ // Note: This always returns an empty String (should probably be changed...)
+ // merge result will be written to editorContent.
+ //
+ // Create a line with default text "New file"
+ gnuFormatter.mergeChangelog(dateLine, "" /* no guessed function name */,
+ firstDefaultContent, changelogEditorPart,
+ changelogFilePath, entryFilePath);
+ final String secondFileEntryRelPath = "eclipse/example/test/OldCoffeeMaker.java";
+ entryFilePath = changelogPath + "/" + secondFileEntryRelPath;
+ final String secondDefaultContent = "Removed.";
+ // Add entry for removed file
+ gnuFormatter.mergeChangelog(dateLine, "" /* no guessed function name */,
+ secondDefaultContent, changelogEditorPart,
+ changelogFilePath, entryFilePath);
+
+ final String actualMergeResult = getContent(changelogEditorPart);
+
+ // Note: Removed files occur in the list first, new file items come after that.
+ final String expectedResult = dateLine + TAB + FILE_ENTRY_START_MARKER +
+ secondFileEntryRelPath+ FILE_ENTRY_END_MARKER +
+ secondDefaultContent + NEW_LINE + TAB + FILE_ENTRY_START_MARKER +
+ firstFileEntryRelPath + FILE_ENTRY_END_MARKER +
+ firstDefaultContent;
+
+ assertEquals(expectedResult, actualMergeResult);
+ }
+
+ /**
+ * Test for a changelog entry with items for removed files, new files and modified
+ * existing files. This test should also catch Eclipse Bz #331244 regressions.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canHaveEntriesWithDefaultTextAndSomeModificationToAnExistingFile() throws Exception {
+ // set date/author line
+ String authorName = "Test Author";
+ String email = "test@example.com";
+ final String dateLine = gnuFormatter.formatDateLine(authorName, email);
+
+ // full absolute path to ChangeLog file (relative to project root)
+ String changelogPath = "/" + project.getTestProject().getName() + "/test/example";
+ final String changelogFilePath = changelogPath + "/ChangeLog";
+
+ // pre-existing content to merge into
+ String content = "2010-10-05 William Shakespeare <will@pear.com>\n\n\t" +
+ "* path/to/file.c: New File.\n\n";
+
+ // add a ChangeLog file to our test project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ content.getBytes());
+ IFile changelogFile = project.addFileToProject( "/test/example", "ChangeLog",
+ newFileInputStream);
+ // Open a document and get the IEditorPart
+ changelogEditorPart = openEditor(changelogFile);
+
+ assertEquals(content, getContent(changelogEditorPart));
+
+ // entry file path (need overlap with changelogPath)
+ final String firstFileEntryRelPath = "eclipse/example/test/NewCoffeeMaker.java";
+ String entryFilePath = changelogPath + "/" + firstFileEntryRelPath;
+
+ final String firstDefaultContent = "New file.";
+
+ // Note: This always returns an empty String (should probably be changed...)
+ // merge result will be written to editorContent.
+ //
+ // Create an item with default text "New file"
+ gnuFormatter.mergeChangelog(dateLine, "" /* no guessed function name */,
+ firstDefaultContent, changelogEditorPart,
+ changelogFilePath, entryFilePath);
+ final String secondFileEntryRelPath = "eclipse/example/test/OldCoffeeMaker.java";
+ entryFilePath = changelogPath + "/" + secondFileEntryRelPath;
+ final String secondDefaultContent = "Removed.";
+ // Add entry for removed file
+ gnuFormatter.mergeChangelog(dateLine, "" /* no guessed function name */,
+ secondDefaultContent, changelogEditorPart,
+ changelogFilePath, entryFilePath);
+ final String thirdFileEntryRelPath = "eclipse/example/test/ModifiedFile.java";
+ entryFilePath = changelogPath + "/" + thirdFileEntryRelPath;
+ final String guessedFunctionName = "main";
+ // Create a bullet point describing a change in some existing file
+ gnuFormatter.mergeChangelog(dateLine, guessedFunctionName,
+ "" /* no default content */, changelogEditorPart,
+ changelogFilePath, entryFilePath);
+
+ final String actualMergeResult = getContent(changelogEditorPart);
+
+ // Note that changes to existing files appear first in the changelog entries.
+ // Second are removed files and new files are last.
+ final String expectedResult = dateLine + TAB + FILE_ENTRY_START_MARKER +
+ thirdFileEntryRelPath + SPACE + FUNCTION_START_MARKER +
+ guessedFunctionName + FUNCTION_END_MARKER + FILE_ENTRY_END_MARKER +
+ NEW_LINE + TAB + FILE_ENTRY_START_MARKER +
+ secondFileEntryRelPath+ FILE_ENTRY_END_MARKER +
+ secondDefaultContent + NEW_LINE + TAB + FILE_ENTRY_START_MARKER +
+ firstFileEntryRelPath + FILE_ENTRY_END_MARKER +
+ firstDefaultContent + NEW_LINE + NEW_LINE +
+ content /* orig. content */;
+
+ assertEquals(expectedResult, actualMergeResult);
+ }
+
+}
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/AllTests.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/AllTests.java
new file mode 100644
index 0000000000..0b6d27817e
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/AllTests.java
@@ -0,0 +1,30 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc. and others.
+ * 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.linuxtools.changelog.core.tests;
+import org.eclipse.linuxtools.changelog.core.formatters.tests.GNUFormatTest;
+import org.eclipse.linuxtools.changelog.parsers.tests.CParserTest;
+import org.eclipse.linuxtools.changelog.parsers.tests.JavaParserTest;
+import org.eclipse.linuxtools.changelog.tests.fixtures.TestChangeLogTestProject;
+import org.junit.runners.Suite;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite.SuiteClasses;
+
+@RunWith(Suite.class)
+@SuiteClasses({
+ ChangeLogWriterTest.class,
+ GNUFormatTest.class,
+ JavaParserTest.class,
+ CParserTest.class,
+ // A small test for the fixture
+ TestChangeLogTestProject.class,
+ }
+)
+
+public class AllTests {
+ // empty
+}
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/ChangeLogWriterTest.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/ChangeLogWriterTest.java
new file mode 100644
index 0000000000..dd4688abed
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/ChangeLogWriterTest.java
@@ -0,0 +1,297 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc. and others.
+ * 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.linuxtools.changelog.core.tests;
+
+import static org.junit.Assert.*;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.linuxtools.changelog.core.ChangeLogWriter;
+import org.eclipse.linuxtools.changelog.core.IFormatterChangeLogContrib;
+import org.eclipse.ui.IEditorPart;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.eclipse.linuxtools.changelog.core.formatters.GNUFormat;
+import org.eclipse.linuxtools.changelog.tests.fixtures.ChangeLogTestProject;
+import org.eclipse.linuxtools.changelog.tests.helpers.EditorHelper;
+
+/**
+ * @author Severin Gehwolf <sgehwolf@redhat.com>
+ *
+ */
+public class ChangeLogWriterTest {
+
+ // The instance of the class under test
+ private ChangeLogWriter clogWriter;
+ // A fake project
+ private ChangeLogTestProject project;
+ // the path elements to the ChangeLog file, absolute to the project root
+ private final String[] CHANGELOG_FILE_PATH_SEGMENTS = { "project-name", "src", "org" };
+ private final String CHANGELOG_FILE_NAME = "ChangeLog";
+ private String changelogFilePath;
+ // IFile handle to '/path/changelog/ChangeLog'
+ private IFile changelogFile;
+ // Current content of the ChangeLog file
+ private final String changeLogContent = "2009-10-14 Some Author <some.author@example.com>\n\n" +
+ "\t* this/file/does/not/really/exist/SpringRoll.java: new file\n\n";
+
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ clogWriter = new ChangeLogWriter();
+ // create a testproject and add a file to it
+ project = new ChangeLogTestProject("changelogWriterProject");
+ // Generate full path to ChangeLog file
+ String changelogPath = "/";
+ for (String segment: CHANGELOG_FILE_PATH_SEGMENTS) {
+ changelogPath += segment + "/";
+ }
+ changelogFilePath = changelogPath + CHANGELOG_FILE_NAME;
+ // add a ChangeLog file to the project at the path specified by
+ // CHANGELOG_FILE_PATH_SEGMENTS
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ changeLogContent.getBytes());
+ changelogFile = project.addFileToProject(changelogPath + CHANGELOG_FILE_NAME, CHANGELOG_FILE_NAME,
+ newFileInputStream);
+ }
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {
+ // dispose testproject
+ project.getTestProject().delete(true, null);
+ }
+
+ /**
+ * Test for setting and getting the content of the ChangeLog
+ * file.
+ */
+ @Test
+ public void testGetSetChangelog() {
+ // Open the ChangeLog file and get the IEdiorPart
+ final IEditorPart currentContent = EditorHelper.openEditor(changelogFile);
+ clogWriter.setChangelog(currentContent);
+ assertEquals(currentContent, clogWriter.getChangelog());
+ }
+
+ @Test
+ public void testGetSetChangelogLocation() {
+ final String path = "/path/to/changelog/file/ChangeLog";
+ clogWriter.setChangelogLocation(path);
+ assertEquals(path, clogWriter.getChangelogLocation());
+ }
+
+ @Test
+ public void testGetSetDateLine() {
+ String authorName = "Test Author";
+ String email = "spongebob@commedycentral.com";
+ clogWriter.setDateLine(new GNUFormat().formatDateLine(
+ authorName, email));
+
+ // Today's date in ISO format
+ Calendar c = new GregorianCalendar();
+ String isoDate = String.format("%1$tY-%1$tm-%1$td", c);
+
+ // expected date/author line
+ String expectedDateLine = isoDate + " " + authorName + " <" + email + ">\n\n";
+ assertEquals(expectedDateLine, clogWriter.getDateLine());
+ }
+
+ @Test
+ public void testGetSetEntryFilePath() {
+ final String entryFilePath = "/some/path/to/some/File.java";
+ clogWriter.setEntryFilePath(entryFilePath);
+ assertEquals(entryFilePath, clogWriter.getEntryFilePath());
+ }
+
+ @Test
+ public void testGetSetFormatter() {
+ IFormatterChangeLogContrib formatter = new GNUFormat();
+ clogWriter.setFormatter(formatter);
+ assertEquals(formatter, clogWriter.getFormatter());
+ }
+
+ @Test
+ public void testGetSetGuessedFName() {
+ final String guessedFunctionName = "getInstance";
+ clogWriter.setGuessedFName(guessedFunctionName);
+ assertEquals(guessedFunctionName, clogWriter.getGuessedFName());
+ }
+
+ /**
+ * Note that there can be several Changelogs inside a directory tree.
+ * The {@link ChangeLogWriter#writeChangeLog()} code assumes that the full path to
+ * the ChangeLog file and the full path to the file for which to generate a ChangeLog
+ * entry have the same ancestor (with some potential overlap).
+ *
+ * Consider the following example:
+ *
+ * 1. The ChangeLog file is <project-root>/src/ChangeLog
+ * 2. The currently open editor contains code of <project-root>/src/org/eclipse/example/Test.java
+ *
+ * In the above case entries in <project-root>/src/ChangeLog *should* be of the form:
+ *
+ * <code>
+ *
+ * YYYY-MM-DD Author Name <email@example.com>
+ *
+ * * org/eclipse/example/Test.java: new File
+ *
+ * </code>
+ *
+ * Similarly, if the ChangeLog file is in <project-root>/ChangeLog and the currently open
+ * file is <project-root>/src/org/eclipse/example/Sun.java, generated entries in
+ * <project-root>/ChangeLog would look like (note the "src" path is added in this case):
+ *
+ * <code>
+ *
+ * YYYY-MM-DD Author Name <email@example.com>
+ *
+ * * src/org/eclipse/example/Sun.java: new File
+ *
+ * </code>
+ *
+ * Test for method {@link org.eclipse.linuxtools.changelog.core.ChangeLogWriter#writeChangeLog()}
+ */
+ @Test
+ public void testWriteChangeLog() throws Exception {
+ // We want paths up to the ChangeLog file to overlap
+ String changedFilePath = "/";
+ for (String segment: CHANGELOG_FILE_PATH_SEGMENTS) {
+ changedFilePath += segment + "/";
+ }
+ final String pathRelativeToChangeLog = "eclipse/example/test/NewCoffeeMaker.java";
+ clogWriter.setEntryFilePath( changedFilePath + pathRelativeToChangeLog );
+
+ // Will show up surrounded by "(" and ")" in ChangeLog
+ String guessedFunctionName = "bazinga";
+ clogWriter.setGuessedFName(guessedFunctionName);
+
+ // set GNU formatter
+ clogWriter.setFormatter(new GNUFormat());
+
+ // Open a document and get the IEditorPart
+ IEditorPart editorContent = EditorHelper.openEditor(changelogFile);
+ clogWriter.setChangelog(editorContent);
+
+ // set date/author line
+ String authorName = "Test Author";
+ String email = "test@example.com";
+ clogWriter.setDateLine(clogWriter.getFormatter().formatDateLine(
+ authorName, email));
+
+ // full absolute path to ChangeLog file (relative to project root)
+ clogWriter.setChangelogLocation(changelogFilePath);
+
+ // Write changelog to buffer - need to save for persistence
+ clogWriter.writeChangeLog();
+
+ // above written content is not persistent yet; save it to make it persistent
+ clogWriter.getChangelog().doSave(null);
+
+ // Today's date in ISO format
+ Calendar c = new GregorianCalendar();
+ String isoDate = String.format("%1$tY-%1$tm-%1$td", c);
+
+ // Construct the changelog entry by hand and match it with what has been written
+ String expectedChangeLogEntry = isoDate + " " + authorName + " <" + email + ">\n\n";
+ expectedChangeLogEntry += "\t* " + pathRelativeToChangeLog + " (" + guessedFunctionName + "): \n\n";
+
+ String expectedContent = expectedChangeLogEntry + changeLogContent;
+
+ // Read in content written to file
+ StringBuffer actualContent = new StringBuffer();
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ new FileInputStream(changelogFile.getLocation().toFile())));
+ String line;
+ while ( (line = br.readLine()) != null ) {
+ actualContent.append(line + "\n");
+ }
+ // Assert proper content has been added
+ assertEquals(expectedContent, actualContent.toString());
+ }
+
+ @Test
+ public void testGetSetDefaultContent() {
+ final String defaultContent = "DISCLAIMER: default ChangeLog content for new files?";
+ clogWriter.setDefaultContent(defaultContent);
+ assertEquals(defaultContent, clogWriter.getDefaultContent());
+ }
+
+
+ /**
+ * Test the use of default text.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canWriteChangeLogToEmptyChangeLogButWithSomeDefaultContent() throws Exception {
+ // set GNU formatter
+ clogWriter.setFormatter(new GNUFormat());
+
+ // Open up a new ChangeLog file at newPathToChangeLog with empty content
+ // and get the IEditorPart
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ "".getBytes()); // no content
+ String destinationPath = "/this/is/some/random/path";
+ IFile emptyChangeLogFile = project.addFileToProject(destinationPath, CHANGELOG_FILE_NAME,
+ newFileInputStream);
+ IEditorPart editorContent = EditorHelper.openEditor(emptyChangeLogFile);
+ clogWriter.setChangelog(editorContent);
+
+ String authorName = "Test Author";
+ String email = "test@example.com";
+ clogWriter.setDateLine(clogWriter.getFormatter().formatDateLine(
+ authorName, email));
+ clogWriter.setChangelogLocation(destinationPath + "/" + CHANGELOG_FILE_NAME);
+
+ // Set some default content
+ String defaultContent = "Removed.";
+ clogWriter.setDefaultContent(defaultContent);
+
+ String relativePathOfChangedFile = "path/to/file/for/new/entry/test.c";
+ clogWriter.setEntryFilePath( destinationPath + "/" + relativePathOfChangedFile );
+
+ clogWriter.setGuessedFName("");
+
+ // Write changelog to buffer - need to save for persistence
+ clogWriter.writeChangeLog();
+
+ // above written content is not persistent yet; save it to make it persistent
+ clogWriter.getChangelog().doSave(null);
+
+ // Construct the changelog entry by hand and match it with what has been written
+ String expectedChangeLogEntry = new GNUFormat().formatDateLine(authorName, email);
+ expectedChangeLogEntry += "\t* " + relativePathOfChangedFile + ": "
+ + defaultContent + "\n";
+
+ // Read in content written to file
+ StringBuffer actualContent = new StringBuffer();
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ new FileInputStream(emptyChangeLogFile.getLocation().toFile())));
+ String line;
+ while ( (line = br.readLine()) != null ) {
+ actualContent.append(line + "\n");
+ }
+ // Assert proper content has been added
+ assertEquals(expectedChangeLogEntry, actualContent.toString());
+ }
+}
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/TestsPluginActivator.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/TestsPluginActivator.java
new file mode 100644
index 0000000000..7c7270b110
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/core/tests/TestsPluginActivator.java
@@ -0,0 +1,50 @@
+package org.eclipse.linuxtools.changelog.core.tests;
+
+import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.osgi.framework.BundleContext;
+
+/**
+ * The activator class controls the plug-in life cycle
+ */
+public class TestsPluginActivator extends AbstractUIPlugin {
+
+ // The plug-in ID
+ public static final String PLUGIN_ID = "org.eclipse.linuxtools.changelog.tests"; //$NON-NLS-1$
+
+ // The shared instance
+ private static TestsPluginActivator plugin;
+
+ /**
+ * The constructor
+ */
+ public TestsPluginActivator() {
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
+ */
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+ plugin = this;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+ */
+ public void stop(BundleContext context) throws Exception {
+ plugin = null;
+ super.stop(context);
+ }
+
+ /**
+ * Returns the shared instance
+ *
+ * @return the shared instance
+ */
+ public static TestsPluginActivator getDefault() {
+ return plugin;
+ }
+
+}
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/parsers/tests/CParserTest.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/parsers/tests/CParserTest.java
new file mode 100644
index 0000000000..69f331f674
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/parsers/tests/CParserTest.java
@@ -0,0 +1,431 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc. and others.
+ * 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.linuxtools.changelog.parsers.tests;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.eclipse.linuxtools.changelog.tests.helpers.EditorHelper.openEditor;
+import static org.eclipse.linuxtools.changelog.tests.helpers.EditorHelper.closeEditor;
+import static org.eclipse.linuxtools.changelog.tests.helpers.EditorHelper.getContent;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IStorage;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.TextSelection;
+import org.eclipse.linuxtools.changelog.parsers.CParser;
+import org.eclipse.linuxtools.changelog.tests.fixtures.CStringStorage;
+import org.eclipse.linuxtools.changelog.tests.fixtures.CStringStorageInput;
+import org.eclipse.linuxtools.changelog.tests.fixtures.ChangeLogTestProject;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IStorageEditorInput;
+import org.eclipse.ui.texteditor.AbstractTextEditor;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+
+/**
+ * CParser test suite.
+ *
+ */
+public class CParserTest {
+
+ // A unique string to mark the place of current selection in source code
+ private static final String OFFSET_MARKER = "<# selection #>";
+ // The parser under test
+ private CParser cParser;
+ // A faked project
+ private ChangeLogTestProject project;
+ // The IEditorPart corresponding to the ChangeLog file
+ private IEditorPart cppSourceEditorPart = null;
+
+ @Before
+ public void setUp() throws Exception {
+ cParser = new CParser();
+ project = new ChangeLogTestProject("c-parser-test-project");
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ // Tests in this class use javaSourceEditorPart. In order to avoid
+ // spill-over from previous runs, truncate content (i.e. manually set
+ // content to the empty string).
+ if (cppSourceEditorPart != null) {
+ AbstractTextEditor castEditor = (AbstractTextEditor) cppSourceEditorPart;
+ IDocumentProvider iDocProvider = castEditor.getDocumentProvider();
+ IDocument changelogContentDoc = iDocProvider.getDocument(castEditor.getEditorInput());
+ changelogContentDoc.set("");
+ cppSourceEditorPart.doSave(null);
+ // Also close open editor in order for default content to work.
+ // I.e. avoid spill over from previous test runs
+ closeEditor(cppSourceEditorPart);
+ }
+ project.getTestProject().delete(true, true, null); // dispose
+ }
+
+ /**
+ * Given an IEditorPart we should be able to retrieve the currently
+ * function active C function inside a C source file.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canParseCurrentFunctionFromCFile() throws Exception {
+ // make testproject a C project
+ project.addCNature();
+
+ final String expectedFunctionName = "doSomething";
+ final String cSourceCode = "static int " + expectedFunctionName + "(char *test)\n"
+ + "{\n"
+ + "int index = 0;\n"
+ + "// " + OFFSET_MARKER + "\n"
+ + "return 0;\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/some_c_file.c")));
+
+ // Add some_c_file.c to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ cSourceCode.getBytes());
+ IFile cSourceFile = project.addFileToProject(
+ "/src",
+ "some_c_file.c", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/some_c_file.c")));
+
+ // Open a source file and get the IEditorPart
+ cppSourceEditorPart = openEditor(cSourceFile);
+ assertEquals(cSourceCode, getContent(cppSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( cppSourceEditorPart instanceof AbstractTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = cSourceCode.indexOf(OFFSET_MARKER);
+ assertTrue(selectionStart >= 0);
+ int selectionLength = OFFSET_MARKER.length();
+ AbstractTextEditor cEditor = (AbstractTextEditor) cppSourceEditorPart;
+ cEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualFunctionName = cParser.parseCurrentFunction(cppSourceEditorPart);
+
+ assertEquals(expectedFunctionName, actualFunctionName);
+ }
+
+ /**
+ * Given an IEditorPart and not being inside any function within a C
+ * source file, no function should be determined.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canDetermineThatInNoFunctionInCFile() throws Exception {
+ // make test project a C project
+ project.addCNature();
+
+ final String cSourceCode = "// Prototype " + OFFSET_MARKER + "\n"
+ + "static int doSomething(char *test);\n\n"
+ + "static int doSomething(char *test)\n"
+ + "{\n"
+ + "int index = 0;\n"
+ + "return 0;\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/some_c_file.c")));
+
+ // Add some_c_file.c to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ cSourceCode.getBytes());
+ IFile cSourceFile = project.addFileToProject(
+ "/src",
+ "some_c_file.c", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/some_c_file.c")));
+
+ // Open a source file and get the IEditorPart
+ cppSourceEditorPart = openEditor(cSourceFile);
+ assertEquals(cSourceCode, getContent(cppSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( cppSourceEditorPart instanceof AbstractTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = cSourceCode.indexOf(OFFSET_MARKER);
+ assertTrue(selectionStart >= 0);
+ int selectionLength = OFFSET_MARKER.length();
+ AbstractTextEditor cEditor = (AbstractTextEditor) cppSourceEditorPart;
+ cEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualFunctionName = cParser.parseCurrentFunction(cppSourceEditorPart);
+
+ assertEquals("" /* expect empty function name */, actualFunctionName);
+ }
+
+ /**
+ * Given an IEditorPart we should be able to retrieve the currently selected
+ * variable identifier inside a C++ file.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canParseCurrentlySelectedVariableIdentifierInCppFile() throws Exception {
+ // make test project a C++ project
+ project.addCCNature();
+
+ final String expectedIdentifier = "myIdentifier";
+ final String className = "shape";
+ final String cppSourceCode = "class " + className + " {\n"
+ + "int x;\n"
+ + "int y;\n"
+ + "private:\n"
+ + "int color;\n"
+ + "float " + expectedIdentifier + ";\n"
+ + "void set_color(int color);\n"
+ +"}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/shape.h")));
+
+ // Add shape.h to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ cppSourceCode.getBytes());
+ IFile cppSourceFile = project.addFileToProject(
+ "/src",
+ "shape.h", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/shape.h")));
+
+ // Open a source file and get the IEditorPart
+ cppSourceEditorPart = openEditor(cppSourceFile);
+ // make sure editor content is correct
+ assertEquals(cppSourceCode, getContent(cppSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( cppSourceEditorPart instanceof AbstractTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = cppSourceCode.indexOf(expectedIdentifier);
+ assertTrue(selectionStart >= 0);
+ // shouldn't need to mark whole length of identifier.
+ int selectionLength = expectedIdentifier.length() - 3;
+ AbstractTextEditor cppEditor = (AbstractTextEditor) cppSourceEditorPart;
+ cppEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualIdentifier = cParser.parseCurrentFunction(cppSourceEditorPart);
+
+ assertEquals(className + "." + expectedIdentifier, actualIdentifier);
+ }
+
+ /**
+ * Given an IEditorPart and not selected any variable identifier in a class, we should
+ * get the class name as selected function name only.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canParseClassNameIfNoVariableIdentifierSelectedInCppFile() throws Exception {
+ // make test project a C++ project
+ project.addCCNature();
+
+ final String className = "shape";
+ final String cppSourceCode = "class " + className + " {\n"
+ + "int x;\n"
+ + "int y;\n"
+ + "// " + OFFSET_MARKER + "\n"
+ + "private:\n"
+ + "int color;\n"
+ + "void set_color(int color);\n"
+ +"}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/shape.h")));
+
+ // Add shape.h to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ cppSourceCode.getBytes());
+ IFile cppSourceFile = project.addFileToProject(
+ "/src",
+ "shape.h", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/shape.h")));
+
+ // Open a source file and get the IEditorPart
+ cppSourceEditorPart = openEditor(cppSourceFile);
+ // make sure editor content is correct
+ assertEquals(cppSourceCode, getContent(cppSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( cppSourceEditorPart instanceof AbstractTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = cppSourceCode.indexOf(OFFSET_MARKER);
+ assertTrue(selectionStart >= 0);
+ int selectionLength = OFFSET_MARKER.length();
+ AbstractTextEditor cppEditor = (AbstractTextEditor) cppSourceEditorPart;
+ cppEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualFunction = cParser.parseCurrentFunction(cppSourceEditorPart);
+
+ assertEquals(className, actualFunction);
+ }
+
+ /**
+ * Given an IEditorPart and current selection is inside a method,
+ * CParser should be able to figure that out.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canParseCurrentMethodNameInCppFile() throws Exception {
+ // make test project a C++ project
+ project.addCCNature();
+
+ final String expectedMethodName = "circle::area";
+ final String cppSourceCode = "#include \"circle.h\"\n"
+ + "#include <math.h>\n"
+ + "float " + expectedMethodName + "() {\n"
+ + "// " + OFFSET_MARKER + "\n"
+ + "return this->radius * this-> radius * M_PI\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/circle.cpp")));
+
+ // Add shape.h to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ cppSourceCode.getBytes());
+ IFile cppSourceFile = project.addFileToProject(
+ "/src",
+ "circle.cpp", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/circle.cpp")));
+
+ // Open a source file and get the IEditorPart
+ cppSourceEditorPart = openEditor(cppSourceFile);
+ // make sure editor content is correct
+ assertEquals(cppSourceCode, getContent(cppSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( cppSourceEditorPart instanceof AbstractTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = cppSourceCode.indexOf(OFFSET_MARKER);
+ assertTrue(selectionStart >= 0);
+ int selectionLength = OFFSET_MARKER.length();
+ AbstractTextEditor cppEditor = (AbstractTextEditor) cppSourceEditorPart;
+ cppEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualMethodName = cParser.parseCurrentFunction(cppSourceEditorPart);
+
+ assertEquals(expectedMethodName, actualMethodName);
+ }
+
+ /**
+ * Given an IEditorPart and current selection is inside a method,
+ * CParser should be able to figure that out.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canParseCurrentFunctionInCppFile() throws Exception {
+ // make test project a C++ project
+ project.addCCNature();
+
+ final String expectedFunction = "main";
+ final String cppSourceCode = "#include \"circle.h\"\n"
+ + "#include <math.h>\n"
+ + "float circle::area() {\n"
+ + "return this->radius * this-> radius * M_PI\n"
+ + "}\n"
+ + "int " + expectedFunction + "() {\n"
+ + "int some_var = 0;\n"
+ + "// " + OFFSET_MARKER + "\n"
+ + "return 0;\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/circle.cpp")));
+
+ // Add shape.h to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ cppSourceCode.getBytes());
+ IFile cppSourceFile = project.addFileToProject(
+ "/src",
+ "circle.cpp", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/circle.cpp")));
+
+ // Open a source file and get the IEditorPart
+ cppSourceEditorPart = openEditor(cppSourceFile);
+ // make sure editor content is correct
+ assertEquals(cppSourceCode, getContent(cppSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( cppSourceEditorPart instanceof AbstractTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = cppSourceCode.indexOf(OFFSET_MARKER);
+ assertTrue(selectionStart >= 0);
+ int selectionLength = OFFSET_MARKER.length();
+ AbstractTextEditor cppEditor = (AbstractTextEditor) cppSourceEditorPart;
+ cppEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualFunction = cParser.parseCurrentFunction(cppSourceEditorPart);
+
+ assertEquals(expectedFunction, actualFunction);
+ }
+
+ /**
+ * Given an IStorageEditorInput we should be able to retrieve the currently
+ * active C function.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canParseCurrentFunctionFromCStringInIStorageEditorInput() throws Exception {
+ final String expectedFunctionName = "doSomething";
+ final String cSourceCode = "static int " + expectedFunctionName + "(char *test)\n"
+ + "{\n"
+ + "int index = 0;\n"
+ + "// " + OFFSET_MARKER + "\n"
+ + "return 0;\n"
+ + "}\n";
+
+ // prepare IStorageEditorInput
+ IStorage cStringStorage = new CStringStorage(cSourceCode);
+ IStorageEditorInput cStringStorageEditorInput = new CStringStorageInput(cStringStorage);
+
+ // Figure out the desired offset
+ int selectOffset = cSourceCode.indexOf(OFFSET_MARKER);
+ assertTrue(selectOffset >= 0);
+
+ final String actualFunctionName = cParser.parseCurrentFunction(cStringStorageEditorInput, selectOffset);
+
+ assertEquals(expectedFunctionName, actualFunctionName);
+ }
+}
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/parsers/tests/JavaParserTest.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/parsers/tests/JavaParserTest.java
new file mode 100644
index 0000000000..1fe9641c51
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/parsers/tests/JavaParserTest.java
@@ -0,0 +1,462 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc. and others.
+ * 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.linuxtools.changelog.parsers.tests;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.eclipse.linuxtools.changelog.tests.helpers.EditorHelper.openEditor;
+import static org.eclipse.linuxtools.changelog.tests.helpers.EditorHelper.closeEditor;
+import static org.eclipse.linuxtools.changelog.tests.helpers.EditorHelper.getContent;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.TextSelection;
+import org.eclipse.linuxtools.changelog.parsers.java.JavaParser;
+import org.eclipse.linuxtools.changelog.tests.fixtures.ChangeLogTestProject;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
+import org.eclipse.ui.texteditor.AbstractTextEditor;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+
+/**
+ * JavaParser test suite.
+ *
+ */
+public class JavaParserTest {
+
+ // A unique string to mark the place of current selection in source code
+ private static final String OFFSET_MARKER = "<# selection #>";
+ // The parser under test
+ private JavaParser javaParser;
+ // A faked project
+ private ChangeLogTestProject project;
+ // The IEditorPart corresponding to the ChangeLog file
+ private IEditorPart javaSourceEditorPart = null;
+
+ @Before
+ public void setUp() throws Exception {
+ javaParser = new JavaParser();
+ project = new ChangeLogTestProject("java-parser-test-project");
+ // make it a Java project
+ project.addJavaNature();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ // Tests in this class use javaSourceEditorPart. In order to avoid
+ // spill-over from previous runs, truncate content (i.e. manually set
+ // content to the empty string).
+ if (javaSourceEditorPart != null) {
+ AbstractTextEditor castEditor = (AbstractTextEditor) javaSourceEditorPart;
+ IDocumentProvider iDocProvider = castEditor.getDocumentProvider();
+ IDocument changelogContentDoc = iDocProvider.getDocument(castEditor.getEditorInput());
+ changelogContentDoc.set("");
+ javaSourceEditorPart.doSave(null);
+ // Also close open editor in order for default content to work.
+ // I.e. avoid spill over from previous test runs
+ closeEditor(javaSourceEditorPart);
+ }
+ project.getTestProject().delete(true, true, null); // dispose
+ }
+
+ /**
+ * Given an IEditorPart we should be able to retrieve the current function
+ * we are in.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canParseCurrentMethod() throws Exception {
+ final String expectedMethodName = "doSomething";
+ final String javaSourceCode = "public class JavaParserExampleClass {\n"
+ + "private void " + expectedMethodName + "(String param) {\n"
+ + "// " + OFFSET_MARKER + "\n"
+ + "}\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Add JavaParserExampleClass.java to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ javaSourceCode.getBytes());
+ IFile javaSourceFile = project.addFileToProject(
+ "/src/org/eclipse/changelog/tests",
+ "JavaParserExampleClass.java", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Open a source file and get the IEditorPart
+ javaSourceEditorPart = openEditor(javaSourceFile);
+ // make sure changelog editor content is right before merging
+ assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( javaSourceEditorPart instanceof AbstractDecoratedTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = javaSourceCode.indexOf(OFFSET_MARKER);
+ assertTrue(selectionStart >= 0);
+ int selectionLength = OFFSET_MARKER.length();
+ AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
+ javaEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualMethodName = javaParser.parseCurrentFunction(javaSourceEditorPart);
+
+ assertEquals(expectedMethodName, actualMethodName);
+ }
+
+ /**
+ * Given an IEditorPart we should be able to retrieve the currently selected
+ * field.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canParseSelectedField() throws Exception {
+ final String expectedFieldName = "testVar";
+ final String javaSourceCode = "public class JavaParserExampleClass {\n"
+ + "private String " + expectedFieldName + " = null;\n"
+ + "private void someMethod(String param) {\n"
+ + "// empty \n"
+ + "}\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Add JavaParserExampleClass.java to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ javaSourceCode.getBytes());
+ IFile javaSourceFile = project.addFileToProject(
+ "/src/org/eclipse/changelog/tests",
+ "JavaParserExampleClass.java", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Open a source file and get the IEditorPart
+ javaSourceEditorPart = openEditor(javaSourceFile);
+ // make sure changelog editor content is right before merging
+ assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( javaSourceEditorPart instanceof AbstractDecoratedTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = javaSourceCode.indexOf(expectedFieldName);
+ assertTrue(selectionStart >= 0);
+ int selectionLength = expectedFieldName.length() - 3; // Shouldn't need to select the entire field
+ AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
+ javaEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualFieldName = javaParser.parseCurrentFunction(javaSourceEditorPart);
+
+ assertEquals(expectedFieldName, actualFieldName);
+ }
+
+ /**
+ * Given an IEditorPart and current selection is in a static instance initializer
+ * block, JavaParser should be able to figure out that we were in an static
+ * initializer block.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canIdentifyStaticInitializerWhenInStaticInstanceInitializer() throws Exception {
+ final String javaSourceCode = "public class JavaParserExampleClass {\n"
+ + "private String someStrVariable = null;\n"
+ // create static instance initializer block
+ + "{\n"
+ + "// " + OFFSET_MARKER + "\n"
+ + "}\n"
+ + "private void someMethod(String param) {\n"
+ + "// empty \n"
+ + "}\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Add JavaParserExampleClass.java to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ javaSourceCode.getBytes());
+ IFile javaSourceFile = project.addFileToProject(
+ "/src/org/eclipse/changelog/tests",
+ "JavaParserExampleClass.java", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Open a source file and get the IEditorPart
+ javaSourceEditorPart = openEditor(javaSourceFile);
+ // make sure changelog editor content is right before merging
+ assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( javaSourceEditorPart instanceof AbstractDecoratedTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = javaSourceCode.indexOf(OFFSET_MARKER);
+ assertTrue(selectionStart >= 0);
+ int selectionLength = OFFSET_MARKER.length();
+ AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
+ javaEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
+
+ assertEquals(JavaParser.STATIC_INITIALIZER_NAME, actualFunctionName);
+ }
+
+ /**
+ * Given an IEditorPart and current selection is in a static class initializer
+ * block, JavaParser should be able to figure out that we were in an static
+ * initializer block.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canIdentifyStaticInitializerWhenInStaticClassInitializer() throws Exception {
+ final String javaSourceCode = "public class JavaParserExampleClass {\n"
+ + "private String someStrVariable = null;\n"
+ // create static class initializer block
+ + "static {\n"
+ + "// " + OFFSET_MARKER + "\n"
+ + "}\n"
+ + "private void someMethod(String param) {\n"
+ + "// empty \n"
+ + "}\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Add JavaParserExampleClass.java to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ javaSourceCode.getBytes());
+ IFile javaSourceFile = project.addFileToProject(
+ "/src/org/eclipse/changelog/tests",
+ "JavaParserExampleClass.java", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Open a source file and get the IEditorPart
+ javaSourceEditorPart = openEditor(javaSourceFile);
+ // make sure changelog editor content is right before merging
+ assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( javaSourceEditorPart instanceof AbstractDecoratedTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = javaSourceCode.indexOf(OFFSET_MARKER);
+ assertTrue(selectionStart >= 0);
+ int selectionLength = OFFSET_MARKER.length();
+ AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
+ javaEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
+
+ assertEquals(JavaParser.STATIC_INITIALIZER_NAME, actualFunctionName);
+ }
+
+ /**
+ * Given an IEditorPart and current selection is inside a method within a nested
+ * class, JavaParser should return a "nestedClass.methodName" construct for the
+ * current function.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canIdentifyMethodWithinNestedClass() throws Exception {
+ final String nestedClassName = "Encapsulated";
+ final String currentMethodName = "getString";
+ final String javaSourceCode = "public class JavaParserExampleClass {\n"
+ + "private String someStrVariable = null;\n"
+ + "static {\n"
+ + "// empty \n"
+ + "}\n"
+ + "private void someMethod(String param) {\n"
+ + "// empty \n"
+ + "}\n"
+ + "private class " + nestedClassName + "{\n"
+ + "public String " + currentMethodName + "() throws Exception {\n"
+ + "// " + OFFSET_MARKER + "\n"
+ + "return \"returnString\";\n"
+ + "}\n"
+ + "}\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Add JavaParserExampleClass.java to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ javaSourceCode.getBytes());
+ IFile javaSourceFile = project.addFileToProject(
+ "/src/org/eclipse/changelog/tests",
+ "JavaParserExampleClass.java", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Open a source file and get the IEditorPart
+ javaSourceEditorPart = openEditor(javaSourceFile);
+ // make sure changelog editor content is right before merging
+ assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( javaSourceEditorPart instanceof AbstractDecoratedTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = javaSourceCode.indexOf(OFFSET_MARKER);
+ assertTrue(selectionStart >= 0);
+ int selectionLength = OFFSET_MARKER.length();
+ AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
+ javaEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String expectedFunctionName = nestedClassName + "." + currentMethodName;
+ final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
+
+ assertEquals(expectedFunctionName, actualFunctionName);
+ }
+
+ /**
+ * Given an IEditorPart and currently a field within a nested
+ * class is selected, JavaParser should return a "nestedClass.fieldName"
+ * construct for the current function.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canIdentifyFieldWithinNestedClass() throws Exception {
+ final String nestedClassName = "Encapsulated";
+ final String currentFieldName = "variableInNestedClass";
+ final String javaSourceCode = "public class JavaParserExampleClass {\n"
+ + "private String someStrVariable = null;\n"
+ + "static {\n"
+ + "// empty \n"
+ + "}\n"
+ + "private void someMethod(String param) {\n"
+ + "// empty \n"
+ + "}\n"
+ + "private class " + nestedClassName + "{\n"
+ + "private int " + currentFieldName + " = 10;\n"
+ + "public String getString() throws Exception {\n"
+ + "// return a String, yay ;-)\n"
+ + "return \"returnString\";\n"
+ + "}\n"
+ + "}\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Add JavaParserExampleClass.java to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ javaSourceCode.getBytes());
+ IFile javaSourceFile = project.addFileToProject(
+ "/src/org/eclipse/changelog/tests",
+ "JavaParserExampleClass.java", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Open a source file and get the IEditorPart
+ javaSourceEditorPart = openEditor(javaSourceFile);
+ // make sure changelog editor content is right before merging
+ assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( javaSourceEditorPart instanceof AbstractDecoratedTextEditor );
+
+ // Select the snippet we want
+ int selectionStart = javaSourceCode.indexOf(currentFieldName);
+ assertTrue(selectionStart >= 0);
+ // select only a part of field name, as this should be sufficient
+ int selectionLength = currentFieldName.length() - 3;
+ AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
+ javaEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String expectedFunctionName = nestedClassName + "." + currentFieldName;
+ final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
+
+ assertEquals(expectedFunctionName, actualFunctionName);
+ }
+
+ /**
+ * Given an IEditorPart and current selection is inside a class but not within a
+ * method, not selecting a field and not in a nested class (somewhere else in the
+ * class) it should return an empty string for the function.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canDetermineThatSelectionIsJustInClass() throws Exception {
+ // Apparently comments don't show up in the compilation units. Makes
+ // sense, right? But we can't use the OFFSET_MARKER trick in this case.
+ final String javaSourceCode = "public class JavaParserExampleClass {\n"
+ + "private String someStrVariable = null;\n"
+ + "\n" // want to select this line indexOf(';') + 2
+ + "\n"
+ + "private void someMethod(String param) {\n"
+ + "// empty \n"
+ + "}\n"
+ + "}\n";
+
+ assertNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Add JavaParserExampleClass.java to project
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ javaSourceCode.getBytes());
+ IFile javaSourceFile = project.addFileToProject(
+ "/src/org/eclipse/changelog/tests",
+ "JavaParserExampleClass.java", newFileInputStream);
+
+ assertNotNull(project.getTestProject().findMember( new Path(
+ "/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
+
+ // Open a source file and get the IEditorPart
+ javaSourceEditorPart = openEditor(javaSourceFile);
+ // make sure changelog editor content is right before merging
+ assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
+
+ // make sure we have the proper editor type
+ assertTrue( javaSourceEditorPart instanceof AbstractDecoratedTextEditor );
+
+ // Select the right point
+ int selectionStart = javaSourceCode.indexOf(';') + 2;
+ assertTrue(selectionStart >= 2);
+ int selectionLength = 0;
+ AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
+ javaEditor.getSelectionProvider().setSelection(
+ new TextSelection(selectionStart, selectionLength));
+
+ final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
+
+ assertEquals("" /* expect empty string */, actualFunctionName);
+ }
+}
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/CStringStorage.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/CStringStorage.java
new file mode 100644
index 0000000000..7cd1ee8ec4
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/CStringStorage.java
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc. and others.
+ * 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.linuxtools.changelog.tests.fixtures;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.eclipse.core.resources.IStorage;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+
+/**
+ * Helper class for a IStorageEditorInput fixture.
+ *
+ */
+public class CStringStorage implements IStorage {
+ private String string;
+
+ public CStringStorage(String input) {
+ this.string = input;
+ }
+
+ public InputStream getContents() throws CoreException {
+ return new ByteArrayInputStream(string.getBytes());
+ }
+
+ public IPath getFullPath() {
+ return null;
+ }
+
+ public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
+ return null;
+ }
+
+ public String getName() {
+ int len = Math.min(5, string.length());
+ return string.substring(0, len).concat("..."); //$NON-NLS-1$
+ }
+
+ public boolean isReadOnly() {
+ return true;
+ }
+}
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/CStringStorageInput.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/CStringStorageInput.java
new file mode 100644
index 0000000000..3b5eb912a0
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/CStringStorageInput.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc. and others.
+ * 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.linuxtools.changelog.tests.fixtures;
+
+import org.eclipse.core.resources.IStorage;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.ui.IPersistableElement;
+import org.eclipse.ui.IStorageEditorInput;
+
+/**
+ * An IStorageEditorInput fixture. Used in CParserTest.
+ *
+ */
+public class CStringStorageInput implements IStorageEditorInput {
+
+ private IStorage storage;
+
+ public CStringStorageInput(IStorage storage) {
+ this.storage = storage;
+ }
+
+ public boolean exists() {
+ return true;
+ }
+
+ public ImageDescriptor getImageDescriptor() {
+ return null;
+ }
+
+ public String getName() {
+ return storage.getName();
+ }
+
+ public IPersistableElement getPersistable() {
+ return null;
+ }
+
+ public IStorage getStorage() {
+ return storage;
+ }
+
+ public String getToolTipText() {
+ return "String-based file: " + storage.getName();
+ }
+
+ public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
+ return null;
+ }
+
+}
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/ChangeLogTestProject.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/ChangeLogTestProject.java
new file mode 100644
index 0000000000..144e31409d
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/ChangeLogTestProject.java
@@ -0,0 +1,148 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc. and others.
+ * 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.linuxtools.changelog.tests.fixtures;
+
+import java.io.InputStream;
+
+import org.eclipse.cdt.core.CCorePlugin;
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
+
+/**
+ * Test fixture for the changelog plug-in tests.
+ *
+ * Functionality:
+ * - Create a new project in the current workspace
+ *
+ */
+public class ChangeLogTestProject {
+
+ private IProject testProject;
+
+ /**
+ * Create a new ChangelogTestProject
+ *
+ * @param projectName The name of the project to be created.
+ * @throws Exception
+ */
+ public ChangeLogTestProject(String projectName) throws Exception {
+ testProject = createNewProject(projectName);
+ }
+
+ /**
+ * @return the testProject
+ */
+ public IProject getTestProject() {
+ return testProject;
+ }
+
+ /**
+ * @param testProject the testProject to set
+ */
+ public void setTestProject(IProject testProject) {
+ this.testProject = testProject;
+ }
+
+ /**
+ * Adds a file to this project at the specified <code>path</code>
+ * and the provided <code>filename</code>. If segments of <code>path</code>
+ * do not exist, they will be created.
+ *
+ * @param destPath The path relative to the project (use '/' as path separator).
+ * @param filename The name of the to be created file
+ * @param fileInputStream A stream to the new files content.
+ */
+ public IFile addFileToProject(String destPath, String filename, InputStream fileInputStream) throws CoreException {
+ String[] pathSegments = destPath.split("/");
+
+ IContainer parent = this.testProject;
+ for (String segment: pathSegments) {
+ if (segment.equals("")) {
+ continue; // ignore
+ }
+ IResource segmentResource = parent.findMember(new Path(IPath.SEPARATOR + segment));
+ if (segmentResource == null) {
+ // create folder
+ IFolder newFolder = parent.getFolder(new Path(segment));
+ newFolder.create(false, true, null);
+ parent = newFolder;
+ } else {
+ // resource existed
+ parent = (IContainer)segmentResource;
+ }
+ }
+ // Finally add the file
+ IFile newFile = parent.getFile(new Path(filename));
+ if (fileInputStream == null) {
+ throw new IllegalStateException("fileInputStream must not be null");
+ }
+ newFile.create(fileInputStream, false, null);
+
+ // refresh project
+ this.testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
+
+ return newFile;
+ }
+
+ /**
+ * Add the Java nature to this project. I.e. make it a Java project.
+ */
+ public IJavaProject addJavaNature() throws CoreException {
+ IProjectDescription description = this.testProject.getDescription();
+ String[] natures = description.getNatureIds();
+ String[] newNatures = new String[natures.length + 1];
+ System.arraycopy(natures, 0, newNatures, 0, natures.length);
+ newNatures[natures.length] = JavaCore.NATURE_ID;
+ description.setNatureIds(newNatures);
+ this.testProject.setDescription(description, null);
+ return JavaCore.create(this.testProject);
+ }
+
+ /**
+ * Add the C nature to this project. I.e. make it a C project.
+ * @throws CoreException
+ */
+ public void addCNature() throws CoreException {
+ this.testProject = CCorePlugin.getDefault().createCDTProject(
+ testProject.getDescription(), testProject, null);
+ }
+
+ /**
+ * Add the C++ nature to this project. I.e. make it a C++ project.
+ * @throws CoreException
+ */
+ public void addCCNature() throws CoreException {
+ addCNature();
+ CCorePlugin.getDefault().convertProjectFromCtoCC(testProject, null);
+ }
+
+ /**
+ * Create a new Eclipse project in the current workspace
+ *
+ * @param name
+ * @throws CoreException if project creation fails for some reason.
+ * @return The newly created project.
+ */
+ private IProject createNewProject(String name) throws CoreException {
+ IProject newProject = ResourcesPlugin.getWorkspace().getRoot()
+ .getProject(name);
+ newProject.create(null);
+ newProject.open(null); // needs to be open
+ return newProject;
+ }
+}
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/TestChangeLogTestProject.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/TestChangeLogTestProject.java
new file mode 100644
index 0000000000..654d820c74
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/fixtures/TestChangeLogTestProject.java
@@ -0,0 +1,150 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc. and others.
+ * 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.linuxtools.changelog.tests.fixtures;
+
+import static org.junit.Assert.*;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.IJavaProject;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.eclipse.cdt.core.model.CoreModel;
+
+/**
+ * @author Redhat Inc.
+ *
+ */
+public class TestChangeLogTestProject {
+
+ private ChangeLogTestProject project;
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ project = new ChangeLogTestProject("com.redhat.testchangelog.project");
+ }
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {
+ // dispose
+ project.getTestProject().delete(true, null);
+ }
+
+ /**
+ * Test method for
+ * {@link org.eclipse.linuxtools.changelog.tests.fixtures.ChangeLogTestProject#addFileToProject(java.lang.String, java.lang.String, java.io.InputStream)}
+ * .
+ */
+ @Test
+ public void testAddFileToProject() throws Exception {
+ String fileContent = "some content";
+ InputStream newFileInputStream = new ByteArrayInputStream(
+ fileContent.getBytes());
+ // Create file "testfile.txt" with content "some content" at
+ // "/this/is/a/testpath"
+ project.addFileToProject("/this/is/a/testpath", "testfile.txt",
+ newFileInputStream);
+ IProject p = project.getTestProject();
+
+ // Paths along the way should have been created
+ IResource member = p.findMember(new Path("/this"));
+ assertNotNull(member);
+ assertEquals("this", member.getName());
+ member = p.findMember(new Path("/this/is"));
+ assertNotNull(member);
+ assertEquals("is", member.getName());
+ member = p.findMember(new Path("/this/is/a"));
+ assertNotNull(member);
+ assertEquals("a", member.getName());
+ member = p.findMember(new Path("/this/is/a/testpath"));
+ assertNotNull(member);
+ assertEquals("testpath", member.getName());
+
+ // Testfile should be around
+ IResource createdFile = p.findMember(new Path("/this/is/a/testpath/testfile.txt"));
+ assertNotNull(createdFile);
+ assertEquals("this/is/a/testpath/testfile.txt", createdFile.getProjectRelativePath().toString());
+ assertEquals("testfile.txt", createdFile.getName());
+ assertTrue(createdFile instanceof IFile);
+
+ // Content should be "some content"
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ new FileInputStream(createdFile.getLocation().toFile())));
+ String actualContent = br.readLine();
+ assertEquals(fileContent, actualContent);
+
+ // this should throw an IllegalStateException
+ boolean exceptionTrown = false;
+ try {
+ project.addFileToProject("donnot/care", "not_important.java",
+ null /* should cause exception */);
+ } catch (IllegalStateException e) {
+ // pass
+ exceptionTrown = true;
+ }
+ assertTrue(exceptionTrown);
+ }
+
+ /**
+ * We should be able to add a Java nature to a project.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canAddJavaNature() throws Exception {
+ IJavaProject javaProject = this.project.addJavaNature();
+ assertNotNull(javaProject);
+ assertTrue(javaProject instanceof IJavaProject);
+ }
+
+ /**
+ * We should be able to add a C nature to a project.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canAddCNature() throws Exception {
+ IProject cProject = this.project.getTestProject();
+ assertTrue(!CoreModel.hasCNature(cProject));
+ // Add C nature
+ this.project.addCNature();
+ cProject = this.project.getTestProject();
+ assertTrue(CoreModel.hasCNature(cProject));
+ }
+
+ /**
+ * We should be able to add a C++ nature to a project.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void canAddCCNature() throws Exception {
+ IProject ccProject = this.project.getTestProject();
+ assertTrue(!CoreModel.hasCCNature(ccProject));
+ // Add C++ nature
+ this.project.addCCNature();
+ ccProject = this.project.getTestProject();
+ assertTrue(CoreModel.hasCCNature(ccProject));
+ }
+
+}
diff --git a/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/helpers/EditorHelper.java b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/helpers/EditorHelper.java
new file mode 100644
index 0000000000..8df14c550a
--- /dev/null
+++ b/changelog/org.eclipse.linuxtools.changelog.tests/src/org/eclipse/linuxtools/changelog/tests/helpers/EditorHelper.java
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc. and others.
+ * 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.linuxtools.changelog.tests.helpers;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.linuxtools.changelog.core.ChangelogPlugin;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.texteditor.AbstractTextEditor;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+
+public class EditorHelper {
+
+ /**
+ * Open file associated with <code>diskresource</code> in current
+ * workspace.
+ *
+ * @param diskresource The file to be opened in the current workspace.
+ * @return The IEditorPart associated with the opened file in the current workspace
+ * or null if opening fails.
+ */
+ public static IEditorPart openEditor(IFile diskresource) {
+ IWorkbench ws = ChangelogPlugin.getDefault().getWorkbench();
+ try {
+ return org.eclipse.ui.ide.IDE.openEditor(ws
+ .getActiveWorkbenchWindow().getActivePage(), diskresource,
+ true);
+ } catch (PartInitException e) {
+ e.printStackTrace();
+
+ return null;
+ }
+ }
+
+ /**
+ * Close editor if it is active.
+ */
+ public static void closeEditor(final IEditorPart editor) {
+ if (editor.getSite().getWorkbenchWindow().getActivePage() != null) {
+ PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
+ public void run() {
+ // close editor
+ editor.getSite().getWorkbenchWindow().getActivePage()
+ .closeEditor(editor, false);
+ }
+ });
+ }
+ }
+
+ /**
+ * Return the content of the given IEditorPart as String.
+ * @param editorPart
+ * @return Content of editorPart.
+ */
+ public static String getContent(IEditorPart editorPart) {
+ AbstractTextEditor castEditor = (AbstractTextEditor) editorPart;
+ IDocumentProvider provider = castEditor.getDocumentProvider();
+ IDocument document = provider.getDocument(castEditor.getEditorInput());
+ return document.get();
+ }
+}

Back to the top