diff --git a/build.gradle b/build.gradle index 34644a62..9d9f5d51 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ plugins { } group = 'com.josdem.jmetadata' -version = '1.3.8' +version = '1.3.9' java { toolchain { diff --git a/src/main/java/com/josdem/jmetadata/metadata/MetadataWriter.java b/src/main/java/com/josdem/jmetadata/metadata/MetadataWriter.java index 660584f4..ff791ada 100644 --- a/src/main/java/com/josdem/jmetadata/metadata/MetadataWriter.java +++ b/src/main/java/com/josdem/jmetadata/metadata/MetadataWriter.java @@ -59,7 +59,7 @@ public void setFile(File file) { | TagException | ReadOnlyFileException | InvalidAudioFrameException nre) { - log.error(nre.getMessage(), nre); + throw new BusinessException(nre.getMessage()); } } diff --git a/src/main/java/com/josdem/jmetadata/util/MethodWrapper.java b/src/main/java/com/josdem/jmetadata/util/MethodWrapper.java deleted file mode 100644 index dbeeffeb..00000000 --- a/src/main/java/com/josdem/jmetadata/util/MethodWrapper.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - Copyright 2025 Jose Morales contact@josdem.io - - 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 com.josdem.jmetadata.util; - -import java.lang.reflect.Method; -import lombok.extern.slf4j.Slf4j; - -@Slf4j -public class MethodWrapper { - private final Method method; - private final Class returnType; - - public static MethodWrapperBuilderPhase1 forClass(String className) { - return new MethodWrapperBuilder(className); - } - - public static MethodWrapperBuilderPhase1 forClass(Class clazz) { - return new MethodWrapperBuilder(clazz); - } - - private MethodWrapper(Method method, Class returnType) { - this.method = method; - this.returnType = returnType; - } - - private MethodWrapper check() { - return this; - } - - public static Class getClass(String string) { - try { - return Class.forName(string); - } catch (ClassNotFoundException e) { - log.error(e.getMessage(), e); - } - return null; - } - - public T invoke(Object... param) { - return new UsingMethodWrapper(null).invoke(param); - } - - public UsingMethodWrapper using(Object object) { - return new UsingMethodWrapper(object); - } - - public class UsingMethodWrapper { - private final Object object; - - private UsingMethodWrapper(Object object) { - this.object = object; - } - - @SuppressWarnings("unchecked") - public X invoke(Object... param) { - try { - if (returnType == null) { - method.invoke(object, param); - return null; - } - return (X) method.invoke(object, param); - } catch (Exception e) { - log.error( - e.getMessage(), new RuntimeException("The method has just exploded in your face")); - return null; - } - } - } - - public static interface MethodWrapperBuilderPhase1 { - MethodWrapperBuilderPhase2 method(String methodName); - } - - public static interface MethodWrapperBuilderPhase2 { - MethodWrapperBuilderPhase3 withParameters(Class... parameters); - } - - public static interface MethodWrapperBuilderPhase3 { - MethodWrapper andReturnType(Class returnType); - } - - private static class MethodWrapperBuilder - implements MethodWrapperBuilderPhase1, - MethodWrapperBuilderPhase2, - MethodWrapperBuilderPhase3 { - private final Class clazz; - private String methodName; - private Class[] parameters; - - private MethodWrapperBuilder(Class clazz) { - this.clazz = clazz; - } - - private MethodWrapperBuilder(String className) { - try { - clazz = Class.forName(className); - } catch (ClassNotFoundException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(className + " does not exist"); - } - } - - public MethodWrapperBuilder method(String methodName) { - this.methodName = methodName; - return this; - } - - public MethodWrapperBuilder withParameters(Class... parameters) { - this.parameters = parameters; - return this; - } - - public MethodWrapper andReturnType(Class returnType) { - Method method; - try { - method = clazz.getDeclaredMethod(methodName, parameters); - if (method.getReturnType().equals(returnType) - || (returnType == null && method.getReturnType().equals(void.class))) { - return new MethodWrapper(method, returnType).check(); - } - } catch (SecurityException e) { - log.error(e.getMessage(), e); - } catch (NoSuchMethodException e) { - log.error(e.getMessage(), e); - } - throw new RuntimeException("Method " + methodName + " does not exist"); - } - } -} diff --git a/src/test/java/com/josdem/jmetadata/controller/CompleteControllerTest.java b/src/test/java/com/josdem/jmetadata/controller/CompleteControllerTest.java index 78ea247e..2df7f7ab 100644 --- a/src/test/java/com/josdem/jmetadata/controller/CompleteControllerTest.java +++ b/src/test/java/com/josdem/jmetadata/controller/CompleteControllerTest.java @@ -33,7 +33,6 @@ import java.awt.Image; import java.io.File; import java.util.List; - import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; diff --git a/src/test/java/com/josdem/jmetadata/metadata/MetadataWriterTest.java b/src/test/java/com/josdem/jmetadata/metadata/MetadataWriterTest.java index 66dba6d9..6ea5c072 100644 --- a/src/test/java/com/josdem/jmetadata/metadata/MetadataWriterTest.java +++ b/src/test/java/com/josdem/jmetadata/metadata/MetadataWriterTest.java @@ -30,6 +30,7 @@ import com.josdem.jmetadata.util.ImageUtils; import java.awt.Image; import java.io.File; +import java.io.IOException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.jaudiotagger.audio.AudioFile; @@ -66,6 +67,15 @@ void setup() throws Exception { metadataWriter.setFile(file); } + @Test + @DisplayName("not setting file due to exception") + void shouldNotSetFile(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + + doThrow(IOException.class).when(audioFileHelper).read(file); + assertThrows(BusinessException.class, () -> metadataWriter.setFile(file)); + } + @Test @DisplayName("writing artist") void shouldWriteArtist(TestInfo testInfo) throws Exception {