From db653ae29c54163c7ca2dd6d2b5063d4623c3a8c Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Sun, 29 May 2011 14:04:38 -0400 Subject: [PATCH] Unit testing. --- src/com/gitblit/wicket/pages/LoginPage.java | 4 tests/com/gitblit/tests/GitBlitTest.java | 13 ++- src/com/gitblit/FileSettings.java | 6 src/com/gitblit/wicket/panels/PathBreadcrumbsPanel.java | 2 tests/com/gitblit/tests/DiffUtilsTest.java | 100 +++++++++++++++++++++++++ src/com/gitblit/utils/ByteFormat.java | 4 tests/com/gitblit/tests/ByteFormatTest.java | 10 +- src/com/gitblit/wicket/pages/EditUserPage.java | 2 tests/com/gitblit/tests/StringUtilsTest.java | 9 +- tests/com/gitblit/tests/GitBlitSuite.java | 1 src/com/gitblit/wicket/pages/BlobDiffPage.java | 4 src/com/gitblit/GitBlitServer.java | 9 +- src/com/gitblit/MakeCertificate.java | 4 src/com/gitblit/utils/DiffUtils.java | 16 ++-- tests/com/gitblit/tests/JGitUtilsTest.java | 15 --- src/com/gitblit/Constants.java | 2 src/com/gitblit/utils/JGitUtils.java | 2 17 files changed, 150 insertions(+), 53 deletions(-) diff --git a/src/com/gitblit/Constants.java b/src/com/gitblit/Constants.java index 3589f49..5523284 100644 --- a/src/com/gitblit/Constants.java +++ b/src/com/gitblit/Constants.java @@ -38,7 +38,7 @@ public static final String ZIP_SERVLET_PATH = "/zip/"; public static final String BORDER = "***********************************************************"; - + public static enum AccessRestrictionType { NONE, PUSH, CLONE, VIEW; diff --git a/src/com/gitblit/FileSettings.java b/src/com/gitblit/FileSettings.java index 393e76c..01176c0 100644 --- a/src/com/gitblit/FileSettings.java +++ b/src/com/gitblit/FileSettings.java @@ -35,7 +35,7 @@ private final Logger logger = LoggerFactory.getLogger(FileSettings.class); private final File propertiesFile; - + private Properties properties = new Properties(); private long lastread; @@ -43,7 +43,7 @@ public FileSettings(String file) { this.propertiesFile = new File(file); } - + @Override public List<String> getAllKeys(String startingWith) { startingWith = startingWith.toLowerCase(); @@ -144,7 +144,7 @@ return strings; } - private synchronized Properties read() { + private synchronized Properties read() { if (propertiesFile.exists() && (propertiesFile.lastModified() > lastread)) { FileInputStream is = null; try { diff --git a/src/com/gitblit/GitBlitServer.java b/src/com/gitblit/GitBlitServer.java index 28b1627..928bf7e 100644 --- a/src/com/gitblit/GitBlitServer.java +++ b/src/com/gitblit/GitBlitServer.java @@ -297,8 +297,9 @@ handler = rootContext; } - logger.info("Git repositories folder " + new File(params.repositoriesFolder).getAbsolutePath()); - + logger.info("Git repositories folder " + + new File(params.repositoriesFolder).getAbsolutePath()); + // Set the server's contexts server.setHandler(handler); @@ -384,7 +385,7 @@ private final ServerSocket socket; private final Server server; - + private final Logger logger = LoggerFactory.getLogger(ShutdownMonitorThread.class); public ShutdownMonitorThread(Server server, Params params) { @@ -424,7 +425,7 @@ @Parameters(separators = " ") private static class Params { - + private static final FileSettings FILESETTINGS = new FileSettings(Constants.PROPERTIES_FILE); /* diff --git a/src/com/gitblit/MakeCertificate.java b/src/com/gitblit/MakeCertificate.java index cd01643..17124b3 100644 --- a/src/com/gitblit/MakeCertificate.java +++ b/src/com/gitblit/MakeCertificate.java @@ -44,7 +44,7 @@ import com.gitblit.utils.TimeUtils; public class MakeCertificate { - + private static final String BC = org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME; public static void main(String... args) { @@ -157,7 +157,7 @@ private static class Params { private static final FileSettings FILESETTINGS = new FileSettings(Constants.PROPERTIES_FILE); - + @Parameter(names = { "--alias" }, description = "Server alias", required = true) public String alias; diff --git a/src/com/gitblit/utils/ByteFormat.java b/src/com/gitblit/utils/ByteFormat.java index fee645c..97b77bd 100644 --- a/src/com/gitblit/utils/ByteFormat.java +++ b/src/com/gitblit/utils/ByteFormat.java @@ -26,7 +26,7 @@ public ByteFormat() { } - + public String format(long value) { return format(new Long(value)); } @@ -51,7 +51,7 @@ } return buf; } - + public Object parseObject(String source, ParsePosition pos) { return null; } diff --git a/src/com/gitblit/utils/DiffUtils.java b/src/com/gitblit/utils/DiffUtils.java index c9d0fc3..969cb91 100644 --- a/src/com/gitblit/utils/DiffUtils.java +++ b/src/com/gitblit/utils/DiffUtils.java @@ -35,23 +35,23 @@ public class DiffUtils { private static final Logger LOGGER = LoggerFactory.getLogger(DiffUtils.class); - + public static String getCommitDiff(Repository r, RevCommit commit, DiffOutputType outputType) { - return getCommitDiff(r, null, commit, null, outputType); + return getDiff(r, null, commit, null, outputType); } - public static String getCommitDiff(Repository r, RevCommit commit, String path, + public static String getDiff(Repository r, RevCommit commit, String path, DiffOutputType outputType) { - return getCommitDiff(r, null, commit, path, outputType); + return getDiff(r, null, commit, path, outputType); } - public static String getCommitDiff(Repository r, RevCommit baseCommit, RevCommit commit, + public static String getDiff(Repository r, RevCommit baseCommit, RevCommit commit, DiffOutputType outputType) { - return getCommitDiff(r, baseCommit, commit, null, outputType); + return getDiff(r, baseCommit, commit, null, outputType); } - public static String getCommitDiff(Repository r, RevCommit baseCommit, RevCommit commit, - String path, DiffOutputType outputType) { + public static String getDiff(Repository r, RevCommit baseCommit, RevCommit commit, String path, + DiffOutputType outputType) { try { RevTree baseTree; if (baseCommit == null) { diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index f99fbfd..ecf12e7 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -186,7 +186,7 @@ if (r != null && r.getDirectory().exists()) { return new File(r.getDirectory(), Constants.R_HEADS).list().length > 0; } - return false; + return false; } public static Date getLastChange(Repository r) { diff --git a/src/com/gitblit/wicket/pages/BlobDiffPage.java b/src/com/gitblit/wicket/pages/BlobDiffPage.java index 4bd61d5..b94da01 100644 --- a/src/com/gitblit/wicket/pages/BlobDiffPage.java +++ b/src/com/gitblit/wicket/pages/BlobDiffPage.java @@ -48,13 +48,13 @@ String diff; if (StringUtils.isEmpty(baseObjectId)) { // use first parent - diff = DiffUtils.getCommitDiff(r, commit, blobPath, diffType); + diff = DiffUtils.getDiff(r, commit, blobPath, diffType); add(new BookmarkablePageLink<Void>("patchLink", PatchPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath))); } else { // base commit specified RevCommit baseCommit = JGitUtils.getCommit(r, baseObjectId); - diff = DiffUtils.getCommitDiff(r, baseCommit, commit, blobPath, diffType); + diff = DiffUtils.getDiff(r, baseCommit, commit, blobPath, diffType); add(new BookmarkablePageLink<Void>("patchLink", PatchPage.class, WicketUtils.newBlobDiffParameter(repositoryName, baseObjectId, objectId, blobPath))); diff --git a/src/com/gitblit/wicket/pages/EditUserPage.java b/src/com/gitblit/wicket/pages/EditUserPage.java index fe40a3d..eafec05 100644 --- a/src/com/gitblit/wicket/pages/EditUserPage.java +++ b/src/com/gitblit/wicket/pages/EditUserPage.java @@ -143,7 +143,7 @@ repos.add(selectedRepositories.next().toLowerCase()); } userModel.repositories.clear(); - userModel.repositories.addAll(repos); + userModel.repositories.addAll(repos); try { GitBlit.self().editUserModel(oldName, userModel, isCreate); } catch (GitBlitException e) { diff --git a/src/com/gitblit/wicket/pages/LoginPage.java b/src/com/gitblit/wicket/pages/LoginPage.java index 56e979c..971ba32 100644 --- a/src/com/gitblit/wicket/pages/LoginPage.java +++ b/src/com/gitblit/wicket/pages/LoginPage.java @@ -45,12 +45,12 @@ setRedirect(true); setResponsePage(getApplication().getHomePage()); } - + add(new Label("title", GitBlit.getString(Keys.web.siteName, Constants.NAME))); add(new Label("name", Constants.NAME)); StatelessForm<Void> loginForm = new StatelessForm<Void>("loginForm") { - + private static final long serialVersionUID = 1L; @Override diff --git a/src/com/gitblit/wicket/panels/PathBreadcrumbsPanel.java b/src/com/gitblit/wicket/panels/PathBreadcrumbsPanel.java index bc98e25..f6c0e4f 100644 --- a/src/com/gitblit/wicket/panels/PathBreadcrumbsPanel.java +++ b/src/com/gitblit/wicket/panels/PathBreadcrumbsPanel.java @@ -87,6 +87,6 @@ this.name = name; this.path = path; this.isLeaf = isLeaf; - } + } } } \ No newline at end of file diff --git a/tests/com/gitblit/tests/ByteFormatTest.java b/tests/com/gitblit/tests/ByteFormatTest.java index e969c4d..462c2f0 100644 --- a/tests/com/gitblit/tests/ByteFormatTest.java +++ b/tests/com/gitblit/tests/ByteFormatTest.java @@ -24,10 +24,10 @@ public void testByteFormat() throws Exception { ByteFormat format = new ByteFormat(); assertTrue(format.format(10).equals("10 b")); - assertTrue(format.format(1024*10).equals("10.0 KB")); - assertTrue(format.format(1024*1000).equals("1,000.0 KB")); - assertTrue(format.format(2*1024*1000).equals("2.0 MB")); - assertTrue(format.format(1024*1024*1000).equals("1,000.0 MB")); - assertTrue(format.format(2*1024*1024*1000).equals("2.0 GB")); + assertTrue(format.format(1024 * 10).equals("10.0 KB")); + assertTrue(format.format(1024 * 1000).equals("1,000.0 KB")); + assertTrue(format.format(2 * 1024 * 1000).equals("2.0 MB")); + assertTrue(format.format(1024 * 1024 * 1000).equals("1,000.0 MB")); + assertTrue(format.format(2 * 1024 * 1024 * 1000).equals("2.0 GB")); } } diff --git a/tests/com/gitblit/tests/DiffUtilsTest.java b/tests/com/gitblit/tests/DiffUtilsTest.java new file mode 100644 index 0000000..34cb853 --- /dev/null +++ b/tests/com/gitblit/tests/DiffUtilsTest.java @@ -0,0 +1,100 @@ +/* + * Copyright 2011 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.tests; + +import junit.framework.TestCase; + +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; + +import com.gitblit.utils.DiffUtils; +import com.gitblit.utils.JGitUtils; +import com.gitblit.utils.JGitUtils.DiffOutputType; + +public class DiffUtilsTest extends TestCase { + + public void testParentCommitDiff() throws Exception { + Repository repository = GitBlitSuite.getHelloworldRepository(); + RevCommit commit = JGitUtils.getCommit(repository, + "1d0c2933a4ae69c362f76797d42d6bd182d05176"); + String diff = DiffUtils.getCommitDiff(repository, commit, DiffOutputType.PLAIN); + repository.close(); + assertTrue(diff != null && diff.length() > 0); + String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\""; + assertTrue(diff.indexOf(expected) > -1); + } + + public void testArbitraryCommitDiff() throws Exception { + Repository repository = GitBlitSuite.getHelloworldRepository(); + RevCommit baseCommit = JGitUtils.getCommit(repository, + "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca"); + RevCommit commit = JGitUtils.getCommit(repository, + "1d0c2933a4ae69c362f76797d42d6bd182d05176"); + String diff = DiffUtils.getDiff(repository, baseCommit, commit, DiffOutputType.PLAIN); + repository.close(); + assertTrue(diff != null && diff.length() > 0); + String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\""; + assertTrue(diff.indexOf(expected) > -1); + } + + public void testPlainFileDiff() throws Exception { + Repository repository = GitBlitSuite.getHelloworldRepository(); + RevCommit commit = JGitUtils.getCommit(repository, + "1d0c2933a4ae69c362f76797d42d6bd182d05176"); + String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffOutputType.PLAIN); + repository.close(); + assertTrue(diff != null && diff.length() > 0); + String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\""; + assertTrue(diff.indexOf(expected) > -1); + } + + public void testFilePatch() throws Exception { + Repository repository = GitBlitSuite.getHelloworldRepository(); + RevCommit commit = JGitUtils.getCommit(repository, + "1d0c2933a4ae69c362f76797d42d6bd182d05176"); + String patch = DiffUtils.getCommitPatch(repository, null, commit, "java.java"); + repository.close(); + assertTrue(patch != null && patch.length() > 0); + String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\""; + assertTrue(patch.indexOf(expected) > -1); + } + + public void testArbitraryFilePatch() throws Exception { + Repository repository = GitBlitSuite.getHelloworldRepository(); + RevCommit baseCommit = JGitUtils.getCommit(repository, + "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca"); + RevCommit commit = JGitUtils.getCommit(repository, + "1d0c2933a4ae69c362f76797d42d6bd182d05176"); + String patch = DiffUtils.getCommitPatch(repository, baseCommit, commit, "java.java"); + repository.close(); + assertTrue(patch != null && patch.length() > 0); + String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\""; + assertTrue(patch.indexOf(expected) > -1); + } + + public void testArbitraryCommitPatch() throws Exception { + Repository repository = GitBlitSuite.getHelloworldRepository(); + RevCommit baseCommit = JGitUtils.getCommit(repository, + "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca"); + RevCommit commit = JGitUtils.getCommit(repository, + "1d0c2933a4ae69c362f76797d42d6bd182d05176"); + String patch = DiffUtils.getCommitPatch(repository, baseCommit, commit, null); + repository.close(); + assertTrue(patch != null && patch.length() > 0); + String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\""; + assertTrue(patch.indexOf(expected) > -1); + } +} diff --git a/tests/com/gitblit/tests/GitBlitSuite.java b/tests/com/gitblit/tests/GitBlitSuite.java index d6064b1..d996364 100644 --- a/tests/com/gitblit/tests/GitBlitSuite.java +++ b/tests/com/gitblit/tests/GitBlitSuite.java @@ -43,6 +43,7 @@ suite.addTestSuite(StringUtilsTest.class); suite.addTestSuite(ByteFormatTest.class); suite.addTestSuite(JGitUtilsTest.class); + suite.addTestSuite(DiffUtilsTest.class); suite.addTestSuite(GitBlitTest.class); return new GitBlitSuite(suite); } diff --git a/tests/com/gitblit/tests/GitBlitTest.java b/tests/com/gitblit/tests/GitBlitTest.java index 69880d9..853ca39 100644 --- a/tests/com/gitblit/tests/GitBlitTest.java +++ b/tests/com/gitblit/tests/GitBlitTest.java @@ -28,12 +28,17 @@ public void testRepositoryModel() throws Exception { List<String> repositories = GitBlit.self().getRepositoryList(); assertTrue("Repository list is empty!", repositories.size() > 0); - assertTrue("Missing Helloworld repository!", repositories.contains(GitBlitSuite.getHelloworldRepository().getDirectory().getName())); - RepositoryModel model = GitBlit.self().getRepositoryModel(GitBlitSuite.getHelloworldRepository().getDirectory().getName()); + assertTrue( + "Missing Helloworld repository!", + repositories.contains(GitBlitSuite.getHelloworldRepository().getDirectory() + .getName())); + RepositoryModel model = GitBlit.self().getRepositoryModel( + GitBlitSuite.getHelloworldRepository().getDirectory().getName()); assertTrue("Helloworld model is null!", model != null); - assertTrue(model.toString().equals(GitBlitSuite.getHelloworldRepository().getDirectory().getName())); + assertTrue(model.toString().equals( + GitBlitSuite.getHelloworldRepository().getDirectory().getName())); } - + public void testUserModel() throws Exception { List<String> users = GitBlit.self().getAllUsernames(); assertTrue("No users found!", users.size() > 0); diff --git a/tests/com/gitblit/tests/JGitUtilsTest.java b/tests/com/gitblit/tests/JGitUtilsTest.java index 39a4a36..d17ab5d 100644 --- a/tests/com/gitblit/tests/JGitUtilsTest.java +++ b/tests/com/gitblit/tests/JGitUtilsTest.java @@ -34,9 +34,7 @@ import com.gitblit.models.RefModel; import com.gitblit.models.TicketModel; import com.gitblit.models.TicketModel.Comment; -import com.gitblit.utils.DiffUtils; import com.gitblit.utils.JGitUtils; -import com.gitblit.utils.JGitUtils.DiffOutputType; public class JGitUtilsTest extends TestCase { @@ -65,10 +63,12 @@ public void testFirstCommit() throws Exception { Repository repository = GitBlitSuite.getHelloworldRepository(); RevCommit commit = JGitUtils.getFirstCommit(repository, null); + Date firstChange = JGitUtils.getFirstChange(repository, null); repository.close(); assertTrue("Could not get first commit!", commit != null); assertTrue("Incorrect first commit!", commit.getName().equals("f554664a346629dc2b839f7292d06bad2db4aece")); + assertTrue(firstChange.equals(new Date(commit.getCommitTime() * 1000L))); } public void testRefs() throws Exception { @@ -135,17 +135,6 @@ assertTrue("PathChangeModel equals itself failed!", path.equals(path)); assertFalse("PathChangeModel equals string failed!", path.equals("")); } - } - - public void testCommitDiff() throws Exception { - Repository repository = GitBlitSuite.getHelloworldRepository(); - RevCommit commit = JGitUtils.getCommit(repository, - "1d0c2933a4ae69c362f76797d42d6bd182d05176"); - String diff = DiffUtils.getCommitDiff(repository, commit, DiffOutputType.PLAIN); - repository.close(); - assertTrue("Failed to generate diff!", diff != null && diff.length() > 0); - String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\""; - assertTrue("Diff content mismatch!", diff.indexOf(expected) > -1); } public void testZip() throws Exception { diff --git a/tests/com/gitblit/tests/StringUtilsTest.java b/tests/com/gitblit/tests/StringUtilsTest.java index 24033b2..49938ae 100644 --- a/tests/com/gitblit/tests/StringUtilsTest.java +++ b/tests/com/gitblit/tests/StringUtilsTest.java @@ -35,7 +35,7 @@ String output = "this<br/>is<br/>a<br/>test<br/><br/>of<br/><br/>line<br/><br/>breaking"; assertTrue(StringUtils.breakLinesForHtml(input).equals(output)); } - + public void testEscapeForHtml() throws Exception { String input = "& < > \" \t"; String output_nochange = "& < > " \t"; @@ -64,11 +64,12 @@ assertTrue(StringUtils.leftPad(input, input.length(), ' ').equals(input)); assertTrue(StringUtils.rightPad(input, input.length(), ' ').equals(input)); } - + public void testSHA1() throws Exception { - assertTrue(StringUtils.getSHA1("blob 16\000what is up, doc?").equals("bd9dbf5aae1a3862dd1526723246b20206e5fc37")); + assertTrue(StringUtils.getSHA1("blob 16\000what is up, doc?").equals( + "bd9dbf5aae1a3862dd1526723246b20206e5fc37")); } - + public void testRootPath() throws Exception { String input = "/nested/path/to/repository"; String output = "/nested/path/to"; -- Gitblit v1.9.1