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

Commit

Permalink
Merge pull request #134 from metafacture/79-throwExceptionForInvalidF…
Browse files Browse the repository at this point in the history
…ixFile

Throw exception for invalid Fix files.
  • Loading branch information
blackwinter authored Feb 9, 2022
2 parents 839bd27 + 29f416e commit ffa65cb
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 23 deletions.
11 changes: 2 additions & 9 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.metafacture.metamorph.api.Maps;
import org.metafacture.metamorph.maps.FileMap;

import java.io.FileNotFoundException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -68,14 +67,8 @@ public void apply(final Metafix metafix, final Record record, final List<String>
}

final RecordTransformer recordTransformer = metafix.getRecordTransformer();
recordTransformer.setRecord(recordTransformer.transformRecord(INCLUDE_FIX.computeIfAbsent(includePath, k -> {
try {
return FixStandaloneSetup.parseFix(Metafix.fixReader(k));
}
catch (final FileNotFoundException e) {
throw new MetafactureException(e);
}
})));
recordTransformer.setRecord(recordTransformer.transformRecord(
INCLUDE_FIX.computeIfAbsent(includePath, FixStandaloneSetup::parseFix)));
}
},
nothing {
Expand Down
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
Expand Up @@ -27,17 +27,17 @@ public static void main(final String[] args) {
throw new IllegalArgumentException(String.format("Usage: %s <fix-file>", FixStandaloneSetup.class.getName()));
}

public static Fix parseFix(final Reader fixDef) {
final String path;
public static Fix parseFix(final String path) {
return (Fix) XtextValidator.getValidatedResource(path, new FixStandaloneSetup()).getContents().get(0);
}

public static Fix parseFix(final Reader fixDef) {
try {
path = absPathToTempFile(fixDef, ".fix");
return parseFix(absPathToTempFile(fixDef, ".fix"));
}
catch (final IOException e) {
throw new MetafactureException(e);
}

return (Fix) XtextValidator.getValidatedResource(path, new FixStandaloneSetup()).getContents().get(0);
}

public static String absPathToTempFile(final Reader fixDef, final String suffix) throws IOException {
Expand Down
11 changes: 5 additions & 6 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
Expand Down Expand Up @@ -103,10 +102,14 @@ public Metafix(final String fixDef) throws FileNotFoundException {
}

public Metafix(final String fixDef, final Map<String, String> vars) throws FileNotFoundException {
this(fixReader(fixDef), vars);
this(vars);

if (isFixFile(fixDef)) {
fixFile = fixDef;
fix = FixStandaloneSetup.parseFix(fixDef);
}
else {
fix = FixStandaloneSetup.parseFix(new StringReader(fixDef));
}
}

Expand All @@ -119,10 +122,6 @@ public Metafix(final Reader fixDef, final Map<String, String> vars) {
fix = FixStandaloneSetup.parseFix(fixDef);
}

/*package-private*/ static Reader fixReader(final String fixDef) throws FileNotFoundException {
return isFixFile(fixDef) ? new FileReader(fixDef) : new StringReader(fixDef);
}

/*package-private*/ static boolean isFixFile(final String fixDef) {
return fixDef.endsWith(FIX_EXTENSION);
}
Expand Down
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 @@
lookup("license.id", ,"data/maps/duepublico-licenses.tsv","sep_char":"\t")

0 comments on commit ffa65cb

Please sign in to comment.