diff --git a/build.gradle b/build.gradle index feeeb39..34644a6 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ plugins { } group = 'com.josdem.jmetadata' -version = '1.3.7' +version = '1.3.8' java { toolchain { diff --git a/src/main/java/com/josdem/jmetadata/controller/CompleteController.java b/src/main/java/com/josdem/jmetadata/controller/CompleteController.java index 2762d13..950cac8 100644 --- a/src/main/java/com/josdem/jmetadata/controller/CompleteController.java +++ b/src/main/java/com/josdem/jmetadata/controller/CompleteController.java @@ -18,7 +18,7 @@ import com.josdem.jmetadata.action.ActionResult; import com.josdem.jmetadata.action.Actions; -import com.josdem.jmetadata.exception.MetadataException; +import com.josdem.jmetadata.exception.BusinessException; import com.josdem.jmetadata.helper.CoverArtRetrofitHelper; import com.josdem.jmetadata.helper.RetrofitHelper; import com.josdem.jmetadata.metadata.MetadataWriter; @@ -145,8 +145,8 @@ public synchronized ActionResult completeAlbum(Metadata metadata) { metadata.setCoverArt(coverArtNew.getImage()); } return ActionResult.Updated; - } catch (MetadataException mde) { - log.error(mde.getMessage(), mde); + } catch (BusinessException bse) { + log.error(bse.getMessage(), bse); return ActionResult.Error; } } diff --git a/src/main/java/com/josdem/jmetadata/gui/MainWindow.java b/src/main/java/com/josdem/jmetadata/gui/MainWindow.java index 4019549..cdc0c2a 100644 --- a/src/main/java/com/josdem/jmetadata/gui/MainWindow.java +++ b/src/main/java/com/josdem/jmetadata/gui/MainWindow.java @@ -36,6 +36,8 @@ import com.josdem.jmetadata.model.User; import com.josdem.jmetadata.observer.ObservValue; import com.josdem.jmetadata.observer.Observer; +import com.josdem.jmetadata.service.impl.ImageServiceImpl; +import com.josdem.jmetadata.util.FileUtils; import com.josdem.jmetadata.util.ImageUtils; import java.awt.Image; import java.awt.Rectangle; @@ -85,10 +87,6 @@ import org.springframework.beans.factory.annotation.Autowired; @Slf4j - -/** - * @understands A principal JAudioScrobbler principal window - */ @SuppressWarnings("unused") public class MainWindow extends JFrame { private static final long serialVersionUID = 1311230231101552007L; @@ -168,7 +166,6 @@ public class MainWindow extends JFrame { private JMenuItem exitMenuItem; private InputMap inputMap; private JScrollPane scrollPane; - private ImageUtils imageUtils = new ImageUtils(); private Set metadataWithAlbum = new HashSet(); private MetadataAdapter metadataAdapter = new MetadataAdapter(); private DialogHelper dialogHelper = new DialogHelper(); @@ -184,6 +181,7 @@ public class MainWindow extends JFrame { public MainWindow() { super(ApplicationConstants.APPLICATION_NAME); + ImageUtils imageUtils = new ImageUtils(new ImageServiceImpl(), new FileUtils()); initialize(); getDescriptionTable().getModel().addTableModelListener(new DescriptionTableModelListener()); } diff --git a/src/main/java/com/josdem/jmetadata/gui/MetadataDialog.java b/src/main/java/com/josdem/jmetadata/gui/MetadataDialog.java index ca8c0bc..636f0a6 100644 --- a/src/main/java/com/josdem/jmetadata/gui/MetadataDialog.java +++ b/src/main/java/com/josdem/jmetadata/gui/MetadataDialog.java @@ -27,6 +27,8 @@ import com.josdem.jmetadata.model.Model; import com.josdem.jmetadata.observer.ObservValue; import com.josdem.jmetadata.observer.Observer; +import com.josdem.jmetadata.service.impl.ImageServiceImpl; +import com.josdem.jmetadata.util.FileUtils; import com.josdem.jmetadata.util.ImageUtils; import java.awt.Image; import java.awt.Rectangle; @@ -122,7 +124,7 @@ public class MetadataDialog extends AllDialog { private JLabel cdsLabel; private JPanel imagePanel; private Image coverArt; - private ImageUtils imageUtils = new ImageUtils(); + private final transient ImageUtils imageUtils; private MetadataHelper metadataHelper = new MetadataHelper(); private MetadataCollaborator metadataCollaborator = new MetadataCollaborator(); private final ControlEngineConfigurator configurator; @@ -132,6 +134,7 @@ public MetadataDialog( super(frame, true, message); this.configurator = controlEngineConfigurator; this.message = message; + imageUtils = new ImageUtils(new ImageServiceImpl(), new FileUtils()); List metadatas = controlEngineConfigurator.getControlEngine().get(Model.METADATA); metadataCollaborator.setMetadatas(metadatas); initializeContentPane(); diff --git a/src/main/java/com/josdem/jmetadata/helper/ImageExporter.java b/src/main/java/com/josdem/jmetadata/helper/ImageExporter.java index 90933c9..b54b97a 100644 --- a/src/main/java/com/josdem/jmetadata/helper/ImageExporter.java +++ b/src/main/java/com/josdem/jmetadata/helper/ImageExporter.java @@ -24,19 +24,19 @@ import java.io.File; import java.io.IOException; import java.util.List; +import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.jaudiotagger.audio.exceptions.CannotReadException; import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; import org.jaudiotagger.tag.TagException; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service +@RequiredArgsConstructor public class ImageExporter { - @Autowired private MetadataService metadataService; - - private ImageUtils imageUtils = new ImageUtils(); + private final ImageUtils imageUtils; + private final MetadataService metadataService; public void export(ExportPackage exportPackage) throws IOException, diff --git a/src/main/java/com/josdem/jmetadata/metadata/MetadataWriter.java b/src/main/java/com/josdem/jmetadata/metadata/MetadataWriter.java index 9aee5a1..660584f 100644 --- a/src/main/java/com/josdem/jmetadata/metadata/MetadataWriter.java +++ b/src/main/java/com/josdem/jmetadata/metadata/MetadataWriter.java @@ -16,13 +16,14 @@ package com.josdem.jmetadata.metadata; -import com.josdem.jmetadata.exception.MetadataException; +import com.josdem.jmetadata.exception.BusinessException; import com.josdem.jmetadata.helper.ArtworkHelper; import com.josdem.jmetadata.helper.AudioFileHelper; import com.josdem.jmetadata.util.ImageUtils; import java.awt.Image; import java.io.File; import java.io.IOException; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.jaudiotagger.audio.AudioFile; @@ -40,27 +41,25 @@ @Slf4j @Service +@RequiredArgsConstructor public class MetadataWriter { private Tag tag; private AudioFile audioFile; - private AudioFileHelper audioFileIOHelper = new AudioFileHelper(); - private ImageUtils imageUtils = new ImageUtils(); - private ArtworkHelper artworkHelper = new ArtworkHelper(); + + private final ImageUtils imageUtils; + private final AudioFileHelper audioFileIOHelper; + private final ArtworkHelper artworkHelper; public void setFile(File file) { try { audioFile = audioFileIOHelper.read(file); tag = audioFile.getTag(); - } catch (CannotReadException nre) { + } catch (CannotReadException + | IOException + | TagException + | ReadOnlyFileException + | InvalidAudioFrameException nre) { log.error(nre.getMessage(), nre); - } catch (IOException ioe) { - log.error(ioe.getMessage(), ioe); - } catch (TagException tae) { - log.error(tae.getMessage(), tae); - } catch (ReadOnlyFileException roe) { - log.error(roe.getMessage(), roe); - } catch (InvalidAudioFrameException iae) { - log.error(iae.getMessage(), iae); } } @@ -68,12 +67,8 @@ public void writeArtist(String artist) { try { tag.setField(FieldKey.ARTIST, artist); audioFile.commit(); - } catch (KeyNotFoundException kne) { - log.error(kne.getMessage(), kne); - } catch (FieldDataInvalidException fie) { - log.error(fie.getMessage(), fie); - } catch (CannotWriteException nwe) { - log.error(nwe.getMessage(), nwe); + } catch (KeyNotFoundException | FieldDataInvalidException | CannotWriteException kne) { + throw new BusinessException(kne.getMessage()); } } @@ -81,30 +76,22 @@ public void writeTitle(String trackName) { try { tag.setField(FieldKey.TITLE, trackName); audioFile.commit(); - } catch (KeyNotFoundException kne) { - log.error(kne.getMessage(), kne); - } catch (FieldDataInvalidException fie) { - log.error(fie.getMessage(), fie); - } catch (CannotWriteException nwe) { - log.error(nwe.getMessage(), nwe); + } catch (KeyNotFoundException | FieldDataInvalidException | CannotWriteException kne) { + throw new BusinessException(kne.getMessage()); } } - public boolean writeAlbum(String album) throws MetadataException { + public boolean writeAlbum(String album) { try { tag.setField(FieldKey.ALBUM, album); audioFile.commit(); return true; - } catch (KeyNotFoundException kne) { - throw new MetadataException(kne.getMessage()); - } catch (FieldDataInvalidException fie) { - throw new MetadataException(fie.getMessage()); - } catch (CannotWriteException nwe) { - throw new MetadataException(nwe.getMessage()); + } catch (KeyNotFoundException | FieldDataInvalidException | CannotWriteException kne) { + throw new BusinessException(kne.getMessage()); } } - public boolean writeTrackNumber(String trackNumber) throws MetadataException { + public boolean writeTrackNumber(String trackNumber) { try { if (StringUtils.isEmpty(trackNumber)) { return false; @@ -112,16 +99,12 @@ public boolean writeTrackNumber(String trackNumber) throws MetadataException { tag.setField(FieldKey.TRACK, trackNumber); audioFile.commit(); return true; - } catch (KeyNotFoundException kne) { - throw new MetadataException(kne.getMessage()); - } catch (FieldDataInvalidException fie) { - throw new MetadataException(fie.getMessage()); - } catch (CannotWriteException nwe) { - throw new MetadataException(nwe.getMessage()); + } catch (KeyNotFoundException | FieldDataInvalidException | CannotWriteException kne) { + throw new BusinessException(kne.getMessage()); } } - public boolean writeTotalTracksNumber(String totalTracksNumber) throws MetadataException { + public boolean writeTotalTracksNumber(String totalTracksNumber) { try { if (StringUtils.isEmpty(totalTracksNumber)) { return false; @@ -129,49 +112,38 @@ public boolean writeTotalTracksNumber(String totalTracksNumber) throws MetadataE tag.setField(FieldKey.TRACK_TOTAL, totalTracksNumber); audioFile.commit(); return true; - } catch (KeyNotFoundException kne) { - throw new MetadataException(kne.getMessage()); - } catch (FieldDataInvalidException fie) { - throw new MetadataException(fie.getMessage()); - } catch (CannotWriteException nwe) { - throw new MetadataException(nwe.getMessage()); + } catch (KeyNotFoundException | FieldDataInvalidException | CannotWriteException kne) { + throw new BusinessException(kne.getMessage()); } } - public boolean writeCoverArt(Image lastfmCoverArt) throws MetadataException { + public void writeCoverArt(Image lastfmCoverArt) { try { File coverArtFile = imageUtils.saveCoverArtToFile(lastfmCoverArt); Artwork artwork = artworkHelper.createArtwork(); artwork.setFromFile(coverArtFile); tag.setField(artwork); audioFile.commit(); - return true; - } catch (KeyNotFoundException kne) { - throw new MetadataException(kne.getMessage()); - } catch (FieldDataInvalidException fie) { - throw new MetadataException(fie.getMessage()); - } catch (CannotWriteException nwe) { - throw new MetadataException(nwe.getMessage()); - } catch (IOException ioe) { - throw new MetadataException(ioe.getMessage()); - } catch (NullPointerException nue) { - throw new MetadataException(nue.getMessage()); + } catch (KeyNotFoundException + | FieldDataInvalidException + | CannotWriteException + | IOException + | NullPointerException kne) { + throw new BusinessException(kne.getMessage()); } } - public boolean removeCoverArt() throws MetadataException { + public boolean removeCoverArt() { try { tag.deleteArtworkField(); audioFile.commit(); return true; - } catch (KeyNotFoundException kne) { - throw new MetadataException(kne.getMessage()); - } catch (CannotWriteException nwe) { - throw new MetadataException(nwe.getMessage()); + } catch (KeyNotFoundException | CannotWriteException kne) { + throw new BusinessException(kne.getMessage()); } } - public boolean writeCdNumber(String cdNumber) throws MetadataException { + public boolean writeCdNumber(String cdNumber) { try { if (StringUtils.isEmpty(cdNumber)) { return false; @@ -179,18 +151,15 @@ public boolean writeCdNumber(String cdNumber) throws MetadataException { tag.setField(FieldKey.DISC_NO, cdNumber); audioFile.commit(); return true; - } catch (KeyNotFoundException kne) { - throw new MetadataException(kne.getMessage()); - } catch (FieldDataInvalidException fie) { - throw new MetadataException(fie.getMessage()); - } catch (CannotWriteException nwe) { - throw new MetadataException(nwe.getMessage()); - } catch (NullPointerException nue) { - throw new MetadataException(nue.getMessage()); + } catch (KeyNotFoundException + | FieldDataInvalidException + | CannotWriteException + | NullPointerException kne) { + throw new BusinessException(kne.getMessage()); } } - public boolean writeTotalCds(String totalCds) throws MetadataException { + public boolean writeTotalCds(String totalCds) { try { if (StringUtils.isEmpty(totalCds)) { return false; @@ -198,18 +167,15 @@ public boolean writeTotalCds(String totalCds) throws MetadataException { tag.setField(FieldKey.DISC_TOTAL, totalCds); audioFile.commit(); return true; - } catch (KeyNotFoundException kne) { - throw new MetadataException(kne.getMessage()); - } catch (FieldDataInvalidException fie) { - throw new MetadataException(fie.getMessage()); - } catch (CannotWriteException nwe) { - throw new MetadataException(nwe.getMessage()); - } catch (NullPointerException nue) { - throw new MetadataException(nue.getMessage()); + } catch (KeyNotFoundException + | FieldDataInvalidException + | CannotWriteException + | NullPointerException kne) { + throw new BusinessException(kne.getMessage()); } } - public boolean writeYear(String year) throws MetadataException { + public boolean writeYear(String year) { try { if (StringUtils.isEmpty(year)) { return false; @@ -217,18 +183,15 @@ public boolean writeYear(String year) throws MetadataException { tag.setField(FieldKey.YEAR, year); audioFile.commit(); return true; - } catch (KeyNotFoundException kne) { - throw new MetadataException(kne.getMessage()); - } catch (FieldDataInvalidException fie) { - throw new MetadataException(fie.getMessage()); - } catch (CannotWriteException nwe) { - throw new MetadataException(nwe.getMessage()); - } catch (NullPointerException nue) { - throw new MetadataException(nue.getMessage()); + } catch (KeyNotFoundException + | FieldDataInvalidException + | CannotWriteException + | NullPointerException kne) { + throw new BusinessException(kne.getMessage()); } } - public boolean writeGenre(String genre) throws MetadataException { + public boolean writeGenre(String genre) { try { if (StringUtils.isEmpty(genre)) { return false; @@ -236,14 +199,11 @@ public boolean writeGenre(String genre) throws MetadataException { tag.setField(FieldKey.GENRE, genre); audioFile.commit(); return true; - } catch (KeyNotFoundException kne) { - throw new MetadataException(kne.getMessage()); - } catch (FieldDataInvalidException fie) { - throw new MetadataException(fie.getMessage()); - } catch (CannotWriteException nwe) { - throw new MetadataException(nwe.getMessage()); - } catch (NullPointerException nue) { - throw new MetadataException(nue.getMessage()); + } catch (KeyNotFoundException + | FieldDataInvalidException + | CannotWriteException + | NullPointerException kne) { + throw new BusinessException(kne.getMessage()); } } } diff --git a/src/main/java/com/josdem/jmetadata/util/ImageUtils.java b/src/main/java/com/josdem/jmetadata/util/ImageUtils.java index d740d3e..aebeec9 100644 --- a/src/main/java/com/josdem/jmetadata/util/ImageUtils.java +++ b/src/main/java/com/josdem/jmetadata/util/ImageUtils.java @@ -18,21 +18,25 @@ import com.josdem.jmetadata.ApplicationConstants; import com.josdem.jmetadata.service.ImageService; -import com.josdem.jmetadata.service.impl.ImageServiceImpl; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; import java.io.File; import java.io.IOException; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; @Slf4j +@Component +@RequiredArgsConstructor public class ImageUtils { - private ImageService imageService = new ImageServiceImpl(); - private FileUtils fileUtils = new FileUtils(); private static final int THREE_HUNDRED = 300; + private final ImageService imageService; + private final FileUtils fileUtils; + public static Image resize(Image image, int width, int height) { BufferedImage bufferedImage = (BufferedImage) image; int type = bufferedImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : bufferedImage.getType(); diff --git a/src/test/java/com/josdem/jmetadata/controller/CompleteControllerTest.java b/src/test/java/com/josdem/jmetadata/controller/CompleteControllerTest.java index 8610ac3..78ea247 100644 --- a/src/test/java/com/josdem/jmetadata/controller/CompleteControllerTest.java +++ b/src/test/java/com/josdem/jmetadata/controller/CompleteControllerTest.java @@ -22,7 +22,7 @@ import static org.mockito.Mockito.when; import com.josdem.jmetadata.action.ActionResult; -import com.josdem.jmetadata.exception.MetadataException; +import com.josdem.jmetadata.exception.BusinessException; import com.josdem.jmetadata.metadata.MetadataWriter; import com.josdem.jmetadata.model.CoverArt; import com.josdem.jmetadata.model.Metadata; @@ -33,12 +33,16 @@ 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; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +@Slf4j class CompleteControllerTest { private static final String ERROR = "Error"; @@ -65,7 +69,7 @@ class CompleteControllerTest { private String totalCds = "2"; @BeforeEach - public void setup() throws Exception { + void setup() throws Exception { MockitoAnnotations.initMocks(this); when(metadata.getArtist()).thenReturn(artist); @@ -120,11 +124,13 @@ public void shouldRemoveCoverArt() throws Exception { } @Test - public void shouldNotUpdateMetadata() throws Exception { + @DisplayName("not completing metadata") + void shouldNotUpdateMetadata(TestInfo testInfo) { + log.info(testInfo.getDisplayName()); when(metadata.getFile()).thenReturn(file); - when(metadataWriter.writeAlbum(album)).thenThrow(new MetadataException(ERROR)); + when(metadataWriter.writeAlbum(album)).thenThrow(new BusinessException(ERROR)); - ActionResult result = controller.completeAlbum(metadata); + var result = controller.completeAlbum(metadata); assertEquals(ActionResult.Error, result); } diff --git a/src/test/java/com/josdem/jmetadata/helper/ImageExporterTest.java b/src/test/java/com/josdem/jmetadata/helper/ImageExporterTest.java index e8a3618..876a2e8 100644 --- a/src/test/java/com/josdem/jmetadata/helper/ImageExporterTest.java +++ b/src/test/java/com/josdem/jmetadata/helper/ImageExporterTest.java @@ -29,12 +29,11 @@ import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; class ImageExporterTest { - @InjectMocks private ImageExporter imageExporter = new ImageExporter(); + private ImageExporter imageExporter; @Mock private ImageUtils imageUtils; @Mock private Metadata metadata; @@ -46,16 +45,17 @@ class ImageExporterTest { @Mock private File root; @BeforeEach - public void setup() throws Exception { + void setup() throws Exception { var title = "Bliksem"; var album = "Bliksem Album"; var artist = "Sander van Doorn"; - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); when(metadata.getAlbum()).thenReturn(album); when(metadata.getArtist()).thenReturn(artist); when(metadata.getTitle()).thenReturn(title); metadataList.add(metadata); exportPackage = new ExportPackage(root, metadataList); + imageExporter = new ImageExporter(imageUtils, metadataService); } @Test diff --git a/src/test/java/com/josdem/jmetadata/metadata/MetadataWriterTest.java b/src/test/java/com/josdem/jmetadata/metadata/MetadataWriterTest.java index a959f5f..66dba6d 100644 --- a/src/test/java/com/josdem/jmetadata/metadata/MetadataWriterTest.java +++ b/src/test/java/com/josdem/jmetadata/metadata/MetadataWriterTest.java @@ -17,29 +17,36 @@ package com.josdem.jmetadata.metadata; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.josdem.jmetadata.exception.BusinessException; import com.josdem.jmetadata.helper.ArtworkHelper; import com.josdem.jmetadata.helper.AudioFileHelper; import com.josdem.jmetadata.util.ImageUtils; -import java.awt.*; +import java.awt.Image; import java.io.File; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.jaudiotagger.audio.AudioFile; +import org.jaudiotagger.tag.FieldDataInvalidException; import org.jaudiotagger.tag.FieldKey; import org.jaudiotagger.tag.Tag; import org.jaudiotagger.tag.datatype.Artwork; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; +import org.junit.jupiter.api.TestInfo; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +@Slf4j class MetadataWriterTest { - @InjectMocks private final MetadataWriter metadataWriter = new MetadataWriter(); + private MetadataWriter metadataWriter; @Mock private AudioFile audioFile; @Mock private Tag tag; @@ -51,22 +58,19 @@ class MetadataWriterTest { @Mock private Artwork artwork; @BeforeEach - public void initialize() { - MockitoAnnotations.initMocks(this); - when(audioFile.getTag()).thenReturn(tag); - } - - @Test - public void shouldSetFile() throws Exception { + void setup() throws Exception { + MockitoAnnotations.openMocks(this); when(audioFileHelper.read(file)).thenReturn(audioFile); + when(audioFile.getTag()).thenReturn(tag); + metadataWriter = new MetadataWriter(imageUtils, audioFileHelper, artworkHelper); metadataWriter.setFile(file); - - verify(audioFile).getTag(); } @Test - public void shouldWriteArtist() throws Exception { - String artist = "Markus Schulz"; + @DisplayName("writing artist") + void shouldWriteArtist(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var artist = "Markus Schulz"; metadataWriter.writeArtist(artist); verify(tag).setField(FieldKey.ARTIST, artist); @@ -74,8 +78,20 @@ public void shouldWriteArtist() throws Exception { } @Test - public void shouldWriteTrackName() throws Exception { - String trackName = "Nowhere"; + @DisplayName("not writing artist due to exception") + void shouldNotWriteArtist(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var artist = "Markus Schulz"; + + doThrow(FieldDataInvalidException.class).when(tag).setField(FieldKey.ARTIST, artist); + assertThrows(BusinessException.class, () -> metadataWriter.writeArtist(artist)); + } + + @Test + @DisplayName("writing track name") + void shouldWriteTrackName(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var trackName = "Nowhere"; metadataWriter.writeTitle(trackName); verify(tag).setField(FieldKey.TITLE, trackName); @@ -83,8 +99,20 @@ public void shouldWriteTrackName() throws Exception { } @Test - public void shouldWriteAlbum() throws Exception { - String album = "Sahara Nights"; + @DisplayName("not writing track name due to exception") + void shouldNotWriteTrackName(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var trackName = "Nowhere"; + + doThrow(FieldDataInvalidException.class).when(tag).setField(FieldKey.TITLE, trackName); + assertThrows(BusinessException.class, () -> metadataWriter.writeTitle(trackName)); + } + + @Test + @DisplayName("writing album") + void shouldWriteAlbum(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var album = "Sahara Nights"; metadataWriter.writeAlbum(album); verify(tag).setField(FieldKey.ALBUM, album); @@ -92,8 +120,20 @@ public void shouldWriteAlbum() throws Exception { } @Test - public void shouldWriteTrackNumber() throws Exception { - String trackNumber = "1"; + @DisplayName("not writing album due to exception") + void shouldNotWriteAlbum(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var album = "Sahara Nights"; + + doThrow(FieldDataInvalidException.class).when(tag).setField(FieldKey.ALBUM, album); + assertThrows(BusinessException.class, () -> metadataWriter.writeAlbum(album)); + } + + @Test + @DisplayName("writing track number") + void shouldWriteTrackNumber(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var trackNumber = "1"; metadataWriter.writeTrackNumber(trackNumber); verify(tag).setField(FieldKey.TRACK, trackNumber); @@ -101,7 +141,19 @@ public void shouldWriteTrackNumber() throws Exception { } @Test - public void shouldWriteTotalTracksNumber() throws Exception { + @DisplayName("not writing track number due to exception") + void shouldNotWriteTrackNumber(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + String trackNumber = "1"; + + doThrow(FieldDataInvalidException.class).when(tag).setField(FieldKey.TRACK, trackNumber); + assertThrows(BusinessException.class, () -> metadataWriter.writeTrackNumber(trackNumber)); + } + + @Test + @DisplayName("writing total tracks number") + void shouldWriteTotalTracksNumber(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); String totalTracksNumber = "10"; metadataWriter.writeTotalTracksNumber(totalTracksNumber); @@ -110,7 +162,22 @@ public void shouldWriteTotalTracksNumber() throws Exception { } @Test - public void shouldWriteCoverArt() throws Exception { + @DisplayName("not writing total tracks number due to exception") + void shouldNotWriteTotalTracksNumber(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var totalTracksNumber = "10"; + + doThrow(FieldDataInvalidException.class) + .when(tag) + .setField(FieldKey.TRACK_TOTAL, totalTracksNumber); + assertThrows( + BusinessException.class, () -> metadataWriter.writeTotalTracksNumber(totalTracksNumber)); + } + + @Test + @DisplayName("writing cover art") + void shouldWriteCoverArt(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); when(imageUtils.saveCoverArtToFile(image)).thenReturn(file); when(artworkHelper.createArtwork()).thenReturn(artwork); @@ -123,10 +190,21 @@ public void shouldWriteCoverArt() throws Exception { } @Test - public void shouldWriteCdNumber() throws Exception { - String cdNumber = "1"; + @DisplayName("not writing cover art due to exception") + void shouldNotWriteCoverArt(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); - boolean result = metadataWriter.writeCdNumber(cdNumber); + doThrow(FieldDataInvalidException.class).when(tag).setField(isA(Artwork.class)); + assertThrows(BusinessException.class, () -> metadataWriter.writeCoverArt(image)); + } + + @Test + @DisplayName("writing cd number") + void shouldWriteCdNumber(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var cdNumber = "1"; + + var result = metadataWriter.writeCdNumber(cdNumber); verify(tag).setField(FieldKey.DISC_NO, cdNumber); verify(audioFile).commit(); @@ -134,10 +212,22 @@ public void shouldWriteCdNumber() throws Exception { } @Test - public void shouldWriteTotalCds() throws Exception { - String totalCds = "2"; + @DisplayName("not writing cd number due to exception") + void shouldNotWriteCdNumber(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var cdNumber = "1"; - boolean result = metadataWriter.writeTotalCds(totalCds); + doThrow(FieldDataInvalidException.class).when(tag).setField(FieldKey.DISC_NO, cdNumber); + assertThrows(BusinessException.class, () -> metadataWriter.writeCdNumber(cdNumber)); + } + + @Test + @DisplayName("writing total cds") + void shouldWriteTotalCds(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var totalCds = "2"; + + var result = metadataWriter.writeTotalCds(totalCds); verify(tag).setField(FieldKey.DISC_TOTAL, totalCds); verify(audioFile).commit(); @@ -145,28 +235,48 @@ public void shouldWriteTotalCds() throws Exception { } @Test - public void shouldNotWritetotalTracksNumberTrackNumberIfEmptyString() throws Exception { + @DisplayName("not writing total cds due to exception") + void shouldNotWriteTotalCds(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var totalCds = "2"; + + doThrow(FieldDataInvalidException.class).when(tag).setField(FieldKey.DISC_TOTAL, totalCds); + assertThrows(BusinessException.class, () -> metadataWriter.writeTotalCds(totalCds)); + } + + @Test + @DisplayName("not writing total tracks number if empty string") + void shouldNotWriteTotalTracksNumberTrackNumberIfEmptyString(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); assertFalse(metadataWriter.writeTrackNumber(StringUtils.EMPTY)); } @Test - public void shouldNotWriteTotalTracksNumberIfEmptyString() throws Exception { + @DisplayName("not writing total tracks number if empty string") + void shouldNotWriteTotalTracksNumberIfEmptyString(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); assertFalse(metadataWriter.writeTotalTracksNumber(StringUtils.EMPTY)); } @Test - public void shouldNotWriteCdNumberIfEmptyString() throws Exception { + @DisplayName("not writing cd number if empty string") + void shouldNotWriteCdNumberIfEmptyString(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); assertFalse(metadataWriter.writeCdNumber(StringUtils.EMPTY)); } @Test - public void shouldNotWriteTotalCdsIfEmptyString() throws Exception { + @DisplayName("not writing total cds if empty string") + void shouldNotWriteTotalCdsIfEmptyString(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); assertFalse(metadataWriter.writeTotalCds(StringUtils.EMPTY)); } @Test - public void shouldWriteYear() throws Exception { - String year = "1990"; + @DisplayName("writing year") + void shouldWriteYear(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var year = "1990"; metadataWriter.writeYear(year); verify(tag).setField(FieldKey.YEAR, year); @@ -174,13 +284,17 @@ public void shouldWriteYear() throws Exception { } @Test - public void shouldNotWriteYearIfEmptyString() throws Exception { + @DisplayName("not writing year due to exception") + void shouldNotWriteYearIfEmptyString(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); assertFalse(metadataWriter.writeYear(StringUtils.EMPTY)); } @Test - public void shouldWriteGenre() throws Exception { - String genre = "Minimal Techno"; + @DisplayName("writing genre") + void shouldWriteGenre(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var genre = "Minimal Techno"; metadataWriter.writeGenre(genre); verify(tag).setField(FieldKey.GENRE, genre); @@ -188,12 +302,26 @@ public void shouldWriteGenre() throws Exception { } @Test - public void shouldNotWriteGenreIfEmptyString() throws Exception { + @DisplayName("not writing genre due to exception") + void shouldNotWriteGenre(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); + var genre = "Minimal Techno"; + + doThrow(FieldDataInvalidException.class).when(tag).setField(FieldKey.GENRE, genre); + assertThrows(BusinessException.class, () -> metadataWriter.writeGenre(genre)); + } + + @Test + @DisplayName("not writing genre if empty string") + void shouldNotWriteGenreIfEmptyString(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); assertFalse(metadataWriter.writeGenre(StringUtils.EMPTY)); } @Test - public void shouldRemoveCoverArt() throws Exception { + @DisplayName("removing cover art") + void shouldRemoveCoverArt(TestInfo testInfo) throws Exception { + log.info(testInfo.getDisplayName()); assertTrue(metadataWriter.removeCoverArt()); verify(tag).deleteArtworkField(); diff --git a/src/test/java/com/josdem/jmetadata/util/ImageUtilsTest.java b/src/test/java/com/josdem/jmetadata/util/ImageUtilsTest.java index 8aa275b..5fbadb9 100644 --- a/src/test/java/com/josdem/jmetadata/util/ImageUtilsTest.java +++ b/src/test/java/com/josdem/jmetadata/util/ImageUtilsTest.java @@ -32,7 +32,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInfo; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -41,7 +40,7 @@ class ImageUtilsTest { private static final Integer THREE_HUNDRED = 300; - @InjectMocks private final ImageUtils imageUtils = new ImageUtils(); + private ImageUtils imageUtils; @Mock private ImageService imageService; @Mock private Image image; @@ -50,13 +49,14 @@ class ImageUtilsTest { @Mock private File root; @BeforeEach - public void setup() throws Exception { + void setup() { MockitoAnnotations.initMocks(this); + imageUtils = new ImageUtils(imageService, fileUtils); } @Test @DisplayName("saving cover art to file") - public void shouldSaveCoverArtToFile(TestInfo testInfo) throws Exception { + void shouldSaveCoverArtToFile(TestInfo testInfo) throws Exception { log.info(testInfo.getDisplayName()); when(imageService.createTempFile()).thenReturn(file); when(image.getHeight(isA(ImageObserver.class))).thenReturn(300); @@ -68,7 +68,7 @@ public void shouldSaveCoverArtToFile(TestInfo testInfo) throws Exception { @Test @DisplayName("not saving cover art if no image") - public void shouldNotSaveCoverArtIfNoImage(TestInfo testInfo) throws Exception { + void shouldNotSaveCoverArtIfNoImage(TestInfo testInfo) throws Exception { log.info(testInfo.getDisplayName()); imageUtils.saveCoverArtToFile(null); @@ -77,7 +77,7 @@ public void shouldNotSaveCoverArtIfNoImage(TestInfo testInfo) throws Exception { @Test @DisplayName("not saving cover art if no root file") - public void shouldNotSaveCoverArtIfRootAndNoImage(TestInfo testInfo) throws Exception { + void shouldNotSaveCoverArtIfRootAndNoImage(TestInfo testInfo) throws Exception { log.info(testInfo.getDisplayName()); imageUtils.saveCoverArtToFile(null, file, StringUtils.EMPTY); @@ -86,7 +86,7 @@ public void shouldNotSaveCoverArtIfRootAndNoImage(TestInfo testInfo) throws Exce @Test @DisplayName("saving cover art to file with custom prefix and path") - public void shouldSaveImageToFile() throws Exception { + void shouldSaveImageToFile() throws Exception { var prefix = "MIRI_"; var path = "PATH"; when(fileUtils.createFile(root, prefix, ApplicationConstants.IMAGE_EXT)).thenReturn(file);