Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/GitCopyrightAdapterTest.java')
-rw-r--r--bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/GitCopyrightAdapterTest.java140
1 files changed, 140 insertions, 0 deletions
diff --git a/bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/GitCopyrightAdapterTest.java b/bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/GitCopyrightAdapterTest.java
new file mode 100644
index 00000000..825e2424
--- /dev/null
+++ b/bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/GitCopyrightAdapterTest.java
@@ -0,0 +1,140 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation 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
+ *
+ * Contributors:
+ * Tomasz Zarna <Tomasz.Zarna@pl.ibm.com> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.releng.tests;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.eclipse.core.resources.IFile;
+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.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.egit.core.op.ConnectProviderOperation;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.PersonIdent;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.util.FileUtils;
+import org.eclipse.releng.tools.git.GitCopyrightAdapter;
+import org.junit.Assert;
+
+public class GitCopyrightAdapterTest extends LocalDiskRepositoryTest {
+
+ private static final IProgressMonitor NULL_MONITOR = new NullProgressMonitor();
+
+ private static final String PROJECT_NAME = "Project";
+
+ private static final String FILE1_NAME = "Foo.java";
+
+ private static final String FILE2_NAME = "Bar.java";
+
+ private Repository db;
+
+ private File trash;
+
+ private File gitDir;
+
+ private IProject project;
+
+ private IFile file1;
+
+ public void setUp() throws Exception {
+ super.setUp();
+ db = createWorkRepository();
+ trash = db.getWorkTree();
+ gitDir = new File(trash, Constants.DOT_GIT);
+ project = createProject(PROJECT_NAME);
+ file1 = project.getFile(FILE1_NAME);
+ connect();
+ }
+
+ public void tearDown() throws Exception {
+ if (project.exists())
+ project.delete(true, true, NULL_MONITOR);
+ if (gitDir.exists())
+ FileUtils.delete(gitDir, FileUtils.RECURSIVE | FileUtils.RETRY);
+ super.tearDown();
+ }
+
+ public void testLastModifiedYear() throws Exception {
+ final Git git = new Git(db);
+ git.add().addFilepattern(PROJECT_NAME + "/" + FILE1_NAME).call();
+ final PersonIdent committer2011 = new PersonIdent(committer,
+ getDateForYear(2011));
+ git.commit().setMessage("old commit").setCommitter(committer2011)
+ .call();
+ git.add().addFilepattern(PROJECT_NAME + "/" + FILE2_NAME).call();
+ git.commit().setMessage("new commit").call();
+
+ final GitCopyrightAdapter adapter = new GitCopyrightAdapter(
+ new IResource[] { project });
+ adapter.initialize(NULL_MONITOR);
+ final int lastModifiedYear = adapter.getLastModifiedYear(file1,
+ NULL_MONITOR);
+
+ Assert.assertEquals(2011, lastModifiedYear);
+ }
+
+ public void testCopyrightUpdateComment() throws Exception {
+ final Git git = new Git(db);
+ git.add().addFilepattern(PROJECT_NAME + "/" + FILE1_NAME).call();
+ git.commit().setMessage("copyright update").call();
+
+ final GitCopyrightAdapter adapter = new GitCopyrightAdapter(
+ new IResource[] { project });
+ adapter.initialize(NULL_MONITOR);
+ final int lastModifiedYear = adapter.getLastModifiedYear(file1,
+ NULL_MONITOR);
+
+ Assert.assertEquals(0, lastModifiedYear);
+ }
+
+ private IProject createProject(String name) throws Exception {
+ final IProject project = ResourcesPlugin.getWorkspace().getRoot()
+ .getProject(name);
+ if (project.exists())
+ project.delete(true, null);
+ final IProjectDescription desc = ResourcesPlugin.getWorkspace()
+ .newProjectDescription(name);
+ desc.setLocation(new Path(new File(db.getWorkTree(), name).getPath()));
+ project.create(desc, null);
+ project.open(null);
+
+ final IFile file1 = project.getFile(FILE1_NAME);
+ file1.create(
+ new ByteArrayInputStream("Hello, world".getBytes(project
+ .getDefaultCharset())), false, null);
+
+ final IFile file2 = project.getFile(FILE2_NAME);
+ file2.create(
+ new ByteArrayInputStream("Hi there".getBytes(project
+ .getDefaultCharset())), false, null);
+ return project;
+ }
+
+ private void connect() throws CoreException {
+ new ConnectProviderOperation(project, gitDir).execute(null);
+ }
+
+ private Date getDateForYear(int year) throws ParseException {
+ final SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
+ return formatter.parse(Integer.toString(year) + "/6/30");
+ }
+
+} \ No newline at end of file

Back to the top