Skip to content

Commit

Permalink
Fixed #500 Add an option to ignore the M2Doc version check while vali…
Browse files Browse the repository at this point in the history
…dating.
  • Loading branch information
ylussaud committed Sep 13, 2023
1 parent c7b5c8c commit 3b6bde0
Show file tree
Hide file tree
Showing 19 changed files with 108 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Obeo.
* Copyright (c) 2017, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -499,8 +499,9 @@ private static List<URI> generate(Generation generation, IClassProvider classPro
Map<String, Object> definitions = GenconfUtils.getVariables(generation, resourceSetForModels);

// validate template
final boolean ignoreVersionCheck = Boolean.valueOf(options.get(M2DocUtils.IGNORE_VERSION_CHECK_OPTION));
final URI resultValidationURI = validate(uriConverter, generatedURI, validationURI, documentTemplate,
queryEnvironment, monitor);
queryEnvironment, ignoreVersionCheck, monitor);

// launch generation
final boolean updateFields = Boolean.valueOf(options.get(M2DocUtils.UPDATE_FIELDS_OPTION));
Expand Down Expand Up @@ -576,8 +577,10 @@ public static boolean validate(Generation generation, IClassProvider classProvid
}

// validate template
final Map<String, String> options = getOptions(generation);
final boolean ignoreVersionCheck = Boolean.valueOf(options.get(M2DocUtils.IGNORE_VERSION_CHECK_OPTION));
res = validate(resourceSetForModel.getURIConverter(), templateURI, validationURI, documentTemplate,
queryEnvironment, monitor) != null;
queryEnvironment, ignoreVersionCheck, monitor) != null;
}

// validate output path
Expand Down Expand Up @@ -620,6 +623,8 @@ public static boolean validate(Generation generation, IClassProvider classProvid
* DocumentTemplate
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param ignoreVersionCheck
* ignore the {@link M2DocUtils#VERSION} check
* @param monitor
* the {@link Monitor}
* @return the validation {@link URI} if the validation isn't OK, <code>null</code> otherwise
Expand All @@ -629,11 +634,12 @@ public static boolean validate(Generation generation, IClassProvider classProvid
* IOException
*/
private static URI validate(URIConverter uriConverter, URI generatedURI, URI validationURI,
DocumentTemplate documentTemplate, IReadOnlyQueryEnvironment queryEnvironment, Monitor monitor)
throws DocumentGenerationException, IOException {
DocumentTemplate documentTemplate, IReadOnlyQueryEnvironment queryEnvironment, boolean ignoreVersionCheck,
Monitor monitor) throws DocumentGenerationException, IOException {
final URI res;

final ValidationMessageLevel validationLevel = M2DocUtils.validate(documentTemplate, queryEnvironment, monitor);
final ValidationMessageLevel validationLevel = M2DocUtils.validate(documentTemplate, queryEnvironment,
ignoreVersionCheck, monitor);
if (validationLevel != ValidationMessageLevel.OK) {
if (validationURI != null) {
res = validationURI;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2022 Obeo.
* Copyright (c) 2016, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -124,6 +124,24 @@ public class M2DocValidator extends TemplateSwitch<ValidationMessageLevel> {
*/
public ValidationMessageLevel validate(DocumentTemplate documentTemplate, IReadOnlyQueryEnvironment queryEnv,
Monitor monitor) {
return validate(documentTemplate, queryEnv, false, monitor);
}

/**
* Validates the given {@link DocumentTemplate} against the given {@link IQueryEnvironment} and variables types.
*
* @param documentTemplate
* the {@link DocumentTemplate}
* @param queryEnv
* the {@link IQueryEnvironment}
* @param ignoreVersionCheck
* ignore the {@link M2DocUtils#VERSION} check
* @param monitor
* the {@link Monitor}
* @return the {@link ValidationMessageLevel}
*/
public ValidationMessageLevel validate(DocumentTemplate documentTemplate, IReadOnlyQueryEnvironment queryEnv,
boolean ignoreVersionCheck, Monitor monitor) {

progressMonitor = monitor;
progressMonitor.beginTask("Validating " + documentTemplate.eResource().getURI(), TOTAL_VALIDATE_MONITOR_WORK);
Expand All @@ -137,7 +155,7 @@ public ValidationMessageLevel validate(DocumentTemplate documentTemplate, IReadO
if (templateProperties.getM2DocVersion() == null) {
documentTemplate.getBody().getValidationMessages().add(new TemplateValidationMessage(
ValidationMessageLevel.WARNING, "No M2Doc version set in the template.", run));
} else if (!M2DocUtils.VERSION.equals(templateProperties.getM2DocVersion())) {
} else if (!ignoreVersionCheck && !M2DocUtils.VERSION.equals(templateProperties.getM2DocVersion())) {
documentTemplate.getBody().getValidationMessages().add(
new TemplateValidationMessage(ValidationMessageLevel.WARNING, "M2Doc version mismatch: template is "
+ templateProperties.getM2DocVersion() + " and runtime is " + M2DocUtils.VERSION, run));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2021 Obeo.
* Copyright (c) 2016, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -146,6 +146,11 @@ public final class M2DocUtils {
*/
public static final String INSTALL_CROSS_REFERENCE_ADAPTER_OPTION = "InstallCrossReferenceAdapter";

/**
* The ignore {@link #VERSION} check option.
*/
public static final String IGNORE_VERSION_CHECK_OPTION = "IgnoreVersionCheck";

/**
* The {@link List} of {@link #registerServicesConfigurator(IServicesConfiguratorDescriptor) registered}
* {@link IServicesConfiguratorDescriptor}.
Expand Down Expand Up @@ -742,8 +747,26 @@ public static DocumentTemplate parseUserContent(URIConverter uriConverter, URI d
*/
public static ValidationMessageLevel validate(DocumentTemplate documentTemplate,
IReadOnlyQueryEnvironment queryEnvironment, Monitor monitor) {
return validate(documentTemplate, queryEnvironment, false, monitor);
}

/**
* Validates the given {@link DocumentTemplate} with the given {@link IReadOnlyQueryEnvironment} and variables types.
*
* @param documentTemplate
* the {@link DocumentTemplate}
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param ignoreVersionCheck
* ignore the {@link #VERSION} check
* @param monitor
* used to track the progress will generating
* @return the {@link ValidationMessageLevel}
*/
public static ValidationMessageLevel validate(DocumentTemplate documentTemplate,
IReadOnlyQueryEnvironment queryEnvironment, boolean ignoreVersionCheck, Monitor monitor) {
final M2DocValidator validator = new M2DocValidator();
return validator.validate(documentTemplate, queryEnvironment, monitor);
return validator.validate(documentTemplate, queryEnvironment, ignoreVersionCheck, monitor);
}

/**
Expand Down Expand Up @@ -984,6 +1007,7 @@ public static Map<String, String> getInitializedOptions(Map<String, String> opti
final Map<String, String> res = new LinkedHashMap<>();

res.put(M2DocUtils.UPDATE_FIELDS_OPTION, Boolean.FALSE.toString());
res.put(M2DocUtils.IGNORE_VERSION_CHECK_OPTION, Boolean.FALSE.toString());
for (IServicesConfigurator configurator : getConfigurators()) {
res.putAll(configurator.getInitializedOptions(options));
}
Expand All @@ -1004,6 +1028,7 @@ public static List<String> getPossibleOptionNames() {
}

res.add(UPDATE_FIELDS_OPTION);
res.add(IGNORE_VERSION_CHECK_OPTION);
res.add(INSTALL_CROSS_REFERENCE_ADAPTER_OPTION);

return res;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Obeo.
* Copyright (c) 2017, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -176,11 +176,13 @@ public void initializeOptionsNotExistingOption() {

GenconfUtils.initializeOptions(generation);

assertEquals(2, generation.getOptions().size());
assertEquals(3, generation.getOptions().size());
assertEquals(M2DocUtils.UPDATE_FIELDS_OPTION, generation.getOptions().get(0).getName());
assertEquals("false", generation.getOptions().get(0).getValue());
assertEquals(TestServiceConfigurator.OPTION, generation.getOptions().get(1).getName());
assertEquals(TestServiceConfigurator.VALUE, generation.getOptions().get(1).getValue());
assertEquals(M2DocUtils.IGNORE_VERSION_CHECK_OPTION, generation.getOptions().get(1).getName());
assertEquals("false", generation.getOptions().get(1).getValue());
assertEquals(TestServiceConfigurator.OPTION, generation.getOptions().get(2).getName());
assertEquals(TestServiceConfigurator.VALUE, generation.getOptions().get(2).getValue());
}

@Test
Expand All @@ -193,11 +195,13 @@ public void initializeOptionsExistingOption() {

GenconfUtils.initializeOptions(generation);

assertEquals(2, generation.getOptions().size());
assertEquals(3, generation.getOptions().size());
assertEquals(TestServiceConfigurator.OPTION, generation.getOptions().get(0).getName());
assertEquals(TestServiceConfigurator.VALUE, generation.getOptions().get(0).getValue());
assertEquals(M2DocUtils.UPDATE_FIELDS_OPTION, generation.getOptions().get(1).getName());
assertEquals("false", generation.getOptions().get(1).getValue());
assertEquals(M2DocUtils.IGNORE_VERSION_CHECK_OPTION, generation.getOptions().get(2).getName());
assertEquals("false", generation.getOptions().get(2).getValue());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Element-Type: testcase
Element-Version: 3.0
External-Reference:
Id: _JCraAIXQEeiTHPcT0aJVUA
Runtime-Version: 2.3.0.201806262310
Save-Time: 2/11/20 1:08 PM
Runtime-Version: 2.5.4.202210020716
Save-Time: 9/13/23, 3:31 PM
Testcase-Type: ecl

------=_.content-0a7243a0-75d3-3d5f-9791-539de0e5b7ac
Expand Down Expand Up @@ -44,7 +44,7 @@ with [get-window "Generation configuration"] {
get-window "Set option name and value." | get-button OK | click
}
with [get-window "Generation configuration"] {
get-table | get-property itemCount | equals 9 | verify-true
get-table | get-property itemCount | equals 10 | verify-true
get-button Add | get-property enablement | equals false | verify-true
}
get-window "Generation configuration" | get-button Cancel | click
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Element-Type: testcase
Element-Version: 3.0
External-Reference:
Id: _DwnzUH31Eeiu8KolG1i86A
Runtime-Version: 2.3.0.201806262310
Save-Time: 2/11/20 1:16 PM
Runtime-Version: 2.5.4.202210020716
Save-Time: 9/13/23, 3:24 PM
Testcase-Type: ecl

------=_.content-0a7243a0-75d3-3d5f-9791-539de0e5b7ac
Expand All @@ -28,9 +28,11 @@ with [get-window "Generation configuration"] {
}
get-window "Generation configuration" | get-tab-folder | get-tab-item "Options (expert)" | click
with [get-window "Generation configuration"] {
get-table | get-property itemCount | equals 2 | verify-true
get-table | get-property itemCount | equals 3 | verify-true
get-table | get-item -path UpdateFields | get-property "columns[0]" | equals UpdateFields | verify-true
get-table | get-item -path UpdateFields | get-property "columns[1]" | equals "false" | verify-true
get-table | get-item -path IgnoreVersionCheck | get-property "columns[0]" | equals IgnoreVersionCheck | verify-true
get-table | get-item -path IgnoreVersionCheck | get-property "columns[1]" | equals "false" | verify-true
get-button Add | get-property enablement | equals true | verify-true
get-button Edit | get-property enablement | equals false | verify-true
get-button Remove | get-property enablement | equals false | verify-true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Element-Type: testcase
Element-Version: 3.0
External-Reference:
Id: _jODMwH9kEei7rrjWYcYqJw
Runtime-Version: 2.3.0.201806262310
Save-Time: 2/11/20 1:08 PM
Runtime-Version: 2.5.4.202210020716
Save-Time: 9/13/23, 3:32 PM
Testcase-Type: ecl

------=_.content-0a7243a0-75d3-3d5f-9791-539de0e5b7ac
Expand Down Expand Up @@ -36,6 +36,6 @@ with [get-window "Generation configuration"] {
get-table | select SiriusSession
get-button Remove | click
}
get-window "Generation configuration" | get-table | get-property itemCount | equals 1 | verify-true
get-window "Generation configuration" | get-table | get-property itemCount | equals 2 | verify-true
get-window "Generation configuration" | get-button Cancel | click
------=_.content-0a7243a0-75d3-3d5f-9791-539de0e5b7ac--
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Element-Version: 3.0
External-Reference:
Id: _hosBgEzBEe6RxPWQEeDEcQ
Runtime-Version: 2.5.4.202210020716
Save-Time: 9/6/23, 4:31 PM
Save-Time: 9/13/23, 3:20 PM
Testcase-Type: ecl

------=_.content-0a7243a0-75d3-3d5f-9791-539de0e5b7ac
Expand Down Expand Up @@ -41,5 +41,6 @@ get-editor "asImageByRepresentationDescriptionName.genconf" | get-text-viewer |
+ " </definitions>\n"
+ " <options name=\"SiriusSession\" value=\"asImageByRepresentationDescriptionName.aird\"/>\n"
+ " <options name=\"UpdateFields\" value=\"false\"/>\n"
+ " <options name=\"IgnoreVersionCheck\" value=\"false\"/>\n"
+ "</genconf:Generation>" | verify-true
------=_.content-0a7243a0-75d3-3d5f-9791-539de0e5b7ac--
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Element-Type: testcase
Element-Version: 3.0
External-Reference:
Id: _uSzOkH3uEeii_sZyBcvLmQ
Runtime-Version: 2.3.0.201806262310
Save-Time: 2/11/20 1:07 PM
Runtime-Version: 2.5.4.202210020716
Save-Time: 9/13/23, 3:26 PM
Testcase-Type: ecl

------=_.content-0a7243a0-75d3-3d5f-9791-539de0e5b7ac
Expand All @@ -28,7 +28,7 @@ with [get-window "Generation configuration"] {
}
get-window "Generation configuration" | get-tab-folder | get-tab-item "Options (expert)" | click
with [get-window "Generation configuration"] {
get-table | get-property itemCount | equals 1 | verify-true
get-table | get-property itemCount | equals 2 | verify-true
get-button Add | get-property enablement | equals true | verify-true
get-button Edit | get-property enablement | equals false | verify-true
get-button Remove | get-property enablement | equals false | verify-true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Element-Version: 3.0
External-Reference:
Id: _kz6UQH3YEeiOZoLGvms2Lg
Runtime-Version: 2.5.4.202210020716
Save-Time: 2/13/23, 10:17 AM
Save-Time: 9/13/23, 4:28 PM
Testcase-Type: ecl

------=_.content-0a7243a0-75d3-3d5f-9791-539de0e5b7ac
Expand All @@ -32,7 +32,7 @@ with [get-window -class WizardDialog] {
}
get-window -class WizardDialog | get-tab-folder | get-tab-item "Options (expert)" | click
with [get-window -class WizardDialog] {
get-table | get-property itemCount | equals 1 | verify-true
get-table | get-property itemCount | equals 2 | verify-true
get-button Add | get-property enablement | equals true | verify-true
get-button Edit | get-property enablement | equals false | verify-true
get-button Remove | get-property enablement | equals false | verify-true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

=== HEADER ===

=== BODY ===

A simple demonstration of a static text :Some text.End of demonstration.
=== FOOTER ===

=== TEMPLATES ===
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="wrongM2DocVersion" templateFileName="wrongM2DocVersion-template.docx" resultFileName="wrongM2DocVersion-actual-generation.docx" validationFileName="wrongM2DocVersion-actual-validation.docx">
<options name="IgnoreVersionCheck" value="true"/>
</genconf:Generation>
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,10 @@ public void parsing() throws FileNotFoundException, IOException {
*/
@Test
public void validation() throws IOException, DocumentGenerationException {
final boolean ignoreVersionCheck = Boolean
.valueOf(GenconfUtils.getOptions(generation).get(M2DocUtils.IGNORE_VERSION_CHECK_OPTION));
final ValidationMessageLevel validationLevel = M2DocUtils.validate(documentTemplate, queryEnvironment,
new BasicMonitor());
ignoreVersionCheck, new BasicMonitor());

final URI expectedValidationURI = getExpectedValidatedURI(new File(testFolderPath));
final URI outputURI;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Obeo.
* Copyright (c) 2017, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -151,8 +151,9 @@ public void getInitializedOptions() {

final Map<String, String> initializedOptions = M2DocUtils.getInitializedOptions(options);

assertEquals(2, initializedOptions.size());
assertEquals(3, initializedOptions.size());
assertEquals("false", initializedOptions.get(M2DocUtils.UPDATE_FIELDS_OPTION));
assertEquals("false", initializedOptions.get(M2DocUtils.IGNORE_VERSION_CHECK_OPTION));
assertEquals(TestServiceConfigurator.VALUE, initializedOptions.get(TestServiceConfigurator.OPTION));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016 Obeo.
* Copyright (c) 2016, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -73,7 +73,7 @@ public void conditionalInferedTypeInElse() throws IOException {
selfTypes.add(new EClassifierType(queryEnvironment, EcorePackage.eINSTANCE.getEClassifier()));
selfTypes.add(new EClassifierType(queryEnvironment, EcorePackage.eINSTANCE.getEPackage()));

validator.validate(documentTemplate, queryEnvironment, new BasicMonitor());
validator.validate(documentTemplate, queryEnvironment, false, new BasicMonitor());

assertEquals(0, conditional.getValidationMessages().size());

Expand Down Expand Up @@ -104,7 +104,7 @@ public void tableRowCellTemplate() {
final DocumentTemplate documentTemplate = M2DocTestUtils.createDocumentTemplate(body);

final M2DocValidator validator = new M2DocValidator();
validator.validate(documentTemplate, queryEnvironment, new BasicMonitor());
validator.validate(documentTemplate, queryEnvironment, false, new BasicMonitor());

assertEquals(1, query.getValidationMessages().size());
assertTemplateValidationMessage(query.getValidationMessages().get(0), ValidationMessageLevel.ERROR,
Expand Down

0 comments on commit 3b6bde0

Please sign in to comment.