Skip to content

Commit

Permalink
Refactor resources API
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses committed Jan 27, 2017
1 parent 211a993 commit eea589c
Show file tree
Hide file tree
Showing 113 changed files with 1,515 additions and 2,146 deletions.
Expand Up @@ -52,17 +52,16 @@ private ComponentKeys() {
/**
* @return the full key of a component, based on its parent projects' key and own key
*/
public static String createEffectiveKey(Project project, Resource resource) {
String key = resource.getKey();
public static String createEffectiveKey(String moduleKey, Resource resource) {
if (!StringUtils.equals(Scopes.PROJECT, resource.getScope())) {
// not a project nor a library
key = new StringBuilder(MAX_COMPONENT_KEY_LENGTH)
.append(project.getKey())
return new StringBuilder(MAX_COMPONENT_KEY_LENGTH)
.append(moduleKey)
.append(':')
.append(resource.getKey())
.toString();
}
return key;
return resource.getKey();
}

public static String createEffectiveKey(String moduleKey, InputPath inputPath) {
Expand Down
Expand Up @@ -19,28 +19,29 @@
*/
package org.sonar.core.component;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.resources.Directory;
import org.sonar.api.resources.Project;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ComponentKeysTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void create_effective_key() {
Project project = new Project("my_project");
assertThat(ComponentKeys.createEffectiveKey(project, project)).isEqualTo("my_project");
Project project = new Project(ProjectDefinition.create().setKey("my_project"));
assertThat(ComponentKeys.createEffectiveKey("my_project", project)).isEqualTo("my_project");

Directory dir = Directory.create("src/org/foo");
assertThat(ComponentKeys.createEffectiveKey(project, dir)).isEqualTo("my_project:src/org/foo");
assertThat(ComponentKeys.createEffectiveKey("my_project", dir)).isEqualTo("my_project:src/org/foo");

InputFile file = mock(InputFile.class);
when(file.relativePath()).thenReturn("foo/Bar.php");
Expand Down
Expand Up @@ -29,6 +29,7 @@
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -141,7 +142,7 @@ public void openStream_network_errors() throws IOException, URISyntaxException {
public boolean matches(Object ex) {
return
// Java 8
ex instanceof NoRouteToHostException
ex instanceof NoRouteToHostException || ex instanceof SocketException
// Java 7 or before
|| ex instanceof SocketTimeoutException;
}
Expand Down
Expand Up @@ -163,5 +163,7 @@ interface Index {

@CheckForNull
InputDir inputDir(String relativePath);

InputModule module();
}
}
Expand Up @@ -33,10 +33,11 @@ public interface InputComponent {
* Component key shared by all part of SonarQube (batch, server, WS...)
*/
String key();

/**
* Is the component an {@link InputFile}
*/
boolean isFile();


}
Expand Up @@ -27,5 +27,4 @@
* @since 5.2
*/
public interface InputModule extends InputComponent {

}
Expand Up @@ -40,6 +40,7 @@
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputDir;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.PathUtils;

Expand Down Expand Up @@ -199,6 +200,11 @@ public DefaultFileSystem add(DefaultInputDir inputDir) {
cache.add(inputDir);
return this;
}

public DefaultFileSystem add(InputModule inputModule) {
cache.add(inputModule);
return this;
}

/**
* Adds a language to the list. To be used only for unit tests that need to use {@link #languages()} without
Expand Down Expand Up @@ -234,6 +240,8 @@ public abstract static class Cache implements Index {

protected abstract void doAdd(InputDir inputDir);

protected abstract void doAdd(InputModule inputModule);

final void add(InputFile inputFile) {
doAdd(inputFile);
}
Expand All @@ -242,6 +250,10 @@ public void add(InputDir inputDir) {
doAdd(inputDir);
}

public void add(InputModule inputModule) {
doAdd(inputModule);
}

}

/**
Expand All @@ -250,6 +262,7 @@ public void add(InputDir inputDir) {
private static class MapCache extends Cache {
private final Map<String, InputFile> fileMap = new HashMap<>();
private final Map<String, InputDir> dirMap = new HashMap<>();
private InputModule module;

@Override
public Iterable<InputFile> inputFiles() {
Expand All @@ -266,6 +279,10 @@ public InputDir inputDir(String relativePath) {
return dirMap.get(relativePath);
}

public InputModule module() {
return module;
}

@Override
protected void doAdd(InputFile inputFile) {
fileMap.put(inputFile.relativePath(), inputFile);
Expand All @@ -275,6 +292,11 @@ protected void doAdd(InputFile inputFile) {
protected void doAdd(InputDir inputDir) {
dirMap.put(inputDir.relativePath(), inputDir);
}

@Override
protected void doAdd(InputModule inputModule) {
module = inputModule;
}
}

@Override
Expand Down
Expand Up @@ -37,10 +37,15 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed
private final Type type;

public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath) {
this(moduleKey, moduleBaseDir, relativePath, Type.MAIN);
this(moduleKey, moduleBaseDir, relativePath, TestInputFileBuilder.batchId++);
}

public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, Type type) {
public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, int batchId) {
this(moduleKey, moduleBaseDir, relativePath, Type.MAIN, batchId);
}

public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, Type type, int batchId) {
super(batchId);
this.moduleKey = moduleKey;
this.relativePath = PathUtils.sanitize(relativePath);
this.moduleBaseDir = moduleBaseDir.normalize();
Expand Down
Expand Up @@ -25,6 +25,11 @@
* @since 5.2
*/
public abstract class DefaultInputComponent implements InputComponent {
private int id;

public DefaultInputComponent(int batchId) {
this.id = batchId;
}

@Override
public boolean equals(Object o) {
Expand All @@ -39,6 +44,10 @@ public boolean equals(Object o) {
return key().equals(that.key());
}

public int batchId() {
return id;
}

@Override
public int hashCode() {
return key().hashCode();
Expand Down
Expand Up @@ -35,6 +35,11 @@ public class DefaultInputDir extends DefaultInputComponent implements InputDir {
private Path moduleBaseDir;

public DefaultInputDir(String moduleKey, String relativePath) {
this(moduleKey, relativePath, TestInputFileBuilder.batchId++);
}

public DefaultInputDir(String moduleKey, String relativePath, int batchId) {
super(batchId);
this.moduleKey = moduleKey;
this.relativePath = PathUtils.sanitize(relativePath);
}
Expand Down
Expand Up @@ -42,6 +42,7 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile
private Metadata metadata;

public DefaultInputFile(DefaultIndexedFile indexedFile, Function<DefaultInputFile, Metadata> metadataGenerator) {
super(indexedFile.batchId());
this.indexedFile = indexedFile;
this.metadataGenerator = metadataGenerator;
this.metadata = null;
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.api.batch.fs.internal;

import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputModule;

/**
Expand All @@ -27,11 +28,21 @@
public class DefaultInputModule extends DefaultInputComponent implements InputModule {

private final String moduleKey;
private final ProjectDefinition definition;

public DefaultInputModule(String moduleKey) {
this.moduleKey = moduleKey;
this(ProjectDefinition.create().setKey(moduleKey), TestInputFileBuilder.batchId++);
}

public DefaultInputModule(ProjectDefinition definition, int batchId) {
super(batchId);
this.definition = definition;
this.moduleKey = definition.getKey();
}

/**
* Module key without branch
*/
@Override
public String key() {
return moduleKey;
Expand All @@ -42,4 +53,8 @@ public boolean isFile() {
return false;
}

public ProjectDefinition definition() {
return definition;
}

}
@@ -0,0 +1,30 @@
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.batch.fs.internal;

import java.util.Collection;

import org.sonar.api.batch.fs.InputComponent;

public interface InputComponentTree {
public Collection<InputComponent> getChildren(InputComponent module);

public InputComponent getParent(InputComponent module);
}
@@ -0,0 +1,41 @@
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.batch.fs.internal;

import java.util.Collection;

import javax.annotation.CheckForNull;

import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.fs.internal.DefaultInputModule;

public interface InputModuleHierarchy {
DefaultInputModule root();

boolean isRoot(InputModule module);

Collection<DefaultInputModule> children(InputModule module);

@CheckForNull
DefaultInputModule parent(InputModule module);

@CheckForNull
String relativePath(InputModule module);
}
Expand Up @@ -28,10 +28,10 @@
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.utils.PathUtils;

/**
* @since 4.2
*/
public class TestInputFileBuilder {
public static int batchId = 1;

private final int id;
private final String relativePath;
private final String moduleKey;
private Path moduleBaseDir;
Expand All @@ -46,9 +46,14 @@ public class TestInputFileBuilder {
private int[] originalLineOffsets;

public TestInputFileBuilder(String moduleKey, String relativePath) {
this(moduleKey, relativePath, batchId++);
}

public TestInputFileBuilder(String moduleKey, String relativePath, int id) {
this.moduleKey = moduleKey;
this.moduleBaseDir = Paths.get(moduleKey);
this.relativePath = PathUtils.sanitize(relativePath);
this.id = id;
}

public TestInputFileBuilder setModuleBaseDir(Path moduleBaseDir) {
Expand Down Expand Up @@ -115,7 +120,7 @@ public TestInputFileBuilder initMetadata(String content) {
}

public DefaultInputFile build() {
DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type);
DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type, id);
indexedFile.setLanguage(language);
DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> new Metadata(lines, nonBlankLines, hash, originalLineOffsets, lastValidOffset));
inputFile.setStatus(status);
Expand Down

0 comments on commit eea589c

Please sign in to comment.