Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Throw exception for invalid Fix files. (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Feb 7, 2022
1 parent 1c34951 commit a9d3bb9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2022 hbz NRW
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.metafacture.metafix;

import org.metafacture.framework.MetafactureException;

public class FixParseException extends MetafactureException {

public FixParseException(final String message) {
super(message);
}

public FixParseException(final String message, final Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.metafacture.metafix.validation;

import org.metafacture.metafix.FixParseException;

import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.ISetup;
import org.eclipse.xtext.XtextStandaloneSetup;
Expand Down Expand Up @@ -31,7 +33,7 @@ private static boolean validate(final XtextResource resource, final ISetup setup

if (count > 0) {
LOG.warn("The {} file '{}' has {} issue{}:",
setup.getClass().getSimpleName(), resource.getURI().toFileString(), count, count > 1 ? "s" : "");
resourceType(setup), resource.getURI().toFileString(), count, count > 1 ? "s" : "");

issues.forEach(i -> LOG.warn("- {}: {} ({}:{})",
i.getSeverity(), i.getMessage(), i.getLineNumber(), i.getColumn()));
Expand Down Expand Up @@ -63,8 +65,17 @@ private static XtextResource getResource(final String path, final ISetup setup)

public static XtextResource getValidatedResource(final String path, final ISetup setup) {
final XtextResource resource = getResource(path, setup);
validate(resource, setup);
return resource;

if (validate(resource, setup)) {
return resource;
}
else {
throw new FixParseException("Invalid " + resourceType(setup) + " resource: " + path);
}
}

private static String resourceType(final ISetup setup) {
return setup.getClass().getSimpleName();
}

public static void main(final String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,13 @@ public void shouldGetDefaultValueForUnknownKey() {
Assertions.assertEquals(VALUE, metafix.getValue(MAP_NAME, KEY));
}

@Test
// See https://github.com/metafacture/metafacture-fix/issues/79
public void shouldThrowExceptionForInvalidFixFile() {
final String fixFile = "src/test/resources/org/metafacture/metafix/fixes/invalid.fix";
MetafixTestHelpers.assertThrows(FixParseException.class, "Invalid FixStandaloneSetup resource: " + fixFile, () -> new Metafix(fixFile));

// TODO: Test logging statements
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -----------license----------
copy_field("metadata.mods:mods.mods:accessCondition.xlink:href","license.id")
lookup("license.id", ,"data/maps/duepublico-licenses.tsv","sep_char":"\t")

0 comments on commit a9d3bb9

Please sign in to comment.