|
| 1 | +/* |
| 2 | + * Copyright 2016-2026 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.ObjectInputStream; |
| 21 | +import java.io.ObjectOutputStream; |
| 22 | +import java.io.Serializable; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Objects; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.function.Supplier; |
| 29 | + |
| 30 | +import javax.annotation.Nullable; |
| 31 | + |
| 32 | +import com.diffplug.common.base.Errors; |
| 33 | +import com.diffplug.common.base.StandardSystemProperty; |
| 34 | +import com.diffplug.common.base.Suppliers; |
| 35 | +import com.diffplug.common.collect.ImmutableList; |
| 36 | +import com.diffplug.common.io.Files; |
| 37 | +import com.diffplug.spotless.extra.P2Provisioner; |
| 38 | + |
| 39 | +public class TestP2Provisioner { |
| 40 | + /** Creates a P2Provisioner which will cache the result of previous calls. */ |
| 41 | + @SuppressWarnings("unchecked") |
| 42 | + private static P2Provisioner caching(String name, Supplier<P2Provisioner> input) { |
| 43 | + File spotlessDir = new File(StandardSystemProperty.USER_DIR.value()).getParentFile(); |
| 44 | + File testlib = new File(spotlessDir, "testlib"); |
| 45 | + File cacheFile = new File(testlib, "build/tmp/testp2provisioner." + name + ".cache"); |
| 46 | + |
| 47 | + Map<CacheKey, ImmutableList<File>> cached; |
| 48 | + if (cacheFile.exists()) { |
| 49 | + try (ObjectInputStream inputStream = new ObjectInputStream(Files.asByteSource(cacheFile).openBufferedStream())) { |
| 50 | + cached = (Map<CacheKey, ImmutableList<File>>) inputStream.readObject(); |
| 51 | + } catch (IOException | ClassNotFoundException e) { |
| 52 | + throw Errors.asRuntime(e); |
| 53 | + } |
| 54 | + } else { |
| 55 | + cached = new HashMap<>(); |
| 56 | + try { |
| 57 | + Files.createParentDirs(cacheFile); |
| 58 | + } catch (IOException e) { |
| 59 | + throw Errors.asRuntime(e); |
| 60 | + } |
| 61 | + } |
| 62 | + return (modelWrapper, mavenProvisioner, cacheDirectory) -> { |
| 63 | + CacheKey key = new CacheKey( |
| 64 | + List.copyOf(modelWrapper.getP2Repos()), |
| 65 | + List.copyOf(modelWrapper.getInstallList()), |
| 66 | + Set.copyOf(modelWrapper.getFilterNames()), |
| 67 | + List.copyOf(modelWrapper.getPureMaven()), |
| 68 | + modelWrapper.isUseMavenCentral(), |
| 69 | + cacheDirectory); |
| 70 | + |
| 71 | + synchronized (TestP2Provisioner.class) { |
| 72 | + ImmutableList<File> result = cached.get(key); |
| 73 | + // double-check that depcache pruning hasn't removed them since our cache cached them |
| 74 | + boolean needsToBeSet = result == null || !result.stream().allMatch(file -> file.exists() && file.isFile() && file.length() > 0); |
| 75 | + if (needsToBeSet) { |
| 76 | + result = ImmutableList.copyOf(input.get().provisionP2Dependencies(modelWrapper, mavenProvisioner, cacheDirectory)); |
| 77 | + cached.put(key, result); |
| 78 | + try (ObjectOutputStream outputStream = new ObjectOutputStream(Files.asByteSink(cacheFile).openBufferedStream())) { |
| 79 | + outputStream.writeObject(cached); |
| 80 | + } catch (IOException e) { |
| 81 | + throw Errors.asRuntime(e); |
| 82 | + } |
| 83 | + } |
| 84 | + return result; |
| 85 | + } |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + /** Creates a default P2Provisioner with caching for tests. */ |
| 90 | + public static P2Provisioner defaultProvisioner() { |
| 91 | + return DEFAULT_PROVISIONER.get(); |
| 92 | + } |
| 93 | + |
| 94 | + private static final Supplier<P2Provisioner> DEFAULT_PROVISIONER = Suppliers.memoize(() -> caching("default", P2Provisioner::createDefault)); |
| 95 | + |
| 96 | + /** |
| 97 | + * Cache key capturing all P2Model state that affects query results. |
| 98 | + * Must be Serializable for disk caching. |
| 99 | + */ |
| 100 | + private static class CacheKey implements Serializable { |
| 101 | + private static final long serialVersionUID = 1L; |
| 102 | + private final List<String> p2Repos; |
| 103 | + private final List<String> installList; |
| 104 | + private final Set<String> filterNames; |
| 105 | + private final List<String> pureMaven; |
| 106 | + private final boolean useMavenCentral; |
| 107 | + @Nullable private final File cacheDirectory; |
| 108 | + |
| 109 | + CacheKey(List<String> p2Repos, List<String> installList, Set<String> filterNames, |
| 110 | + List<String> pureMaven, boolean useMavenCentral, @Nullable File cacheDirectory) { |
| 111 | + this.p2Repos = p2Repos; |
| 112 | + this.installList = installList; |
| 113 | + this.filterNames = filterNames; |
| 114 | + this.pureMaven = pureMaven; |
| 115 | + this.useMavenCentral = useMavenCentral; |
| 116 | + this.cacheDirectory = cacheDirectory; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean equals(Object o) { |
| 121 | + if (this == o) |
| 122 | + return true; |
| 123 | + if (o == null || getClass() != o.getClass()) |
| 124 | + return false; |
| 125 | + CacheKey cacheKey = (CacheKey) o; |
| 126 | + return useMavenCentral == cacheKey.useMavenCentral && |
| 127 | + Objects.equals(p2Repos, cacheKey.p2Repos) && |
| 128 | + Objects.equals(installList, cacheKey.installList) && |
| 129 | + Objects.equals(filterNames, cacheKey.filterNames) && |
| 130 | + Objects.equals(pureMaven, cacheKey.pureMaven) && |
| 131 | + Objects.equals(cacheDirectory, cacheKey.cacheDirectory); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public int hashCode() { |
| 136 | + return Objects.hash(p2Repos, installList, filterNames, pureMaven, useMavenCentral, cacheDirectory); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments