Skip to content

Commit 08d935c

Browse files
author
msendetsky
committed
Merge remote-tracking branch 'origin/master' into using-abstract-accessor-instead-of-random-access-file
2 parents 0dda32a + 580acf5 commit 08d935c

File tree

6 files changed

+138
-94
lines changed

6 files changed

+138
-94
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
<version>4.12</version>
5050
<scope>test</scope>
5151
</dependency>
52+
<dependency>
53+
<groupId>nl.jqno.equalsverifier</groupId>
54+
<artifactId>equalsverifier</artifactId>
55+
<version>2.3.2</version>
56+
<scope>test</scope>
57+
</dependency>
5258
</dependencies>
5359

5460
<build>

src/main/java/com/mpatric/mp3agic/ID3v1Tag.java

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.UnsupportedEncodingException;
44
import java.util.Arrays;
5+
import java.util.Objects;
56

67
public class ID3v1Tag implements ID3v1 {
78

@@ -227,16 +228,7 @@ public void setComment(String comment) {
227228

228229
@Override
229230
public int hashCode() {
230-
final int prime = 31;
231-
int result = 1;
232-
result = prime * result + ((album == null) ? 0 : album.hashCode());
233-
result = prime * result + ((artist == null) ? 0 : artist.hashCode());
234-
result = prime * result + ((comment == null) ? 0 : comment.hashCode());
235-
result = prime * result + genre;
236-
result = prime * result + ((title == null) ? 0 : title.hashCode());
237-
result = prime * result + ((track == null) ? 0 : track.hashCode());
238-
result = prime * result + ((year == null) ? 0 : year.hashCode());
239-
return result;
231+
return Objects.hash(album, artist, comment, genre, title, track, year);
240232
}
241233

242234
@Override
@@ -247,39 +239,14 @@ public boolean equals(Object obj) {
247239
return false;
248240
if (getClass() != obj.getClass())
249241
return false;
250-
ID3v1Tag other = (ID3v1Tag) obj;
251-
if (album == null) {
252-
if (other.album != null)
253-
return false;
254-
} else if (!album.equals(other.album))
255-
return false;
256-
if (artist == null) {
257-
if (other.artist != null)
258-
return false;
259-
} else if (!artist.equals(other.artist))
260-
return false;
261-
if (comment == null) {
262-
if (other.comment != null)
263-
return false;
264-
} else if (!comment.equals(other.comment))
265-
return false;
266-
if (genre != other.genre)
267-
return false;
268-
if (title == null) {
269-
if (other.title != null)
270-
return false;
271-
} else if (!title.equals(other.title))
272-
return false;
273-
if (track == null) {
274-
if (other.track != null)
275-
return false;
276-
} else if (!track.equals(other.track))
277-
return false;
278-
if (year == null) {
279-
if (other.year != null)
280-
return false;
281-
} else if (!year.equals(other.year))
282-
return false;
283-
return true;
242+
final ID3v1Tag other = (ID3v1Tag) obj;
243+
return
244+
Objects.equals(album, other.album) &&
245+
Objects.equals(artist, other.artist) &&
246+
Objects.equals(comment, other.comment) &&
247+
Objects.equals(genre, other.genre) &&
248+
Objects.equals(title, other.title) &&
249+
Objects.equals(track, other.track) &&
250+
Objects.equals(year, other.year);
284251
}
285252
}

src/main/java/com/mpatric/mp3agic/MutableInteger.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mpatric.mp3agic;
22

3+
import java.util.Objects;
4+
35
public class MutableInteger {
46

57
private int value;
@@ -22,10 +24,7 @@ public void setValue(int value) {
2224

2325
@Override
2426
public int hashCode() {
25-
final int prime = 31;
26-
int result = 1;
27-
result = prime * result + value;
28-
return result;
27+
return Objects.hash(value);
2928
}
3029

3130
@Override
@@ -36,9 +35,7 @@ public boolean equals(Object obj) {
3635
return false;
3736
if (getClass() != obj.getClass())
3837
return false;
39-
MutableInteger other = (MutableInteger) obj;
40-
if (value != other.value)
41-
return false;
42-
return true;
38+
final MutableInteger other = (MutableInteger) obj;
39+
return Objects.equals(value, other.value);
4340
}
4441
}

src/test/java/com/mpatric/mp3agic/ID3v1TagTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mpatric.mp3agic;
22

3+
import nl.jqno.equalsverifier.EqualsVerifier;
4+
import nl.jqno.equalsverifier.Warning;
35
import org.junit.Test;
46

57
import static org.junit.Assert.*;
@@ -161,4 +163,37 @@ public void shouldReturnEmptyTrackIfNotSetOn11Tag() throws Exception {
161163
ID3v1 id3tag = new ID3v1Tag(tagBuffer);
162164
assertEquals("", id3tag.getTrack());
163165
}
166+
167+
@Test
168+
public void shouldReturnCorrectGenreDescription() throws Exception {
169+
for (int genre = 0; genre < ID3v1Genres.GENRES.length; ++genre) {
170+
final ID3v1 id3tag = new ID3v1Tag();
171+
id3tag.setGenre(genre);
172+
assertEquals(ID3v1Genres.GENRES[genre], id3tag.getGenreDescription());
173+
}
174+
}
175+
176+
@Test
177+
public void shouldReturnUnknownGenreDescriptionForOutOfRangeGenre() throws Exception {
178+
final ID3v1 id3tag = new ID3v1Tag();
179+
id3tag.setGenre(Integer.MAX_VALUE);
180+
assertEquals("Unknown", id3tag.getGenreDescription());
181+
}
182+
183+
@Test
184+
public void shouldReturnCorrectVersion() throws Exception {
185+
final ID3v1 id3tag1 = new ID3v1Tag();
186+
assertEquals("0", id3tag1.getVersion());
187+
final ID3v1 id3tag2 = new ID3v1Tag();
188+
id3tag2.setTrack("1");
189+
assertEquals("1", id3tag2.getVersion());
190+
}
191+
192+
@Test
193+
public void shouldCorrectlyImplementHashCodeAndEquals() throws Exception {
194+
EqualsVerifier.forClass(ID3v1Tag.class)
195+
.usingGetClass()
196+
.suppress(Warning.NONFINAL_FIELDS)
197+
.verify();
198+
}
164199
}

src/test/java/com/mpatric/mp3agic/Mp3FileTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import java.io.File;
66
import java.io.IOException;
77
import java.nio.channels.SeekableByteChannel;
8+
import java.nio.charset.StandardCharsets;
89
import java.nio.file.Files;
910
import java.nio.file.Paths;
1011
import java.nio.file.StandardOpenOption;
12+
import java.util.Arrays;
1113

1214
import static junit.framework.TestCase.assertFalse;
1315
import static org.junit.Assert.*;
@@ -204,6 +206,29 @@ private void testShouldInitialiseProperlyWhenNotScanningFile(Mp3File mp3File) th
204206
assertTrue(mp3File.hasId3v2Tag());
205207
}
206208

209+
@Test
210+
public void shouldSetId3v1Tag() throws Exception {
211+
final Mp3File mp3File = new Mp3File(MP3_WITH_NO_TAGS);
212+
final ID3v1 id3tag = new ID3v1Tag();
213+
id3tag.setTrack("5");
214+
id3tag.setArtist("ARTIST");
215+
id3tag.setTitle("TITLE");
216+
id3tag.setAlbum("ALBUM");
217+
id3tag.setYear("1997");
218+
id3tag.setGenre(13);
219+
id3tag.setComment("");
220+
String saveFilename = mp3File.getFilename() + ".copy";
221+
try {
222+
mp3File.setId3v1Tag(id3tag);
223+
mp3File.save(saveFilename);
224+
Mp3File newMp3File = new Mp3File(saveFilename);
225+
assertTrue(newMp3File.hasId3v1Tag());
226+
assertEquals(id3tag, newMp3File.getId3v1Tag());
227+
} finally {
228+
TestHelper.deleteFile(saveFilename);
229+
}
230+
}
231+
207232
@Test
208233
public void shouldRemoveId3v1Tag() throws Exception {
209234
String filename = MP3_WITH_ID3V1_AND_ID3V23_AND_CUSTOM_TAGS;
@@ -230,6 +255,27 @@ private void testShouldRemoveId3v1Tag(Mp3File mp3File) throws Exception {
230255
}
231256
}
232257

258+
@Test
259+
public void shouldSetId3v2Tag() throws Exception {
260+
final Mp3File mp3File = new Mp3File(MP3_WITH_NO_TAGS);
261+
final ID3v2 id3tag = new ID3v24Tag();
262+
id3tag.setArtist("ARTIST");
263+
id3tag.setTitle("TITLE");
264+
id3tag.setAlbum("ALBUM");
265+
id3tag.setYear("1954");
266+
id3tag.setGenre(0x0d);
267+
String saveFilename = mp3File.getFilename() + ".copy";
268+
try {
269+
mp3File.setId3v2Tag(id3tag);
270+
mp3File.save(saveFilename);
271+
Mp3File newMp3File = new Mp3File(saveFilename);
272+
assertTrue(newMp3File.hasId3v2Tag());
273+
assertEquals(id3tag, newMp3File.getId3v2Tag());
274+
} finally {
275+
TestHelper.deleteFile(saveFilename);
276+
}
277+
}
278+
233279
@Test
234280
public void shouldRemoveId3v2Tag() throws Exception {
235281
String filename = MP3_WITH_ID3V1_AND_ID3V23_AND_CUSTOM_TAGS;
@@ -256,6 +302,22 @@ private void testShouldRemoveId3v2Tag(Mp3File mp3File) throws Exception {
256302
}
257303
}
258304

305+
@Test
306+
public void shouldSetCustomTag() throws Exception {
307+
final Mp3File mp3File = new Mp3File(MP3_WITH_NO_TAGS);
308+
final byte[] customTag = "CUSTOM_TAG".getBytes(StandardCharsets.UTF_8);
309+
String saveFilename = mp3File.getFilename() + ".copy";
310+
try {
311+
mp3File.setCustomTag(customTag);
312+
mp3File.save(saveFilename);
313+
Mp3File newMp3File = new Mp3File(saveFilename);
314+
assertTrue(newMp3File.hasCustomTag());
315+
assertTrue(Arrays.equals(newMp3File.getCustomTag(), customTag));
316+
} finally {
317+
TestHelper.deleteFile(saveFilename);
318+
}
319+
}
320+
259321
@Test
260322
public void shouldRemoveCustomTag() throws Exception {
261323
String filename = MP3_WITH_ID3V1_AND_ID3V23_AND_CUSTOM_TAGS;
@@ -294,6 +356,18 @@ public void shouldRemoveId3v1AndId3v2AndCustomTagsForFileConstructor() throws Ex
294356
testShouldRemoveId3v1AndId3v2AndCustomTags(new Mp3File(filename));
295357
}
296358

359+
@Test
360+
public void shouldReturnCorrectLengthInSeconds() throws Exception {
361+
final Mp3File mp3File = new Mp3File(MP3_WITH_ID3V1_AND_ID3V23_TAGS);
362+
assertEquals(0, mp3File.getLengthInSeconds());
363+
}
364+
365+
@Test
366+
public void shouldCorrectlyIdentifyIfVBR() throws Exception {
367+
final Mp3File mp3File = new Mp3File(MP3_WITH_ID3V1_AND_ID3V23_TAGS);
368+
assertTrue(mp3File.isVbr());
369+
}
370+
297371
private void testShouldRemoveId3v1AndId3v2AndCustomTags(Mp3File mp3File) throws Exception {
298372
String saveFilename = mp3File.getFilename() + ".copy";
299373
try {
Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mpatric.mp3agic;
22

3+
import nl.jqno.equalsverifier.EqualsVerifier;
4+
import nl.jqno.equalsverifier.Warning;
35
import org.junit.Test;
46

57
import static org.junit.Assert.assertEquals;
@@ -28,47 +30,10 @@ public void setsValue() {
2830
}
2931

3032
@Test
31-
public void equalsItself() {
32-
MutableInteger integer = new MutableInteger(8);
33-
assertEquals(integer, integer);
34-
}
35-
36-
@Test
37-
public void equalIfValueEqual() {
38-
MutableInteger eight = new MutableInteger(8);
39-
MutableInteger eightAgain = new MutableInteger(8);
40-
assertEquals(eight, eightAgain);
41-
}
42-
43-
@Test
44-
public void notEqualToNull() {
45-
MutableInteger integer = new MutableInteger(8);
46-
assertFalse(integer.equals(null));
47-
}
48-
49-
@Test
50-
public void notEqualToDifferentClass() {
51-
MutableInteger integer = new MutableInteger(8);
52-
assertFalse(integer.equals("8"));
53-
}
54-
55-
@Test
56-
public void notEqualIfValueNotEqual() {
57-
MutableInteger eight = new MutableInteger(8);
58-
MutableInteger nine = new MutableInteger(9);
59-
assertNotEquals(eight, nine);
60-
}
61-
62-
@Test
63-
public void hashCodeIsConsistent() {
64-
MutableInteger integer = new MutableInteger(8);
65-
assertEquals(integer.hashCode(), integer.hashCode());
66-
}
67-
68-
@Test
69-
public void equalObjectsHaveSameHashCode() {
70-
MutableInteger eight = new MutableInteger(8);
71-
MutableInteger eightAgain = new MutableInteger(8);
72-
assertEquals(eight.hashCode(), eightAgain.hashCode());
33+
public void shouldCorrectlyImplementHashCodeAndEquals() throws Exception {
34+
EqualsVerifier.forClass(MutableInteger.class)
35+
.usingGetClass()
36+
.suppress(Warning.NONFINAL_FIELDS)
37+
.verify();
7338
}
7439
}

0 commit comments

Comments
 (0)