private Long id;
private String kee;
private String path;
+ private String moduleKey;
private String name;
private String longName;
private String qualifier;
return this;
}
+ @Override
+ public String moduleKey() {
+ return moduleKey;
+ }
+
+ public ComponentDto setModuleKey(String moduleKey) {
+ this.moduleKey = moduleKey;
+ return this;
+ }
+
@Override
public String name() {
return name;
return (String) getProperty("path");
}
+ public String moduleKey() {
+ return (String) getProperty("moduleKey");
+ }
+
public String name() {
return (String) getProperty("name");
}
void copyFrom(Component component) {
setProperty("key", component.key());
setProperty("path", component.path());
+ setProperty("moduleKey", component.moduleKey());
setProperty("name", component.name());
setProperty("longName", component.longName());
setProperty("qualifier", component.qualifier());
package org.sonar.core.component;
import com.google.common.base.Strings;
+import org.apache.commons.lang.StringUtils;
import org.sonar.api.component.Component;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Resource;
public class ResourceComponent implements Component {
private String key;
private String path;
+ private String moduleKey;
private String name;
private String longName;
private String qualifier;
public ResourceComponent(Resource resource, @Nullable Snapshot snapshot) {
this.key = resource.getEffectiveKey();
this.path = resource.getPath();
+ // Kind of a hack as it depends on format of resource effective key
+ this.moduleKey = StringUtils.removeEnd(resource.getEffectiveKey(), ":" + resource.getKey());
if (Strings.isNullOrEmpty(key)) {
throw new IllegalArgumentException("Missing component key");
}
return path;
}
+ @Override
+ public String moduleKey() {
+ return moduleKey;
+ }
+
public String name() {
return name;
}
return new ComponentDto()
.setId(resourceDto.getId())
.setKey(resourceDto.getKey())
+ .setModuleKey(resourceDto.getModuleKey())
.setPath(resourceDto.getPath())
.setLongName(resourceDto.getLongName())
.setName(resourceDto.getName())
private String name;
private String longName;
private Long rootId;
+ private String moduleKey;
private String path;
private String scope;
private String qualifier;
return this;
}
+ public String getModuleKey() {
+ return moduleKey;
+ }
+
+ public ResourceDto setModuleKey(String s) {
+ this.moduleKey = s;
+ return this;
+ }
+
public String getLongName() {
return longName;
}
<result property="key" column="kee"/>
<result property="deprecatedKey" column="deprecated_kee"/>
<result property="path" column="path"/>
+ <result property="moduleKey" column="moduleKey"/>
<result property="name" column="name"/>
<result property="longName" column="long_name"/>
<result property="rootId" column="root_id"/>
</resultMap>
<select id="selectResources" parameterType="map" resultMap="resourceResultMap">
- select *
- from projects p
+ select p.*, r.kee moduleKey
+ from projects p left outer join projects r on p.root_id=r.id
<where>
<if test="qualifiers != null and qualifiers.length!=0">
and p.qualifier in
</select>
<select id="selectResource" parameterType="long" resultMap="resourceResultMap">
- select * from projects where id=#{id}
+ select p.*, r.kee moduleKey
+ from projects p left outer join projects r on p.root_id=r.id
+ where p.id=#{id}
</select>
<select id="selectResourcesById" parameterType="map" resultMap="resourceResultMap">
- select * from projects p where p.enabled=${_true}
+ select p.*, r.kee moduleKey
+ from projects p left outer join projects r on p.root_id=r.id
+ where p.enabled=${_true}
and p.id in
<foreach collection="ids" open="(" close=")" item="key" separator=",">
#{key}
BeanGraph beanGraph = new BeanGraph(new TinkerGraph());
ComponentVertex vertex = beanGraph.createVertex(ComponentVertex.class);
Component file = MockSourceFile.createMain("myproject:org/Foo.java").setName("Foo.java").setQualifier(Qualifiers.FILE)
- .setPath("src/org/Foo.java");
+ .setPath("src/org/Foo.java").setModuleKey("myproject");
vertex.copyFrom(file);
assertThat(vertex.name()).isEqualTo("Foo.java");
assertThat(vertex.qualifier()).isEqualTo(Qualifiers.FILE);
assertThat(vertex.path()).isEqualTo("src/org/Foo.java");
+ assertThat(vertex.moduleKey()).isEqualTo("myproject");
}
@Test
*/
package org.sonar.core.component;
+import org.junit.Before;
import org.junit.Test;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.File;
public class ResourceComponentTest {
- Resource file = new File("foo.c").setEffectiveKey("myproject:path/to/foo.c");
+ private Resource file;
+
+ @Before
+ public void prepare() {
+ file = new File("foo.c").setEffectiveKey("myproject:path/to/foo.c");
+ file.setKey("path/to/foo.c");
+ }
@Test
public void db_ids_should_be_optional() {
public void should_use_effective_key() {
ResourceComponent component = new ResourceComponent(file);
assertThat(component.key()).isEqualTo("myproject:path/to/foo.c");
+ assertThat(component.moduleKey()).isEqualTo("myproject");
}
@Test
ResourceDto resource = dao.getResource(1L);
assertThat(resource.getPath()).isNull();
+ assertThat(resource.getModuleKey()).isNull();
assertThat(resource.getName()).isEqualTo("Struts");
assertThat(resource.getLongName()).isEqualTo("Apache Struts");
assertThat(resource.getScope()).isEqualTo("PRJ");
assertThat(resource.isEnabled()).isTrue();
}
+ @Test
+ public void get_resource_path_and_module_key() {
+ setupData("fixture");
+
+ ResourceDto dir = dao.getResource(3L);
+ assertThat(dir.getPath()).isEqualTo("src/org/struts");
+ assertThat(dir.getModuleKey()).isEqualTo("org.struts:struts-core");
+
+ ResourceDto file = dao.getResource(4L);
+ assertThat(file.getPath()).isEqualTo("src/org/struts/RequestContext.java");
+ assertThat(file.getModuleKey()).isEqualTo("org.struts:struts-core");
+ }
+
@Test
public void getResource_not_found() {
setupData("fixture");
public void should_find_root_project_by_component_key() {
setupData("fixture");
- ResourceDto resource = dao.getRootProjectByComponentKey("org.struts:struts:org.struts.RequestContext");
+ ResourceDto resource = dao.getRootProjectByComponentKey("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(resource.getName()).isEqualTo("Struts");
- resource = dao.getRootProjectByComponentKey("org.struts:struts:org.struts");
+ resource = dao.getRootProjectByComponentKey("org.struts:struts-core:src/org/struts");
assertThat(resource.getName()).isEqualTo("Struts");
resource = dao.getRootProjectByComponentKey("org.struts:struts-core");
assertThat(dao.findAuthorizedChildrenComponentIds(newArrayList("org.struts:struts"), null, "user")).hasSize(4);
assertThat(dao.findAuthorizedChildrenComponentIds(newArrayList("org.struts:struts-core"), null, "user")).hasSize(3);
- assertThat(dao.findAuthorizedChildrenComponentIds(newArrayList("org.struts:struts:org.struts"), null, "user")).hasSize(2);
- assertThat(dao.findAuthorizedChildrenComponentIds(newArrayList("org.struts:struts:org.struts.RequestContext"), null, "user")).hasSize(1);
+ assertThat(dao.findAuthorizedChildrenComponentIds(newArrayList("org.struts:struts-core:src/org/struts"), null, "user")).hasSize(2);
+ assertThat(dao.findAuthorizedChildrenComponentIds(newArrayList("org.struts:struts-core:src/org/struts/RequestContext.java"), null, "user")).hasSize(1);
assertThat(dao.findAuthorizedChildrenComponentIds(newArrayList("unknown"), null, "user")).isEmpty();
assertThat(dao.findAuthorizedChildrenComponentIds(Collections.<String>emptyList(), null, "user")).isEmpty();
setupData("fixture");
assertThat(dao.findByKey("org.struts:struts")).isNotNull();
- Component<?> component = dao.findByKey("org.struts:struts:org.struts.RequestContext");
+ Component<?> component = dao.findByKey("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(component).isNotNull();
- assertThat(component.path()).isEqualTo("src/main/java/RequestContext.java");
+ assertThat(component.path()).isEqualTo("src/org/struts/RequestContext.java");
assertThat(dao.findByKey("unknown")).isNull();
}
depth="[null]" scope="PRJ" qualifier="TRK" created_at="2008-12-01 13:58:00.00" build_date="2008-12-01 13:58:00.00"
version="[null]" path=""/>
- <!-- project -->
+ <!-- module -->
<projects id="2" root_id="1" kee="org.struts:struts-core" name="Struts Core"
scope="PRJ" qualifier="BRC" long_name="Struts Core"
description="[null]" enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
version="[null]" path="1."/>
<!-- directory -->
- <projects long_name="org.struts" id="3" scope="DIR" qualifier="PAC" kee="org.struts:struts:org.struts"
- name="org.struts" root_id="1"
+ <projects long_name="org.struts" id="3" scope="DIR" qualifier="DIR" kee="org.struts:struts-core:src/org/struts"
+ name="src/org/struts" root_id="2"
description="[null]"
- enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/org/struts"/>
<snapshots id="3" project_id="3" parent_snapshot_id="2" root_project_id="1" root_snapshot_id="1"
status="P" islast="[true]" purge_status="[null]"
period1_mode="[null]" period1_param="[null]" period1_date="[null]"
version="[null]" path="1.2."/>
<!-- file -->
- <projects long_name="org.struts.RequestContext" id="4" scope="FIL" qualifier="FIL" kee="org.struts:struts:org.struts.RequestContext"
- name="RequestContext" root_id="1"
+ <projects long_name="org.struts.RequestContext" id="4" scope="FIL" qualifier="FIL" kee="org.struts:struts-core:src/org/struts/RequestContext.java"
+ name="RequestContext.java" root_id="2"
description="[null]"
- enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/main/java/RequestContext.java"/>
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/org/struts/RequestContext.java"/>
<snapshots id="4" project_id="4" parent_snapshot_id="3" root_project_id="1" root_snapshot_id="1"
status="P" islast="[true]" purge_status="[null]"
*/
package org.sonar.api.component;
+import javax.annotation.CheckForNull;
+
public interface Component<C extends Component> {
String key();
/**
+ * Path of the component relative to basedir of the parent module.
+ * @return null if this component is not a child of a module
* @since 4.2
*/
+ @CheckForNull
String path();
+ /**
+ * Key of the module this component belong to.
+ * @return null for components that are module themself
+ * @since 4.2
+ */
+ @CheckForNull
+ String moduleKey();
+
String name();
String longName();
public class MockSourceFile implements SourceFile {
private String key;
private String path;
+ private String moduleKey;
private String qualifier;
private String language;
private String name;
return this;
}
+ @Override
+ public String moduleKey() {
+ return moduleKey;
+ }
+
+ public MockSourceFile setModuleKey(String moduleKey) {
+ this.moduleKey = moduleKey;
+ return this;
+ }
+
public String qualifier() {
return qualifier;
}
return getKey();
}
+ @Override
+ public String moduleKey() {
+ return null;
+ }
+
@Override
public String name() {
return getName();
hash[:name] = component.name if component.name
hash[:longName] = component.longName if component.longName
hash[:path] = component.path if component.path
+ hash[:moduleKey] = component.moduleKey if component.moduleKey
hash
end
return JsonUtils.getString(json, "path");
}
+ /**
+ * @since 4.2
+ */
+ @CheckForNull
+ public String moduleKey() {
+ return JsonUtils.getString(json, "moduleKey");
+ }
+
public String name() {
return JsonUtils.getString(json, "name");
}