~ubuntu-branches/ubuntu/utopic/libjaudiotagger-java/utopic

« back to all changes in this revision

Viewing changes to srctest/org/jaudiotagger/audio/ogg/OggVorbisHeaderTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath, Damien Raude-Morvan, Varun Hiremath
  • Date: 2009-04-01 19:17:56 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090401191756-bygniim270guy7o1
Tags: 1.0.9-1
[ Damien Raude-Morvan ]
* New upstream release
* debian/watch: Use java.net repository (which contains new releases!)
* debian/control:
  - Build-Depends on default-jdk-builddep
  - Bump Standards-Version to 3.8.1 (no changes needed)
  - Change section to "java"
* debian/rules: use default-java as JAVA_HOME
* debina/orig-tar.{sh|excludes}: strip audio and others binary files from ZIP
* debian/build.xml:
  - compile with "nowarn" to keep build log readable
  - exclude LogFormatter from build (use com.sun classes)
* debian/ant.properties: new source directory is "src" in orig.tar.gz
* Add myself as Uploaders

[ Varun Hiremath ]
* Accept changes made by Damien Raude-Morvan (Closes: #522130)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.jaudiotagger.audio.ogg;
 
2
 
 
3
import junit.framework.TestCase;
 
4
import org.jaudiotagger.AbstractTestCase;
 
5
import org.jaudiotagger.audio.AudioFile;
 
6
import org.jaudiotagger.audio.AudioFileIO;
 
7
import org.jaudiotagger.audio.ogg.util.OggPageHeader;
 
8
import org.jaudiotagger.tag.vorbiscomment.VorbisCommentFieldKey;
 
9
import org.jaudiotagger.tag.vorbiscomment.VorbisCommentTag;
 
10
 
 
11
import java.io.File;
 
12
import java.io.RandomAccessFile;
 
13
 
 
14
/**
 
15
 * Basic Vorbis tests
 
16
 */
 
17
public class OggVorbisHeaderTest extends TestCase
 
18
{
 
19
 
 
20
 
 
21
    /**
 
22
     * Testing reading of vorbis audio header info
 
23
     */
 
24
    public void testReadFile()
 
25
    {
 
26
        Exception exceptionCaught = null;
 
27
        try
 
28
        {
 
29
            File testFile = AbstractTestCase.copyAudioToTmp("test.ogg", new File("testReadFile.ogg"));
 
30
            AudioFile f = AudioFileIO.read(testFile);
 
31
 
 
32
            //assertEquals("192",f.getAudioHeader().getBitRate());
 
33
            //assertEquals("Ogg Vorbis v1",f.getAudioHeader().getEncodingType());
 
34
            //assertEquals("2",f.getAudioHeader().getChannels());
 
35
            //assertEquals("44100",f.getAudioHeader().getSampleRate());
 
36
 
 
37
 
 
38
            assertTrue(f.getTag() instanceof VorbisCommentTag);
 
39
 
 
40
 
 
41
        }
 
42
        catch (Exception e)
 
43
        {
 
44
            e.printStackTrace();
 
45
            exceptionCaught = e;
 
46
        }
 
47
        assertNull(exceptionCaught);
 
48
    }
 
49
 
 
50
    /**
 
51
     * Testing reading of vorbis audio header info
 
52
     * <p/>
 
53
     * TODO, need to replace with file that is not copyrighted
 
54
     */
 
55
    public void testReadPaddedFile()
 
56
    {
 
57
        Exception exceptionCaught = null;
 
58
        try
 
59
        {
 
60
            File orig = new File("testdata", "test2.ogg");
 
61
            if (!orig.isFile())
 
62
            {
 
63
                return;
 
64
            }
 
65
 
 
66
            File testFile = AbstractTestCase.copyAudioToTmp("test2.ogg", new File("test2.ogg"));
 
67
            AudioFile f = AudioFileIO.read(testFile);
 
68
 
 
69
            f.getTag().setAlbum("bbbbbbb");
 
70
            f.commit();
 
71
 
 
72
            //assertEquals("192",f.getAudioHeader().getBitRate());
 
73
            //assertEquals("Ogg Vorbis v1",f.getAudioHeader().getEncodingType());
 
74
            //assertEquals("2",f.getAudioHeader().getChannels());
 
75
            //assertEquals("44100",f.getAudioHeader().getSampleRate());
 
76
 
 
77
            //assertTrue(f.getTag() instanceof VorbisCommentTag);
 
78
 
 
79
 
 
80
        }
 
81
        catch (Exception e)
 
82
        {
 
83
            e.printStackTrace();
 
84
            exceptionCaught = e;
 
85
        }
 
86
        assertNull(exceptionCaught);
 
87
    }
 
88
 
 
89
    /**
 
90
     * Test simple write to file, comment and setup header just spread over one page before and afterwards
 
91
     */
 
92
    public void testWriteFile()
 
93
    {
 
94
        Exception exceptionCaught = null;
 
95
        try
 
96
        {
 
97
            File testFile = AbstractTestCase.copyAudioToTmp("test.ogg", new File("testWriteTagToFile.ogg"));
 
98
            AudioFile f = AudioFileIO.read(testFile);
 
99
 
 
100
            //Size of VorbisComment should increase
 
101
            assertTrue(f.getTag() instanceof VorbisCommentTag);
 
102
            f.getTag().setAlbum("bbbbbbb");
 
103
            f.commit();
 
104
 
 
105
            f = AudioFileIO.read(testFile);
 
106
            assertTrue(f.getTag() instanceof VorbisCommentTag);
 
107
            assertEquals("bbbbbbb", f.getTag().getFirstAlbum());
 
108
 
 
109
            OggFileReader ofr = new OggFileReader();
 
110
            OggPageHeader oph = ofr.readOggPageHeader(new RandomAccessFile(testFile, "r"), 0);
 
111
            assertEquals(30, oph.getPageLength());
 
112
            assertEquals(0, oph.getPageSequence());
 
113
            assertEquals(559748870, oph.getSerialNumber());
 
114
            assertEquals(-2111591604, oph.getCheckSum());
 
115
 
 
116
            oph = ofr.readOggPageHeader(new RandomAccessFile(testFile, "r"), 1);
 
117
            assertEquals(3745, oph.getPageLength());
 
118
            assertEquals(1, oph.getPageSequence());
 
119
            assertEquals(559748870, oph.getSerialNumber());
 
120
            assertEquals(233133993, oph.getCheckSum());
 
121
 
 
122
 
 
123
        }
 
124
        catch (Exception e)
 
125
        {
 
126
            e.printStackTrace();
 
127
            exceptionCaught = e;
 
128
        }
 
129
        assertNull(exceptionCaught);
 
130
    }
 
131
 
 
132
    /**
 
133
     * Test writing to file where previoslu comment was spread over many pages, now only over one so the sequence nos
 
134
     * for all subsequent pages have to be redone with checksums
 
135
     */
 
136
    public void testWritePreviouslyLargeFile()
 
137
    {
 
138
        Exception exceptionCaught = null;
 
139
        try
 
140
        {
 
141
            File testFile = AbstractTestCase.copyAudioToTmp("testlargeimage.ogg", new File("testWritePreviouslyLargeFile.ogg"));
 
142
            AudioFile f = AudioFileIO.read(testFile);
 
143
 
 
144
            //Size of VorbisComment should decrease just setting a nonsical but muuch smaller value for image
 
145
            assertTrue(f.getTag() instanceof VorbisCommentTag);
 
146
            VorbisCommentTag vorbisTag = (VorbisCommentTag) f.getTag();
 
147
            vorbisTag.set(vorbisTag.createTagField(VorbisCommentFieldKey.COVERART, "ccc"));
 
148
            f.commit();
 
149
 
 
150
            f = AudioFileIO.read(testFile);
 
151
            assertTrue(f.getTag() instanceof VorbisCommentTag);
 
152
 
 
153
            OggFileReader ofr = new OggFileReader();
 
154
            OggPageHeader oph = ofr.readOggPageHeader(new RandomAccessFile(testFile, "r"), 0);
 
155
            assertEquals(30, oph.getPageLength());
 
156
            assertEquals(0, oph.getPageSequence());
 
157
            assertEquals(559748870, oph.getSerialNumber());
 
158
            assertEquals(-2111591604, oph.getCheckSum());
 
159
            assertEquals(2, oph.getHeaderType());
 
160
 
 
161
            oph = ofr.readOggPageHeader(new RandomAccessFile(testFile, "r"), 1);
 
162
            assertEquals(3783, oph.getPageLength());
 
163
            assertEquals(1, oph.getPageSequence());
 
164
            assertEquals(559748870, oph.getSerialNumber());
 
165
            assertEquals(1677220898, oph.getCheckSum());
 
166
            assertEquals(0, oph.getHeaderType());
 
167
 
 
168
            //First Audio Frames
 
169
            oph = ofr.readOggPageHeader(new RandomAccessFile(testFile, "r"), 2);
 
170
            assertEquals(4156, oph.getPageLength());
 
171
            assertEquals(2, oph.getPageSequence());
 
172
            assertEquals(559748870, oph.getSerialNumber());
 
173
            assertEquals(1176378771, oph.getCheckSum());
 
174
            assertEquals(0, oph.getHeaderType());
 
175
 
 
176
 
 
177
        }
 
178
        catch (Exception e)
 
179
        {
 
180
            e.printStackTrace();
 
181
            exceptionCaught = e;
 
182
        }
 
183
        assertNull(exceptionCaught);
 
184
    }
 
185
 
 
186
    /**
 
187
     * Testing writing multi page comment header (existing header is multipage)
 
188
     */
 
189
    public void testLargeWriteFile()
 
190
    {
 
191
        Exception exceptionCaught = null;
 
192
        try
 
193
        {
 
194
            File testFile = AbstractTestCase.copyAudioToTmp("testlargeimage.ogg", new File("testLargeWriteFile.ogg"));
 
195
            AudioFile f = AudioFileIO.read(testFile);
 
196
 
 
197
            //Size of VorbisComment should increase
 
198
            assertTrue(f.getTag() instanceof VorbisCommentTag);
 
199
            f.getTag().setAlbum("bbbbbbb");
 
200
            f.commit();
 
201
 
 
202
            f = AudioFileIO.read(testFile);
 
203
            assertTrue(f.getTag() instanceof VorbisCommentTag);
 
204
            assertEquals("bbbbbbb", f.getTag().getFirstAlbum());
 
205
 
 
206
            OggFileReader ofr = new OggFileReader();
 
207
            OggPageHeader oph = ofr.readOggPageHeader(new RandomAccessFile(testFile, "r"), 0);
 
208
            assertEquals(30, oph.getPageLength());
 
209
            assertEquals(0, oph.getPageSequence());
 
210
            assertEquals(559748870, oph.getSerialNumber());
 
211
            assertEquals(-2111591604, oph.getCheckSum());
 
212
            assertEquals(2, oph.getHeaderType());
 
213
 
 
214
            oph = ofr.readOggPageHeader(new RandomAccessFile(testFile, "r"), 1);
 
215
            assertEquals(65025, oph.getPageLength());
 
216
            assertEquals(1, oph.getPageSequence());
 
217
            assertEquals(559748870, oph.getSerialNumber());
 
218
            assertEquals(-1172108515, oph.getCheckSum());
 
219
            assertEquals(0, oph.getHeaderType());
 
220
        }
 
221
        catch (Exception e)
 
222
        {
 
223
            e.printStackTrace();
 
224
            exceptionCaught = e;
 
225
        }
 
226
        assertNull(exceptionCaught);
 
227
    }
 
228
 
 
229
    /**
 
230
     * Testing writing multi page comment header where the setup header has to be split because there is not enough
 
231
     * room on the last Comment header Page
 
232
     */
 
233
    public void testLargeWriteFileWithSplitSetupHeader()
 
234
    {
 
235
        Exception exceptionCaught = null;
 
236
        int count = 0;
 
237
        try
 
238
        {
 
239
            File testFile = AbstractTestCase.copyAudioToTmp("testlargeimage.ogg", new File("testAwkwardSizeWriteFile.ogg"));
 
240
            AudioFile f = AudioFileIO.read(testFile);
 
241
 
 
242
            //Size of VorbisComment should increase and to a level that the setupheader cant fit completely
 
243
            //in last page pf comment header so has to be split over two pages
 
244
            assertTrue(f.getTag() instanceof VorbisCommentTag);
 
245
            StringBuffer sb = new StringBuffer();
 
246
            for (int i = 0; i < 24000; i++)
 
247
            {
 
248
                sb.append("z");
 
249
            }
 
250
            f.getTag().setAlbum("bbbbbbb");
 
251
            f.getTag().setTitle(sb.toString());
 
252
            f.commit();
 
253
 
 
254
            f = AudioFileIO.read(testFile);
 
255
            assertTrue(f.getTag() instanceof VorbisCommentTag);
 
256
            assertEquals("bbbbbbb", f.getTag().getFirstAlbum());
 
257
            assertEquals(sb.toString(), f.getTag().getFirstTitle());
 
258
 
 
259
            //Identification Header type oggFlag =2
 
260
            OggFileReader ofr = new OggFileReader();
 
261
            OggPageHeader oph = ofr.readOggPageHeader(new RandomAccessFile(testFile, "r"), 0);
 
262
            assertEquals(30, oph.getPageLength());
 
263
            assertEquals(0, oph.getPageSequence());
 
264
            assertEquals(559748870, oph.getSerialNumber());
 
265
            assertEquals(-2111591604, oph.getCheckSum());
 
266
            assertEquals(2, oph.getHeaderType());
 
267
 
 
268
            //Start of Comment Header, ogg Flag =0
 
269
            oph = ofr.readOggPageHeader(new RandomAccessFile(testFile, "r"), 1);
 
270
            assertEquals(65025, oph.getPageLength());
 
271
            assertEquals(1, oph.getPageSequence());
 
272
            assertEquals(559748870, oph.getSerialNumber());
 
273
            assertEquals(2037809131, oph.getCheckSum());
 
274
            assertEquals(0, oph.getHeaderType());
 
275
 
 
276
            //Continuing Comment Header, ogg Flag = 1
 
277
            oph = ofr.readOggPageHeader(new RandomAccessFile(testFile, "r"), 2);
 
278
            assertEquals(1, oph.getHeaderType());
 
279
 
 
280
            //Addtional checking that audio is also readable
 
281
            RandomAccessFile raf = new RandomAccessFile(testFile, "r");
 
282
            OggPageHeader lastPageHeader = null;
 
283
            while (raf.getFilePointer() < raf.length())
 
284
            {
 
285
                OggPageHeader pageHeader = OggPageHeader.read(raf);
 
286
                int packetLengthTotal = 0;
 
287
                for (OggPageHeader.PacketStartAndLength packetAndStartLength : pageHeader.getPacketList())
 
288
                {
 
289
                    packetLengthTotal += packetAndStartLength.getLength();
 
290
                }
 
291
                assertEquals(pageHeader.getPageLength(), packetLengthTotal);
 
292
                if (lastPageHeader != null)
 
293
                {
 
294
                    assertEquals(lastPageHeader.getPageSequence() + 1, pageHeader.getPageSequence());
 
295
                }
 
296
                raf.seek(raf.getFilePointer() + pageHeader.getPageLength());
 
297
                count++;
 
298
                lastPageHeader = pageHeader;
 
299
            }
 
300
            assertEquals(raf.length(), raf.getFilePointer());
 
301
 
 
302
        }
 
303
        catch (Exception e)
 
304
        {
 
305
            e.printStackTrace();
 
306
            exceptionCaught = e;
 
307
        }
 
308
        assertNull(exceptionCaught);
 
309
        assertEquals(26, count);
 
310
    }
 
311
}