Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 dev #83

Merged
merged 5 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public class Analyzer {
private static final Logger LOGGER = Logger.getLogger(Analyzer.class.getName());

/** Number of thread to run the analysis */
private static int THREAD_NB = 1;
private static final int THREAD_NB = Runtime.getRuntime().availableProcessors();

/**
* Define ratio of max memory allocated to the JVM to use for the analysis.
*/
private static final double MAX_MEMORY_THRESHOLD = 0.90;

/**
* <h1>{@link #check(List, List, List)}</h1>
Expand Down Expand Up @@ -108,35 +113,35 @@ public List<CheckResult> check(List<File> pInputFiles, List<String> pLanguageIds
* extension).
*/
final List<CheckerContainer> checkers;

try {

final long maxMemory = Runtime.getRuntime().maxMemory();
checkers = CheckerService.getCheckers(languageIds, excludedCheckIds);
for (CheckerContainer checker : checkers) {
for (File file : pInputFiles) {
if (checker.canVerifyFormat(this.getFileExtension(file.getAbsolutePath()))) {
final CallableChecker callableAnalysis = new CallableChecker(
checker.getChecker(), file);
if ((MAX_MEMORY_THRESHOLD * maxMemory < (Runtime.getRuntime().totalMemory()
- Runtime.getRuntime().freeMemory()))
&& !analyzers.isEmpty()) {
for (Future<List<CheckResult>> analysis : analyzers) {
analysisResultCheckResult.addAll(analysis.get());
}
analyzers.clear();
Runtime.getRuntime().gc();
}
analyzers.add(service.submit(callableAnalysis));

}
}
}
} catch (NullContributionException | CoreException e) {
e.printStackTrace();
}
for (Future<List<CheckResult>> analysis : analyzers) {
try {
for (Future<List<CheckResult>> analysis : analyzers) {
analysisResultCheckResult.addAll(analysis.get());
} catch (InterruptedException interruptedException) {
LOGGER.throwing(this.getClass().getName(), methodName, interruptedException);
} catch (ExecutionException executionException) {
if (executionException.getCause() instanceof IOException) {
throw ((IOException) executionException.getCause());
} else if (executionException.getCause() instanceof JFlexException) {
throw ((JFlexException) executionException.getCause());
} else {
executionException.printStackTrace();
}
}
} catch (NullContributionException | ExecutionException | InterruptedException
| CoreException e) {
LOGGER.throwing(this.getClass().getName(), methodName, e);
}
return analysisResultCheckResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;

import fr.cnes.analysis.tools.analyzer.datas.AbstractChecker;
Expand All @@ -32,6 +33,8 @@ public class CheckerContainer {
private AbstractChecker checker;
/** Whether or not the checker is a metric */
private boolean isMetric;
/** Contribution of the checker */
private IConfigurationElement contribution;

/**
* @param pId
Expand Down Expand Up @@ -76,6 +79,7 @@ public CheckerContainer(String pId, String pName, LanguageContainer pLanguage,
this.name = pName;
this.language = pLanguage;
this.checker = pChecker;
this.contribution = pCheckerContribution;
this.checker.setContribution(pCheckerContribution);
this.isMetric = pIsMetric;
}
Expand Down Expand Up @@ -142,10 +146,17 @@ protected final void setLanguage(LanguageContainer pLanguage) {
}

/**
* @return the checker
* @return the checker A clone class of the checker.
* @throws CoreException
* when {@link AbstractChecker} class can't be created from
* extension point.
*/
public final AbstractChecker getChecker() {
return checker;
public final AbstractChecker getChecker() throws CoreException {
final AbstractChecker cloneChecker = (AbstractChecker) this.checker.getContribution()
.createExecutableExtension(
CheckerService.CHECKER_EP_ELEMENT_CHECK_ATT_CLASS);
cloneChecker.setContribution(contribution);
return cloneChecker;
}

/**
Expand Down
144 changes: 95 additions & 49 deletions ...s/tools/fortran77/rules/TestGlobal_4.java → ...is/tools/fortran77/rules/TestGlobal1.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/************************************************************************************************/
/* i-Code CNES is a static code analyzer. */
/* i-Code CNES is a static code analyzer. */
/* This software is a free software, under the terms of the Eclipse Public License version 1.0. */
/* http://www.eclipse.org/legal/epl-v10.html */
/* http://www.eclipse.org/legal/epl-v10.html */
/************************************************************************************************/

package fr.cnes.analysis.tools.fortran77.rules;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import fr.cnes.analysis.tools.analyzer.Analyzer;
import fr.cnes.analysis.tools.analyzer.datas.CheckResult;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
Expand All @@ -19,26 +17,44 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.Path;
import org.junit.Test;

public class TestGlobal_4 {
/** This list contains all the violations when the analyse is executed **/
public static List<CheckResult> list = new LinkedList<CheckResult>();
List<File> listFiles = new LinkedList<File>();
import fr.cnes.analysis.tools.analyzer.Analyzer;
import fr.cnes.analysis.tools.analyzer.datas.CheckResult;
import fr.cnes.analysis.tools.analyzer.exception.JFlexException;

/**
* This class is intended to run test on all the files contained in the package
* f77_1 of resources.
*/
public class TestGlobal1 {
/** This list contains all the violations when the analysis is executed **/
private static List<CheckResult> list = new LinkedList<CheckResult>();
/** File to analyze */
private final List<File> listFiles = new LinkedList<File>();

/**********************/
/** PARAMS TO DEFINE **/
/**********************/

/** Folder where the function find files to execute the analyze **/
final String resources = "/resources/f77_4";
private final String resources = "/resources/f77_1";
/** Extension of the files to be analyzed **/
final String extension = "f";
private final String extension = "f";
/** Id to execute the analysis **/
final String ruleExtensionId = "fr.cnes.analysis.tools.fortran77.analyzer.rule";
private final String languageId = "fr.cnes.analysis.tools.languages.f77";
/** Id of metrics of Fortran 77 that are excluded from the analysis */
private final String[] excludedIds = new String[] {
"fr.cnes.analysis.tools.fortran77.metrics.F77METComplexitySimplified",
"fr.cnes.analysis.tools.fortran77.metrics.F77METNesting",
"fr.cnes.analysis.tools.fortran77.metrics.F77METLineOfCode",
"fr.cnes.analysis.tools.fortran77.metrics.F77METRatioComment"
};

/**
* Function test to validate global test. This function do - get all files
Expand All @@ -51,48 +67,57 @@ public class TestGlobal_4 {
public void runGlobalTest() {
try {
/** File where to save the result of the execution **/
File fileResult = new File("resources/globalTest_4.txt");
// fileResult.deleteOnExit();
final File fileResult = new File("resources/globalTest_1.txt");
fileResult.deleteOnExit();
/**
* File to compare with fileResult. This file is the verified
* version of the execution
**/
File fileResultValidated = new File("resources/globalTestValidated_4.txt");
final File fileResultValidated = new File("resources/globalTestValidated_1.txt");

/** Get resources file **/
String resourcesPath = System.getProperty("user.dir") + resources;
File folder = new File(resourcesPath);
final String resourcesPath = System.getProperty("user.dir") + resources;
final File folder = new File(resourcesPath);
/** Save files into a list **/
getFilesIntoFolder(folder);

/** Create the analysis job **/
Analyzer analysis = new Analyzer();
List<String> extensionIds = new ArrayList<>();
extensionIds.add(ruleExtensionId);
final Analyzer analysis = new Analyzer();
final List<String> extensionIds = new ArrayList<>();
extensionIds.add(languageId);

TestGlobal_4.list = analysis.check(listFiles, extensionIds, null);
TestGlobal1.list = analysis.check(listFiles, extensionIds, Arrays.asList(excludedIds));

/** Export values into file **/
createExportFile(fileResult);

/** Compare new test with last one **/
assertTrue(compareFiles(fileResult, fileResultValidated));
} catch (Exception e) {
fail("Erreur d'analyse " + e.getMessage());

} catch (IOException exception) {
fail("Erreur d'analyse (IOException)");
exception.printStackTrace();
} catch (JFlexException exception) {
fail("Erreur d'analyse (JFlexException)");
exception.printStackTrace();
}

}

/**
* @param folder
* to run analysis on.
*/
private void getFilesIntoFolder(File folder) {
if (folder.isDirectory()) {
File[] listOfFiles = folder.listFiles();
final File[] listOfFiles = folder.listFiles();
/** For each file, check the extension and save IPath into List **/
for (File file : listOfFiles) {
if (file.isDirectory())
if (file.isDirectory()) {
getFilesIntoFolder(file);
else {
int i = file.getAbsolutePath().lastIndexOf(".");
} else {
final int i = file.getAbsolutePath().lastIndexOf(".");
if (file.getAbsolutePath().substring(i + 1).equals(extension)) {

listFiles.add(file);
}
}
Expand All @@ -104,11 +129,14 @@ private void getFilesIntoFolder(File folder) {
/**
* Function to create the error file. The file has the following: RuleName
* FileName NumError
*
* @param fileResult
* The file to export result in.
*/
private void createExportFile(File fileResult) {
try {
/** Create result file **/
BufferedWriter output = new BufferedWriter(new FileWriter(fileResult));
final BufferedWriter output = new BufferedWriter(new FileWriter(fileResult));

/** If the list is bigger than one **/
if (list.size() > 1) {
Expand All @@ -117,15 +145,15 @@ private void createExportFile(File fileResult) {
int errors = 1;
/** Iterate over the elements **/
for (int i = 1; i < list.size(); i++) {
CheckResult violation = list.get(i);
final CheckResult violation = list.get(i);
/** If the is more errors in the same rule **/
if (violation.getName().equals(rule)) {
/**
* If there is more errors in the same file -> increase
* error
**/
if (new Path(violation.getFile().getAbsolutePath()).lastSegment()
.equals(file)) {
.equals(file)) {
errors++;
}
/** If the filename has change -> print error **/
Expand All @@ -147,46 +175,64 @@ private void createExportFile(File fileResult) {
/** Only one error -> print directly **/
else if (list.size() > 0) {
output.write(list.get(0).getName() + " "
+ new Path(list.get(0).getFile().getAbsolutePath()).lastSegment() + " 1\n");
+ new Path(list.get(0).getFile().getAbsolutePath()).lastSegment()
+ " 1\n");
}
/** After run for all files: close file writer **/
output.close();
} catch (IOException e) {
} catch (IOException exception) {
fail("Erreur d'analyse (IOException)");
}
}

/**
* Comparison between files
*
* @param fileResult
* File generated by the analysis.
* @param fileResultValidated
* file to compare the analysis with.
* @return equals indicate the equality of the files
*/
private boolean compareFiles(File fileResult, File fileResultValidated) {
boolean equals = true;
try {
/** Create reader for both files **/
FileReader fR1 = new FileReader(fileResult);
FileReader fR2 = new FileReader(fileResultValidated);
BufferedReader reader1 = new BufferedReader(fR1);
BufferedReader reader2 = new BufferedReader(fR2);

try (final BufferedReader testReader = new BufferedReader(new FileReader(fileResult));
final BufferedReader validatedReader = new BufferedReader(
new FileReader(fileResultValidated));) {
/** String for compare **/
String line1 = null;
String line2 = null;
String testedLine = null;
String validatedLine = null;

/** Loop over the lines to find differences **/
while (((line1 = reader1.readLine()) != null)
&& ((line2 = reader2.readLine()) != null)) {
if (!line1.equalsIgnoreCase(line2)) {
testedLine = testReader.readLine();
validatedLine = validatedReader.readLine();
while ((testReader.readLine() != null) && ((validatedReader.readLine()) != null)
&& equals) {
if (!testedLine.equalsIgnoreCase(validatedLine)) {
equals = false;
break;
}
testedLine = testReader.readLine();
validatedLine = validatedReader.readLine();
}
/**
* Both documents should be at their last line at the end of the
* test.
*/
final boolean bothDocumentEndingNull = (testedLine == null && validatedLine == null);
final boolean bothDocumentEndingWithSameLine = (testedLine != null
&& validatedLine != null)
&& !testedLine.equalsIgnoreCase(validatedLine);
if (bothDocumentEndingNull || bothDocumentEndingWithSameLine) {
equals = false;
}
reader1.close();
reader2.close();

} catch (FileNotFoundException e) {
testReader.close();
validatedReader.close();
} catch (

FileNotFoundException exception) {
fail("Erreur d'analyse (FileNotFoundException)");
} catch (IOException e) {
} catch (IOException exception) {
fail("Erreur d'analyse (IOException)");
}
/** Return value **/
Expand Down
Loading