Why does jmimemagic throw exception with sql files?

0

Hi, I work with Play Framework 1.4 and Java, I have a Test class with UnitTest that tests a class that validates files with jmimemagic, but when validating files with an extension sql throws the exception MagicMatchNotFoundException

Why does jmimemagic throw this exception with the sql files?

net.sf.jmimemagic.MagicMatchNotFoundException
at net.sf.jmimemagic.Magic.getMagicMatch(Magic.java:368)
at net.sf.jmimemagic.Magic.getMagicMatch(Magic.java:240)
at sui.validateFileTest$Utils.validateFile(validateFileTest.java:41)
at sui.validateFileTest.validateFileFormatInvalidTest(validateFileTest.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at play.test.PlayJUnitRunner$StartPlay$2$1.evaluate(PlayJUnitRunner.java:128)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:70)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.runner.JUnitCore.run(JUnitCore.java:105)
at org.junit.runner.JUnitCore.run(JUnitCore.java:94)
at play.test.TestEngine.run(TestEngine.java:188)
at controllers.TestRunner$1.doJobWithResult(TestRunner.java:101)
at controllers.TestRunner$1.doJobWithResult(TestRunner.java:1)
at play.jobs.Job$2.apply(Job.java:208)
at play.db.jpa.JPA.withTransaction(JPA.java:258)
at play.db.jpa.JPA.withinFilter(JPA.java:217)
at play.db.jpa.JPAPlugin$TransactionalFilter.withinFilter(JPAPlugin.java:298)
at play.jobs.Job.withinFilter(Job.java:185)
at play.jobs.Job.call(Job.java:204)
at play.jobs.Job$1.call(Job.java:119)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)

Utils class

import java.io.File;
import java.util.Arrays;
import java.util.List;

public static class Utils {

public static List<String> getFormats() {

    String[] formats = { "pdf", "doc", "docx", "csv", "xls", "xlsx", "odt", "jpg", "png", "jpeg" };

    List<String> list = Arrays.asList(formats);

    return list;

}

public static void validateFile(Validation validation, File file)
        throws MagicParseException, MagicMatchNotFoundException, MagicException {

    if (file == null)
        throw new IllegalArgumentException("File is null");

    Magic magic = new Magic();
    MagicMatch match = magic.getMagicMatch(file, false);

    if (file.length() == 0)
        validation.addError("file", "File is empty");

    if (!getFormats().contains(match.getExtension()))
        validation.addError("file", "Format not valid");

}
}

Test class

import static org.hamcrest.CoreMatchers.is;
import java.io.File;
import org.junit.Before;
import org.junit.Test;
import net.sf.jmimemagic.Magic;
import net.sf.jmimemagic.MagicException;
import net.sf.jmimemagic.MagicMatch;
import net.sf.jmimemagic.MagicMatchNotFoundException;
import net.sf.jmimemagic.MagicParseException;
import play.data.validation.Validation;
import play.test.UnitTest;

public class validateFileTest extends UnitTest {

private Validation validation;

@Before
public void init() {

validation = Validation.current();
validation.clear();
}

@Test
public void validateFileFormatInvalidTest()
    throws MagicParseException, MagicMatchNotFoundException, MagicException {

File file = new File("./docs/file-sql.sql");
Utils.validateFile(validation, file);

assertThat(validation.hasErrors(), is(true));
}

@Test
public void validateFileFormatValidTest() throws MagicParseException, MagicMatchNotFoundException, MagicException {

File file = new File("./docs/file-pdf.pdf");
Utils.validateFile(validation, file);

assertThat(validation.hasErrors(), is(false));

}

@Test(expected = IllegalArgumentException.class)
public void validateFileNullTest() throws MagicParseException, MagicMatchNotFoundException, MagicException {

Utils.validateFile(validation, null);

}

@Test
public void validateFileEmptyTest() throws MagicParseException, MagicMatchNotFoundException, MagicException {

File file = new File("./docs/file-empty.docx");
Utils.validateFile(validation, file);

assertThat(validation.hasErrors(), is(false));
}
} 
    
asked by David Navarro Astudillo 14.07.2017 в 18:09
source

1 answer

0

Jmimemagic does not support some extensions of files reason why this exception MagicMatchNotFoundException throws, to solve it I had to enclose in a multicatch method "magic.getMagicMatch (file, false)" to capture the exception and add the error to the validations of the field "file".

public static class Utils {

    public static List<String> getFormats() {

        String[] formats = { "pdf", "doc", "docx", "csv", "xls", "xlsx", "odt", "jpg", "png", "jpeg" };

        List<String> list = Arrays.asList(formats);

        return list;

    }

    public static void validateFile(Validation validation, File file) {

        if (file == null)
            throw new IllegalArgumentException("File is null");

        Magic magic = new Magic();
        MagicMatch match;
        try {

            match = magic.getMagicMatch(file, false);

            if (file.length() == 0)
                validation.addError("file", "File is empty");

            if (!getFormats().contains(match.getExtension()))
                validation.addError("file", "Format not valid");

        } catch (MagicParseException | MagicMatchNotFoundException | MagicException e) {
            validation.addError("file", "Format not valid");
        }

    }
}
    
answered by 27.07.2017 / 16:35
source