Skip to content

Commit

Permalink
Publish model coverage report tool for MD/CSM
Browse files Browse the repository at this point in the history
  the project is published under Apache 2.0 license terms
  • Loading branch information
tamasborbas committed Jan 30, 2023
1 parent e7c44ad commit e10daf1
Show file tree
Hide file tree
Showing 11 changed files with 817 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: io.incquery.cameo.model-coverage-report
Bundle-SymbolicName: io.incquery.cameo.model-coverage-report;singleton:=true
Bundle-Version: 0.1.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Automatic-Module-Name: io.incquery.cameo.model-coverage-report
Export-Package: .
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<resourceDescriptor critical="false"
date="${build.timestamp}"
description="Model coverage reporting plugin"
id="17151"
mdVersionMax="higher"
mdVersionMin="19.0 beta"
name="Model coverage reporting plugin"
product=""
type="Plugin">
<version human="${human.version}" resource="${internal.version}" internal="${internal.version}"/>
<provider name="IncQuery Labs cPlc."/>
<edition>Reader</edition>
<edition>Community</edition>
<edition>Standard</edition>
<edition>Professional Java</edition>
<edition>Professional C++</edition>
<edition>Professional C#</edition>
<edition>Professional ArcStyler</edition>
<edition>Professional EFFS ArcStyler</edition>
<edition>OptimalJ</edition>
<edition>Professional</edition>
<edition>Architect</edition>
<edition>Enterprise</edition>

<requiredResource id="1440" name="SysML"/>

<installation>
<!-- START AUTO-GENERATED -->
<!-- END AUTO-GENERATED -->
</installation>
</resourceDescriptor>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<plugin
id="io.incquery.cameo.model-coverage-report"
name="IncQuery Labs' Model Coverage Report Plugin"
internalVersion="${internal.version}"
version="${human.version}"
provider-name="IncQuery Labs cPlc."
class-lookup="LocalFirst"
ownClassloader="true"
class="io.incquery.cameo.modelcoveragereport.ModelCoverageReportPlugin">

<requires>
<required-plugin id="com.nomagic.magicdraw.plugins.impl.sysml" name="SysML"/>
</requires>

<runtime>
<!-- START AUTO-GENERATED -->
<!-- END AUTO-GENERATED -->
</runtime>

</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
*
* Copyright (c) 2002 NoMagic, Inc. All Rights Reserved.
*/

package io.incquery.cameo.modelcoveragereport;

import com.nomagic.actions.AMConfigurator;
import com.nomagic.actions.ActionsCategory;
import com.nomagic.actions.ActionsManager;
import com.nomagic.actions.NMAction;
import com.nomagic.magicdraw.actions.MDActionsCategory;

public class MainMenuConfigurator implements AMConfigurator {

String REPORTING = "Report";

/**
* Action will be added to manager.
*/
private NMAction[] actions;

/**
* Creates configurator.
*
* @param action
* action to be added to main menu.
*/
public MainMenuConfigurator(NMAction... actions) {
this.actions = actions;
}

/**
* @see com.nomagic.actions.AMConfigurator#configure(ActionsManager) Methods
* adds action to given manager Examples category.
*/
@Override
public void configure(ActionsManager manager) {
// searching for Examples action category
ActionsCategory category = (ActionsCategory) manager.getActionFor(REPORTING);

if (category == null) {
// creating new category
category = new MDActionsCategory(REPORTING, REPORTING);
category.setNested(true);
manager.addCategory(category);
}
for (NMAction action : actions) {
category.addAction(action);
}
}

@Override
public int getPriority() {
return AMConfigurator.MEDIUM_PRIORITY;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.incquery.cameo.modelcoveragereport;

import io.incquery.cameo.modelcoveragereport.actions.MetaModelCoverageAction;
import com.nomagic.magicdraw.actions.ActionsConfiguratorsManager;
import com.nomagic.magicdraw.plugins.Plugin;

public class ModelCoverageReportPlugin extends Plugin {

@Override
public boolean close() {
return true;
}

@Override
public void init() {

ActionsConfiguratorsManager manager = ActionsConfiguratorsManager.getInstance();
manager.addMainMenuConfigurator(new MainMenuConfigurator(new MetaModelCoverageAction()));
manager.addContainmentBrowserContextConfigurator(new ProfileCoverageActionConfigurator());

}

@Override
public boolean isSupported() {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.incquery.cameo.modelcoveragereport;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import io.incquery.cameo.modelcoveragereport.actions.ProfileCoverageAction;
import com.nomagic.actions.AMConfigurator;
import com.nomagic.actions.ActionsCategory;
import com.nomagic.actions.ActionsManager;
import com.nomagic.magicdraw.actions.ActionsStateUpdater;
import com.nomagic.magicdraw.actions.BrowserContextAMConfigurator;
import com.nomagic.magicdraw.actions.MDActionsCategory;
import com.nomagic.magicdraw.ui.browser.Node;
import com.nomagic.magicdraw.ui.browser.Tree;
import com.nomagic.uml2.ext.magicdraw.mdprofiles.Profile;

public class ProfileCoverageActionConfigurator implements BrowserContextAMConfigurator {

@Override
public void configure(ActionsManager manager, Tree tree) {
ActionsCategory cat = manager.getCategory("REPORT");
if(cat == null) {
cat = new MDActionsCategory("REPORT", "Generate Report...");
manager.addCategory(cat);
}
Set<Profile> selectedObjects = Arrays.stream(tree.getSelectedNodes()).map(Node::getUserObject)
.filter(Profile.class::isInstance).map(Profile.class::cast).collect(Collectors.toSet());
if(!selectedObjects.isEmpty()) {
cat.addAction(new ProfileCoverageAction(selectedObjects.iterator().next()));
ActionsStateUpdater.updateActionsState();
}
}

@Override
public int getPriority() {
return AMConfigurator.MEDIUM_PRIORITY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package io.incquery.cameo.modelcoveragereport.actions;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.nomagic.magicdraw.actions.MDAction;
import com.nomagic.magicdraw.core.Application;
import com.nomagic.magicdraw.core.GUILog;
import com.nomagic.magicdraw.core.Project;

import javax.annotation.CheckForNull;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

public abstract class CoverageReportAction extends MDAction {
private static final long serialVersionUID = 1L;
protected static final String TEMPLATE__CAT_ID = "%S_COVERAGE_REPORT_GENERATOR";
protected static final String TEMPLATE__CAT_NAME = "Generate %s Coverage Report";
protected final String type;
private static File baseDir = null;

@Override
public final void actionPerformed(@CheckForNull ActionEvent actionEvent) {
// select output directory
JFileChooser outputDirectorySelector = new JFileChooser();
outputDirectorySelector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
outputDirectorySelector.setDialogTitle("Output Directory Selector");
outputDirectorySelector.setCurrentDirectory(baseDir);
if(outputDirectorySelector.showOpenDialog(Application.getInstance().getMainFrame()) == JFileChooser.APPROVE_OPTION) {
baseDir = outputDirectorySelector.getSelectedFile();
} else {
return;
}

// execute the report generation
Result result = actionPerformed();

// user feedback
GUILog log = Application.getInstance().getGUILog();
if(result.succeeded) {
log.log(result.message);
} else if(result.error != null) {
log.showError(result.message, result.error);
} else {
log.showError(result.message);
}
}

protected File getBaseDir() {
return baseDir;
}

public abstract Result actionPerformed();

protected CoverageReportAction(String type) {
super(String.format(TEMPLATE__CAT_ID, type), String.format(TEMPLATE__CAT_NAME, type), null, null);
this.type = type.toLowerCase();
}
protected final void serializeToJson(Object coverageDTO, File outputDir) {
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
File outputFile = new File(outputDir, String.format("%sCoverageInfo.json", type));
try (FileWriter writer = new FileWriter(outputFile)) {
gson.toJson(coverageDTO, writer);
} catch (IOException e) {
e.printStackTrace();
}
}

protected final File setupDestinationDirectory(Project project) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss");
String projectName = project.getName();

return new File(Paths.get(
getBaseDir() != null
? getBaseDir().getAbsolutePath()
: "./ModelCoverageInfo",
projectName,
simpleDate.format(timestamp)).toString());
}

protected static final class Result {
public final boolean succeeded;
public final String message;
public final Throwable error;

private static final String OUTPUT_TEMPLATE = "Report files were successfully generated into the following directory: %s";

public Result(File outputDir) {
this.succeeded = true;
this.message = String.format(OUTPUT_TEMPLATE, outputDir.toString());
this.error = null;
}
public Result(String message, Throwable error) {
this.succeeded = true;
this.message = message;
this.error = error;
}
}
}
Loading

0 comments on commit e10daf1

Please sign in to comment.