Skip to content

Commit

Permalink
add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
yasu-s committed Feb 25, 2013
1 parent 71134a0 commit f4e0361
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;

import hudson.FilePath;
Expand All @@ -25,9 +24,6 @@ public abstract class CodeMetricsUtil {
/** */
private static final String[] PROPERTIES_NAMES = new String[] { "name", "maintainabilityIndex", "cyclomaticComplexity", "classCoupling", "depthOfInheritance", "linesOfCode" };

/** */
private static final int BUILD_ACTION_TOKEN_POS = 4;

private CodeMetricsUtil() {}

/**
Expand Down Expand Up @@ -201,27 +197,6 @@ public static boolean saveReports(FilePath folder, FilePath[] files) throws IOEx
return r;
}

public static String[] getBuildActionTokens(String requestURI, String contextPath) {
List<String> tokens = new ArrayList<String>();

String path = requestURI;
if (!StringUtil.isNullOrSpace(contextPath)) {
if (!requestURI.startsWith(contextPath)) return tokens.toArray(new String[0]);
path = requestURI.substring(contextPath.length());
}

int indexJob = path.indexOf("/job");
if (indexJob < 0) return tokens.toArray(new String[0]);

String[] parts = path.substring(indexJob + "/job".length()).split("/");

for (int i = BUILD_ACTION_TOKEN_POS; i < parts.length - 1; i++) {
tokens.add(parts[i]);
}

return tokens.toArray(new String[0]);
}

public static AbstractBean<?> searchBean(AbstractBean<?> bean, String[] tokens) {
return searchBean(bean, tokens, 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.jenkinsci.plugins.vs_code_metrics.util;

import static org.junit.Assert.*;

import java.io.File;

import hudson.FilePath;

import org.jenkinsci.plugins.vs_code_metrics.bean.*;
import org.junit.Test;

public class CodeMetricsUtilTest {

@Test
public void testCreateCodeMetrics() throws Exception {
File file = new File(this.getClass().getResource("test_createbean.xml").getPath());
FilePath path = new FilePath(file);

CodeMetrics bean = CodeMetricsUtil.createCodeMetrics(path);

assertNotNull(bean);
assertEquals(2, bean.getChildren().size());
}

@Test
public void testSaveReports() throws Exception {
File file = new File(this.getClass().getResource("test_save.xml").getPath());
FilePath folder = new FilePath(file.getParentFile());
FilePath[] files = new FilePath[1];
files[0] = new FilePath(file);

boolean r = CodeMetricsUtil.saveReports(folder, files);
assertTrue("saveReports method success.", r);
}

@Test
public void testSearchBean() throws Exception {
File file = new File(this.getClass().getResource("test_createbean.xml").getPath());
FilePath path = new FilePath(file);

CodeMetrics bean = CodeMetricsUtil.createCodeMetrics(path);

AbstractBean<?> result = CodeMetricsUtil.searchBean(bean, new String[0]);
assertEquals(bean, result);

Module module = bean.getChildren().get("CodeMetricsTest.exe");
result = CodeMetricsUtil.searchBean(bean, new String[] { "CodeMetricsTest.exe" });
assertEquals(module, result);

result = CodeMetricsUtil.searchBean(bean, new String[] { "dummy" });
assertNull(result);

Namespace namespace = module.getChildren().get("CodeMetricsTest");
result = CodeMetricsUtil.searchBean(bean, new String[] { "CodeMetricsTest.exe", "CodeMetricsTest" });
assertEquals(namespace, result);

Type type = namespace.getChildren().get("Program");
result = CodeMetricsUtil.searchBean(bean, new String[] { "CodeMetricsTest.exe", "CodeMetricsTest", "Program" });
assertEquals(type, result);
assertEquals(2, result.getChildren().size());
assertEquals(100, type.getMaintainabilityIndex());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><report><module loc="1" doi="1" cls="0" cyc="2" mi="100" name="CodeMetricsTest.exe"><namespace loc="1" doi="1" cls="0" cyc="2" mi="100" name="CodeMetricsTest"><type loc="1" doi="1" cls="0" cyc="2" mi="100" name="Program"><member loc="0" cls="0" cyc="1" mi="100" name="Main(string[]) : void"/><member loc="1" cls="0" cyc="1" mi="100" name="Program()"/></type></namespace></module><module loc="1" doi="1" cls="0" cyc="3" mi="90" name="CodeMetricsTest2.exe"><namespace loc="1" doi="1" cls="0" cyc="3" mi="95" name="CodeMetricsTest2"><type loc="1" doi="1" cls="0" cyc="3" mi="90" name="Program"><member loc="0" cls="0" cyc="3" mi="88" name="Main(string[]) : void"/><member loc="1" cls="0" cyc="2" mi="92" name="Program()"/></type></namespace></module></report>
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="utf-8"?>
<CodeMetricsReport Version="10.0">
<Targets>
<Target Name="C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\CodeMetricsTest.exe">
<Modules>
<Module Name="CodeMetricsTest.exe" AssemblyVersion="1.0.0.0" FileVersion="1.0.0.0">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="100" />
<Metric Name="CyclomaticComplexity" Value="2" />
<Metric Name="ClassCoupling" Value="0" />
<Metric Name="DepthOfInheritance" Value="1" />
<Metric Name="LinesOfCode" Value="1" />
</Metrics>
<Namespaces>
<Namespace Name="CodeMetricsTest">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="100" />
<Metric Name="CyclomaticComplexity" Value="2" />
<Metric Name="ClassCoupling" Value="0" />
<Metric Name="DepthOfInheritance" Value="1" />
<Metric Name="LinesOfCode" Value="1" />
</Metrics>
<Types>
<Type Name="Program">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="100" />
<Metric Name="CyclomaticComplexity" Value="2" />
<Metric Name="ClassCoupling" Value="0" />
<Metric Name="DepthOfInheritance" Value="1" />
<Metric Name="LinesOfCode" Value="1" />
</Metrics>
<Members>
<Member Name="Main(string[]) : void" File="D:\CodeMetricsTest\Program.cs" Line="11">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="100" />
<Metric Name="CyclomaticComplexity" Value="1" />
<Metric Name="ClassCoupling" Value="0" />
<Metric Name="LinesOfCode" Value="0" />
</Metrics>
</Member>
<Member Name="Program()">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="100" />
<Metric Name="CyclomaticComplexity" Value="1" />
<Metric Name="ClassCoupling" Value="0" />
<Metric Name="LinesOfCode" Value="1" />
</Metrics>
</Member>
</Members>
</Type>
</Types>
</Namespace>
</Namespaces>
</Module>
<Module Name="CodeMetricsTest2.exe" AssemblyVersion="1.0.0.0" FileVersion="1.0.0.0">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="90" />
<Metric Name="CyclomaticComplexity" Value="3" />
<Metric Name="ClassCoupling" Value="0" />
<Metric Name="DepthOfInheritance" Value="1" />
<Metric Name="LinesOfCode" Value="1" />
</Metrics>
<Namespaces>
<Namespace Name="CodeMetricsTest2">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="95" />
<Metric Name="CyclomaticComplexity" Value="3" />
<Metric Name="ClassCoupling" Value="0" />
<Metric Name="DepthOfInheritance" Value="1" />
<Metric Name="LinesOfCode" Value="1" />
</Metrics>
<Types>
<Type Name="Program">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="90" />
<Metric Name="CyclomaticComplexity" Value="3" />
<Metric Name="ClassCoupling" Value="0" />
<Metric Name="DepthOfInheritance" Value="1" />
<Metric Name="LinesOfCode" Value="1" />
</Metrics>
<Members>
<Member Name="Main(string[]) : void" File="D:\CodeMetricsTest2\Program.cs" Line="11">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="88" />
<Metric Name="CyclomaticComplexity" Value="3" />
<Metric Name="ClassCoupling" Value="0" />
<Metric Name="LinesOfCode" Value="0" />
</Metrics>
</Member>
<Member Name="Program()">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="92" />
<Metric Name="CyclomaticComplexity" Value="2" />
<Metric Name="ClassCoupling" Value="0" />
<Metric Name="LinesOfCode" Value="1" />
</Metrics>
</Member>
</Members>
</Type>
</Types>
</Namespace>
</Namespaces>
</Module>
</Modules>
</Target>
</Targets>
</CodeMetricsReport>

0 comments on commit f4e0361

Please sign in to comment.