Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2012 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Tomasz Zarna <Tomasz.Zarna@pl.ibm.com> - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.releng.tests; |
12 |
|
13 |
import static org.junit.Assert.assertEquals; |
14 |
|
15 |
import java.io.ByteArrayInputStream; |
16 |
import java.io.File; |
17 |
import java.text.ParseException; |
18 |
import java.text.SimpleDateFormat; |
19 |
import java.util.Date; |
20 |
|
21 |
import org.eclipse.core.resources.IFile; |
22 |
import org.eclipse.core.resources.IProject; |
23 |
import org.eclipse.core.resources.IProjectDescription; |
24 |
import org.eclipse.core.resources.IResource; |
25 |
import org.eclipse.core.resources.ResourcesPlugin; |
26 |
import org.eclipse.core.runtime.CoreException; |
27 |
import org.eclipse.core.runtime.IProgressMonitor; |
28 |
import org.eclipse.core.runtime.NullProgressMonitor; |
29 |
import org.eclipse.core.runtime.Path; |
30 |
import org.eclipse.egit.core.op.ConnectProviderOperation; |
31 |
import org.eclipse.jgit.api.Git; |
32 |
import org.eclipse.jgit.lib.Constants; |
33 |
import org.eclipse.jgit.lib.PersonIdent; |
34 |
import org.eclipse.jgit.lib.Repository; |
35 |
import org.eclipse.jgit.util.FileUtils; |
36 |
import org.eclipse.releng.tools.git.GitCopyrightAdapter; |
37 |
import org.junit.After; |
38 |
import org.junit.Before; |
39 |
import org.junit.Test; |
40 |
|
41 |
public class GitCopyrightAdapterTest extends LocalDiskRepositoryTest { |
42 |
|
43 |
private static final IProgressMonitor NULL_MONITOR = new NullProgressMonitor(); |
44 |
|
45 |
private static final String PROJECT_NAME = "Project"; |
46 |
|
47 |
private static final String FILE1_NAME = "Foo.java"; |
48 |
|
49 |
private static final String FILE2_NAME = "Bar.java"; |
50 |
|
51 |
private Repository db; |
52 |
|
53 |
private File trash; |
54 |
|
55 |
private File gitDir; |
56 |
|
57 |
private IProject project; |
58 |
|
59 |
private IFile file1; |
60 |
|
61 |
@Override |
62 |
@Before |
63 |
public void setUp() throws Exception { |
64 |
super.setUp(); |
65 |
db = createWorkRepository(); |
66 |
trash = db.getWorkTree(); |
67 |
gitDir = new File(trash, Constants.DOT_GIT); |
68 |
project = createProject(PROJECT_NAME); |
69 |
file1 = project.getFile(FILE1_NAME); |
70 |
connect(); |
71 |
} |
72 |
|
73 |
@Override |
74 |
@After |
75 |
public void tearDown() throws Exception { |
76 |
if (project.exists()) |
77 |
project.delete(true, true, NULL_MONITOR); |
78 |
if (gitDir.exists()) |
79 |
FileUtils.delete(gitDir, FileUtils.RECURSIVE | FileUtils.RETRY); |
80 |
super.tearDown(); |
81 |
} |
82 |
|
83 |
@Test |
84 |
public void testLastModifiedYear() throws Exception { |
85 |
final Git git = new Git(db); |
86 |
git.add().addFilepattern(PROJECT_NAME + "/" + FILE1_NAME).call(); |
87 |
final PersonIdent committer2011 = new PersonIdent(committer, |
88 |
getDateForYear(2011)); |
89 |
git.commit().setMessage("old commit").setCommitter(committer2011) |
90 |
.call(); |
91 |
git.add().addFilepattern(PROJECT_NAME + "/" + FILE2_NAME).call(); |
92 |
git.commit().setMessage("new commit").call(); |
93 |
|
94 |
final GitCopyrightAdapter adapter = new GitCopyrightAdapter( |
95 |
new IResource[] { project }); |
96 |
adapter.initialize(NULL_MONITOR); |
97 |
final int lastModifiedYear = adapter.getLastModifiedYear(file1, |
98 |
NULL_MONITOR); |
99 |
|
100 |
assertEquals(2011, lastModifiedYear); |
101 |
} |
102 |
|
103 |
@Test |
104 |
public void testCopyrightUpdateComment() throws Exception { |
105 |
final Git git = new Git(db); |
106 |
git.add().addFilepattern(PROJECT_NAME + "/" + FILE1_NAME).call(); |
107 |
git.commit().setMessage("copyright update").call(); |
108 |
|
109 |
final GitCopyrightAdapter adapter = new GitCopyrightAdapter( |
110 |
new IResource[] { project }); |
111 |
adapter.initialize(NULL_MONITOR); |
112 |
final int lastModifiedYear = adapter.getLastModifiedYear(file1, |
113 |
NULL_MONITOR); |
114 |
|
115 |
assertEquals(0, lastModifiedYear); |
116 |
} |
117 |
|
118 |
private IProject createProject(String name) throws Exception { |
119 |
final IProject project = ResourcesPlugin.getWorkspace().getRoot() |
120 |
.getProject(name); |
121 |
if (project.exists()) |
122 |
project.delete(true, null); |
123 |
final IProjectDescription desc = ResourcesPlugin.getWorkspace() |
124 |
.newProjectDescription(name); |
125 |
desc.setLocation(new Path(new File(db.getWorkTree(), name).getPath())); |
126 |
project.create(desc, null); |
127 |
project.open(null); |
128 |
|
129 |
final IFile file1 = project.getFile(FILE1_NAME); |
130 |
file1.create( |
131 |
new ByteArrayInputStream("Hello, world".getBytes(project |
132 |
.getDefaultCharset())), false, null); |
133 |
|
134 |
final IFile file2 = project.getFile(FILE2_NAME); |
135 |
file2.create( |
136 |
new ByteArrayInputStream("Hi there".getBytes(project |
137 |
.getDefaultCharset())), false, null); |
138 |
return project; |
139 |
} |
140 |
|
141 |
private void connect() throws CoreException { |
142 |
new ConnectProviderOperation(project, gitDir).execute(null); |
143 |
} |
144 |
|
145 |
private Date getDateForYear(int year) throws ParseException { |
146 |
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); |
147 |
return formatter.parse(Integer.toString(year) + "/6/30"); |
148 |
} |
149 |
|
150 |
} |