blob: 01e880d095370148725e573d69617f3ed46c7897 [file] [log] [blame]
package org.polarsys.chess.contracts.verificationService.test.runtime.tests;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.papyrus.junit.utils.rules.PluginResource;
import org.eclipse.papyrus.junit.utils.rules.ResourceSetFixture;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Package;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import org.polarsys.chess.OSSImporter.core.actions.ImportOSSFileAction;
import org.polarsys.chess.contracts.profile.chesscontract.util.EntityUtil;
import org.polarsys.chess.diagram.ui.services.CHESSDiagramsGeneratorService;
import org.polarsys.chess.diagram.ui.services.ResultsGeneratorService;
import org.polarsys.chess.service.core.model.ChessSystemModel;
import org.polarsys.chess.service.core.model.UMLStateMachineModel;
import eu.fbk.eclipse.standardtools.ExecOcraCommands.ui.services.OCRAExecService;
import eu.fbk.eclipse.standardtools.ModelTranslatorToOcra.core.services.OSSTranslatorServiceAPI;
import eu.fbk.eclipse.standardtools.StateMachineTranslatorToSmv.core.services.SMVTranslatorServiceAPI;
import eu.fbk.eclipse.standardtools.diagram.DiagramDescriptor;
import eu.fbk.eclipse.standardtools.diagram.DocumentGenerator;
import eu.fbk.eclipse.standardtools.diagram.ui.docGenerators.DocumentGeneratorServiceFromOssModel;
import eu.fbk.eclipse.standardtools.utils.core.utils.FileSystemUtil;
import eu.fbk.eclipse.standardtools.utils.core.utils.StringArrayUtil;
import eu.fbk.tools.adapter.ui.preferences.PreferenceConstants;
import eu.fbk.tools.editor.oss.oss.OSS;
//@Headless
public class TestBasicOperations extends AbstractPapyrusTest {
@Rule
public ErrorCollector collector = new ErrorCollector();
private final String configFileName = "configTest.properties";
private String testOutput;
// private String testTempOutput;
/*
* private String OCRAFilePath; private String nuXmvFilePath; private String
* xSAPFilePath;
*/
private EntityUtil entityUtil = EntityUtil.getInstance();
private static final Logger logger = Logger.getLogger(TestBasicOperations.class);
private final String projectFolderPath = "resources/SSR_fi/";
private final String projectPath = projectFolderPath + "SSR.di";
@Rule
public final ResourceSetFixture resourceSetFixture = new ResourceSetFixture();
private Class getSystemComponent() throws Exception {
Model model = getModel();
Package umlSelectedPackage = entityUtil.getSystemViewPackage(model);
System.out.println("umlSelectedPackage: " + umlSelectedPackage);
Class umlSelectedComponent = entityUtil.getSystemElement(model);
return umlSelectedComponent;
}
@Test
@PluginResource(projectPath)
public void testExportStateMachinesAsMonolithicSmv() throws Exception {
File outputFolder = new File(testOutput);
String selectedDirectory = outputFolder.getAbsolutePath();
// IWorkspaceRoot wRoot = ResourcesPlugin.getWorkspace().getRoot();
// logger.debug("wRoot: " + wRoot);
Class umlSelectedComponent = getSystemComponent();
exportModelAsMonolithicSmvFile(selectedDirectory, umlSelectedComponent);
String oracleFolder = projectFolderPath + "/MonolithicSmvFile";
verifyDirsAreEqual(Paths.get(oracleFolder), Paths.get(selectedDirectory));
verifyDirsAreEqual(Paths.get(selectedDirectory), Paths.get(oracleFolder));
}
public static File exportModelAsMonolithicSmvFile(String selectedDirectory, Class umlSelectedComponent)
throws Exception, IOException {
OSSTranslatorServiceAPI ossTranslatorServiceAPI = new OSSTranslatorServiceAPI(ChessSystemModel.getInstance());
Object ocraModel = ossTranslatorServiceAPI.exportRootComponentToOssModel(umlSelectedComponent, true,
new NullProgressMonitor());
// String fileName =
// toolToOCRAModelTranslator.getFileName(umlSelectedComponent);
logger.debug("generateOssFileFromOssModel");
String fileName = ossTranslatorServiceAPI.getFileName(umlSelectedComponent);
File ossFile = ossTranslatorServiceAPI.exportOSSModelToOSSFile(ocraModel, fileName, selectedDirectory);
File smvMapFile = createSmvMapFile(selectedDirectory, umlSelectedComponent);
String outputFilePath = selectedDirectory + File.separator + umlSelectedComponent.getName().replace(".", "_")
+ "_monolithic.smv";
OCRAExecService ocraExecService = OCRAExecService.getInstance(ChessSystemModel.getInstance());
ocraExecService.executePrintSystemImplementation(ossFile, smvMapFile, outputFilePath, true, true);
return new File(outputFilePath);
}
public static File createSmvMapFile(String selectedDirectory, Class umlSelectedComponent)
throws Exception, IOException {
SMVTranslatorServiceAPI smvTranslatorServiceAPI = SMVTranslatorServiceAPI
.getInstance(ChessSystemModel.getInstance(), UMLStateMachineModel.getInstance());
HashMap<String, String> mapSmvFile_ComponentName = smvTranslatorServiceAPI
.exportAllStateMachinesToSMVFiles(umlSelectedComponent, selectedDirectory, new NullProgressMonitor());
File smvMapFile = FileSystemUtil.createSmvMapFile("map", selectedDirectory, mapSmvFile_ComponentName);
return smvMapFile;
}
@Test
@PluginResource(projectPath)
public void testGenerationDocumentation() throws Exception {
ChessSystemModel chessSystemModel = ChessSystemModel.getInstance();
Model model = getModel();
openEditor(model);
OSSTranslatorServiceAPI ocraTranslatorService = OSSTranslatorServiceAPI.getInstance(chessSystemModel);
Class umlSelectedComponent = getSystemComponent();
Package activePackage = umlSelectedComponent.getNearestPackage();
boolean isDiscreteTime = false;
final OSS ossModel = ocraTranslatorService.exportRootComponentToOssModel(umlSelectedComponent, isDiscreteTime,
new NullProgressMonitor());
String imageExtension = ".svg";
String currentProjectName = "SSR";
String docFormat = "html";
CHESSDiagramsGeneratorService chessDiagramsGeneratorService = CHESSDiagramsGeneratorService.getInstance();
Collection<Diagram> chessDiagrams = chessDiagramsGeneratorService.getDiagrams();
System.out.println("num diagrams: " + chessDiagrams.size());
DocumentGeneratorServiceFromOssModel documentGeneratorService = new DocumentGeneratorServiceFromOssModel(
ossModel, chessSystemModel, activePackage);
documentGeneratorService.setParametersBeforeDocumentGeneration(testOutput, imageExtension, true, true, true,
true, true, true, true, true, true, true, true, true, true, true, true);
final DocumentGenerator documentGenerator = documentGeneratorService.createDocumentFile(currentProjectName,
docFormat, ossModel.getSystem(), new NullProgressMonitor());
documentGeneratorService.addLocalAttributeDescriptors(umlSelectedComponent, documentGenerator);
System.out.println("resultsGeneratorService");
final ResultsGeneratorService resultsGeneratorService = new ResultsGeneratorService();
resultsGeneratorService.setParametersBeforeDocumentGeneration(testOutput, true, imageExtension);
resultsGeneratorService.addResultsDescriptors(umlSelectedComponent, activePackage, documentGenerator);
chessDiagramsGeneratorService.setParametersBeforeDiagramsGenerator(testOutput, imageExtension);
System.out.println("diagramDescriptors");
Set<DiagramDescriptor> diagramDescriptors = new HashSet<DiagramDescriptor>();
for (Diagram diagram : chessDiagrams) {
DiagramDescriptor dd = chessDiagramsGeneratorService.createDiagramWithDescriptor(diagram, null,
new NullProgressMonitor());
if (dd != null) {
diagramDescriptors.add(dd);
}
}
documentGeneratorService.addDiagramDescriptors(diagramDescriptors, documentGenerator);
documentGenerator.generate(testOutput);
String oracleFolder = projectFolderPath + "/Documentation";
File outputFolder = new File(testOutput);
String selectedDirectory = outputFolder.getAbsolutePath();
verifyDirsHaveSameSize(Paths.get(oracleFolder), Paths.get(selectedDirectory));
verifyDirsHaveSameSize(Paths.get(selectedDirectory), Paths.get(oracleFolder));
}
public static IEditorPart openEditor(final Model model) throws Exception {
IFile iFile = getUmlFile(model);
return EntityUtil.getInstance().openCurrentModelIntoEditor(iFile);
}
public static IFile getUmlFile(final Model model) {
final org.eclipse.emf.ecore.resource.Resource resource = model.eResource();
URI resourceURI = resource.getURI();
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath workspacePath = workspace.getRoot().getLocation();
IPath finalPath = workspacePath.append(resourceURI.toPlatformString(false));
final IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(finalPath);
// System.out.println("location: "+location);
IFile iFile = files[0];
return iFile;
}
@Before
public void setTestParameters() throws Exception {
testOutput = cleanDirectory("testOutput");
String testTempOutput = cleanDirectory("testTempOutput");
String OCRAFilePath = getConfigTestProperties().getProperty("OCRAFilePath");
String nuXmvFilePath = getConfigTestProperties().getProperty("nuXmvFilePath");
// String xSAPFilePath =
// getConfigTestProperties().getProperty("xSAPFilePath");
File testTempOutputFile = new File(testTempOutput);
File ocraFile = new File(OCRAFilePath);
File nuXmvFile = new File(nuXmvFilePath);
eu.fbk.tools.adapter.ui.Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.TOOL_WORKSPACE,
// "C:/Users/Alberto/Google Drive/AMASS
// Project/ARTA_p1/eclipse/git_home/CHESS_FBK/plugins/contracts/org.polarsys.chess.contracts.verificationService.test.runtime/testTempOutput"
testTempOutputFile.getAbsolutePath());
eu.fbk.tools.adapter.ui.Activator.getDefault().getPreferenceStore().setValue(
PreferenceConstants.OCRA_EXECUTABLE,
// "C:/Users/Alberto/Google Drive/AMASS
// Project/ARTA_p1/eclipse/git_home/CHESS_FBK/plugins/contracts/org.polarsys.chess.contracts.verificationService.test.runtime/resources/tools/ocra_win64.exe"
ocraFile.getAbsolutePath());
eu.fbk.tools.adapter.ui.Activator.getDefault().getPreferenceStore().setValue(
PreferenceConstants.NUXMV_EXECUTABLE,
// "C:/Users/Alberto/Google Drive/AMASS
// Project/ARTA_p1/eclipse/git_home/CHESS_FBK/plugins/contracts/org.polarsys.chess.contracts.verificationService.test.runtime/resources/tools/xSAP_win64.exe"
nuXmvFile.getAbsolutePath());
}
Model getModel() {
return (Model) resourceSetFixture.getModel();
}
private void verifyDirsAreEqual(Path correctResultsDir, Path toCheckResultsDir) throws IOException {
Files.walkFileTree(correctResultsDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
FileVisitResult result = super.visitFile(file, attrs);
// get the relative file name from path "one"
Path correctFilePath = correctResultsDir.relativize(file);
// construct the path for the counterpart file in "other"
Path toCheckFilePath = toCheckResultsDir.resolve(correctFilePath);
try {
Assert.assertTrue(file + " is not equal to " + toCheckFilePath,
compareTwoFilesIgnoreEOL(file, toCheckFilePath));
} catch (Throwable t) {
collector.addError(t);
}
return result;
}
});
}
private void verifyDirsHaveSameSize(Path correctResultsDir, Path toCheckResultsDir) throws IOException {
Files.walkFileTree(correctResultsDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
FileVisitResult result = super.visitFile(file, attrs);
// get the relative file name from path "one"
Path correctFilePath = correctResultsDir.relativize(file);
// construct the path for the counterpart file in "other"
Path toCheckFilePath = toCheckResultsDir.resolve(correctFilePath);
try {
Assert.assertTrue(file + " has not the same size of " + toCheckFilePath,
sameSize(file, toCheckFilePath));
} catch (Throwable t) {
collector.addError(t);
}
return result;
}
});
}
private static String getFileSizeKiloBytes(File file) {
return (double) file.length() / 1024 + " kb";
}
private boolean sameSize(Path p1, Path p2) {
String sizeFile1 = getFileSizeKiloBytes(p1.toFile());
String sizeFile2 = getFileSizeKiloBytes(p2.toFile());
return sizeFile1.equals(sizeFile2);
}
private static boolean compareTwoFilesIgnoreEOL(Path p1, Path p2) throws IOException {
BufferedReader reader1 = new BufferedReader(new FileReader(p1.toFile()));
BufferedReader reader2 = new BufferedReader(new FileReader(p2.toFile()));
String line1 = reader1.readLine();
String line2 = reader2.readLine();
boolean areEqual = true;
while (line1 != null || line2 != null) {
if (line1 == null || line2 == null) {
areEqual = false;
break;
} else if (!StringArrayUtil.equalsIgnoreNewlineStyle(line1, line2)) {
areEqual = false;
break;
}
line1 = reader1.readLine();
line2 = reader2.readLine();
}
reader1.close();
reader2.close();
return areEqual;
}
private String cleanDirectory(String propertyDirectoryPathName) throws IOException {
String workspaceDir = getConfigTestProperties().getProperty(propertyDirectoryPathName);
File workspaceDirFile = new File(workspaceDir);
FileUtils.deleteDirectory(workspaceDirFile);
workspaceDirFile.mkdirs();
return workspaceDir;
}
private Properties getConfigTestProperties() throws IOException {
File configFile = new File(configFileName);
FileReader reader = new FileReader(configFile);
Properties props = new Properties();
props.load(reader);
reader.close();
return props;
}
}