Skip to content

Commit f8b86e5

Browse files
committed
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
1 parent f62173a commit f8b86e5

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

okhttp/src/main/kotlin/okhttp3/Cache.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ import java.util.TreeSet
144144
* [rfc_7234]: http://tools.ietf.org/html/rfc7234
145145
*/
146146
@OptIn(ExperimentalFileSystem::class)
147-
class Cache
148-
internal constructor(
147+
class Cache(
149148
directory: Path,
150149
maxSize: Long,
151150
fileSystem: FileSystem

okhttp/src/test/java/okhttp3/CacheTest.java

+51-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package okhttp3;
1818

19-
import java.io.File;
2019
import java.io.IOException;
2120
import java.net.CookieManager;
2221
import java.net.HttpURLConnection;
@@ -25,6 +24,7 @@
2524
import java.security.cert.Certificate;
2625
import java.text.DateFormat;
2726
import java.text.SimpleDateFormat;
27+
import java.util.ArrayList;
2828
import java.util.Date;
2929
import java.util.Iterator;
3030
import java.util.List;
@@ -44,6 +44,8 @@
4444
import okio.Buffer;
4545
import okio.BufferedSink;
4646
import okio.BufferedSource;
47+
import okio.FileSystem;
48+
import okio.ForwardingFileSystem;
4749
import okio.GzipSink;
4850
import okio.Okio;
4951
import okio.Path;
@@ -84,6 +86,7 @@ public final class CacheTest {
8486
platform.assumeNotBouncyCastle();
8587

8688
server.setProtocolNegotiationEnabled(false);
89+
fileSystem.emulateUnix();
8790
cache = new Cache(Path.get("/cache/"), Integer.MAX_VALUE, fileSystem);
8891
client = clientTestRule.newClientBuilder()
8992
.cache(cache)
@@ -2610,6 +2613,53 @@ private RecordedRequest assertConditionallyCached(MockResponse response) throws
26102613
lastModifiedDate);
26112614
}
26122615

2616+
@Test
2617+
public void testPublicPathConstructor() throws IOException {
2618+
List<String> events = new ArrayList<>();
2619+
2620+
fileSystem.createDirectories(cache.directoryPath());
2621+
2622+
fileSystem.createDirectories(cache.directoryPath());
2623+
2624+
FileSystem loggingFileSystem = new ForwardingFileSystem(fileSystem) {
2625+
@Override
2626+
public Path onPathParameter(Path path, java.lang.String functionName, java.lang.String parameterName) {
2627+
events.add(functionName + ":" + path);
2628+
return path;
2629+
}
2630+
2631+
@Override
2632+
public Path onPathResult(Path path, java.lang.String functionName) {
2633+
events.add(functionName + ":" + path);
2634+
return path;
2635+
}
2636+
};
2637+
Path path = Path.get("/cache");
2638+
Cache c = new Cache(path, 100000L, loggingFileSystem);
2639+
2640+
assertThat(c.directoryPath()).isEqualTo(path);
2641+
2642+
c.size();
2643+
2644+
assertThat(events).containsExactly("metadataOrNull:/cache/journal.bkp",
2645+
"metadataOrNull:/cache",
2646+
"sink:/cache/journal.bkp",
2647+
"delete:/cache/journal.bkp",
2648+
"metadataOrNull:/cache/journal",
2649+
"metadataOrNull:/cache",
2650+
"sink:/cache/journal.tmp",
2651+
"metadataOrNull:/cache/journal",
2652+
"atomicMove:/cache/journal.tmp",
2653+
"atomicMove:/cache/journal",
2654+
"appendingSink:/cache/journal");
2655+
2656+
events.clear();
2657+
2658+
c.size();
2659+
2660+
assertThat(events).isEmpty();
2661+
}
2662+
26132663
private void assertFullyCached(MockResponse response) throws Exception {
26142664
server.enqueue(response.setBody("A"));
26152665
server.enqueue(response.setBody("B"));

0 commit comments

Comments
 (0)