diff options
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/util')
16 files changed, 683 insertions, 60 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/BlockListTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/BlockListTest.java index a270ca8861..b8b9cbe558 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/BlockListTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/BlockListTest.java @@ -296,6 +296,7 @@ public class BlockListTest { public void testAddRejectsBadIndexes() { BlockList<Integer> list = new BlockList<>(4); list.add(Integer.valueOf(41)); + assertEquals(Integer.valueOf(41), list.get(0)); try { list.add(-1, Integer.valueOf(42)); @@ -316,6 +317,7 @@ public class BlockListTest { public void testRemoveRejectsBadIndexes() { BlockList<Integer> list = new BlockList<>(4); list.add(Integer.valueOf(41)); + assertEquals(Integer.valueOf(41), list.get(0)); try { list.remove(-1); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/ChangeIdUtilTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/ChangeIdUtilTest.java index 32652494d2..44e8632228 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/ChangeIdUtilTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/ChangeIdUtilTest.java @@ -12,7 +12,9 @@ package org.eclipse.jgit.util; import static org.junit.Assert.assertEquals; -import java.util.concurrent.TimeUnit; +import java.time.Duration; +import java.time.Instant; +import java.time.ZoneId; import org.eclipse.jgit.junit.MockSystemReader; import org.eclipse.jgit.lib.ObjectId; @@ -53,9 +55,9 @@ public class ChangeIdUtilTest { MockSystemReader mockSystemReader = new MockSystemReader(); - final long when = mockSystemReader.getCurrentTime(); + Instant when = mockSystemReader.now(); - final int tz = new MockSystemReader().getTimezone(when); + ZoneId tz = new MockSystemReader().getTimeZoneAt(when); PersonIdent author = new PersonIdent("J. Author", "ja@example.com"); { @@ -218,23 +220,23 @@ public class ChangeIdUtilTest { @Test public void testACommitWithSubjectBodyBugBrackersAndSob() throws Exception { assertEquals( - "a commit with subject body, bug. brackers and sob\n\nText\n\nBug: 33\nChange-Id: I90ecb589bef766302532c3e00915e10114b00f62\n[bracket]\nSigned-off-by: me@you.too\n", - call("a commit with subject body, bug. brackers and sob\n\nText\n\nBug: 33\n[bracket]\nSigned-off-by: me@you.too\n\n")); + "a commit with subject body, bug, brackers and sob\n\nText\n\nBug: 33\n[bracket]\nChange-Id: I94dc6ed919a4baaa7c1bf8712717b888c6b90363\nSigned-off-by: me@you.too\n", + call("a commit with subject body, bug, brackers and sob\n\nText\n\nBug: 33\n[bracket]\nSigned-off-by: me@you.too\n\n")); } @Test public void testACommitWithSubjectBodyBugLineWithASpaceAndSob() throws Exception { assertEquals( - "a commit with subject body, bug. line with a space and sob\n\nText\n\nBug: 33\nChange-Id: I864e2218bdee033c8ce9a7f923af9e0d5dc16863\n \nSigned-off-by: me@you.too\n", - call("a commit with subject body, bug. line with a space and sob\n\nText\n\nBug: 33\n \nSigned-off-by: me@you.too\n\n")); + "a commit with subject body, bug, line with a space and sob\n\nText\n\nBug: 33\n \nChange-Id: I126b472d2e0e64ad8187d61857f0169f9ccdae86\nSigned-off-by: me@you.too\n", + call("a commit with subject body, bug, line with a space and sob\n\nText\n\nBug: 33\n \nSigned-off-by: me@you.too\n\n")); } @Test public void testACommitWithSubjectBodyBugEmptyLineAndSob() throws Exception { assertEquals( - "a commit with subject body, bug. empty line and sob\n\nText\n\nBug: 33\nChange-Id: I33f119f533313883e6ada3df600c4f0d4db23a76\n \nSigned-off-by: me@you.too\n", - call("a commit with subject body, bug. empty line and sob\n\nText\n\nBug: 33\n \nSigned-off-by: me@you.too\n\n")); + "a commit with subject body, bug, empty line and sob\n\nText\n\nBug: 33\n\nChange-Id: Ic3b61b6e39a0815669b65302e9e75e6a5a019a26\nSigned-off-by: me@you.too\n", + call("a commit with subject body, bug, empty line and sob\n\nText\n\nBug: 33\n\nSigned-off-by: me@you.too\n\n")); } @Test @@ -342,9 +344,7 @@ public class ChangeIdUtilTest { /** Increment the {@link #author} and {@link #committer} times. */ protected void tick() { - final long delta = TimeUnit.MILLISECONDS.convert(5 * 60, - TimeUnit.SECONDS); - final long now = author.getWhen().getTime() + delta; + Instant now = author.getWhenAsInstant().plus(Duration.ofMinutes(5)); author = new PersonIdent(author, now, tz); committer = new PersonIdent(committer, now, tz); @@ -528,7 +528,7 @@ public class ChangeIdUtilTest { } @Test - public void testChangeIdAfterBugOrIssue() throws Exception { + public void testChangeIdAfterOtherFooters() throws Exception { assertEquals("a\n" + // "\n" + // "Bug: 42\n" + // @@ -541,6 +541,18 @@ public class ChangeIdUtilTest { assertEquals("a\n" + // "\n" + // + "Bug: 42\n" + // + " multi-line Bug footer\n" + // + "Change-Id: Icc953ef35f1a4ee5eb945132aefd603ae3d9dd9f\n" + // + SOB1,// + call("a\n" + // + "\n" + // + "Bug: 42\n" + // + " multi-line Bug footer\n" + // + SOB1)); + + assertEquals("a\n" + // + "\n" + // "Issue: 42\n" + // "Change-Id: Ie66e07d89ae5b114c0975b49cf326e90331dd822\n" + // SOB1,// @@ -548,6 +560,14 @@ public class ChangeIdUtilTest { "\n" + // "Issue: 42\n" + // SOB1)); + + assertEquals("a\n" + // + "\n" + // + "Other: none\n" + // + "Change-Id: Ide70e625dea61854206378a377dd12e462ae720f\n",// + call("a\n" + // + "\n" + // + "Other: none\n")); } @Test diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FSTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FSTest.java index 171d80c3da..3a7fa2388e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FSTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FSTest.java @@ -13,6 +13,7 @@ package org.eclipse.jgit.util; import static java.time.Instant.EPOCH; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeNoException; import static org.junit.Assume.assumeTrue; @@ -28,8 +29,12 @@ import java.nio.file.attribute.PosixFilePermission; import java.time.Duration; import java.time.ZoneId; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.eclipse.jgit.errors.CommandFailedException; @@ -195,6 +200,23 @@ public class FSTest { } @Test + @SuppressWarnings("boxing") + public void testConcurrentSymlinkSupport() + throws ExecutionException, InterruptedException { + Assume.assumeTrue(FS.DETECTED.supportsSymlinks()); + int n = 3; + List<CompletableFuture<Boolean>> futures = new ArrayList<>(); + for (int i = 0; i < n; i++) { + futures.add(CompletableFuture.supplyAsync( + () -> FS.DETECTED.newInstance().supportsSymlinks())); + } + + for (int i = 0; i < n; i++) { + assertTrue(futures.get(i).get()); + } + } + + @Test public void testFsTimestampResolution() throws Exception { DateTimeFormatter formatter = DateTimeFormatter .ofPattern("uuuu-MMM-dd HH:mm:ss.nnnnnnnnn", Locale.ENGLISH) @@ -233,4 +255,26 @@ public class FSTest { assertFalse(RepositoryCache.FileKey .isGitRepository(new File("repo.git"), FS.DETECTED)); } + + @Test + public void testSearchPath() throws IOException { + File f1 = new File(trash, "file1"); + FileUtils.createNewFile(f1); + f1.setExecutable(true); + File f2 = new File(trash, "file2"); + FileUtils.createNewFile(f2); + assertEquals(f1, FS.searchPath(trash.getAbsolutePath(), "file1")); + assertNull(FS.searchPath(trash.getAbsolutePath(), "file2")); + } + + @Test + public void testSearchPathEmptyPath() { + assertNull(FS.searchPath("", "file1")); + assertNull(FS.searchPath(File.pathSeparator, "file1")); + assertNull(FS.searchPath(File.pathSeparator + File.pathSeparator, + "file1")); + assertNull(FS.searchPath( + " " + File.pathSeparator + " " + File.pathSeparator + " \t", + "file1")); + } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilsTest.java index 2b1fb2ef04..5106540227 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilsTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilsTest.java @@ -74,7 +74,9 @@ public class FileUtilsTest { try { FileUtils.delete(f, FileUtils.SKIP_MISSING); } catch (IOException e) { - fail("deletion of non-existing file must not fail with option SKIP_MISSING"); + throw new AssertionError( + "deletion of non-existing file must not fail with option SKIP_MISSING", + e); } } @@ -108,7 +110,9 @@ public class FileUtilsTest { try { FileUtils.delete(d, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING); } catch (IOException e) { - fail("recursive deletion of non-existing directory must not fail with option SKIP_MISSING"); + throw new AssertionError( + "recursive deletion of non-existing directory must not fail with option SKIP_MISSING", + e); } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateFormatterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateFormatterTest.java index 0bd7e0bd61..7ef386f6ee 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateFormatterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateFormatterTest.java @@ -90,7 +90,8 @@ public class GitDateFormatterTest { public void LOCALE() { String date = new GitDateFormatter(Format.LOCALE).formatDate(ident); assertTrue("Sep 20, 2011 7:09:25 PM -0400".equals(date) - || "Sep 20, 2011, 7:09:25 PM -0400".equals(date)); // JDK-8206961 + || "Sep 20, 2011, 7:09:25 PM -0400".equals(date) // JDK-8206961 + || "Sep 20, 2011, 7:09:25\u202FPM -0400".equals(date)); // JDK-8304925 } @Test @@ -98,6 +99,7 @@ public class GitDateFormatterTest { String date = new GitDateFormatter(Format.LOCALELOCAL) .formatDate(ident); assertTrue("Sep 20, 2011 7:39:25 PM".equals(date) - || "Sep 20, 2011, 7:39:25 PM".equals(date)); // JDK-8206961 + || "Sep 20, 2011, 7:39:25 PM".equals(date) // JDK-8206961 + || "Sep 20, 2011, 7:39:25\u202FPM".equals(date)); // JDK-8304925 } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitTimeParserBadlyFormattedTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitTimeParserBadlyFormattedTest.java new file mode 100644 index 0000000000..a59d7bc7bb --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitTimeParserBadlyFormattedTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2012, Christian Halstrick and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.util; + +import static org.junit.Assert.assertThrows; + +import java.text.ParseException; + +import org.eclipse.jgit.junit.MockSystemReader; +import org.junit.After; +import org.junit.Before; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +/** + * Tests which assert that unparseable Strings lead to ParseExceptions + */ +@RunWith(Theories.class) +public class GitTimeParserBadlyFormattedTest { + private String dateStr; + + @Before + public void setUp() { + MockSystemReader mockSystemReader = new MockSystemReader(); + SystemReader.setInstance(mockSystemReader); + } + + @After + public void tearDown() { + SystemReader.setInstance(null); + } + + public GitTimeParserBadlyFormattedTest(String dateStr) { + this.dateStr = dateStr; + } + + @DataPoints + public static String[] getDataPoints() { + return new String[] { "", ".", "...", "1970", "3000.3000.3000", "3 yesterday ago", + "now yesterday ago", "yesterdays", "3.day. 2.week.ago", + "day ago", "Gra Feb 21 15:35:00 2007 +0100", + "Sun Feb 21 15:35:00 2007 +0100", + "Wed Feb 21 15:35:00 Grand +0100" }; + } + + @Theory + public void badlyFormattedWithoutRef() { + assertThrows( + "The expected ParseException while parsing '" + dateStr + + "' did not occur.", + ParseException.class, () -> GitTimeParser.parse(dateStr)); + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitTimeParserTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitTimeParserTest.java new file mode 100644 index 0000000000..0e5eb283a4 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitTimeParserTest.java @@ -0,0 +1,247 @@ +/* + * Copyright (C) 2024, Christian Halstrick and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.text.ParseException; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Period; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; + +import org.eclipse.jgit.junit.MockSystemReader; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class GitTimeParserTest { + MockSystemReader mockSystemReader; + + @Before + public void setUp() { + mockSystemReader = new MockSystemReader(); + SystemReader.setInstance(mockSystemReader); + } + + @After + public void tearDown() { + SystemReader.setInstance(null); + } + + @Test + public void yesterday() throws ParseException { + LocalDateTime parse = GitTimeParser.parse("yesterday"); + + LocalDateTime now = SystemReader.getInstance().civilNow(); + assertEquals(Period.between(parse.toLocalDate(), now.toLocalDate()), + Period.ofDays(1)); + } + + @Test + public void never() throws ParseException { + LocalDateTime parse = GitTimeParser.parse("never"); + assertEquals(LocalDateTime.MAX, parse); + } + + @Test + public void now_pointInTime() throws ParseException { + LocalDateTime aTime = asLocalDateTime("2007-02-21 15:35:00 +0100"); + + LocalDateTime parsedNow = GitTimeParser.parse("now", aTime); + + assertEquals(aTime, parsedNow); + } + + @Test + public void now_systemTime() throws ParseException { + LocalDateTime firstNow = GitTimeParser.parse("now"); + assertEquals(SystemReader.getInstance().civilNow(), firstNow); + mockSystemReader.tick(10); + LocalDateTime secondNow = GitTimeParser.parse("now"); + assertTrue(secondNow.isAfter(firstNow)); + } + + @Test + public void weeksAgo() throws ParseException { + LocalDateTime aTime = asLocalDateTime("2007-02-21 15:35:00 +0100"); + + LocalDateTime parse = GitTimeParser.parse("2 weeks ago", aTime); + assertEquals(asLocalDateTime("2007-02-07 15:35:00 +0100"), parse); + } + + @Test + public void daysAndWeeksAgo() throws ParseException { + LocalDateTime aTime = asLocalDateTime("2007-02-21 15:35:00 +0100"); + + LocalDateTime twoWeeksAgoActual = GitTimeParser.parse("2 weeks ago", + aTime); + + LocalDateTime twoWeeksAgoExpected = asLocalDateTime( + "2007-02-07 15:35:00 +0100"); + assertEquals(twoWeeksAgoExpected, twoWeeksAgoActual); + + LocalDateTime combinedWhitespace = GitTimeParser + .parse("3 days 2 weeks ago", aTime); + LocalDateTime combinedWhitespaceExpected = asLocalDateTime( + "2007-02-04 15:35:00 +0100"); + assertEquals(combinedWhitespaceExpected, combinedWhitespace); + + LocalDateTime combinedDots = GitTimeParser.parse("3.day.2.week.ago", + aTime); + LocalDateTime combinedDotsExpected = asLocalDateTime( + "2007-02-04 15:35:00 +0100"); + assertEquals(combinedDotsExpected, combinedDots); + } + + @Test + public void hoursAgo() throws ParseException { + LocalDateTime aTime = asLocalDateTime("2007-02-21 17:35:00 +0100"); + + LocalDateTime twoHoursAgoActual = GitTimeParser.parse("2 hours ago", + aTime); + + LocalDateTime twoHoursAgoExpected = asLocalDateTime( + "2007-02-21 15:35:00 +0100"); + assertEquals(twoHoursAgoExpected, twoHoursAgoActual); + } + + @Test + public void hoursAgo_acrossDay() throws ParseException { + LocalDateTime aTime = asLocalDateTime("2007-02-21 00:35:00 +0100"); + + LocalDateTime twoHoursAgoActual = GitTimeParser.parse("2 hours ago", + aTime); + + LocalDateTime twoHoursAgoExpected = asLocalDateTime( + "2007-02-20 22:35:00 +0100"); + assertEquals(twoHoursAgoExpected, twoHoursAgoActual); + } + + @Test + public void minutesHoursAgoCombined() throws ParseException { + LocalDateTime aTime = asLocalDateTime("2007-02-04 15:35:00 +0100"); + + LocalDateTime combinedWhitespace = GitTimeParser + .parse("3 hours 2 minutes ago", aTime); + LocalDateTime combinedWhitespaceExpected = asLocalDateTime( + "2007-02-04 12:33:00 +0100"); + assertEquals(combinedWhitespaceExpected, combinedWhitespace); + + LocalDateTime combinedDots = GitTimeParser + .parse("3.hours.2.minutes.ago", aTime); + LocalDateTime combinedDotsExpected = asLocalDateTime( + "2007-02-04 12:33:00 +0100"); + assertEquals(combinedDotsExpected, combinedDots); + } + + @Test + public void minutesAgo() throws ParseException { + LocalDateTime aTime = asLocalDateTime("2007-02-21 17:35:10 +0100"); + + LocalDateTime twoMinutesAgo = GitTimeParser.parse("2 minutes ago", + aTime); + + LocalDateTime twoMinutesAgoExpected = asLocalDateTime( + "2007-02-21 17:33:10 +0100"); + assertEquals(twoMinutesAgoExpected, twoMinutesAgo); + } + + @Test + public void minutesAgo_acrossDay() throws ParseException { + LocalDateTime aTime = asLocalDateTime("2007-02-21 00:35:10 +0100"); + + LocalDateTime minutesAgoActual = GitTimeParser.parse("40 minutes ago", + aTime); + + LocalDateTime minutesAgoExpected = asLocalDateTime( + "2007-02-20 23:55:10 +0100"); + assertEquals(minutesAgoExpected, minutesAgoActual); + } + + @Test + public void iso() throws ParseException { + String dateStr = "2007-02-21 15:35:00 +0100"; + + LocalDateTime actual = GitTimeParser.parse(dateStr); + + LocalDateTime expected = asLocalDateTime(dateStr); + assertEquals(expected, actual); + } + + @Test + public void rfc() throws ParseException { + String dateStr = "Wed, 21 Feb 2007 15:35:00 +0100"; + + LocalDateTime actual = GitTimeParser.parse(dateStr); + + LocalDateTime expected = asLocalDateTime(dateStr, + "EEE, dd MMM yyyy HH:mm:ss Z"); + assertEquals(expected, actual); + } + + @Test + public void shortFmt() throws ParseException { + assertParsing("2007-02-21", "yyyy-MM-dd"); + } + + @Test + public void shortWithDots() throws ParseException { + assertParsing("2007.02.21", "yyyy.MM.dd"); + } + + @Test + public void shortWithSlash() throws ParseException { + assertParsing("02/21/2007", "MM/dd/yyyy"); + } + + @Test + public void shortWithDotsReverse() throws ParseException { + assertParsing("21.02.2007", "dd.MM.yyyy"); + } + + @Test + public void defaultFmt() throws ParseException { + assertParsing("Wed Feb 21 15:35:00 2007 +0100", + "EEE MMM dd HH:mm:ss yyyy Z"); + } + + @Test + public void local() throws ParseException { + assertParsing("Wed Feb 21 15:35:00 2007", "EEE MMM dd HH:mm:ss yyyy"); + } + + private static void assertParsing(String dateStr, String format) + throws ParseException { + LocalDateTime actual = GitTimeParser.parse(dateStr); + + LocalDateTime expected = asLocalDateTime(dateStr, format); + assertEquals(expected, actual); + } + + private static LocalDateTime asLocalDateTime(String dateStr) { + return asLocalDateTime(dateStr, "yyyy-MM-dd HH:mm:ss Z"); + } + + private static LocalDateTime asLocalDateTime(String dateStr, + String pattern) { + DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern); + TemporalAccessor ta = fmt + .withZone(SystemReader.getInstance().getTimeZoneId()) + .withLocale(SystemReader.getInstance().getLocale()) + .parse(dateStr); + return ta.isSupported(ChronoField.HOUR_OF_DAY) ? LocalDateTime.from(ta) + : LocalDate.from(ta).atStartOfDay(); + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HookTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HookTest.java index 1231aefee0..b7490f0b1f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HookTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HookTest.java @@ -157,7 +157,7 @@ public class HookTest extends RepositoryTestCase { git.commit().setMessage("commit") .setHookOutputStream(new PrintStream(out)).call(); } catch (AbortedByHookException e) { - fail("unexpected hook failure"); + throw new AssertionError("unexpected hook failure", e); } assertEquals("unexpected hook output", "test pre-commit\ntest commit-msg .git/COMMIT_EDITMSG\ntest post-commit\n", diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java index cbe4eb2eb0..a3a5697ef4 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HttpSupportTest.java @@ -47,18 +47,6 @@ public class HttpSupportTest { } @Test - public void testMalformedUri() throws Exception { - // Valid URL, but backslash is not allowed in a URI in the userinfo part - // per RFC 3986: https://tools.ietf.org/html/rfc3986#section-3.2.1 . - // Test that conversion to URI to call the ProxySelector does not throw - // an exception. - Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL( - "http://infor\\c.jones@somehost/somewhere/someproject.git")); - assertNotNull(proxy); - assertEquals(Proxy.Type.HTTP, proxy.type()); - } - - @Test public void testCorrectUri() throws Exception { // Backslash escaped as %5C is correct. Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL( diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtilsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtilsTest.java index e80c07509a..e4ef302359 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtilsTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtilsTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011, Leonard Broman <leonard.broman@gmail.com> and others + * Copyright (C) 2011, 2024 Leonard Broman <leonard.broman@gmail.com> and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0 which is available at @@ -15,6 +15,7 @@ import static org.junit.Assert.fail; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; +import java.util.List; import org.eclipse.jgit.lib.Constants; import org.junit.Test; @@ -24,7 +25,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; public class RawParseUtilsTest { String commit = "tree e3a1035abd2b319bb01e57d69b0ba6cab289297e\n" + "parent 54e895b87c0768d2317a2b17062e3ad9f76a8105\n" + - "committer A U Thor <author@xample.com 1528968566 +0200\n" + + "committer A U Thor <author@example.com> 1528968566 +0200\n" + "gpgsig -----BEGIN PGP SIGNATURE-----\n" + " \n" + " wsBcBAABCAAQBQJbGB4pCRBK7hj4Ov3rIwAAdHIIAENrvz23867ZgqrmyPemBEZP\n" + @@ -67,21 +68,24 @@ public class RawParseUtilsTest { public void testHeaderStart() { byte[] headerName = "some".getBytes(UTF_8); byte[] commitBytes = commit.getBytes(UTF_8); - assertEquals(625, RawParseUtils.headerStart(headerName, commitBytes, 0)); - assertEquals(625, RawParseUtils.headerStart(headerName, commitBytes, 4)); + assertEquals(627, + RawParseUtils.headerStart(headerName, commitBytes, 0)); + assertEquals(627, + RawParseUtils.headerStart(headerName, commitBytes, 4)); byte[] missingHeaderName = "missing".getBytes(UTF_8); assertEquals(-1, RawParseUtils.headerStart(missingHeaderName, commitBytes, 0)); byte[] fauxHeaderName = "other".getBytes(UTF_8); - assertEquals(-1, RawParseUtils.headerStart(fauxHeaderName, commitBytes, 625 + 4)); + assertEquals(-1, RawParseUtils.headerStart(fauxHeaderName, commitBytes, + 627 + 4)); } @Test public void testHeaderEnd() { byte[] commitBytes = commit.getBytes(UTF_8); - int[] expected = new int[] {45, 93, 148, 619, 637}; + int[] expected = new int[] { 45, 93, 150, 621, 639 }; int start = 0; for (int i = 0; i < expected.length; i++) { start = RawParseUtils.headerEnd(commitBytes, start); @@ -89,4 +93,44 @@ public class RawParseUtilsTest { start += 1; } } + + @Test + public void testHeaderValue() { + byte[] commitBytes = commit.getBytes(UTF_8); + List<String> headers = List.of( + "e3a1035abd2b319bb01e57d69b0ba6cab289297e", + "54e895b87c0768d2317a2b17062e3ad9f76a8105", + "A U Thor <author@example.com> 1528968566 +0200", + "-----BEGIN PGP SIGNATURE-----\n" + + "\n" + + "wsBcBAABCAAQBQJbGB4pCRBK7hj4Ov3rIwAAdHIIAENrvz23867ZgqrmyPemBEZP\n" + + "U24B1Tlq/DWvce2buaxmbNQngKZ0pv2s8VMc11916WfTIC9EKvioatmpjduWvhqj\n" + + "znQTFyiMor30pyYsfrqFuQZvqBW01o8GEWqLg8zjf9Rf0R3LlOEw86aT8CdHRlm6\n" + + "wlb22xb8qoX4RB+LYfz7MhK5F+yLOPXZdJnAVbuyoMGRnDpwdzjL5Hj671+XJxN5\n" + + "SasRdhxkkfw/ZnHxaKEc4juMz8Nziz27elRwhOQqlTYoXNJnsV//wy5Losd7aKi1\n" + + "xXXyUpndEOmT0CIcKHrN/kbYoVL28OJaxoBuva3WYQaRrzEe3X02NMxZe9gkSqA=\n" + + "=TClh\n" + + "-----END PGP SIGNATURE-----", + "other header"); + int start = 0; + for (String header : headers) { + int endOfTag = RawParseUtils.next(commitBytes, start, ' '); + int end = RawParseUtils.headerEnd(commitBytes, start); + + assertEquals(header, new String( + RawParseUtils.headerValue(commitBytes, endOfTag, end), + UTF_8)); + start = end + 1; + } + } + + @Test + public void testLastHeaderEnd() { + byte[] raw = "headerA A header\nheaderB Another header".getBytes(UTF_8); + int bStart = RawParseUtils.headerStart("headerB".getBytes(UTF_8), raw, + 0); + assertEquals(25, bStart); + int bEnd = RawParseUtils.nextLfSkippingSplitLines(raw, bStart); + assertEquals(raw.length, bEnd); + } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_ParsePersonIdentTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_ParsePersonIdentTest.java index ee3ce8d98c..6d23db81d8 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_ParsePersonIdentTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_ParsePersonIdentTest.java @@ -10,10 +10,13 @@ package org.eclipse.jgit.util; +import static java.time.Instant.EPOCH; +import static java.time.ZoneOffset.UTC; import static org.junit.Assert.assertEquals; -import java.util.Date; -import java.util.TimeZone; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZoneOffset; import org.eclipse.jgit.lib.PersonIdent; import org.junit.Test; @@ -22,8 +25,8 @@ public class RawParseUtils_ParsePersonIdentTest { @Test public void testParsePersonIdent_legalCases() { - final Date when = new Date(1234567890000l); - final TimeZone tz = TimeZone.getTimeZone("GMT-7"); + Instant when = Instant.ofEpochMilli(1234567890000L); + ZoneId tz = ZoneOffset.ofHours(-7); assertPersonIdent("Me <me@example.com> 1234567890 -0700", new PersonIdent("Me", "me@example.com", when, tz)); @@ -50,8 +53,8 @@ public class RawParseUtils_ParsePersonIdentTest { @Test public void testParsePersonIdent_fuzzyCases() { - final Date when = new Date(1234567890000l); - final TimeZone tz = TimeZone.getTimeZone("GMT-7"); + Instant when = Instant.ofEpochMilli(1234567890000L); + ZoneId tz = ZoneOffset.ofHours(-7); assertPersonIdent( "A U Thor <author@example.com>, C O. Miter <comiter@example.com> 1234567890 -0700", @@ -64,8 +67,8 @@ public class RawParseUtils_ParsePersonIdentTest { @Test public void testParsePersonIdent_incompleteCases() { - final Date when = new Date(1234567890000l); - final TimeZone tz = TimeZone.getTimeZone("GMT-7"); + Instant when = Instant.ofEpochMilli(1234567890000L); + ZoneId tz = ZoneOffset.ofHours(-7); assertPersonIdent("Me <> 1234567890 -0700", new PersonIdent("Me", "", when, tz)); @@ -76,26 +79,26 @@ public class RawParseUtils_ParsePersonIdentTest { assertPersonIdent(" <> 1234567890 -0700", new PersonIdent("", "", when, tz)); - assertPersonIdent("<>", new PersonIdent("", "", 0, 0)); + assertPersonIdent("<>", new PersonIdent("", "", EPOCH, UTC)); - assertPersonIdent(" <>", new PersonIdent("", "", 0, 0)); + assertPersonIdent(" <>", new PersonIdent("", "", EPOCH, UTC)); assertPersonIdent("<me@example.com>", new PersonIdent("", - "me@example.com", 0, 0)); + "me@example.com", EPOCH, UTC)); assertPersonIdent(" <me@example.com>", new PersonIdent("", - "me@example.com", 0, 0)); + "me@example.com", EPOCH, UTC)); - assertPersonIdent("Me <>", new PersonIdent("Me", "", 0, 0)); + assertPersonIdent("Me <>", new PersonIdent("Me", "", EPOCH, UTC)); assertPersonIdent("Me <me@example.com>", new PersonIdent("Me", - "me@example.com", 0, 0)); + "me@example.com", EPOCH, UTC)); assertPersonIdent("Me <me@example.com> 1234567890", new PersonIdent( - "Me", "me@example.com", 0, 0)); + "Me", "me@example.com", EPOCH, UTC)); assertPersonIdent("Me <me@example.com> 1234567890 ", new PersonIdent( - "Me", "me@example.com", 0, 0)); + "Me", "me@example.com", EPOCH, UTC)); } @Test @@ -104,6 +107,21 @@ public class RawParseUtils_ParsePersonIdentTest { assertPersonIdent("Me <me@example.com 1234567890 -0700", null); } + @Test + public void testParsePersonIdent_badTz() { + PersonIdent tooBig = RawParseUtils + .parsePersonIdent("Me <me@example.com> 1234567890 +8315"); + assertEquals(tooBig.getZoneOffset().getTotalSeconds(), 0); + + PersonIdent tooSmall = RawParseUtils + .parsePersonIdent("Me <me@example.com> 1234567890 -8315"); + assertEquals(tooSmall.getZoneOffset().getTotalSeconds(), 0); + + PersonIdent notATime = RawParseUtils + .parsePersonIdent("Me <me@example.com> 1234567890 -0370"); + assertEquals(notATime.getZoneOffset().getTotalSeconds(), 0); + } + private static void assertPersonIdent(String line, PersonIdent expected) { PersonIdent actual = RawParseUtils.parsePersonIdent(line); assertEquals(expected, actual); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RefMapTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RefMapTest.java index 627417d462..0c9cb2d2b0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RefMapTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RefMapTest.java @@ -268,6 +268,7 @@ public class RefMapTest { assertFalse(itr.hasNext()); } + @SuppressWarnings("ModifiedButNotUsed") @Test public void testPut_KeyMustMatchName_NoPrefix() { final Ref refA = newRef("refs/heads/A", ID_ONE); @@ -280,6 +281,7 @@ public class RefMapTest { } } + @SuppressWarnings("ModifiedButNotUsed") @Test public void testPut_KeyMustMatchName_WithPrefix() { final Ref refA = newRef("refs/heads/A", ID_ONE); @@ -421,7 +423,7 @@ public class RefMapTest { Map.Entry<String, Ref> ent_b = itr.next(); assertEquals(ent_a.hashCode(), "A".hashCode()); - assertEquals(ent_a, ent_a); + assertTrue(ent_a.equals(ent_a)); assertFalse(ent_a.equals(ent_b)); assertEquals(a.toString(), ent_a.toString()); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RelativeDateFormatterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RelativeDateFormatterTest.java index 214bbca944..a927d8dbef 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RelativeDateFormatterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RelativeDateFormatterTest.java @@ -16,7 +16,7 @@ import static org.eclipse.jgit.util.RelativeDateFormatter.SECOND_IN_MILLIS; import static org.eclipse.jgit.util.RelativeDateFormatter.YEAR_IN_MILLIS; import static org.junit.Assert.assertEquals; -import java.util.Date; +import java.time.Instant; import org.eclipse.jgit.junit.MockSystemReader; import org.junit.After; @@ -37,9 +37,9 @@ public class RelativeDateFormatterTest { private static void assertFormat(long ageFromNow, long timeUnit, String expectedFormat) { - Date d = new Date(SystemReader.getInstance().getCurrentTime() - - ageFromNow * timeUnit); - String s = RelativeDateFormatter.format(d); + long millis = ageFromNow * timeUnit; + Instant aTime = SystemReader.getInstance().now().minusMillis(millis); + String s = RelativeDateFormatter.format(aTime); assertEquals(expectedFormat, s); } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StringUtilsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StringUtilsTest.java index aa7247e105..9a1c710752 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StringUtilsTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StringUtilsTest.java @@ -12,6 +12,7 @@ package org.eclipse.jgit.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; @@ -153,4 +154,41 @@ public class StringUtilsTest { () -> StringUtils.parseLongWithSuffix("8000000000000000000G", false)); } + + @Test + public void testCommonPrefix() { + assertEquals("", StringUtils.commonPrefix((String[]) null)); + assertEquals("", StringUtils.commonPrefix(new String[] {})); + assertEquals("", StringUtils.commonPrefix(new String[] { null })); + assertEquals("", StringUtils.commonPrefix(null, null)); + assertEquals("", StringUtils.commonPrefix("", "")); + assertEquals("", StringUtils.commonPrefix(null, "")); + assertEquals("", StringUtils.commonPrefix("abcd", null, null)); + assertEquals("", StringUtils.commonPrefix(null, null, "abcd")); + assertEquals("", StringUtils.commonPrefix("", "abcd")); + assertEquals("", StringUtils.commonPrefix("abcd", "efgh")); + assertEquals("abcd", StringUtils.commonPrefix("abcd")); + assertEquals("ab", StringUtils.commonPrefix("abcd", "ab")); + assertEquals("abcd", StringUtils.commonPrefix("abcd", "abcdefgh")); + assertEquals("foo bar ", + StringUtils.commonPrefix("foo bar 42", "foo bar 24")); + } + + @Test + public void testTrim() { + assertEquals("a", StringUtils.trim("a", '/')); + assertEquals("aaaa", StringUtils.trim("aaaa", '/')); + assertEquals("aaa", StringUtils.trim("/aaa", '/')); + assertEquals("aaa", StringUtils.trim("aaa/", '/')); + assertEquals("aaa", StringUtils.trim("/aaa/", '/')); + assertEquals("aa/aa", StringUtils.trim("/aa/aa/", '/')); + assertEquals("aa/aa", StringUtils.trim("aa/aa", '/')); + + assertEquals("", StringUtils.trim("", '/')); + assertEquals("", StringUtils.trim("/", '/')); + assertEquals("", StringUtils.trim("//", '/')); + assertEquals("", StringUtils.trim("///", '/')); + + assertNull(StringUtils.trim(null, '/')); + } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/http/HttpCookiesMatcher.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/http/HttpCookiesMatcher.java index 5a2bd976c7..c1c72804da 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/http/HttpCookiesMatcher.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/http/HttpCookiesMatcher.java @@ -10,7 +10,7 @@ package org.eclipse.jgit.util.http; import java.net.HttpCookie; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import org.hamcrest.Description; @@ -26,7 +26,7 @@ public final class HttpCookiesMatcher { public static Matcher<Iterable<? extends HttpCookie>> containsInOrder( Iterable<HttpCookie> expectedCookies, int allowedMaxAgeDelta) { - final List<Matcher<? super HttpCookie>> cookieMatchers = new LinkedList<>(); + final List<Matcher<? super HttpCookie>> cookieMatchers = new ArrayList<>(); for (HttpCookie cookie : expectedCookies) { cookieMatchers .add(new HttpCookieMatcher(cookie, allowedMaxAgeDelta)); @@ -92,7 +92,7 @@ public final class HttpCookiesMatcher { } @SuppressWarnings("boxing") - protected static void describeCookie(Description description, + private static void describeCookie(Description description, HttpCookie cookie) { description.appendText("HttpCookie["); description.appendText("name: ").appendValue(cookie.getName()) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/ByteBufferInputStreamTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/ByteBufferInputStreamTest.java new file mode 100644 index 0000000000..ec9f96ed96 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/ByteBufferInputStreamTest.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2023, SAP SE and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.util.io; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; + +import org.eclipse.jgit.internal.JGitText; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ByteBufferInputStreamTest { + + private static final byte data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, + 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; + + private ByteBuffer buf; + + private ByteBufferInputStream is; + + @Before + public void setup() { + buf = ByteBuffer.wrap(data); + is = new ByteBufferInputStream(buf); + } + + @After + public void tearDown() { + is.close(); + } + + @Test + public void testRead() throws IOException { + assertEquals(0x00, is.read()); + assertEquals(0x01, is.read()); + assertEquals(0x02, is.read()); + assertEquals(0x03, is.read()); + assertEquals(0x04, is.read()); + assertEquals(0x05, is.read()); + assertEquals(0x06, is.read()); + assertEquals(0x07, is.read()); + assertEquals(0x08, is.read()); + assertEquals(0x09, is.read()); + assertEquals(0x0A, is.read()); + assertEquals(0x0B, is.read()); + assertEquals(0x0C, is.read()); + assertEquals(0x0D, is.read()); + assertEquals(0x0E, is.read()); + assertEquals(0x0F, is.read()); + assertEquals(-1, is.read()); + } + + @Test + public void testReadMultiple() throws IOException { + byte[] x = new byte[5]; + int n = is.read(x); + assertEquals(5, n); + assertArrayEquals(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 }, x); + } + + @Test + public void testReadMultipleOffset() throws IOException { + byte[] x = new byte[7]; + int n = is.read(x, 4, 3); + assertEquals(3, n); + assertArrayEquals( + new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02 }, + x); + } + + @Test + public void testReadAll() throws IOException { + byte[] x = is.readAllBytes(); + assertEquals(16, x.length); + assertArrayEquals(data, x); + } + + @Test + public void testMarkReset() throws IOException { + byte[] x = new byte[5]; + int n = is.read(x); + assertEquals(11, is.available()); + assertTrue(is.markSupported()); + is.mark(is.available()); + is.reset(); + byte[] y = new byte[5]; + int m = is.read(y); + assertEquals(n, m); + assertArrayEquals(new byte[] { 0x05, 0x06, 0x07, 0x08, 0x09 }, y); + } + + @Test + public void testClosed() { + is.close(); + Exception e = assertThrows(IOException.class, () -> is.read()); + assertEquals(JGitText.get().inputStreamClosed, e.getMessage()); + } + + @Test + public void testReadNBytes() throws IOException { + byte[] x = is.readNBytes(4); + assertArrayEquals(new byte[] { 0x00, 0x01, 0x02, 0x03 }, x); + } + + @Test + public void testReadNBytesOffset() throws IOException { + byte[] x = new byte[10]; + Arrays.fill(x, (byte) 0x0F); + is.readNBytes(x, 3, 4); + assertArrayEquals(new byte[] { 0x0F, 0x0F, 0x0F, 0x00, 0x01, 0x02, 0x03, + 0x0F, 0x0F, 0x0F }, x); + } + + @Test + public void testRead0() throws IOException { + byte[] x = new byte[7]; + int n = is.read(x, 4, 0); + assertEquals(0, n); + + is.readAllBytes(); + n = is.read(x, 4, 3); + assertEquals(-1, n); + } + + @Test + public void testSkip() throws IOException { + assertEquals(15, is.skip(15)); + assertEquals(0x0F, is.read()); + assertEquals(-1, is.read()); + } + + @Test + public void testSkip0() throws IOException { + assertEquals(0, is.skip(0)); + assertEquals(0x00, is.read()); + } +} |