Skip to main content
summaryrefslogtreecommitdiffstats
blob: ea85ef5e73c7aa4eacd48f34db84df984f0ccb41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*******************************************************************************
 * 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 static org.junit.Assert.assertEquals;

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.junit.LocalDiskRepositoryTestCase;
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.After;
import org.junit.Before;
import org.junit.Test;

public class GitCopyrightAdapterTest extends LocalDiskRepositoryTestCase {

	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;

	@Override
	@Before
	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();
	}

	@Override
	@After
	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();
	}

	@Test
	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);

		assertEquals(2011, lastModifiedYear);
	}

	@Test
	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);

		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");
	}

}

Back to the top