diff --git a/bundles/org.eclipse.releng.tests/META-INF/MANIFEST.MF b/bundles/org.eclipse.releng.tests/META-INF/MANIFEST.MF index 62b65b5..6afb41a 100644 --- a/bundles/org.eclipse.releng.tests/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.releng.tests/META-INF/MANIFEST.MF @@ -9,6 +9,10 @@ Bundle-Vendor: %Plugin.providerName Bundle-Localization: plugin Require-Bundle: org.eclipse.core.runtime, org.eclipse.pde.tools.versioning, - org.junit + org.junit, + org.eclipse.jgit;bundle-version="[3.0.0,4.0.0)";resolution:=optional, + org.eclipse.core.resources, + org.eclipse.egit.core;bundle-version="[3.0.0,4.0.0)";resolution:=optional, + org.eclipse.releng.tools;bundle-version="3.6.100" Export-Package: org.eclipse.releng.tests Bundle-RequiredExecutionEnvironment: J2SE-1.4 diff --git a/bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/AllRelengTests.java b/bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/AllRelengTests.java new file mode 100644 index 0000000..37d0904 --- /dev/null +++ b/bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/AllRelengTests.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2013 Tomasz Zarna 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 - initial API and implementation + *******************************************************************************/ +package org.eclipse.releng.tests; + +import junit.framework.Test; +import junit.framework.TestSuite; + +public class AllRelengTests extends TestSuite { + public static Test suite() { + return new AllRelengTests(); + } + + public AllRelengTests() { + addTestSuite(BuildTests.class); + if (isJGitAvailable()) { + addTestSuite(GitCopyrightAdapterTest.class); + } + } + + private boolean isJGitAvailable() { + try { + Class.forName("org.eclipse.jgit.api.Git"); + } catch (ClassNotFoundException e) { + return false; + } + return true; + } +} 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 0000000..a33fef0 --- /dev/null +++ b/bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/GitCopyrightAdapterTest.java @@ -0,0 +1,140 @@ +/******************************************************************************* + * Copyright (c) 2013 Tomasz Zarna 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 - 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 diff --git a/bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/LocalDiskRepositoryTest.java b/bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/LocalDiskRepositoryTest.java new file mode 100644 index 0000000..fa97718 --- /dev/null +++ b/bundles/org.eclipse.releng.tests/src/org/eclipse/releng/tests/LocalDiskRepositoryTest.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2009-2010, Google Inc. + * Copyright (C) 2008, Robin Rosenberg + * Copyright (C) 2007, Shawn O. Pearce + * and other copyright owners as documented in the project's IP log. + * + * This class was originally copied from + * org.eclipse.jgit.junit.LocalDiskRepositoryTest + * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=378047 + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.releng.tests; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import junit.framework.TestCase; + +import org.eclipse.jgit.internal.storage.file.FileRepository; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.PersonIdent; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.RepositoryCache; +import org.junit.Assert; + +/** + * This class is a modified version of org.eclipse.jgit.junit.LocalDiskRepositoryTestCase + * + */ +public class LocalDiskRepositoryTest extends TestCase { + private static int testCount; + protected PersonIdent committer; + private final File trash = new File(new File("target"), "trash"); + private final List toClose = new ArrayList(); + + protected void setUp() throws Exception { + committer = new PersonIdent("J. Committer", "jcommitter@example.com"); + } + + protected void tearDown() throws Exception { + RepositoryCache.clear(); + for (Iterator it = toClose.iterator(); it.hasNext();) { + Repository r = (Repository) it.next(); + r.close(); + } + toClose.clear(); + } + + /** + * Creates a new empty repository within a new empty working directory. + * + * @return the newly created repository, opened for access + * @throws IOException + * the repository could not be created in the temporary area + */ + protected Repository createWorkRepository() throws IOException { + return createRepository(false /* not bare */); + } + + /** + * Creates a new empty repository. + * + * @param bare + * true to create a bare repository; false to make a repository + * within its working directory + * @return the newly created repository, opened for access + * @throws IOException + * the repository could not be created in the temporary area + */ + private Repository createRepository(boolean bare) throws IOException { + File gitdir = createUniqueTestGitDir(bare); + Repository db = new FileRepository(gitdir); + Assert.assertFalse(gitdir.exists()); + db.create(); + toClose.add(db); + return db; + } + + /** + * Creates a new unique directory for a test repository + * + * @param bare + * true for a bare repository; false for a repository with a + * working directory + * @return a unique directory for a test repository + * @throws IOException + */ + protected File createUniqueTestGitDir(boolean bare) throws IOException { + String gitdirName = createUniqueTestFolderPrefix(); + if (!bare) + gitdirName += "/"; + gitdirName += Constants.DOT_GIT; + File gitdir = new File(trash, gitdirName); + return gitdir.getCanonicalFile(); + } + + private String createUniqueTestFolderPrefix() { + return "test" + (System.currentTimeMillis() + "_" + (testCount++)); + } +}