001/* 002 * SonarQube 003 * Copyright (C) 2009-2017 SonarSource SA 004 * mailto:info AT sonarsource DOT com 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.batch.fs.internal; 021 022import java.io.StringReader; 023import java.nio.charset.Charset; 024import java.nio.file.Path; 025import java.nio.file.Paths; 026 027import javax.annotation.Nullable; 028import org.sonar.api.batch.fs.InputFile; 029import org.sonar.api.utils.PathUtils; 030 031/** 032 * Intended to be used in unit tests that need to create {@link InputFile}s. 033 * 034 * @since 6.3 035 */ 036public class TestInputFileBuilder { 037 private static int batchId = 1; 038 039 private final int id; 040 private final String relativePath; 041 private final String moduleKey; 042 private Path moduleBaseDir; 043 private String language; 044 private InputFile.Type type = InputFile.Type.MAIN; 045 private InputFile.Status status; 046 private int lines = -1; 047 private Charset charset; 048 private int lastValidOffset = -1; 049 private String hash; 050 private int nonBlankLines; 051 private int[] originalLineOffsets; 052 private boolean publish = true; 053 054 public TestInputFileBuilder(String moduleKey, String relativePath) { 055 this(moduleKey, relativePath, batchId++); 056 } 057 058 public TestInputFileBuilder(String moduleKey, String relativePath, int id) { 059 this.moduleKey = moduleKey; 060 this.moduleBaseDir = Paths.get(moduleKey); 061 this.relativePath = PathUtils.sanitize(relativePath); 062 this.id = id; 063 } 064 065 public static int nextBatchId() { 066 return batchId++; 067 } 068 069 public TestInputFileBuilder setModuleBaseDir(Path moduleBaseDir) { 070 this.moduleBaseDir = moduleBaseDir.normalize(); 071 return this; 072 } 073 074 public TestInputFileBuilder setLanguage(@Nullable String language) { 075 this.language = language; 076 return this; 077 } 078 079 public TestInputFileBuilder setType(InputFile.Type type) { 080 this.type = type; 081 return this; 082 } 083 084 public TestInputFileBuilder setStatus(InputFile.Status status) { 085 this.status = status; 086 return this; 087 } 088 089 public TestInputFileBuilder setLines(int lines) { 090 this.lines = lines; 091 return this; 092 } 093 094 public TestInputFileBuilder setCharset(Charset charset) { 095 this.charset = charset; 096 return this; 097 } 098 099 public TestInputFileBuilder setLastValidOffset(int lastValidOffset) { 100 this.lastValidOffset = lastValidOffset; 101 return this; 102 } 103 104 public TestInputFileBuilder setHash(String hash) { 105 this.hash = hash; 106 return this; 107 } 108 109 public TestInputFileBuilder setNonBlankLines(int nonBlankLines) { 110 this.nonBlankLines = nonBlankLines; 111 return this; 112 } 113 114 public TestInputFileBuilder setOriginalLineOffsets(int[] originalLineOffsets) { 115 this.originalLineOffsets = originalLineOffsets; 116 return this; 117 } 118 119 public TestInputFileBuilder setPublish(boolean publish) { 120 this.publish = publish; 121 return this; 122 } 123 124 public TestInputFileBuilder setMetadata(Metadata metadata) { 125 this.setLines(metadata.lines()); 126 this.setLastValidOffset(metadata.lastValidOffset()); 127 this.setNonBlankLines(metadata.nonBlankLines()); 128 this.setHash(metadata.hash()); 129 this.setOriginalLineOffsets(metadata.originalLineOffsets()); 130 return this; 131 } 132 133 public TestInputFileBuilder initMetadata(String content) { 134 return setMetadata(new FileMetadata().readMetadata(new StringReader(content))); 135 } 136 137 public DefaultInputFile build() { 138 DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type, id); 139 indexedFile.setLanguage(language); 140 DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(new Metadata(lines, nonBlankLines, hash, originalLineOffsets, lastValidOffset))); 141 inputFile.setStatus(status); 142 inputFile.setCharset(charset); 143 inputFile.setPublish(publish); 144 return inputFile; 145 } 146}