Line data Source code
1 : // Copyright (C) 2020 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance.testsuite.change; 16 : 17 : import static com.google.common.base.Preconditions.checkNotNull; 18 : 19 : import com.google.common.base.Strings; 20 : import com.google.gerrit.common.RawInputUtil; 21 : import com.google.gerrit.server.edit.tree.ChangeFileContentModification; 22 : import com.google.gerrit.server.edit.tree.DeleteFileModification; 23 : import com.google.gerrit.server.edit.tree.RenameFileModification; 24 : import com.google.gerrit.server.edit.tree.TreeModification; 25 : import java.util.function.Consumer; 26 : 27 : /** Builder to simplify file content specification. */ 28 : public class FileContentBuilder<T> { 29 : private final T builder; 30 : private final String filePath; 31 : private final int newGitFileMode; 32 : private final Consumer<TreeModification> modificationToBuilderAdder; 33 : 34 : FileContentBuilder( 35 : T builder, 36 : String filePath, 37 : int newGitFileMode, 38 7 : Consumer<TreeModification> modificationToBuilderAdder) { 39 7 : checkNotNull(Strings.emptyToNull(filePath), "File path must not be null or empty."); 40 7 : this.builder = builder; 41 7 : this.filePath = filePath; 42 7 : this.newGitFileMode = newGitFileMode; 43 7 : this.modificationToBuilderAdder = modificationToBuilderAdder; 44 7 : } 45 : 46 : /** Content of the file. Must not be empty. */ 47 : public T content(String content) { 48 7 : checkNotNull( 49 7 : Strings.emptyToNull(content), 50 : "Empty file content is not supported. Adjust test API if necessary."); 51 7 : modificationToBuilderAdder.accept( 52 7 : new ChangeFileContentModification(filePath, RawInputUtil.create(content), newGitFileMode)); 53 7 : return builder; 54 : } 55 : 56 : /** Deletes the file. */ 57 : public T delete() { 58 5 : modificationToBuilderAdder.accept(new DeleteFileModification(filePath)); 59 5 : return builder; 60 : } 61 : 62 : /** 63 : * Renames the file while keeping its content. 64 : * 65 : * <p>If you want to both rename the file and adjust its content, delete the old path via {@link 66 : * #delete()} and provide the desired content for the new path via {@link #content(String)}. If 67 : * you use that approach, make sure to use a new content which is similar enough to the old (at 68 : * least 60% line similarity) as otherwise Gerrit/Git won't identify it as a rename. 69 : * 70 : * <p>To create copied files, you need to go even one step further. Also rename the file you copy 71 : * at the same time (-> delete old path + add two paths with the old content)! If you also want to 72 : * adjust the content of the copy, you need to also slightly modify the content of the renamed 73 : * file. Adjust the content of the copy slightly more if you want to control which file ends up as 74 : * copy and which as rename (but keep the 60% line similarity threshold in mind). 75 : * 76 : * @param newFilePath new path of the file 77 : */ 78 : public T renameTo(String newFilePath) { 79 4 : modificationToBuilderAdder.accept(new RenameFileModification(filePath, newFilePath)); 80 4 : return builder; 81 : } 82 : }