From bf6ef0f6051e7c8d9e87c42dd55b8a3a36044a78 Mon Sep 17 00:00:00 2001 From: lberki Date: Mon, 29 May 2017 11:00:40 +0200 Subject: Migrate tests to Truth. RELNOTES: None. PiperOrigin-RevId: 157378037 --- .../java/com/google/devtools/build/android/BUILD | 1 + .../DensitySpecificManifestProcessorTest.java | 20 +- .../android/DensitySpecificResourceFilterTest.java | 6 +- .../build/android/ManifestMergerActionTest.java | 2 +- .../build/android/ziputils/BufferedFileTest.java | 14 +- .../android/ziputils/CentralDirectoryTest.java | 49 ++- .../build/android/ziputils/DataDescriptorTest.java | 58 +-- .../build/android/ziputils/DirectoryEntryTest.java | 86 ++-- .../ziputils/EndOfCentralDirectoryTest.java | 59 ++- .../android/ziputils/LocalFileHeaderTest.java | 64 ++- .../build/android/ziputils/SplitZipTest.java | 43 +- .../devtools/build/android/ziputils/ViewTest.java | 46 +-- .../devtools/build/android/ziputils/ZipInTest.java | 148 ++++--- .../build/lib/analysis/AnalysisUtilsTest.java | 3 +- .../downloader/HttpConnectorMultiplexerTest.java | 2 +- .../repository/downloader/ProxyHelperTest.java | 80 ++-- .../transports/BinaryFormatFileTransportTest.java | 8 +- .../lib/pkgcache/BuildFileModificationTest.java | 25 +- .../CompileOneDependencyTransformerTest.java | 3 +- .../build/lib/pkgcache/IOExceptionsTest.java | 13 +- .../build/lib/pkgcache/PackageCacheTest.java | 53 ++- .../build/lib/pkgcache/PathPackageLocatorTest.java | 77 ++-- .../lib/pkgcache/TargetPatternEvaluatorTest.java | 116 +++--- .../build/lib/profiler/ProfilerChartTest.java | 105 +++-- .../devtools/build/lib/profiler/ProfilerTest.java | 97 +++-- .../rules/proto/ProtoCompileActionBuilderTest.java | 3 +- .../common/options/AssignmentConverterTest.java | 18 +- .../common/options/BoolOrEnumConverterTest.java | 25 +- .../CommaSeparatedOptionListConverterTest.java | 32 +- .../devtools/common/options/EnumConverterTest.java | 27 +- .../common/options/GenericTypeHelperTest.java | 9 +- .../common/options/LogLevelConverterTest.java | 16 +- .../devtools/common/options/OptionsDataTest.java | 32 +- .../common/options/OptionsMapConversionTest.java | 13 +- .../devtools/common/options/OptionsParserTest.java | 441 +++++++++++---------- .../devtools/common/options/OptionsTest.java | 46 ++- 36 files changed, 910 insertions(+), 930 deletions(-) (limited to 'src/test/java/com') diff --git a/src/test/java/com/google/devtools/build/android/BUILD b/src/test/java/com/google/devtools/build/android/BUILD index d500d3b2f3..70b6fb6fae 100644 --- a/src/test/java/com/google/devtools/build/android/BUILD +++ b/src/test/java/com/google/devtools/build/android/BUILD @@ -140,6 +140,7 @@ java_test( "//src/tools/android/java/com/google/devtools/build/android:android_builder_lib", "//third_party:guava", "//third_party:junit4", + "//third_party:truth", ], ) diff --git a/src/test/java/com/google/devtools/build/android/DensitySpecificManifestProcessorTest.java b/src/test/java/com/google/devtools/build/android/DensitySpecificManifestProcessorTest.java index 9d02a28ef8..b62717dba7 100644 --- a/src/test/java/com/google/devtools/build/android/DensitySpecificManifestProcessorTest.java +++ b/src/test/java/com/google/devtools/build/android/DensitySpecificManifestProcessorTest.java @@ -18,7 +18,6 @@ import static com.google.devtools.build.android.DensitySpecificManifestProcessor import static com.google.devtools.build.android.DensitySpecificManifestProcessor.SCREEN_SIZES; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import com.google.common.base.Joiner; @@ -225,19 +224,20 @@ public class DensitySpecificManifestProcessorTest { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.parse(Files.newInputStream(manifest)); NodeList compatibleScreensNodes = doc.getElementsByTagName("compatible-screens"); - assertEquals(1, compatibleScreensNodes.getLength()); + assertThat(compatibleScreensNodes.getLength()).isEqualTo(1); Node compatibleScreens = compatibleScreensNodes.item(0); NodeList screens = doc.getElementsByTagName("screen"); - assertEquals(densities.size() * SCREEN_SIZES.size(), - screens.getLength()); + assertThat(screens.getLength()).isEqualTo(densities.size() * SCREEN_SIZES.size()); for (int i = 0; i < screens.getLength(); i++) { Node s = screens.item(i); - assertTrue(s.getParentNode().isSameNode(compatibleScreens)); + assertThat(s.getParentNode().isSameNode(compatibleScreens)).isTrue(); if (s.getNodeType() == Node.ELEMENT_NODE) { Element screen = (Element) s; - assertTrue(sizeDensities.remove( - screen.getAttribute("android:screenSize") - + screen.getAttribute("android:screenDensity"))); + assertThat( + sizeDensities.remove( + screen.getAttribute("android:screenSize") + + screen.getAttribute("android:screenDensity"))) + .isTrue(); } } assertThat(sizeDensities).isEmpty(); @@ -247,9 +247,9 @@ public class DensitySpecificManifestProcessorTest { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.parse(Files.newInputStream(manifest)); NodeList compatibleScreensNodes = doc.getElementsByTagName("compatible-screens"); - assertEquals(0, compatibleScreensNodes.getLength()); + assertThat(compatibleScreensNodes.getLength()).isEqualTo(0); NodeList screens = doc.getElementsByTagName("screen"); - assertEquals(0, screens.getLength()); + assertThat(screens.getLength()).isEqualTo(0); } } diff --git a/src/test/java/com/google/devtools/build/android/DensitySpecificResourceFilterTest.java b/src/test/java/com/google/devtools/build/android/DensitySpecificResourceFilterTest.java index a7b1383051..5c60d4dc0a 100644 --- a/src/test/java/com/google/devtools/build/android/DensitySpecificResourceFilterTest.java +++ b/src/test/java/com/google/devtools/build/android/DensitySpecificResourceFilterTest.java @@ -13,7 +13,7 @@ // limitations under the License. package com.google.devtools.build.android; -import static org.junit.Assert.assertArrayEquals; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import com.google.common.collect.ImmutableList; @@ -241,7 +241,7 @@ public class DensitySpecificResourceFilterTest { String[] actual = filteredResources.build().toArray(new String[0]); Arrays.sort(expected); Arrays.sort(actual); - assertArrayEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } @Before @@ -281,6 +281,6 @@ public class DensitySpecificResourceFilterTest { Arrays.sort(expected); Arrays.sort(actual); - assertArrayEquals(expected, actual); + assertThat(actual).isEqualTo(expected); } } diff --git a/src/test/java/com/google/devtools/build/android/ManifestMergerActionTest.java b/src/test/java/com/google/devtools/build/android/ManifestMergerActionTest.java index 61d77dd128..1f16f5bc7b 100644 --- a/src/test/java/com/google/devtools/build/android/ManifestMergerActionTest.java +++ b/src/test/java/com/google/devtools/build/android/ManifestMergerActionTest.java @@ -58,7 +58,7 @@ public class ManifestMergerActionTest { final File expectedManifestDirectory = manifestData.resolve("expected").toFile(); final File[] expectedManifestDirectoryManifests = expectedManifestDirectory.listFiles(new PatternFilenameFilter(".*AndroidManifest\\.xml$")); - assertThat(expectedManifestDirectoryManifests.length).isEqualTo(1); + assertThat(expectedManifestDirectoryManifests).hasLength(1); final Path expectedManifest = expectedManifestDirectoryManifests[0].toPath(); Files.createDirectories(working.resolve("output")); diff --git a/src/test/java/com/google/devtools/build/android/ziputils/BufferedFileTest.java b/src/test/java/com/google/devtools/build/android/ziputils/BufferedFileTest.java index e5bc7a4c45..e4836871cb 100644 --- a/src/test/java/com/google/devtools/build/android/ziputils/BufferedFileTest.java +++ b/src/test/java/com/google/devtools/build/android/ziputils/BufferedFileTest.java @@ -14,20 +14,16 @@ package com.google.devtools.build.android.ziputils; import static com.google.common.truth.Truth.assertWithMessage; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; -/** - * Unit tests for {@link BufferedFile}. - */ +/** Unit tests for {@link BufferedFile}. */ @RunWith(JUnit4.class) public class BufferedFileTest { @@ -215,7 +211,7 @@ public class BufferedFileTest { assertWithMessage(msg + " - capacity, ").that(buf.capacity()).isAtLeast(expectLimit); assertWithMessage(msg + " - capacity, ").that(buf.capacity()).isAtMost(capacityBound); if (len > 0 && expectLimit > 0) { - assertEquals(msg + " - value, ", (byte) off, buf.get(0)); + assertWithMessage(msg + " - value, ").that(buf.get(0)).isEqualTo((byte) off); } } diff --git a/src/test/java/com/google/devtools/build/android/ziputils/CentralDirectoryTest.java b/src/test/java/com/google/devtools/build/android/ziputils/CentralDirectoryTest.java index 29ff27d309..b005f4701b 100644 --- a/src/test/java/com/google/devtools/build/android/ziputils/CentralDirectoryTest.java +++ b/src/test/java/com/google/devtools/build/android/ziputils/CentralDirectoryTest.java @@ -13,19 +13,16 @@ // limitations under the License. package com.google.devtools.build.android.ziputils; +import static com.google.common.truth.Truth.assertWithMessage; import static com.google.devtools.build.android.ziputils.DirectoryEntry.CENTIM; -import static org.junit.Assert.assertEquals; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -/** - * Unit tests for {@link CentralDirectory}. - */ +/** Unit tests for {@link CentralDirectory}. */ @RunWith(JUnit4.class) public class CentralDirectoryTest { @@ -44,10 +41,10 @@ public class CentralDirectoryTest { int expPos = 0; int expLimit = 90; // expect the buffer to have been reset to 0 (CentralDirectory does NOT slice). - assertEquals("View not at position 0", expPos, view.buffer.position()); - assertEquals("Buffer not at position 0", expPos, buffer.position()); - assertEquals("Buffer limit changed", expLimit, view.buffer.limit()); - assertEquals("Buffer limit changed", expLimit, buffer.limit()); + assertWithMessage("View not at position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Buffer not at position 0").that(buffer.position()).isEqualTo(expPos); + assertWithMessage("Buffer limit changed").that(view.buffer.limit()).isEqualTo(expLimit); + assertWithMessage("Buffer limit changed").that(buffer.limit()).isEqualTo(expLimit); } /** @@ -73,12 +70,12 @@ public class CentralDirectoryTest { } // Parse the entries. CentralDirectory cdir = CentralDirectory.viewOf(inputBuffer).at(0).parse(); - assertEquals("Count", 20, cdir.getCount()); - assertEquals("Position after parse", expSize, cdir.buffer.position()); - assertEquals("Limit after parse", 10000, cdir.buffer.limit()); + assertWithMessage("Count").that(cdir.getCount()).isEqualTo(20); + assertWithMessage("Position after parse").that(cdir.buffer.position()).isEqualTo(expSize); + assertWithMessage("Limit after parse").that(cdir.buffer.limit()).isEqualTo(10000); cdir.buffer.flip(); - assertEquals("Position after finish", 0, cdir.buffer.position()); - assertEquals("Limit after finish", expSize, cdir.buffer.limit()); + assertWithMessage("Position after finish").that(cdir.buffer.position()).isEqualTo(0); + assertWithMessage("Limit after finish").that(cdir.buffer.limit()).isEqualTo(expSize); } /** @@ -105,20 +102,20 @@ public class CentralDirectoryTest { extra = new byte[extra.length + 1]; comment = comment + "," + i; } - assertEquals("Count", 20, cdir.getCount()); - assertEquals("Position after build", expSize, cdir.buffer.position()); - assertEquals("Limit after build", 10000, cdir.buffer.limit()); + assertWithMessage("Count").that(cdir.getCount()).isEqualTo(20); + assertWithMessage("Position after build").that(cdir.buffer.position()).isEqualTo(expSize); + assertWithMessage("Limit after build").that(cdir.buffer.limit()).isEqualTo(10000); cdir.buffer.flip(); - assertEquals("Position after finish build", 0, cdir.buffer.position()); - assertEquals("Limit after finish build", expSize, cdir.buffer.limit()); + assertWithMessage("Position after finish build").that(cdir.buffer.position()).isEqualTo(0); + assertWithMessage("Limit after finish build").that(cdir.buffer.limit()).isEqualTo(expSize); // now try to parse the directory we just created. cdir.at(0).parse(); - assertEquals("Count", 20, cdir.getCount()); - assertEquals("Position after re-parse", expSize, cdir.buffer.position()); - assertEquals("Limit after re-parse", expSize, cdir.buffer.limit()); + assertWithMessage("Count").that(cdir.getCount()).isEqualTo(20); + assertWithMessage("Position after re-parse").that(cdir.buffer.position()).isEqualTo(expSize); + assertWithMessage("Limit after re-parse").that(cdir.buffer.limit()).isEqualTo(expSize); cdir.buffer.flip(); - assertEquals("Position after finish parse", 0, cdir.buffer.position()); - assertEquals("Limit after finish parse", expSize, cdir.buffer.limit()); + assertWithMessage("Position after finish parse").that(cdir.buffer.position()).isEqualTo(0); + assertWithMessage("Limit after finish parse").that(cdir.buffer.limit()).isEqualTo(expSize); } } diff --git a/src/test/java/com/google/devtools/build/android/ziputils/DataDescriptorTest.java b/src/test/java/com/google/devtools/build/android/ziputils/DataDescriptorTest.java index ffb6f64870..567f2919a6 100644 --- a/src/test/java/com/google/devtools/build/android/ziputils/DataDescriptorTest.java +++ b/src/test/java/com/google/devtools/build/android/ziputils/DataDescriptorTest.java @@ -13,24 +13,20 @@ // limitations under the License. package com.google.devtools.build.android.ziputils; +import static com.google.common.truth.Truth.assertWithMessage; import static com.google.devtools.build.android.ziputils.DataDescriptor.EXTCRC; import static com.google.devtools.build.android.ziputils.DataDescriptor.EXTLEN; import static com.google.devtools.build.android.ziputils.DataDescriptor.EXTSIG; import static com.google.devtools.build.android.ziputils.DataDescriptor.EXTSIZ; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.zip.ZipInputStream; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; -/** - * Unit tests for {@link DataDescriptor}. - */ +/** Unit tests for {@link DataDescriptor}. */ @RunWith(JUnit4.class) public class DataDescriptorTest { @@ -53,10 +49,18 @@ public class DataDescriptorTest { int expSize = marker == DataDescriptor.SIGNATURE ? ZipInputStream.EXTHDR : ZipInputStream.EXTHDR - 4; int expPos = 0; - assertEquals("not based at current position[" + marker + "]", expMark, view.get(EXTSIG)); - assertEquals("Not slice with position 0[" + marker + "]", expPos, view.buffer.position()); - assertEquals("Not sized with comment[" + marker + "]", expSize, view.getSize()); - assertEquals("Not limited to size[" + marker + "]", expSize, view.buffer.limit()); + assertWithMessage("not based at current position[" + marker + "]") + .that(view.get(EXTSIG)) + .isEqualTo(expMark); + assertWithMessage("Not slice with position 0[" + marker + "]") + .that(view.buffer.position()) + .isEqualTo(expPos); + assertWithMessage("Not sized with comment[" + marker + "]") + .that(view.getSize()) + .isEqualTo(expSize); + assertWithMessage("Not limited to size[" + marker + "]") + .that(view.buffer.limit()) + .isEqualTo(expSize); } } @@ -69,11 +73,11 @@ public class DataDescriptorTest { int expSize = ZipInputStream.EXTHDR; int expPos = 0; int expMarker = (int) ZipInputStream.EXTSIG; - assertTrue("no marker", view.hasMarker()); - assertEquals("No marker", expMarker, view.get(EXTSIG)); - assertEquals("Not at position 0", expPos, view.buffer.position()); - assertEquals("Not sized correctly", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); + assertWithMessage("no marker").that(view.hasMarker()).isTrue(); + assertWithMessage("No marker").that(view.get(EXTSIG)).isEqualTo(expMarker); + assertWithMessage("Not at position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized correctly").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); } /** @@ -90,10 +94,10 @@ public class DataDescriptorTest { int expMark = (int) ZipInputStream.EXTSIG; int expSize = ZipInputStream.EXTHDR; int expPos = 0; - assertEquals("not based at current position", expMark, view.get(EXTSIG)); - assertEquals("Not slice with position 0", expPos, view.buffer.position()); - assertEquals("Not sized with comment", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); + assertWithMessage("not based at current position").that(view.get(EXTSIG)).isEqualTo(expMark); + assertWithMessage("Not slice with position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized with comment").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); } /** @@ -105,10 +109,10 @@ public class DataDescriptorTest { DataDescriptor view = DataDescriptor.allocate(); view.copy(buffer); int expSize = view.getSize(); - assertEquals("buffer not advanced as expected", expSize, buffer.position()); + assertWithMessage("buffer not advanced as expected").that(buffer.position()).isEqualTo(expSize); buffer.position(0); DataDescriptor clone = DataDescriptor.viewOf(buffer); - assertEquals("Fail to copy mark", view.get(EXTSIG), clone.get(EXTSIG)); + assertWithMessage("Fail to copy mark").that(clone.get(EXTSIG)).isEqualTo(view.get(EXTSIG)); } /** @@ -123,8 +127,8 @@ public class DataDescriptorTest { .set(EXTCRC, crc) .set(EXTSIZ, compressed) .set(EXTLEN, uncompressed); - assertEquals("CRC", crc, view.get(EXTCRC)); - assertEquals("Compressed size", compressed, view.get(EXTSIZ)); - assertEquals("Uncompressed size", uncompressed, view.get(EXTLEN)); + assertWithMessage("CRC").that(view.get(EXTCRC)).isEqualTo(crc); + assertWithMessage("Compressed size").that(view.get(EXTSIZ)).isEqualTo(compressed); + assertWithMessage("Uncompressed size").that(view.get(EXTLEN)).isEqualTo(uncompressed); } } diff --git a/src/test/java/com/google/devtools/build/android/ziputils/DirectoryEntryTest.java b/src/test/java/com/google/devtools/build/android/ziputils/DirectoryEntryTest.java index 6452dcbd12..4fa7c5409c 100644 --- a/src/test/java/com/google/devtools/build/android/ziputils/DirectoryEntryTest.java +++ b/src/test/java/com/google/devtools/build/android/ziputils/DirectoryEntryTest.java @@ -13,6 +13,7 @@ // limitations under the License. package com.google.devtools.build.android.ziputils; +import static com.google.common.truth.Truth.assertWithMessage; import static com.google.devtools.build.android.ziputils.DirectoryEntry.CENATT; import static com.google.devtools.build.android.ziputils.DirectoryEntry.CENATX; import static com.google.devtools.build.android.ziputils.DirectoryEntry.CENCRC; @@ -27,20 +28,15 @@ import static com.google.devtools.build.android.ziputils.DirectoryEntry.CENTIM; import static com.google.devtools.build.android.ziputils.DirectoryEntry.CENVEM; import static com.google.devtools.build.android.ziputils.DirectoryEntry.CENVER; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertEquals; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.zip.ZipInputStream; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; -/** - * Unit tests for {@link DirectoryEntry}. - */ +/** Unit tests for {@link DirectoryEntry}. */ @RunWith(JUnit4.class) public class DirectoryEntryTest { @@ -67,10 +63,10 @@ public class DirectoryEntryTest { int expMark = (int) ZipInputStream.CENSIG; int expSize = ZipInputStream.CENHDR + filenameLength + extraLength + commentLength; int expPos = 0; - assertEquals("not based at current position", expMark, view.get(CENSIG)); - assertEquals("Not slice with position 0", expPos, view.buffer.position()); - assertEquals("Not sized with comment", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); + assertWithMessage("not based at current position").that(view.get(CENSIG)).isEqualTo(expMark); + assertWithMessage("Not slice with position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized with comment").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); } /** @@ -85,12 +81,12 @@ public class DirectoryEntryTest { + extraData.length + comment.getBytes(UTF_8).length; int expPos = 0; DirectoryEntry view = DirectoryEntry.allocate(filename, extraData, comment); - assertEquals("Incorrect filename", filename, view.getFilename()); - Assert.assertArrayEquals("Incorrect extra data", extraData, view.getExtraData()); - assertEquals("Incorrect comment", comment, view.getComment()); - assertEquals("Not at position 0", expPos, view.buffer.position()); - assertEquals("Not sized correctly", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); + assertWithMessage("Incorrect filename").that(view.getFilename()).isEqualTo(filename); + assertWithMessage("Incorrect extra data").that(view.getExtraData()).isEqualTo(extraData); + assertWithMessage("Incorrect comment").that(view.getComment()).isEqualTo(comment); + assertWithMessage("Not at position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized correctly").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); } /** @@ -112,13 +108,13 @@ public class DirectoryEntryTest { + comment.getBytes(UTF_8).length; int expPos = 0; DirectoryEntry view = DirectoryEntry.view(buffer, filename, extraData, comment); - assertEquals("not based at current position", expMark, view.get(CENSIG)); - assertEquals("Not slice with position 0", expPos, view.buffer.position()); - assertEquals("Not sized with filename", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); - assertEquals("Incorrect filename", filename, view.getFilename()); - Assert.assertArrayEquals("Incorrect extra data", extraData, view.getExtraData()); - assertEquals("Incorrect comment", comment, view.getComment()); + assertWithMessage("not based at current position").that(view.get(CENSIG)).isEqualTo(expMark); + assertWithMessage("Not slice with position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized with filename").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); + assertWithMessage("Incorrect filename").that(view.getFilename()).isEqualTo(filename); + assertWithMessage("Incorrect extra data").that(view.getExtraData()).isEqualTo(extraData); + assertWithMessage("Incorrect comment").that(view.getComment()).isEqualTo(comment); } /** @@ -133,13 +129,17 @@ public class DirectoryEntryTest { DirectoryEntry view = DirectoryEntry.allocate(filename, extraData, comment); view.copy(buffer); int expSize = view.getSize(); - assertEquals("buffer not advanced as expected", expSize, buffer.position()); + assertWithMessage("buffer not advanced as expected").that(buffer.position()).isEqualTo(expSize); buffer.position(0); DirectoryEntry clone = DirectoryEntry.viewOf(buffer); - assertEquals("Fail to copy mark", view.get(CENSIG), clone.get(CENSIG)); - assertEquals("Fail to copy comment", view.getFilename(), clone.getFilename()); - Assert.assertArrayEquals("Fail to copy comment", view.getExtraData(), clone.getExtraData()); - assertEquals("Fail to copy comment", view.getComment(), clone.getComment()); + assertWithMessage("Fail to copy mark").that(clone.get(CENSIG)).isEqualTo(view.get(CENSIG)); + assertWithMessage("Fail to copy comment") + .that(clone.getFilename()) + .isEqualTo(view.getFilename()); + assertWithMessage("Fail to copy comment") + .that(clone.getExtraData()) + .isEqualTo(view.getExtraData()); + assertWithMessage("Fail to copy comment").that(clone.getComment()).isEqualTo(view.getComment()); } /** @@ -172,17 +172,17 @@ public class DirectoryEntryTest { .set(CENATX, extAttr) .set(CENATT, intAttr) .set(CENOFF, offset); - assertEquals("CRC", crc, view.get(CENCRC)); - assertEquals("Compressed size", compressed, view.get(CENSIZ)); - assertEquals("Uncompressed size", uncompressed, view.get(CENLEN)); - assertEquals("Flags", flags, view.get(CENFLG)); - assertEquals("Method", method, view.get(CENHOW)); - assertEquals("Modified time", time, view.get(CENTIM)); - assertEquals("Version needed", version, view.get(CENVER)); - assertEquals("Version made by", versionMadeBy, view.get(CENVEM)); - assertEquals("Disk", disk, view.get(CENDSK)); - assertEquals("External attributes", extAttr, view.get(CENATX)); - assertEquals("Internal attributes", intAttr, view.get(CENATT)); - assertEquals("Offset", offset, view.get(CENOFF)); + assertWithMessage("CRC").that(view.get(CENCRC)).isEqualTo(crc); + assertWithMessage("Compressed size").that(view.get(CENSIZ)).isEqualTo(compressed); + assertWithMessage("Uncompressed size").that(view.get(CENLEN)).isEqualTo(uncompressed); + assertWithMessage("Flags").that(view.get(CENFLG)).isEqualTo(flags); + assertWithMessage("Method").that(view.get(CENHOW)).isEqualTo(method); + assertWithMessage("Modified time").that(view.get(CENTIM)).isEqualTo(time); + assertWithMessage("Version needed").that(view.get(CENVER)).isEqualTo(version); + assertWithMessage("Version made by").that(view.get(CENVEM)).isEqualTo(versionMadeBy); + assertWithMessage("Disk").that(view.get(CENDSK)).isEqualTo(disk); + assertWithMessage("External attributes").that(view.get(CENATX)).isEqualTo(extAttr); + assertWithMessage("Internal attributes").that(view.get(CENATT)).isEqualTo(intAttr); + assertWithMessage("Offset").that(view.get(CENOFF)).isEqualTo(offset); } } diff --git a/src/test/java/com/google/devtools/build/android/ziputils/EndOfCentralDirectoryTest.java b/src/test/java/com/google/devtools/build/android/ziputils/EndOfCentralDirectoryTest.java index edd6115088..7f0534cf38 100644 --- a/src/test/java/com/google/devtools/build/android/ziputils/EndOfCentralDirectoryTest.java +++ b/src/test/java/com/google/devtools/build/android/ziputils/EndOfCentralDirectoryTest.java @@ -13,6 +13,7 @@ // limitations under the License. package com.google.devtools.build.android.ziputils; +import static com.google.common.truth.Truth.assertWithMessage; import static com.google.devtools.build.android.ziputils.EndOfCentralDirectory.ENDDCD; import static com.google.devtools.build.android.ziputils.EndOfCentralDirectory.ENDDSK; import static com.google.devtools.build.android.ziputils.EndOfCentralDirectory.ENDOFF; @@ -21,19 +22,15 @@ import static com.google.devtools.build.android.ziputils.EndOfCentralDirectory.E import static com.google.devtools.build.android.ziputils.EndOfCentralDirectory.ENDSUB; import static com.google.devtools.build.android.ziputils.EndOfCentralDirectory.ENDTOT; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.zip.ZipInputStream; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; -/** - * Unit tests for {@link EndOfCentralDirectory}. - */ +/** Unit tests for {@link EndOfCentralDirectory}. */ @RunWith(JUnit4.class) public class EndOfCentralDirectoryTest { @Test @@ -52,10 +49,10 @@ public class EndOfCentralDirectoryTest { int expMark = (int) ZipInputStream.ENDSIG; int expSize = ZipInputStream.ENDHDR + comLength; // fixed + comment int expPos = 0; - assertEquals("not based at current position", expMark, view.get(ENDSIG)); - assertEquals("Not slice with position 0", expPos, view.buffer.position()); - assertEquals("Not sized with comment", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); + assertWithMessage("not based at current position").that(view.get(ENDSIG)).isEqualTo(expMark); + assertWithMessage("Not slice with position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized with comment").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); } @Test @@ -66,12 +63,12 @@ public class EndOfCentralDirectoryTest { String expComment = comment != null ? comment : ""; EndOfCentralDirectory view = EndOfCentralDirectory.allocate(comment); String commentResult = view.getComment(); - assertEquals("Incorrect comment", expComment, commentResult); + assertWithMessage("Incorrect comment").that(commentResult).isEqualTo(expComment); int expSize = ZipInputStream.ENDHDR + (comment != null ? comment.getBytes(UTF_8).length : 0); int expPos = 0; - assertEquals("Not at position 0", expPos, view.buffer.position()); - assertEquals("Not sized correctly", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); + assertWithMessage("Not at position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized correctly").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); } } @@ -88,11 +85,11 @@ public class EndOfCentralDirectoryTest { int expMark = (int) ZipInputStream.ENDSIG; int expSize = ZipInputStream.ENDHDR + comment.length(); int expPos = 0; - assertEquals("not based at current position", expMark, view.get(ENDSIG)); - assertEquals("Not slice with position 0", expPos, view.buffer.position()); - assertEquals("Not sized with comment", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); - assertEquals("Incorrect comment", comment, view.getComment()); + assertWithMessage("not based at current position").that(view.get(ENDSIG)).isEqualTo(expMark); + assertWithMessage("Not slice with position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized with comment").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); + assertWithMessage("Incorrect comment").that(view.getComment()).isEqualTo(comment); } @Test @@ -101,11 +98,11 @@ public class EndOfCentralDirectoryTest { EndOfCentralDirectory view = EndOfCentralDirectory.allocate("comment"); view.copy(buffer); int expSize = view.getSize(); - assertEquals("buffer not advanced as expected", expSize, buffer.position()); + assertWithMessage("buffer not advanced as expected").that(buffer.position()).isEqualTo(expSize); buffer.position(0); EndOfCentralDirectory clone = EndOfCentralDirectory.viewOf(buffer); - assertEquals("Fail to copy mark", view.get(ENDSIG), clone.get(ENDSIG)); - assertEquals("Fail to copy comment", view.getComment(), clone.getComment()); + assertWithMessage("Fail to copy mark").that(clone.get(ENDSIG)).isEqualTo(view.get(ENDSIG)); + assertWithMessage("Fail to copy comment").that(clone.getComment()).isEqualTo(view.getComment()); } @Test @@ -123,11 +120,13 @@ public class EndOfCentralDirectoryTest { .set(ENDDSK, disk) .set(ENDSUB, local) .set(ENDTOT, total); - assertEquals("Central directory start disk", cdDisk, view.get(ENDDCD)); - assertEquals("Central directory file offset", cdOffset, view.get(ENDOFF)); - assertEquals("Central directory size", cdSize, view.get(ENDSIZ)); - assertEquals("This disk number", disk, view.get(ENDDSK)); - assertEquals("Number of records on this disk", local, view.get(ENDSUB)); - assertEquals("Total number of central directory records", total, view.get(ENDTOT)); + assertWithMessage("Central directory start disk").that(view.get(ENDDCD)).isEqualTo(cdDisk); + assertWithMessage("Central directory file offset").that(view.get(ENDOFF)).isEqualTo(cdOffset); + assertWithMessage("Central directory size").that(view.get(ENDSIZ)).isEqualTo(cdSize); + assertWithMessage("This disk number").that(view.get(ENDDSK)).isEqualTo(disk); + assertWithMessage("Number of records on this disk").that(view.get(ENDSUB)).isEqualTo(local); + assertWithMessage("Total number of central directory records") + .that(view.get(ENDTOT)) + .isEqualTo(total); } } diff --git a/src/test/java/com/google/devtools/build/android/ziputils/LocalFileHeaderTest.java b/src/test/java/com/google/devtools/build/android/ziputils/LocalFileHeaderTest.java index 7791f2d8ba..4c347b854a 100644 --- a/src/test/java/com/google/devtools/build/android/ziputils/LocalFileHeaderTest.java +++ b/src/test/java/com/google/devtools/build/android/ziputils/LocalFileHeaderTest.java @@ -13,6 +13,7 @@ // limitations under the License. package com.google.devtools.build.android.ziputils; +import static com.google.common.truth.Truth.assertWithMessage; import static com.google.devtools.build.android.ziputils.LocalFileHeader.LOCCRC; import static com.google.devtools.build.android.ziputils.LocalFileHeader.LOCFLG; import static com.google.devtools.build.android.ziputils.LocalFileHeader.LOCHOW; @@ -22,20 +23,15 @@ import static com.google.devtools.build.android.ziputils.LocalFileHeader.LOCSIZ; import static com.google.devtools.build.android.ziputils.LocalFileHeader.LOCTIM; import static com.google.devtools.build.android.ziputils.LocalFileHeader.LOCVER; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertEquals; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.zip.ZipInputStream; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; -/** - * Unit tests for {@link LocalFileHeader}. - */ +/** Unit tests for {@link LocalFileHeader}. */ @RunWith(JUnit4.class) public class LocalFileHeaderTest { @@ -57,10 +53,10 @@ public class LocalFileHeaderTest { int expMark = (int) ZipInputStream.LOCSIG; int expSize = ZipInputStream.LOCHDR + filenameLength + extraLength; // fixed + comment int expPos = 0; - assertEquals("not based at current position", expMark, view.get(LOCSIG)); - assertEquals("Not slice with position 0", expPos, view.buffer.position()); - assertEquals("Not sized with comment", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); + assertWithMessage("not based at current position").that(view.get(LOCSIG)).isEqualTo(expMark); + assertWithMessage("Not slice with position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized with comment").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); } @Test @@ -71,11 +67,11 @@ public class LocalFileHeaderTest { + extraData.length; int expPos = 0; LocalFileHeader view = LocalFileHeader.allocate(filename, extraData); - assertEquals("Incorrect filename", filename, view.getFilename()); - Assert.assertArrayEquals("Incorrect extra data", extraData, view.getExtraData()); - assertEquals("Not at position 0", expPos, view.buffer.position()); - assertEquals("Not sized correctly", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); + assertWithMessage("Incorrect filename").that(view.getFilename()).isEqualTo(filename); + assertWithMessage("Incorrect extra data").that(view.getExtraData()).isEqualTo(extraData); + assertWithMessage("Not at position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized correctly").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); } @Test @@ -91,11 +87,11 @@ public class LocalFileHeaderTest { int expSize = ZipInputStream.LOCHDR + filename.getBytes(UTF_8).length; int expPos = 0; LocalFileHeader view = LocalFileHeader.view(buffer, filename, null); - assertEquals("not based at current position", expMark, view.get(LOCSIG)); - assertEquals("Not slice with position 0", expPos, view.buffer.position()); - assertEquals("Not sized with filename", expSize, view.getSize()); - assertEquals("Not limited to size", expSize, view.buffer.limit()); - assertEquals("Incorrect filename", filename, view.getFilename()); + assertWithMessage("not based at current position").that(view.get(LOCSIG)).isEqualTo(expMark); + assertWithMessage("Not slice with position 0").that(view.buffer.position()).isEqualTo(expPos); + assertWithMessage("Not sized with filename").that(view.getSize()).isEqualTo(expSize); + assertWithMessage("Not limited to size").that(view.buffer.limit()).isEqualTo(expSize); + assertWithMessage("Incorrect filename").that(view.getFilename()).isEqualTo(filename); } @Test @@ -104,11 +100,13 @@ public class LocalFileHeaderTest { LocalFileHeader view = LocalFileHeader.allocate("pkg/foo.class", null); view.copy(buffer); int expSize = view.getSize(); - assertEquals("buffer not advanced as expected", expSize, buffer.position()); + assertWithMessage("buffer not advanced as expected").that(buffer.position()).isEqualTo(expSize); buffer.position(0); LocalFileHeader clone = LocalFileHeader.viewOf(buffer); - assertEquals("Fail to copy mark", view.get(LOCSIG), view.get(LOCSIG)); - assertEquals("Fail to copy comment", view.getFilename(), clone.getFilename()); + assertWithMessage("Fail to copy mark").that(view.get(LOCSIG)).isEqualTo(view.get(LOCSIG)); + assertWithMessage("Fail to copy comment") + .that(clone.getFilename()) + .isEqualTo(view.getFilename()); } @Test @@ -128,12 +126,12 @@ public class LocalFileHeaderTest { .set(LOCHOW, method) .set(LOCTIM, time) .set(LOCVER, version); - assertEquals("CRC", crc, view.get(LOCCRC)); - assertEquals("Compressed size", compressed, view.get(LOCSIZ)); - assertEquals("Uncompressed size", uncompressed, view.get(LOCLEN)); - assertEquals("Flags", flags, view.get(LOCFLG)); - assertEquals("Method", method, view.get(LOCHOW)); - assertEquals("Modified time", time, view.get(LOCTIM)); - assertEquals("Version needed", version, view.get(LOCVER)); + assertWithMessage("CRC").that(view.get(LOCCRC)).isEqualTo(crc); + assertWithMessage("Compressed size").that(view.get(LOCSIZ)).isEqualTo(compressed); + assertWithMessage("Uncompressed size").that(view.get(LOCLEN)).isEqualTo(uncompressed); + assertWithMessage("Flags").that(view.get(LOCFLG)).isEqualTo(flags); + assertWithMessage("Method").that(view.get(LOCHOW)).isEqualTo(method); + assertWithMessage("Modified time").that(view.get(LOCTIM)).isEqualTo(time); + assertWithMessage("Version needed").that(view.get(LOCVER)).isEqualTo(version); } } diff --git a/src/test/java/com/google/devtools/build/android/ziputils/SplitZipTest.java b/src/test/java/com/google/devtools/build/android/ziputils/SplitZipTest.java index 5d6f861159..87cc5ce6d8 100644 --- a/src/test/java/com/google/devtools/build/android/ziputils/SplitZipTest.java +++ b/src/test/java/com/google/devtools/build/android/ziputils/SplitZipTest.java @@ -14,26 +14,21 @@ package com.google.devtools.build.android.ziputils; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.fail; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableSet; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; import java.util.Date; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; -/** - * Unit tests for {@link SplitZip}. - */ +/** Unit tests for {@link SplitZip}. */ @RunWith(JUnit4.class) public class SplitZipTest { private FakeFileSystem fileSystem; @@ -59,7 +54,9 @@ public class SplitZipTest { instance.addOutput((String) null); fail("should have failed"); } catch (Exception ex) { - assertTrue("NullPointerException expected", ex instanceof NullPointerException); + assertWithMessage("NullPointerException expected") + .that(ex instanceof NullPointerException) + .isTrue(); } try { SplitZip result = instance @@ -67,7 +64,7 @@ public class SplitZipTest { "out/shard1.jar")) .addOutput(new ZipOut(fileSystem.getOutputChannel("out/shard2.jar", false), "out/shard2.jar")); - assertSame(instance, result); + assertThat(result).isSameAs(instance); } catch (IOException ex) { fail("Unexpected exception: " + ex); } @@ -78,7 +75,7 @@ public class SplitZipTest { SplitZip instance = new SplitZip(); String res = "res"; SplitZip result = instance.setResourceFile(res); - assertSame(instance, result); + assertThat(result).isSameAs(instance); } @Test @@ -93,9 +90,9 @@ public class SplitZipTest { public void testSetMainClassListFile() { SplitZip instance = new SplitZip(); SplitZip result = instance.setMainClassListFile((String) null); - assertSame(instance, result); + assertThat(result).isSameAs(instance); result = instance.setMainClassListFile("no format checks"); - assertSame(instance, result); + assertThat(result).isSameAs(instance); } @Test @@ -113,7 +110,7 @@ public class SplitZipTest { public void testSetEntryDate() { SplitZip instance = new SplitZip(); SplitZip result = instance.setEntryDate(null); - assertSame(instance, result); + assertThat(result).isSameAs(instance); } @Test @@ -122,7 +119,7 @@ public class SplitZipTest { Date now = new Date(); instance.setEntryDate(now); Date result = instance.getEntryDate(); - assertSame(result, now); + assertThat(now).isSameAs(result); instance.setEntryDate(null); assertThat(instance.getEntryDate()).isNull(); } @@ -131,7 +128,7 @@ public class SplitZipTest { public void testUseDefaultEntryDate() { SplitZip instance = new SplitZip(); SplitZip result = instance.useDefaultEntryDate(); - assertSame(instance, result); + assertThat(result).isSameAs(instance); Date date = instance.getEntryDate(); assertThat(date).isEqualTo(DosTime.DOS_EPOCH); } @@ -144,7 +141,9 @@ public class SplitZipTest { instance.addInput(noexists); fail("should not be able to add non existing file: " + noexists); } catch (IOException ex) { - assertTrue("FileNotFoundException expected", ex instanceof FileNotFoundException); + assertWithMessage("FileNotFoundException expected") + .that(ex instanceof FileNotFoundException) + .isTrue(); } } @@ -156,7 +155,9 @@ public class SplitZipTest { instance.addInputs(Arrays.asList(noexists)); fail("should not be able to add non existing file: " + noexists); } catch (IOException ex) { - assertTrue("FileNotFoundException expected", ex instanceof FileNotFoundException); + assertWithMessage("FileNotFoundException expected") + .that(ex instanceof FileNotFoundException) + .isTrue(); } } diff --git a/src/test/java/com/google/devtools/build/android/ziputils/ViewTest.java b/src/test/java/com/google/devtools/build/android/ziputils/ViewTest.java index 91fbdb691a..17915270dc 100644 --- a/src/test/java/com/google/devtools/build/android/ziputils/ViewTest.java +++ b/src/test/java/com/google/devtools/build/android/ziputils/ViewTest.java @@ -13,23 +13,17 @@ // limitations under the License. package com.google.devtools.build.android.ziputils; +import static com.google.common.truth.Truth.assertWithMessage; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; -import org.junit.Assert; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; - -/** - * Unit tests for {@link View}. - */ +/** Unit tests for {@link View}. */ @RunWith(JUnit4.class) public class ViewTest { @@ -44,10 +38,10 @@ public class ViewTest { buffer.putInt(12345678); int fromBuf = buffer.getInt(0); int fromView = instance.getInt(0); - assertEquals("must assume buffer ownership", fromBuf, fromView); + assertWithMessage("must assume buffer ownership").that(fromView).isEqualTo(fromBuf); int posBuf = buffer.position(); int posView = instance.buffer.position(); - assertEquals("must assume buffer ownership", posBuf, posView); + assertWithMessage("must assume buffer ownership").that(posView).isEqualTo(posBuf); } @Test @@ -56,10 +50,10 @@ public class ViewTest { ByteBuffer buffer = ByteBuffer.allocate(100); TestView instance = new TestView(buffer); View result = instance.at(fileOffset); - assertSame("didn't return this", instance, result); + assertWithMessage("didn't return this").that(result).isSameAs(instance); long resultValue = instance.fileOffset(); - assertEquals("didn't return set value", fileOffset, resultValue); + assertWithMessage("didn't return set value").that(resultValue).isEqualTo(fileOffset); } @Test @@ -68,7 +62,7 @@ public class ViewTest { TestView instance = new TestView(buffer); long expResult = -1L; long result = instance.fileOffset(); - assertEquals("default file offset should be -1", expResult, result); + assertWithMessage("default file offset should be -1").that(result).isEqualTo(expResult); } @Test @@ -77,18 +71,18 @@ public class ViewTest { TestView instance = new TestView(buffer); int limit = instance.buffer.limit(); int pos = instance.buffer.position(); - assertEquals("initial limit", 100, limit); - assertEquals("initial position", 0, pos); + assertWithMessage("initial limit").that(limit).isEqualTo(100); + assertWithMessage("initial position").that(pos).isEqualTo(0); instance.putInt(1234); limit = instance.buffer.limit(); pos = instance.buffer.position(); - assertEquals("limit unchanged", 100, limit); - assertEquals("position advanced", 4, pos); + assertWithMessage("limit unchanged").that(limit).isEqualTo(100); + assertWithMessage("position advanced").that(pos).isEqualTo(4); instance.buffer.flip(); int finishedLimit = instance.buffer.limit(); int finishedPos = instance.buffer.position(); - assertEquals("must set limit to position", pos, finishedLimit); - assertEquals("must set position to 0", 0, finishedPos); + assertWithMessage("must set limit to position").that(finishedLimit).isEqualTo(pos); + assertWithMessage("must set position to 0").that(finishedPos).isEqualTo(0); } @Test @@ -101,9 +95,9 @@ public class ViewTest { instance.buffer.rewind(); int result = file.write(instance.buffer); file.close(); - assertEquals("incorrect number of bytes written", expResult, result); + assertWithMessage("incorrect number of bytes written").that(result).isEqualTo(expResult); byte[] bytesWritten = fileSystem.toByteArray("hello"); - Assert.assertArrayEquals("incorrect bytes written", bytes, bytesWritten); + assertWithMessage("incorrect bytes written").that(bytesWritten).isEqualTo(bytes); } @Test @@ -115,7 +109,7 @@ public class ViewTest { TestView instance = new TestView(buffer); byte[] expResult = "lo wo".getBytes(UTF_8); byte[] result = instance.getBytes(off, len); - assertArrayEquals("incorrect bytes returned", expResult, result); + assertWithMessage("incorrect bytes returned").that(result).isEqualTo(expResult); try { instance.getBytes(bytes.length - len + 1, len); fail("expected Exception"); @@ -139,7 +133,7 @@ public class ViewTest { TestView instance = new TestView(buffer); String expResult = "world"; String result = instance.getString(off, len); - assertEquals("didn't return this", expResult, result); + assertWithMessage("didn't return this").that(result).isEqualTo(expResult); try { instance.getString(off + 1, len); fail("expected Exception"); @@ -160,7 +154,7 @@ public class ViewTest { TestView instance = new TestView(ByteBuffer.wrap(bytes)); int expValue = 0x08070605; int value = instance.getInt(4); - assertEquals("Byte order incorrect", expValue, value); + assertWithMessage("Byte order incorrect").that(value).isEqualTo(expValue); } static class TestView extends View { diff --git a/src/test/java/com/google/devtools/build/android/ziputils/ZipInTest.java b/src/test/java/com/google/devtools/build/android/ziputils/ZipInTest.java index c29fc864c9..17b6108387 100644 --- a/src/test/java/com/google/devtools/build/android/ziputils/ZipInTest.java +++ b/src/test/java/com/google/devtools/build/android/ziputils/ZipInTest.java @@ -13,6 +13,8 @@ // limitations under the License. package com.google.devtools.build.android.ziputils; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static com.google.devtools.build.android.ziputils.DirectoryEntry.CENHOW; import static com.google.devtools.build.android.ziputils.DirectoryEntry.CENLEN; import static com.google.devtools.build.android.ziputils.DirectoryEntry.CENOFF; @@ -23,10 +25,6 @@ import static com.google.devtools.build.android.ziputils.EndOfCentralDirectory.E import static com.google.devtools.build.android.ziputils.EndOfCentralDirectory.ENDSUB; import static com.google.devtools.build.android.ziputils.EndOfCentralDirectory.ENDTOT; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; import com.google.devtools.build.android.ziputils.ZipIn.ZipEntry; @@ -87,7 +85,7 @@ public class ZipInTest { fileSystem.addFile(filename, bytes); zipIn = newZipIn(filename); result = zipIn.endOfCentralDirectory(); - assertNotNull(subcase + "found", result); + assertWithMessage(subcase + "found").that(result).isNotNull(); subcase = " EOCD not there at all, "; bytes = new byte[]{ @@ -108,7 +106,9 @@ public class ZipInTest { zipIn.endOfCentralDirectory(); fail(subcase + "expected IllegalStateException"); } catch (Exception ex) { - assertSame(subcase + "caught exception", IllegalStateException.class, ex.getClass()); + assertWithMessage(subcase + "caught exception") + .that(ex.getClass()) + .isSameAs(IllegalStateException.class); } // If we can't read it, it's not there @@ -131,7 +131,9 @@ public class ZipInTest { zipIn.endOfCentralDirectory(); fail(subcase + "expected IndexOutOfBoundsException"); } catch (Exception ex) { - assertSame(subcase + "caught exception", IndexOutOfBoundsException.class, ex.getClass()); + assertWithMessage(subcase + "caught exception") + .that(ex.getClass()) + .isSameAs(IndexOutOfBoundsException.class); } // Current implementation doesn't know to scan past a bad EOCD record. @@ -155,7 +157,9 @@ public class ZipInTest { zipIn.endOfCentralDirectory(); fail(subcase + "expected IndexOutOfBoundsException"); } catch (Exception ex) { - assertSame(subcase + "caught exception", IndexOutOfBoundsException.class, ex.getClass()); + assertWithMessage(subcase + "caught exception") + .that(ex.getClass()) + .isSameAs(IndexOutOfBoundsException.class); } // Minimal format checking here, assuming the EndOfDirectoryTest class @@ -176,7 +180,9 @@ public class ZipInTest { zipIn.endOfCentralDirectory(); fail(subcase + "expected IllegalArgumentException"); } catch (Exception ex) { - assertSame(subcase + "caught exception", IllegalArgumentException.class, ex.getClass()); + assertWithMessage(subcase + "caught exception") + .that(ex.getClass()) + .isSameAs(IllegalArgumentException.class); } subcase = " EOCD no comment, "; @@ -190,9 +196,11 @@ public class ZipInTest { fileSystem.addFile(filename, bytes); zipIn = newZipIn(filename); result = zipIn.endOfCentralDirectory(); - assertNotNull(subcase + "found", result); - assertEquals(subcase + "comment", "", result.getComment()); - assertEquals(subcase + "marker", ZipInputStream.ENDSIG, (int) result.get(ENDSIG)); + assertWithMessage(subcase + "found").that(result).isNotNull(); + assertWithMessage(subcase + "comment").that(result.getComment()).isEqualTo(""); + assertWithMessage(subcase + "marker") + .that((int) result.get(ENDSIG)) + .isEqualTo(ZipInputStream.ENDSIG); subcase = " EOCD comment, "; bytes = new byte[100]; @@ -202,14 +210,17 @@ public class ZipInTest { offset = bytes.length - ZipInputStream.ENDHDR - commentLen; buffer.position(offset); EndOfCentralDirectory.view(buffer, comment); - assertEquals(subcase + "setup", comment, - new String(bytes, bytes.length - commentLen, commentLen, UTF_8)); + assertWithMessage(subcase + "setup") + .that(new String(bytes, bytes.length - commentLen, commentLen, UTF_8)) + .isEqualTo(comment); fileSystem.addFile(filename, bytes); zipIn = newZipIn(filename); result = zipIn.endOfCentralDirectory(); - assertNotNull(subcase + "found", result); - assertEquals(subcase + "comment", comment, result.getComment()); - assertEquals(subcase + "marker", ZipInputStream.ENDSIG, (int) result.get(ENDSIG)); + assertWithMessage(subcase + "found").that(result).isNotNull(); + assertWithMessage(subcase + "comment").that(result.getComment()).isEqualTo(comment); + assertWithMessage(subcase + "marker") + .that((int) result.get(ENDSIG)) + .isEqualTo(ZipInputStream.ENDSIG); subcase = " EOCD extra data, "; bytes = new byte[100]; @@ -222,9 +233,11 @@ public class ZipInTest { fileSystem.addFile(filename, bytes); zipIn = newZipIn(filename); result = zipIn.endOfCentralDirectory(); - assertNotNull(subcase + "found", result); - assertEquals(subcase + "comment", "", result.getComment()); - assertEquals(subcase + "marker", ZipInputStream.ENDSIG, (int) result.get(ENDSIG)); + assertWithMessage(subcase + "found").that(result).isNotNull(); + assertWithMessage(subcase + "comment").that(result.getComment()).isEqualTo(""); + assertWithMessage(subcase + "marker") + .that((int) result.get(ENDSIG)) + .isEqualTo(ZipInputStream.ENDSIG); } /** @@ -274,11 +287,13 @@ public class ZipInTest { fileSystem.addFile(filename, bytes); zipIn = newZipIn(filename); CentralDirectory result = zipIn.centralDirectory(); - assertNotNull(subcase + "found", result); + assertWithMessage(subcase + "found").that(result).isNotNull(); List list = result.list(); - assertEquals(subcase + "size", count, list.size()); + assertWithMessage(subcase + "size").that(list.size()).isEqualTo(count); for (int i = 0; i < list.size(); i++) { - assertEquals(subcase + "offset check[" + i + "]", i, list.get(i).get(CENOFF)); + assertWithMessage(subcase + "offset check[" + i + "]") + .that(list.get(i).get(CENOFF)) + .isEqualTo(i); } } @@ -297,18 +312,21 @@ public class ZipInTest { builder.create(filename); final ZipIn zipIn = newZipIn(filename); - zipIn.scanEntries(new EntryHandler() { - int count = 0; - @Override - public void handle(ZipIn in, LocalFileHeader header, DirectoryEntry dirEntry, - ByteBuffer data) throws IOException { - assertSame(zipIn, in); - String filename = "pkg/f" + count + ".class"; - assertEquals(filename, header.getFilename()); - assertEquals(filename, dirEntry.getFilename()); - count++; - } - }); + zipIn.scanEntries( + new EntryHandler() { + int count = 0; + + @Override + public void handle( + ZipIn in, LocalFileHeader header, DirectoryEntry dirEntry, ByteBuffer data) + throws IOException { + assertThat(in).isSameAs(zipIn); + String filename = "pkg/f" + count + ".class"; + assertThat(header.getFilename()).isEqualTo(filename); + assertThat(dirEntry.getFilename()).isEqualTo(filename); + count++; + } + }); } /** @@ -332,12 +350,12 @@ public class ZipInTest { header = zipIn.nextHeaderFrom(offset); String name = "pkg/f" + count + ".class"; if (header != null) { - assertEquals(name, header.getFilename()); + assertThat(header.getFilename()).isEqualTo(name); count++; offset = (int) header.fileOffset() + 4; } } while(header != null); - assertEquals(ENTRY_COUNT, count); + assertThat(count).isEqualTo(ENTRY_COUNT); } /** @@ -360,12 +378,12 @@ public class ZipInTest { LocalFileHeader header = zipIn.nextHeaderFrom(null); for (DirectoryEntry dirEntry : list) { name = "pkg/f" + count + ".class"; - assertEquals(name, dirEntry.getFilename()); - assertEquals(name, header.getFilename()); + assertThat(dirEntry.getFilename()).isEqualTo(name); + assertThat(header.getFilename()).isEqualTo(name); header = zipIn.nextHeaderFrom(dirEntry); count++; } - assertNull(header); + assertThat(header).isNull(); } /** @@ -389,8 +407,8 @@ public class ZipInTest { for (DirectoryEntry dirEntry : list) { name = "pkg/f" + count + ".class"; header = zipIn.localHeaderFor(dirEntry); - assertEquals(name, dirEntry.getFilename()); - assertEquals(name, header.getFilename()); + assertThat(dirEntry.getFilename()).isEqualTo(name); + assertThat(header.getFilename()).isEqualTo(name); count++; } } @@ -416,8 +434,8 @@ public class ZipInTest { for (DirectoryEntry dirEntry : list) { name = "pkg/f" + count + ".class"; header = zipIn.localHeaderAt(dirEntry.get(CENOFF)); - assertEquals(name, dirEntry.getFilename()); - assertEquals(name, header.getFilename()); + assertThat(dirEntry.getFilename()).isEqualTo(name); + assertThat(header.getFilename()).isEqualTo(name); count++; } } @@ -443,15 +461,15 @@ public class ZipInTest { zipEntry = zipIn.nextFrom(offset); String name = "pkg/f" + count + ".class"; if (zipEntry.getCode() != ZipEntry.Status.ENTRY_NOT_FOUND) { - assertNotNull(zipEntry.getHeader()); - assertNotNull(zipEntry.getDirEntry()); - assertEquals(name, zipEntry.getHeader().getFilename()); - assertEquals(name, zipEntry.getDirEntry().getFilename()); + assertThat(zipEntry.getHeader()).isNotNull(); + assertThat(zipEntry.getDirEntry()).isNotNull(); + assertThat(zipEntry.getHeader().getFilename()).isEqualTo(name); + assertThat(zipEntry.getDirEntry().getFilename()).isEqualTo(name); count++; offset = (int) zipEntry.getHeader().fileOffset() + 4; } } while(zipEntry.getCode() != ZipEntry.Status.ENTRY_NOT_FOUND); - assertEquals(ENTRY_COUNT, count); + assertThat(count).isEqualTo(ENTRY_COUNT); } /** @@ -477,14 +495,14 @@ public class ZipInTest { break; } name = "pkg/f" + count + ".class"; - assertNotNull(zipEntry.getHeader()); - assertNotNull(zipEntry.getDirEntry()); - assertEquals(name, zipEntry.getHeader().getFilename()); - assertEquals(name, zipEntry.getDirEntry().getFilename()); + assertThat(zipEntry.getHeader()).isNotNull(); + assertThat(zipEntry.getDirEntry()).isNotNull(); + assertThat(zipEntry.getHeader().getFilename()).isEqualTo(name); + assertThat(zipEntry.getDirEntry().getFilename()).isEqualTo(name); zipEntry = zipIn.nextFrom(dirEntry); count++; } - assertEquals(ENTRY_COUNT, count); + assertThat(count).isEqualTo(ENTRY_COUNT); } /** @@ -508,13 +526,13 @@ public class ZipInTest { for (DirectoryEntry dirEntry : list) { zipEntry = zipIn.entryAt(dirEntry.get(CENOFF)); name = "pkg/f" + count + ".class"; - assertNotNull(zipEntry.getHeader()); - assertNotNull(zipEntry.getDirEntry()); - assertEquals(name, zipEntry.getHeader().getFilename()); - assertEquals(name, zipEntry.getDirEntry().getFilename()); + assertThat(zipEntry.getHeader()).isNotNull(); + assertThat(zipEntry.getDirEntry()).isNotNull(); + assertThat(zipEntry.getHeader().getFilename()).isEqualTo(name); + assertThat(zipEntry.getDirEntry().getFilename()).isEqualTo(name); count++; } - assertEquals(ENTRY_COUNT, count); + assertThat(count).isEqualTo(ENTRY_COUNT); } /** @@ -539,16 +557,16 @@ public class ZipInTest { String name = "pkg/f" + count + ".class"; if (header != null) { ZipEntry zipEntry = zipIn.entryWith(header); - assertNotNull(zipEntry.getDirEntry()); - assertSame(header, zipEntry.getHeader()); - assertEquals(name, zipEntry.getHeader().getFilename()); - assertEquals(name, zipEntry.getDirEntry().getFilename()); - assertEquals(name, header.getFilename()); + assertThat(zipEntry.getDirEntry()).isNotNull(); + assertThat(zipEntry.getHeader()).isSameAs(header); + assertThat(zipEntry.getHeader().getFilename()).isEqualTo(name); + assertThat(zipEntry.getDirEntry().getFilename()).isEqualTo(name); + assertThat(header.getFilename()).isEqualTo(name); count++; offset = (int) header.fileOffset() + 4; } } while(header != null); - assertEquals(ENTRY_COUNT, count); + assertThat(count).isEqualTo(ENTRY_COUNT); } private ZipIn newZipIn(String filename) throws IOException { diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisUtilsTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisUtilsTest.java index 821140001f..4c6f2e4e58 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisUtilsTest.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisUtilsTest.java @@ -19,7 +19,6 @@ import static com.google.devtools.build.lib.analysis.AnalysisUtils.checkProvider import static org.junit.Assert.fail; import com.google.auto.value.AutoValue; - import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -38,7 +37,7 @@ public class AnalysisUtilsTest { checkProvider(AutoValue_AnalysisUtilsTest_AutoValuedClass.class); fail("Expected IllegalArgumentException, but nothing was thrown."); } catch (IllegalArgumentException e) { - assertThat(e.getMessage()).contains("generated by @AutoValue"); + assertThat(e).hasMessageThat().contains("generated by @AutoValue"); } } diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnectorMultiplexerTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnectorMultiplexerTest.java index dedf316868..1928144add 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnectorMultiplexerTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnectorMultiplexerTest.java @@ -157,7 +157,7 @@ public class HttpConnectorMultiplexerTest { multiplexer.connect(asList(URL1, URL2, URL3), ""); fail("Expected IOException"); } catch (IOException e) { - assertThat(e.getMessage()).contains("All mirrors are down"); + assertThat(e).hasMessageThat().contains("All mirrors are down"); } verify(connector, times(3)).connect(any(URL.class), any(ImmutableMap.class)); verify(sleeper, times(2)).sleepMillis(anyLong()); diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelperTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelperTest.java index 8da2e6e0ef..5aa7d107cf 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelperTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/ProxyHelperTest.java @@ -15,7 +15,6 @@ package com.google.devtools.build.lib.bazel.repository.downloader; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import com.google.common.collect.ImmutableMap; @@ -65,40 +64,40 @@ public class ProxyHelperTest { public void testNoProxy() throws Exception { // Empty address. Proxy proxy = ProxyHelper.createProxy(null); - assertEquals(Proxy.NO_PROXY, proxy); + assertThat(proxy).isEqualTo(Proxy.NO_PROXY); proxy = ProxyHelper.createProxy(""); - assertEquals(Proxy.NO_PROXY, proxy); + assertThat(proxy).isEqualTo(Proxy.NO_PROXY); Map env = ImmutableMap.of(); ProxyHelper helper = new ProxyHelper(env); proxy = helper.createProxyIfNeeded(new URL("https://www.something.com")); - assertEquals(Proxy.NO_PROXY, proxy); + assertThat(proxy).isEqualTo(Proxy.NO_PROXY); } @Test public void testProxyDefaultPort() throws Exception { Proxy proxy = ProxyHelper.createProxy("http://my.example.com"); - assertEquals(Proxy.Type.HTTP, proxy.type()); + assertThat(proxy.type()).isEqualTo(Proxy.Type.HTTP); assertThat(proxy.toString()).endsWith(":80"); - assertEquals("my.example.com", System.getProperty("http.proxyHost")); - assertEquals("80", System.getProperty("http.proxyPort")); + assertThat(System.getProperty("http.proxyHost")).isEqualTo("my.example.com"); + assertThat(System.getProperty("http.proxyPort")).isEqualTo("80"); proxy = ProxyHelper.createProxy("https://my.example.com"); assertThat(proxy.toString()).endsWith(":443"); - assertEquals("my.example.com", System.getProperty("https.proxyHost")); - assertEquals("443", System.getProperty("https.proxyPort")); + assertThat(System.getProperty("https.proxyHost")).isEqualTo("my.example.com"); + assertThat(System.getProperty("https.proxyPort")).isEqualTo("443"); } @Test public void testProxyExplicitPort() throws Exception { Proxy proxy = ProxyHelper.createProxy("http://my.example.com:12345"); assertThat(proxy.toString()).endsWith(":12345"); - assertEquals("my.example.com", System.getProperty("http.proxyHost")); - assertEquals("12345", System.getProperty("http.proxyPort")); + assertThat(System.getProperty("http.proxyHost")).isEqualTo("my.example.com"); + assertThat(System.getProperty("http.proxyPort")).isEqualTo("12345"); proxy = ProxyHelper.createProxy("https://my.example.com:12345"); assertThat(proxy.toString()).endsWith(":12345"); - assertEquals("my.example.com", System.getProperty("https.proxyHost")); - assertEquals("12345", System.getProperty("https.proxyPort")); + assertThat(System.getProperty("https.proxyHost")).isEqualTo("my.example.com"); + assertThat(System.getProperty("https.proxyPort")).isEqualTo("12345"); } @Test @@ -107,7 +106,7 @@ public class ProxyHelperTest { ProxyHelper.createProxy("my.example.com"); fail("Expected protocol error"); } catch (IOException e) { - assertThat(e.getMessage()).contains("Proxy address my.example.com is not a valid URL"); + assertThat(e).hasMessageThat().contains("Proxy address my.example.com is not a valid URL"); } } @@ -117,7 +116,9 @@ public class ProxyHelperTest { ProxyHelper.createProxy("my.example.com:12345"); fail("Expected protocol error"); } catch (IOException e) { - assertThat(e.getMessage()).contains("Proxy address my.example.com:12345 is not a valid URL"); + assertThat(e) + .hasMessageThat() + .contains("Proxy address my.example.com:12345 is not a valid URL"); } } @@ -127,7 +128,8 @@ public class ProxyHelperTest { ProxyHelper.createProxy("http://my.example.com:foo"); fail("Should have thrown an error for invalid port"); } catch (IOException e) { - assertThat(e.getMessage()) + assertThat(e) + .hasMessageThat() .contains("Proxy address http://my.example.com:foo is not a valid URL"); } } @@ -135,30 +137,30 @@ public class ProxyHelperTest { @Test public void testProxyAuth() throws Exception { Proxy proxy = ProxyHelper.createProxy("http://foo:barbaz@my.example.com"); - assertEquals(Proxy.Type.HTTP, proxy.type()); + assertThat(proxy.type()).isEqualTo(Proxy.Type.HTTP); assertThat(proxy.toString()).endsWith(":80"); - assertEquals("my.example.com", System.getProperty("http.proxyHost")); - assertEquals("80", System.getProperty("http.proxyPort")); - assertEquals("foo", System.getProperty("http.proxyUser")); - assertEquals("barbaz", System.getProperty("http.proxyPassword")); + assertThat(System.getProperty("http.proxyHost")).isEqualTo("my.example.com"); + assertThat(System.getProperty("http.proxyPort")).isEqualTo("80"); + assertThat(System.getProperty("http.proxyUser")).isEqualTo("foo"); + assertThat(System.getProperty("http.proxyPassword")).isEqualTo("barbaz"); proxy = ProxyHelper.createProxy("https://biz:bat@my.example.com"); assertThat(proxy.toString()).endsWith(":443"); - assertEquals("my.example.com", System.getProperty("https.proxyHost")); - assertEquals("443", System.getProperty("https.proxyPort")); - assertEquals("biz", System.getProperty("https.proxyUser")); - assertEquals("bat", System.getProperty("https.proxyPassword")); + assertThat(System.getProperty("https.proxyHost")).isEqualTo("my.example.com"); + assertThat(System.getProperty("https.proxyPort")).isEqualTo("443"); + assertThat(System.getProperty("https.proxyUser")).isEqualTo("biz"); + assertThat(System.getProperty("https.proxyPassword")).isEqualTo("bat"); } @Test public void testEncodedProxyAuth() throws Exception { Proxy proxy = ProxyHelper.createProxy("http://foo:b%40rb%40z@my.example.com"); - assertEquals(Proxy.Type.HTTP, proxy.type()); + assertThat(proxy.type()).isEqualTo(Proxy.Type.HTTP); assertThat(proxy.toString()).endsWith(":80"); - assertEquals("my.example.com", System.getProperty("http.proxyHost")); - assertEquals("80", System.getProperty("http.proxyPort")); - assertEquals("foo", System.getProperty("http.proxyUser")); - assertEquals("b@rb@z", System.getProperty("http.proxyPassword")); + assertThat(System.getProperty("http.proxyHost")).isEqualTo("my.example.com"); + assertThat(System.getProperty("http.proxyPort")).isEqualTo("80"); + assertThat(System.getProperty("http.proxyUser")).isEqualTo("foo"); + assertThat(System.getProperty("http.proxyPassword")).isEqualTo("b@rb@z"); } @Test @@ -167,27 +169,27 @@ public class ProxyHelperTest { ProxyHelper.createProxy("http://foo@my.example.com"); fail("Should have thrown an error for invalid auth"); } catch (IOException e) { - assertThat(e.getMessage()).contains("No password given for proxy"); + assertThat(e).hasMessageThat().contains("No password given for proxy"); } } @Test public void testNoProxyAuth() throws Exception { Proxy proxy = ProxyHelper.createProxy("http://localhost:3128/"); - assertEquals(Proxy.Type.HTTP, proxy.type()); + assertThat(proxy.type()).isEqualTo(Proxy.Type.HTTP); assertThat(proxy.toString()).endsWith(":3128"); - assertEquals("localhost", System.getProperty("http.proxyHost")); - assertEquals("3128", System.getProperty("http.proxyPort")); + assertThat(System.getProperty("http.proxyHost")).isEqualTo("localhost"); + assertThat(System.getProperty("http.proxyPort")).isEqualTo("3128"); } @Test public void testTrailingSlash() throws Exception { Proxy proxy = ProxyHelper.createProxy("http://foo:bar@example.com:8000/"); - assertEquals(Proxy.Type.HTTP, proxy.type()); + assertThat(proxy.type()).isEqualTo(Proxy.Type.HTTP); assertThat(proxy.toString()).endsWith(":8000"); - assertEquals("example.com", System.getProperty("http.proxyHost")); - assertEquals("8000", System.getProperty("http.proxyPort")); - assertEquals("foo", System.getProperty("http.proxyUser")); - assertEquals("bar", System.getProperty("http.proxyPassword")); + assertThat(System.getProperty("http.proxyHost")).isEqualTo("example.com"); + assertThat(System.getProperty("http.proxyPort")).isEqualTo("8000"); + assertThat(System.getProperty("http.proxyUser")).isEqualTo("foo"); + assertThat(System.getProperty("http.proxyPassword")).isEqualTo("bar"); } } diff --git a/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransportTest.java b/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransportTest.java index 8f99efca83..c7ef2181a7 100644 --- a/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransportTest.java +++ b/src/test/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransportTest.java @@ -15,8 +15,6 @@ package com.google.devtools.build.lib.buildeventstream.transports; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; import com.google.devtools.build.lib.buildeventstream.ArtifactGroupNamer; @@ -103,7 +101,7 @@ public class BinaryFormatFileTransportTest { // Get a file that doesn't exist by creating a new file and immediately deleting it. File output = tmp.newFile(); String path = output.getAbsolutePath(); - assertTrue(output.delete()); + assertThat(output.delete()).isTrue(); BuildEventStreamProtos.BuildEvent started = BuildEventStreamProtos.BuildEvent.newBuilder() @@ -135,7 +133,7 @@ public class BinaryFormatFileTransportTest { // Close the file. transport.ch.close(); - assertFalse(transport.ch.isOpen()); + assertThat(transport.ch.isOpen()).isFalse(); // This should not throw an exception. transport.sendBuildEvent(buildEvent, artifactGroupNamer); @@ -166,7 +164,7 @@ public class BinaryFormatFileTransportTest { transport.sendBuildEvent(buildEvent, artifactGroupNamer); closeFuture.get(); - assertFalse(transport.ch.isOpen()); + assertThat(transport.ch.isOpen()).isFalse(); // There should have only been one write. try (InputStream in = new FileInputStream(output)) { diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/BuildFileModificationTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/BuildFileModificationTest.java index 1633111cea..60c056f42b 100644 --- a/src/test/java/com/google/devtools/build/lib/pkgcache/BuildFileModificationTest.java +++ b/src/test/java/com/google/devtools/build/lib/pkgcache/BuildFileModificationTest.java @@ -13,11 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.pkgcache; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static com.google.common.truth.Truth.assertThat; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableList; @@ -145,7 +141,7 @@ public class BuildFileModificationTest extends FoundationTestCase { Path build = scratch.file( "a/BUILD", "cc_library(name='a', feet='stinky')".getBytes(StandardCharsets.ISO_8859_1)); Package a1 = getPackage("a"); - assertTrue(a1.containsErrors()); + assertThat(a1.containsErrors()).isTrue(); assertContainsEvent("//a:a: no such attribute 'feet'"); eventCollector.clear(); // writeContent updates mtime and ctime. Note that we keep the content length exactly the same. @@ -155,8 +151,8 @@ public class BuildFileModificationTest extends FoundationTestCase { invalidatePackages(); Package a2 = getPackage("a"); - assertNotSame(a1, a2); - assertFalse(a2.containsErrors()); + assertThat(a2).isNotSameAs(a1); + assertThat(a2.containsErrors()).isFalse(); assertNoEvents(); } @@ -170,13 +166,14 @@ public class BuildFileModificationTest extends FoundationTestCase { clock.advanceMillis(1); FileSystemUtils.writeContent( path, "cc_library(name = 'bar')\n".getBytes(StandardCharsets.ISO_8859_1)); - assertSame(oldPkg, getPackage("pkg")); // Change only becomes visible after invalidatePackages. + assertThat(getPackage("pkg")) + .isSameAs(oldPkg); // Change only becomes visible after invalidatePackages. invalidatePackages(); Package newPkg = getPackage("pkg"); - assertNotSame(oldPkg, newPkg); - assertNotNull(newPkg.getTarget("bar")); + assertThat(newPkg).isNotSameAs(oldPkg); + assertThat(newPkg.getTarget("bar")).isNotNull(); } @Test @@ -193,7 +190,7 @@ public class BuildFileModificationTest extends FoundationTestCase { invalidatePackages(); Package a2 = getPackage("a"); - assertNotSame(a1, a2); + assertThat(a2).isNotSameAs(a1); assertNoEvents(); } @@ -206,11 +203,11 @@ public class BuildFileModificationTest extends FoundationTestCase { // Change ctime to 1. clock.advanceMillis(1); path.setLastModifiedTime(1001); - assertSame(oldPkg, getPackage("pkg")); // change not yet visible + assertThat(getPackage("pkg")).isSameAs(oldPkg); // change not yet visible invalidatePackages(); Package newPkg = getPackage("pkg"); - assertNotSame(oldPkg, newPkg); + assertThat(newPkg).isNotSameAs(oldPkg); } } diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/CompileOneDependencyTransformerTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/CompileOneDependencyTransformerTest.java index 7ac30babb6..d421e9229c 100644 --- a/src/test/java/com/google/devtools/build/lib/pkgcache/CompileOneDependencyTransformerTest.java +++ b/src/test/java/com/google/devtools/build/lib/pkgcache/CompileOneDependencyTransformerTest.java @@ -14,7 +14,6 @@ package com.google.devtools.build.lib.pkgcache; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; import com.google.devtools.build.lib.cmdline.Label; @@ -106,7 +105,7 @@ public class CompileOneDependencyTransformerTest extends PackageLoadingTestCase } private static Set getFailFast(ResolvedTargets result) { - assertFalse(result.hasError()); + assertThat(result.hasError()).isFalse(); return result.getTargets(); } diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/IOExceptionsTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/IOExceptionsTest.java index 6472f01279..3cbcf24a36 100644 --- a/src/test/java/com/google/devtools/build/lib/pkgcache/IOExceptionsTest.java +++ b/src/test/java/com/google/devtools/build/lib/pkgcache/IOExceptionsTest.java @@ -13,8 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.pkgcache; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static com.google.common.truth.Truth.assertThat; import com.google.common.base.Function; import com.google.devtools.build.lib.cmdline.Label; @@ -105,7 +104,7 @@ public class IOExceptionsTest extends PackageLoadingTestCase { return null; } }; - assertFalse(visitTransitively(Label.parseAbsolute("//pkg:x"))); + assertThat(visitTransitively(Label.parseAbsolute("//pkg:x"))).isFalse(); scratch.overwriteFile("pkg/BUILD", "# another comment to force reload", "sh_library(name = 'x')"); @@ -113,7 +112,7 @@ public class IOExceptionsTest extends PackageLoadingTestCase { syncPackages(); eventCollector.clear(); reporter.addHandler(failFastHandler); - assertTrue(visitTransitively(Label.parseAbsolute("//pkg:x"))); + assertThat(visitTransitively(Label.parseAbsolute("//pkg:x"))).isTrue(); assertNoEvents(); } @@ -134,7 +133,7 @@ public class IOExceptionsTest extends PackageLoadingTestCase { return null; } }; - assertFalse(visitTransitively(Label.parseAbsolute("//top:top"))); + assertThat(visitTransitively(Label.parseAbsolute("//top:top"))).isFalse(); assertContainsEvent("no such package 'pkg'"); // The traditional label visitor does not propagate the original IOException message. // assertContainsEvent("custom crash"); @@ -148,7 +147,7 @@ public class IOExceptionsTest extends PackageLoadingTestCase { syncPackages(); eventCollector.clear(); reporter.addHandler(failFastHandler); - assertTrue(visitTransitively(Label.parseAbsolute("//top:top"))); + assertThat(visitTransitively(Label.parseAbsolute("//top:top"))).isTrue(); assertNoEvents(); } @@ -167,6 +166,6 @@ public class IOExceptionsTest extends PackageLoadingTestCase { return null; } }; - assertFalse(visitTransitively(Label.parseAbsolute("//top/pkg:x"))); + assertThat(visitTransitively(Label.parseAbsolute("//top/pkg:x"))).isFalse(); } } diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/PackageCacheTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/PackageCacheTest.java index 3b7908e74d..aad829ecd1 100644 --- a/src/test/java/com/google/devtools/build/lib/pkgcache/PackageCacheTest.java +++ b/src/test/java/com/google/devtools/build/lib/pkgcache/PackageCacheTest.java @@ -14,11 +14,6 @@ package com.google.devtools.build.lib.pkgcache; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import com.google.common.base.Predicates; @@ -192,7 +187,7 @@ public class PackageCacheTest extends FoundationTestCase { getPackage(packageName); fail(); } catch (NoSuchPackageException e) { - assertThat(e.getMessage()).contains(expectedMessage); + assertThat(e).hasMessageThat().contains(expectedMessage); } } @@ -200,11 +195,10 @@ public class PackageCacheTest extends FoundationTestCase { public void testGetPackage() throws Exception { createPkg1(); Package pkg1 = getPackage("pkg1"); - assertEquals("pkg1", pkg1.getName()); - assertEquals("/workspace/pkg1/BUILD", - pkg1.getFilename().toString()); - assertSame(pkg1, getPackageManager().getPackage(reporter, - PackageIdentifier.createInMainRepo("pkg1"))); + assertThat(pkg1.getName()).isEqualTo("pkg1"); + assertThat(pkg1.getFilename().toString()).isEqualTo("/workspace/pkg1/BUILD"); + assertThat(getPackageManager().getPackage(reporter, PackageIdentifier.createInMainRepo("pkg1"))) + .isSameAs(pkg1); } @Test @@ -234,7 +228,7 @@ public class PackageCacheTest extends FoundationTestCase { createPkg1(); Label label = Label.parseAbsolute("//pkg1:foo"); Target target = getTarget(label); - assertEquals(label, target.getLabel()); + assertThat(target.getLabel()).isEqualTo(label); } @Test @@ -278,7 +272,7 @@ public class PackageCacheTest extends FoundationTestCase { // Found: Package missing = getPackage("missing"); - assertEquals("missing", missing.getName()); + assertThat(missing.getName()).isEqualTo("missing"); } /** @@ -305,7 +299,7 @@ public class PackageCacheTest extends FoundationTestCase { getPackage("broken"); fail(); } catch (BuildFileContainsErrorsException e) { - assertThat(e.getMessage()).contains("/workspace/broken/BUILD (Permission denied)"); + assertThat(e).hasMessageThat().contains("/workspace/broken/BUILD (Permission denied)"); } eventCollector.clear(); @@ -316,7 +310,7 @@ public class PackageCacheTest extends FoundationTestCase { invalidatePackages(); // resets cache of failures Package broken = getPackage("broken"); - assertEquals("broken", broken.getName()); + assertThat(broken.getName()).isEqualTo("broken"); assertNoEvents(); } @@ -329,17 +323,17 @@ public class PackageCacheTest extends FoundationTestCase { setOptions("--package_path=/workspace:/otherroot"); Package oldPkg = getPackage("pkg"); - assertSame(oldPkg, getPackage("pkg")); // change not yet visible - assertEquals(buildFile1, oldPkg.getFilename()); - assertEquals(rootDirectory, oldPkg.getSourceRoot()); + assertThat(getPackage("pkg")).isSameAs(oldPkg); // change not yet visible + assertThat(oldPkg.getFilename()).isEqualTo(buildFile1); + assertThat(oldPkg.getSourceRoot()).isEqualTo(rootDirectory); buildFile1.delete(); invalidatePackages(); Package newPkg = getPackage("pkg"); - assertNotSame(oldPkg, newPkg); - assertEquals(buildFile2, newPkg.getFilename()); - assertEquals(scratch.dir("/otherroot"), newPkg.getSourceRoot()); + assertThat(newPkg).isNotSameAs(oldPkg); + assertThat(newPkg.getFilename()).isEqualTo(buildFile2); + assertThat(newPkg.getSourceRoot()).isEqualTo(scratch.dir("/otherroot")); // TODO(bazel-team): (2009) test BUILD file moves in the other direction too. } @@ -412,7 +406,7 @@ public class PackageCacheTest extends FoundationTestCase { private void assertPackageLoadingFails(String pkgName, String expectedError) throws Exception { Package pkg = getPackage(pkgName); - assertTrue(pkg.containsErrors()); + assertThat(pkg.containsErrors()).isTrue(); assertContainsEvent(expectedError); } @@ -425,7 +419,7 @@ public class PackageCacheTest extends FoundationTestCase { reporter.removeHandler(failFastHandler); List events = getPackage("e").getEvents(); assertThat(events).hasSize(1); - assertEquals(2, events.get(0).getLocation().getStartLineAndColumn().getLine()); + assertThat(events.get(0).getLocation().getStartLineAndColumn().getLine()).isEqualTo(2); } /** Static tests (i.e. no changes to filesystem, nor calls to sync). */ @@ -501,8 +495,7 @@ public class PackageCacheTest extends FoundationTestCase { // root. It's as if we've merged c and c/d in the first root. // c/d is still a subpackage--found in the second root: - assertEquals(rootDir2.getRelative("c/d/BUILD"), - getPackage("c/d").getFilename()); + assertThat(getPackage("c/d").getFilename()).isEqualTo(rootDir2.getRelative("c/d/BUILD")); // Subpackage labels are still valid... assertLabelValidity(true, "//c/d:foo.txt"); @@ -512,14 +505,14 @@ public class PackageCacheTest extends FoundationTestCase { "Label '//c:d/x' crosses boundary of subpackage 'c/d' (have you deleted c/d/BUILD? " + "If so, use the --deleted_packages=c/d option)"); - assertTrue(getPackageManager().isPackage( - reporter, PackageIdentifier.createInMainRepo("c/d"))); + assertThat(getPackageManager().isPackage(reporter, PackageIdentifier.createInMainRepo("c/d"))) + .isTrue(); setOptions("--deleted_packages=c/d"); invalidatePackages(); - assertFalse(getPackageManager().isPackage( - reporter, PackageIdentifier.createInMainRepo("c/d"))); + assertThat(getPackageManager().isPackage(reporter, PackageIdentifier.createInMainRepo("c/d"))) + .isFalse(); // c/d is no longer a subpackage--even though there's a BUILD file in the // second root: @@ -557,6 +550,6 @@ public class PackageCacheTest extends FoundationTestCase { "outs = ['y/z.h'],", "cmd = '')"); Package p = getPackage("x"); - assertTrue(p.containsErrors()); + assertThat(p.containsErrors()).isTrue(); } } diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/PathPackageLocatorTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/PathPackageLocatorTest.java index 5cf6ada07b..f7d1aa44db 100644 --- a/src/test/java/com/google/devtools/build/lib/pkgcache/PathPackageLocatorTest.java +++ b/src/test/java/com/google/devtools/build/lib/pkgcache/PathPackageLocatorTest.java @@ -14,9 +14,6 @@ package com.google.devtools.build.lib.pkgcache; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; import com.google.common.collect.ImmutableList; @@ -182,54 +179,58 @@ public class PathPackageLocatorTest extends FoundationTestCase { @Test public void testGetPackageBuildFile() throws Exception { AtomicReference cache = UnixGlob.DEFAULT_SYSCALLS_REF; - assertEquals(buildFile_1A, locator.getPackageBuildFile( - PackageIdentifier.createInMainRepo("A"))); - assertEquals(buildFile_1A, locator.getPackageBuildFileNullable( - PackageIdentifier.createInMainRepo("A"), cache)); - assertEquals(buildFile_1B, locator.getPackageBuildFile( - PackageIdentifier.createInMainRepo("B"))); - assertEquals(buildFile_1B, locator.getPackageBuildFileNullable( - PackageIdentifier.createInMainRepo("B"), cache)); - assertEquals(buildFile_2C, locator.getPackageBuildFile( - PackageIdentifier.createInMainRepo("C"))); - assertEquals(buildFile_2C, locator.getPackageBuildFileNullable( - PackageIdentifier.createInMainRepo("C"), cache)); - assertEquals(buildFile_2CD, locator.getPackageBuildFile( - PackageIdentifier.createInMainRepo("C/D"))); - assertEquals(buildFile_2CD, locator.getPackageBuildFileNullable( - PackageIdentifier.createInMainRepo("C/D"), cache)); + assertThat(locator.getPackageBuildFile(PackageIdentifier.createInMainRepo("A"))) + .isEqualTo(buildFile_1A); + assertThat(locator.getPackageBuildFileNullable(PackageIdentifier.createInMainRepo("A"), cache)) + .isEqualTo(buildFile_1A); + assertThat(locator.getPackageBuildFile(PackageIdentifier.createInMainRepo("B"))) + .isEqualTo(buildFile_1B); + assertThat(locator.getPackageBuildFileNullable(PackageIdentifier.createInMainRepo("B"), cache)) + .isEqualTo(buildFile_1B); + assertThat(locator.getPackageBuildFile(PackageIdentifier.createInMainRepo("C"))) + .isEqualTo(buildFile_2C); + assertThat(locator.getPackageBuildFileNullable(PackageIdentifier.createInMainRepo("C"), cache)) + .isEqualTo(buildFile_2C); + assertThat(locator.getPackageBuildFile(PackageIdentifier.createInMainRepo("C/D"))) + .isEqualTo(buildFile_2CD); + assertThat( + locator.getPackageBuildFileNullable(PackageIdentifier.createInMainRepo("C/D"), cache)) + .isEqualTo(buildFile_2CD); checkFails("C/E", "no such package 'C/E': BUILD file not found on package path"); - assertNull(locator.getPackageBuildFileNullable( - PackageIdentifier.createInMainRepo("C/E"), cache)); - assertEquals(buildFile_2F, - locator.getPackageBuildFile(PackageIdentifier.createInMainRepo("F"))); + assertThat( + locator.getPackageBuildFileNullable(PackageIdentifier.createInMainRepo("C/E"), cache)) + .isNull(); + assertThat(locator.getPackageBuildFile(PackageIdentifier.createInMainRepo("F"))) + .isEqualTo(buildFile_2F); checkFails("F/G", "no such package 'F/G': BUILD file not found on package path"); - assertNull(locator.getPackageBuildFileNullable( - PackageIdentifier.createInMainRepo("F/G"), cache)); - assertEquals(buildFile_2FGH, locator.getPackageBuildFile( - PackageIdentifier.createInMainRepo("F/G/H"))); - assertEquals(buildFile_2FGH, locator.getPackageBuildFileNullable( - PackageIdentifier.createInMainRepo("F/G/H"), cache)); + assertThat( + locator.getPackageBuildFileNullable(PackageIdentifier.createInMainRepo("F/G"), cache)) + .isNull(); + assertThat(locator.getPackageBuildFile(PackageIdentifier.createInMainRepo("F/G/H"))) + .isEqualTo(buildFile_2FGH); + assertThat( + locator.getPackageBuildFileNullable(PackageIdentifier.createInMainRepo("F/G/H"), cache)) + .isEqualTo(buildFile_2FGH); checkFails("I", "no such package 'I': BUILD file not found on package path"); } @Test public void testGetPackageBuildFileWithSymlinks() throws Exception { - assertEquals(buildFile_3A, locatorWithSymlinks.getPackageBuildFile( - PackageIdentifier.createInMainRepo("A"))); - assertEquals(buildFile_3B, locatorWithSymlinks.getPackageBuildFile( - PackageIdentifier.createInMainRepo("B"))); - assertEquals(buildFile_3CI, locatorWithSymlinks.getPackageBuildFile( - PackageIdentifier.createInMainRepo("C/I"))); + assertThat(locatorWithSymlinks.getPackageBuildFile(PackageIdentifier.createInMainRepo("A"))) + .isEqualTo(buildFile_3A); + assertThat(locatorWithSymlinks.getPackageBuildFile(PackageIdentifier.createInMainRepo("B"))) + .isEqualTo(buildFile_3B); + assertThat(locatorWithSymlinks.getPackageBuildFile(PackageIdentifier.createInMainRepo("C/I"))) + .isEqualTo(buildFile_3CI); checkFails( locatorWithSymlinks, "C/D", "no such package 'C/D': BUILD file not found on package path"); } @Test public void testGetWorkspaceFile() throws Exception { - assertEquals(rootDir1WorkspaceFile, locator.getWorkspaceFile()); + assertThat(locator.getWorkspaceFile()).isEqualTo(rootDir1WorkspaceFile); } private Path setLocator(String root) { @@ -286,11 +287,11 @@ public class PathPackageLocatorTest extends FoundationTestCase { // No warning if workspace == cwd. PathPackageLocator.create(null, ImmutableList.of("./foo"), reporter, workspace, workspace); - assertSame(0, eventCollector.count()); + assertThat(eventCollector.count()).isSameAs(0); PathPackageLocator.create( null, ImmutableList.of("./foo"), reporter, workspace, workspace.getRelative("foo")); - assertSame(1, eventCollector.count()); + assertThat(eventCollector.count()).isSameAs(1); assertContainsEvent("The package path element './foo' will be taken relative"); } diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorTest.java index da003add73..7f7376c5f3 100644 --- a/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorTest.java +++ b/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorTest.java @@ -15,10 +15,6 @@ package com.google.devtools.build.lib.pkgcache; import static com.google.common.truth.Truth.assertThat; import static com.google.devtools.build.lib.pkgcache.FilteringPolicies.FILTER_TESTS; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import com.google.common.collect.ImmutableList; @@ -36,15 +32,13 @@ import com.google.devtools.build.lib.util.Pair; import com.google.devtools.build.lib.vfs.ModifiedFileSet; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; - +import java.util.Arrays; +import java.util.Set; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.util.Arrays; -import java.util.Set; - /** Tests for {@link TargetPatternEvaluator}. */ @RunWith(JUnit4.class) public class TargetPatternEvaluatorTest extends AbstractTargetPatternEvaluatorTest { @@ -170,7 +164,7 @@ public class TargetPatternEvaluatorTest extends AbstractTargetPatternEvaluatorTe throws TargetParsingException, InterruptedException { ResolvedTargets result = parseTargetPatternList(parser, parsingListener, Arrays.asList(patterns), true); - assertTrue(result.hasError()); + assertThat(result.hasError()).isTrue(); return targetsToLabels(result.getTargets()); } @@ -188,7 +182,7 @@ public class TargetPatternEvaluatorTest extends AbstractTargetPatternEvaluatorTe } private static Set getFailFast(ResolvedTargets result) { - assertFalse(result.hasError()); + assertThat(result.hasError()).isFalse(); return result.getTargets(); } @@ -198,7 +192,7 @@ public class TargetPatternEvaluatorTest extends AbstractTargetPatternEvaluatorTe parser.parseTargetPattern(parsingListener, target, false); fail("target='" + target + "', expected error: " + expectedError); } catch (TargetParsingException e) { - assertThat(e.getMessage()).contains(expectedError); + assertThat(e).hasMessageThat().contains(expectedError); } } @@ -224,14 +218,13 @@ public class TargetPatternEvaluatorTest extends AbstractTargetPatternEvaluatorTe @Test public void testParsingStandardLabel() throws Exception { - assertEquals("//foo:foo1", - parseIndividualTarget("//foo:foo1").toString()); + assertThat(parseIndividualTarget("//foo:foo1").toString()).isEqualTo("//foo:foo1"); } @Test public void testAbsolutePatternEndsWithSlashAll() throws Exception { scratch.file("foo/all/BUILD", "cc_library(name = 'all')"); - assertEquals("//foo/all:all", parseIndividualTarget("//foo/all").toString()); + assertThat(parseIndividualTarget("//foo/all").toString()).isEqualTo("//foo/all:all"); assertNoEvents(); } @@ -249,8 +242,8 @@ public class TargetPatternEvaluatorTest extends AbstractTargetPatternEvaluatorTe } private void assertWildcardConflict(String label, String suffix) throws Exception { - assertEquals(label, parseIndividualTarget(label).toString()); - assertSame(1, eventCollector.count()); + assertThat(parseIndividualTarget(label).toString()).isEqualTo(label); + assertThat(eventCollector.count()).isSameAs(1); assertContainsEvent(String.format("The target pattern '%s' is ambiguous: '%s' is both " + "a wildcard, and the name of an existing cc_library rule; " + "using the latter interpretation", label, suffix)); @@ -262,13 +255,13 @@ public class TargetPatternEvaluatorTest extends AbstractTargetPatternEvaluatorTe parseIndividualTarget("//missing:foo1"); fail("TargetParsingException expected"); } catch (TargetParsingException e) { - assertThat(e.getMessage()).startsWith("no such package"); + assertThat(e).hasMessageThat().startsWith("no such package"); } } @Test public void testParsingStandardLabelWithRelativeParser() throws Exception { - assertEquals("//foo:foo1", parseIndividualTargetRelative("//foo:foo1").toString()); + assertThat(parseIndividualTargetRelative("//foo:foo1").toString()).isEqualTo("//foo:foo1"); } @Test @@ -277,19 +270,18 @@ public class TargetPatternEvaluatorTest extends AbstractTargetPatternEvaluatorTe parseIndividualTarget("//foo:missing"); fail("TargetParsingException expected"); } catch (TargetParsingException e) { - assertThat(e.getMessage()).startsWith("no such target"); + assertThat(e).hasMessageThat().startsWith("no such target"); } } @Test public void testParsingStandardLabelShorthand() throws Exception { - assertEquals("//foo:foo1", - parseIndividualTarget("foo:foo1").toString()); + assertThat(parseIndividualTarget("foo:foo1").toString()).isEqualTo("//foo:foo1"); } @Test public void testParsingStandardLabelShorthandRelative() throws Exception { - assertEquals("//foo:foo1", parseIndividualTargetRelative(":foo1").toString()); + assertThat(parseIndividualTargetRelative(":foo1").toString()).isEqualTo("//foo:foo1"); } @Test @@ -477,8 +469,10 @@ public class TargetPatternEvaluatorTest extends AbstractTargetPatternEvaluatorTe public void testDefaultPackage() throws Exception { scratch.file("experimental/BUILD", "cc_library(name = 'experimental', srcs = [ 'experimental.cc' ])"); - assertEquals("//experimental:experimental", parseIndividualTarget("//experimental").toString()); - assertEquals("//experimental:experimental", parseIndividualTarget("experimental").toString()); + assertThat(parseIndividualTarget("//experimental").toString()) + .isEqualTo("//experimental:experimental"); + assertThat(parseIndividualTarget("experimental").toString()) + .isEqualTo("//experimental:experimental"); assertNoEvents(); } @@ -491,28 +485,26 @@ public class TargetPatternEvaluatorTest extends AbstractTargetPatternEvaluatorTe scratch.file("sub/dir/BUILD", "exports_files(['dir2'])"); scratch.file("sub/dir/dir/BUILD", "exports_files(['dir'])"); // sub/dir/dir is a package - assertEquals("//sub/dir/dir:dir", parseIndividualTarget("sub/dir/dir").toString()); + assertThat(parseIndividualTarget("sub/dir/dir").toString()).isEqualTo("//sub/dir/dir:dir"); // sub/dir is a package but not sub/dir/dir2 - assertEquals("//sub/dir:dir2", parseIndividualTarget("sub/dir/dir2").toString()); + assertThat(parseIndividualTarget("sub/dir/dir2").toString()).isEqualTo("//sub/dir:dir2"); // sub is a package but not sub/dir2 - assertEquals("//sub:dir2/dir2", parseIndividualTarget("sub/dir2/dir2").toString()); + assertThat(parseIndividualTarget("sub/dir2/dir2").toString()).isEqualTo("//sub:dir2/dir2"); } @Test public void testFindsLongestPlausiblePackageName() throws Exception { - assertEquals("//foo/bar:baz", - parseIndividualTarget("foo/bar/baz").toString()); - assertEquals("//foo/bar:baz/bang", - parseIndividualTarget("foo/bar/baz/bang").toString()); - assertEquals("//foo:baz/bang", - parseIndividualTarget("foo/baz/bang").toString()); + assertThat(parseIndividualTarget("foo/bar/baz").toString()).isEqualTo("//foo/bar:baz"); + assertThat(parseIndividualTarget("foo/bar/baz/bang").toString()) + .isEqualTo("//foo/bar:baz/bang"); + assertThat(parseIndividualTarget("foo/baz/bang").toString()).isEqualTo("//foo:baz/bang"); } @Test public void testParsesIterableOfLabels() throws Exception { Set