From f8b86e510e3cb0b7cf36ac7a929c29ca558e4bf1 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sat, 6 Feb 2021 16:20:36 +0000 Subject: [PATCH] Public API and test Public API and test Public API and test Review comments Public API and test Public API and test Public API and test Review comments --- okhttp/src/main/kotlin/okhttp3/Cache.kt | 3 +- okhttp/src/test/java/okhttp3/CacheTest.java | 52 ++++++++++++++++++++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/okhttp/src/main/kotlin/okhttp3/Cache.kt b/okhttp/src/main/kotlin/okhttp3/Cache.kt index fc56504ce940..5f2541419e4f 100644 --- a/okhttp/src/main/kotlin/okhttp3/Cache.kt +++ b/okhttp/src/main/kotlin/okhttp3/Cache.kt @@ -144,8 +144,7 @@ import java.util.TreeSet * [rfc_7234]: http://tools.ietf.org/html/rfc7234 */ @OptIn(ExperimentalFileSystem::class) -class Cache -internal constructor( +class Cache( directory: Path, maxSize: Long, fileSystem: FileSystem diff --git a/okhttp/src/test/java/okhttp3/CacheTest.java b/okhttp/src/test/java/okhttp3/CacheTest.java index 614b2adf3027..62084ae0c188 100644 --- a/okhttp/src/test/java/okhttp3/CacheTest.java +++ b/okhttp/src/test/java/okhttp3/CacheTest.java @@ -16,7 +16,6 @@ package okhttp3; -import java.io.File; import java.io.IOException; import java.net.CookieManager; import java.net.HttpURLConnection; @@ -25,6 +24,7 @@ import java.security.cert.Certificate; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; @@ -44,6 +44,8 @@ import okio.Buffer; import okio.BufferedSink; import okio.BufferedSource; +import okio.FileSystem; +import okio.ForwardingFileSystem; import okio.GzipSink; import okio.Okio; import okio.Path; @@ -84,6 +86,7 @@ public final class CacheTest { platform.assumeNotBouncyCastle(); server.setProtocolNegotiationEnabled(false); + fileSystem.emulateUnix(); cache = new Cache(Path.get("/cache/"), Integer.MAX_VALUE, fileSystem); client = clientTestRule.newClientBuilder() .cache(cache) @@ -2610,6 +2613,53 @@ private RecordedRequest assertConditionallyCached(MockResponse response) throws lastModifiedDate); } + @Test + public void testPublicPathConstructor() throws IOException { + List events = new ArrayList<>(); + + fileSystem.createDirectories(cache.directoryPath()); + + fileSystem.createDirectories(cache.directoryPath()); + + FileSystem loggingFileSystem = new ForwardingFileSystem(fileSystem) { + @Override + public Path onPathParameter(Path path, java.lang.String functionName, java.lang.String parameterName) { + events.add(functionName + ":" + path); + return path; + } + + @Override + public Path onPathResult(Path path, java.lang.String functionName) { + events.add(functionName + ":" + path); + return path; + } + }; + Path path = Path.get("/cache"); + Cache c = new Cache(path, 100000L, loggingFileSystem); + + assertThat(c.directoryPath()).isEqualTo(path); + + c.size(); + + assertThat(events).containsExactly("metadataOrNull:/cache/journal.bkp", + "metadataOrNull:/cache", + "sink:/cache/journal.bkp", + "delete:/cache/journal.bkp", + "metadataOrNull:/cache/journal", + "metadataOrNull:/cache", + "sink:/cache/journal.tmp", + "metadataOrNull:/cache/journal", + "atomicMove:/cache/journal.tmp", + "atomicMove:/cache/journal", + "appendingSink:/cache/journal"); + + events.clear(); + + c.size(); + + assertThat(events).isEmpty(); + } + private void assertFullyCached(MockResponse response) throws Exception { server.enqueue(response.setBody("A")); server.enqueue(response.setBody("B"));