Skip to content

Commit 4243eb5

Browse files
committed
Load test resources from classpath
1 parent d3c6843 commit 4243eb5

15 files changed

Lines changed: 119 additions & 118 deletions

isoparser/src/test/java/com/coremedia/drm/packager/isoparser/RoundTripTest.java

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,109 +16,93 @@
1616

1717
package com.coremedia.drm.packager.isoparser;
1818

19-
import junit.framework.TestCase;
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.InputStream;
21+
import java.nio.channels.Channels;
22+
import java.nio.channels.ReadableByteChannel;
23+
2024
import org.mp4parser.IsoFile;
2125
import org.mp4parser.support.BoxComparator;
2226
import org.mp4parser.tools.ByteBufferByteChannel;
2327

24-
import java.io.ByteArrayOutputStream;
25-
import java.io.FileInputStream;
26-
import java.nio.channels.Channels;
28+
import junit.framework.TestCase;
2729

2830
/**
2931
* Tests ISO Roundtrip.
3032
*/
3133
public class RoundTripTest extends TestCase {
32-
String defaultTestFileDir;
33-
34-
@Override
35-
protected void setUp() throws Exception {
36-
super.setUp();
37-
defaultTestFileDir = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
38-
/* Logger.getLogger("").setLevel(Level.ALL);
39-
Handler[] handlers = Logger.getLogger("").getHandlers();
40-
for (Handler handler : handlers) {
41-
handler.setLevel(Level.ALL);
42-
}*/
43-
}
44-
4534
/* public void testRoundDeleteMe() throws Exception {
4635
testRoundTrip_1("/suckerpunch-distantplanet_h1080p.mov");
4736
}*/
4837
public void testRoundTrip_TinyExamples_Old() throws Exception {
49-
testRoundTrip_1(defaultTestFileDir + "/Tiny Sample - OLD.mp4");
38+
testRoundTrip_1("Tiny Sample - OLD.mp4");
5039
}
5140

5241
public void testRoundTrip_TinyExamples_Metaxed() throws Exception {
53-
testRoundTrip_1(defaultTestFileDir + "/Tiny Sample - NEW - Metaxed.mp4");
42+
testRoundTrip_1("Tiny Sample - NEW - Metaxed.mp4");
5443
}
5544

5645
public void testRoundTrip_TinyExamples_Untouched() throws Exception {
57-
testRoundTrip_1(defaultTestFileDir + "/Tiny Sample - NEW - Untouched.mp4");
46+
testRoundTrip_1("Tiny Sample - NEW - Untouched.mp4");
5847
}
5948

60-
6149
public void testRoundTrip_1a() throws Exception {
62-
testRoundTrip_1(defaultTestFileDir + "/multiTrack.3gp");
50+
testRoundTrip_1("multiTrack.3gp");
6351
}
6452

6553
public void testRoundTrip_1b() throws Exception {
66-
testRoundTrip_1(defaultTestFileDir + "/MOV00006.3gp");
54+
testRoundTrip_1("MOV00006.3gp");
6755
}
6856

6957
public void testRoundTrip_1c() throws Exception {
70-
testRoundTrip_1(defaultTestFileDir + "/Beethoven - Bagatelle op.119 no.11 i.m4a");
58+
testRoundTrip_1("Beethoven - Bagatelle op.119 no.11 i.m4a");
7159
}
7260

7361
public void testRoundTrip_1d() throws Exception {
74-
testRoundTrip_1(defaultTestFileDir + "/test.m4p");
62+
testRoundTrip_1("test.m4p");
7563
}
7664

7765
public void testRoundTrip_1e() throws Exception {
78-
testRoundTrip_1(defaultTestFileDir + "/test-pod.m4a");
66+
testRoundTrip_1("test-pod.m4a");
7967
}
8068

8169
public void testRoundTrip_QuickTimeFormat() throws Exception {
82-
testRoundTrip_1(defaultTestFileDir + "/QuickTimeFormat.mp4");
70+
testRoundTrip_1("QuickTimeFormat.mp4");
8371
}
8472

85-
8673
public void testRoundTrip_1(String originalFile) throws Exception {
8774

8875
long start1 = System.currentTimeMillis();
8976
long start2 = System.currentTimeMillis();
77+
78+
try (InputStream is = getClass().getClassLoader().getResourceAsStream(originalFile);
79+
ReadableByteChannel channel = Channels.newChannel(is)) {
80+
IsoFile isoFile = new IsoFile(channel);
9081

91-
IsoFile isoFile = new IsoFile(new FileInputStream(originalFile).getChannel());
92-
93-
long start3 = System.currentTimeMillis();
94-
95-
96-
long start4 = System.currentTimeMillis();
97-
Walk.through(isoFile);
98-
long start5 = System.currentTimeMillis();
82+
long start3 = System.currentTimeMillis();
9983

10084

101-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
102-
isoFile.getBox(Channels.newChannel(baos));
85+
long start4 = System.currentTimeMillis();
86+
Walk.through(isoFile);
87+
long start5 = System.currentTimeMillis();
10388

89+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
90+
isoFile.getBox(Channels.newChannel(baos));
10491

105-
long start6 = System.currentTimeMillis();
92+
long start6 = System.currentTimeMillis();
10693

10794
/* System.err.println("Preparing tmp copy took: " + (start2 - start1) + "ms");
10895
System.err.println("Parsing took : " + (start3 - start2) + "ms");
10996
System.err.println("Writing took : " + (start6 - start3) + "ms");
11097
System.err.println("Walking took : " + (start5 - start4) + "ms");*/
11198

11299

113-
IsoFile copyViaIsoFileReparsed = new IsoFile(new ByteBufferByteChannel(baos.toByteArray()));
114-
BoxComparator.check(isoFile, copyViaIsoFileReparsed, "moov[0]/mvhd[0]", "moov[0]/trak[0]/tkhd[0]", "moov[0]/trak[0]/mdia[0]/mdhd[0]");
115-
isoFile.close();
116-
copyViaIsoFileReparsed.close();
117-
// as windows cannot delete file when something is memory mapped and the garbage collector
118-
// doesn't necessarily free the Buffers quickly enough we cannot delete the file here (we could but only for linux)
119-
120-
100+
IsoFile copyViaIsoFileReparsed = new IsoFile(new ByteBufferByteChannel(baos.toByteArray()));
101+
BoxComparator.check(isoFile, copyViaIsoFileReparsed, "moov[0]/mvhd[0]", "moov[0]/trak[0]/tkhd[0]", "moov[0]/trak[0]/mdia[0]/mdhd[0]");
102+
isoFile.close();
103+
copyViaIsoFileReparsed.close();
104+
// as windows cannot delete file when something is memory mapped and the garbage collector
105+
// doesn't necessarily free the Buffers quickly enough we cannot delete the file here (we could but only for linux)
106+
}
121107
}
122-
123-
124108
}

isoparser/src/test/java/org/mp4parser/SkippingBoxTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import java.io.FileInputStream;
88
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.channels.Channels;
11+
import java.nio.channels.ReadableByteChannel;
912

1013
import org.junit.Before;
1114
import org.junit.Test;
@@ -20,9 +23,9 @@ public class SkippingBoxTest {
2023

2124
@Before
2225
public void setup() throws IOException {
23-
FileInputStream fis = new FileInputStream(PathTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/test.m4p");
24-
isoFile = new IsoFile(fis.getChannel(), new PropertyBoxParserImpl().skippingBoxes("mdat", "mvhd"));
25-
fis.close();
26+
try (FileInputStream fis = new FileInputStream(getClass().getClassLoader().getResource("test.m4p").getPath())) {
27+
isoFile = new IsoFile(fis.getChannel(), new PropertyBoxParserImpl().skippingBoxes("mdat", "mvhd"));
28+
}
2629
}
2730

2831

isoparser/src/test/java/org/mp4parser/tools/PathTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ public class PathTest {
1414

1515
@Before
1616
public void setup() throws IOException {
17-
isoFile = new IsoFile(new FileInputStream(PathTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/multiTrack.3gp").getChannel());
17+
try (FileInputStream fis = new FileInputStream(getClass().getClassLoader().getResource("multiTrack.3gp").getPath())) {
18+
isoFile = new IsoFile(fis.getChannel());
19+
}
1820
}
1921

2022

muxer/src/test/java/org/mp4parser/muxer/CencFileRoundtripTest.java

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package org.mp4parser.muxer;
22

3+
import java.io.ByteArrayOutputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.OutputStream;
7+
import java.nio.channels.Channels;
8+
import java.util.HashMap;
9+
import java.util.Iterator;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.UUID;
13+
14+
import javax.crypto.SecretKey;
15+
import javax.crypto.spec.SecretKeySpec;
16+
317
import org.junit.Assert;
418
import org.junit.Before;
519
import org.junit.Test;
@@ -14,16 +28,7 @@
1428
import org.mp4parser.tools.ByteBufferByteChannel;
1529
import org.mp4parser.tools.RangeStartMap;
1630

17-
import javax.crypto.SecretKey;
18-
import javax.crypto.spec.SecretKeySpec;
19-
import java.io.ByteArrayOutputStream;
20-
import java.io.FileOutputStream;
21-
import java.io.IOException;
22-
import java.nio.channels.Channels;
23-
import java.util.*;
24-
2531
public class CencFileRoundtripTest {
26-
private String baseDir = CencFileRoundtripTest.class.getProtectionDomain().getCodeSource().getLocation().getFile();
2732
private Map<UUID, SecretKey> keys;
2833
private RangeStartMap<Integer, UUID> keyRotation1;
2934
private RangeStartMap<Integer, UUID> keyRotation2;
@@ -59,67 +64,67 @@ public void setUp() throws Exception {
5964

6065
@Test
6166
public void testSingleKeysStdMp4_cbc1() throws IOException {
62-
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cbc1", false);
67+
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cbc1", false);
6368
}
6469

6570

6671
@Test
6772
public void testSingleKeysFragMp4_cbc1() throws IOException {
68-
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cbc1", false);
73+
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cbc1", false);
6974
}
7075

7176
@Test
7277
public void testSingleKeysStdMp4_cenc() throws IOException {
73-
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cenc", false);
78+
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cenc", false);
7479
}
7580

7681
@Test
7782
public void testSingleKeysFragMp4_cenc() throws IOException {
78-
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cenc", false);
83+
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation1, "cenc", false);
7984
}
8085

8186

8287
@Test
8388
public void testClearLeadStdMp4_2_cbc1() throws IOException {
84-
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cbc1", false);
89+
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cbc1", false);
8590
}
8691

8792
@Test
8893
public void testClearLeadFragMp4_2_cbc1() throws IOException {
89-
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cbc1", false);
94+
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cbc1", false);
9095
}
9196

9297

9398
@Test
9499
public void testClearLeadStdMp4_2_cenc() throws IOException {
95-
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", false);
100+
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", false);
96101
}
97102

98103
@Test
99104
public void testClearLeadFragMp4_2_cenc() throws IOException {
100-
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", false);
105+
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", false);
101106
}
102107

103108

104109
@Test
105110
public void testMultipleKeysStdMp4_2_cbc1() throws IOException {
106-
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cbc1", false);
111+
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cbc1", false);
107112
}
108113

109114
@Test
110115
public void testMultipleKeysFragMp4_2_cbc1() throws IOException {
111-
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cbc1", false);
116+
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cbc1", false);
112117
}
113118

114119

115120
@Test
116121
public void testMultipleKeysStdMp4_2_cenc() throws IOException {
117-
testMultipleKeys(new DefaultMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cenc", false);
122+
testMultipleKeys(new DefaultMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cenc", false);
118123
}
119124

120125
@Test
121126
public void testMultipleKeysFragMp4_2_cenc() throws IOException {
122-
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cenc", false);
127+
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation3, "cenc", false);
123128
}
124129

125130

@@ -128,13 +133,14 @@ public void testMultipleKeysFragMp4_2_cenc() throws IOException {
128133

129134
@Test
130135
public void testMultipleKeysFragMp4_2_cenc_pseudo_encrypted() throws IOException {
131-
testMultipleKeys(new FragmentedMp4Builder(), baseDir + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", true);
136+
testMultipleKeys(new FragmentedMp4Builder(), "BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4", keys, keyRotation2, "cenc", true);
132137
}
133138

134139
private void testMultipleKeys(Mp4Builder builder, String testFile, Map<UUID, SecretKey> keys,
135140
RangeStartMap<Integer, UUID> keyRotation,
136141
String encAlgo, boolean encryptButClear) throws IOException {
137-
Movie m1 = MovieCreator.build(testFile);
142+
String filePath = getClass().getClassLoader().getResource(testFile).getPath();
143+
Movie m1 = MovieCreator.build(filePath);
138144
Movie m2 = new Movie();
139145
for (Track track : m1.getTracks()) {
140146
CencEncryptingTrackImpl cencEncryptingTrack =
@@ -145,7 +151,9 @@ private void testMultipleKeys(Mp4Builder builder, String testFile, Map<UUID, Sec
145151

146152
ByteArrayOutputStream baos = new ByteArrayOutputStream();
147153
c.writeContainer(Channels.newChannel(baos));
148-
new FileOutputStream("m2.mp4").write(baos.toByteArray());
154+
try (OutputStream os = new FileOutputStream("m2.mp4")) {
155+
os.write(baos.toByteArray());
156+
}
149157

150158
Movie m3 = MovieCreator.build(
151159
new ByteBufferByteChannel(baos.toByteArray()),

muxer/src/test/java/org/mp4parser/muxer/FragmentedMp4BuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class FragmentedMp4BuilderTest {
1616
@Test
1717
public void testSimpleMuxing() throws Exception {
1818
Movie m = new Movie();
19-
Movie v = MovieCreator.build(FragmentedMp4BuilderTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4");
20-
Movie a = MovieCreator.build(FragmentedMp4BuilderTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/BBB_qpfile_10sec/output_audio-2ch-20s.mp4");
19+
Movie v = MovieCreator.build(FragmentedMp4BuilderTest.class.getClassLoader().getResource("BBB_qpfile_10sec/BBB_fixedres_B_180x320_80.mp4").toURI().getPath());
20+
Movie a = MovieCreator.build(FragmentedMp4BuilderTest.class.getClassLoader().getResource("BBB_qpfile_10sec/output_audio-2ch-20s.mp4").toURI().getPath());
2121

2222
m.addTrack(v.getTracks().get(0));
2323
m.addTrack(a.getTracks().get(0));

muxer/src/test/java/org/mp4parser/muxer/InTestMovieCreator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package org.mp4parser.muxer;
22

3-
import org.mp4parser.muxer.container.mp4.MovieCreator;
4-
53
import java.io.IOException;
4+
import java.net.URISyntaxException;
5+
6+
import org.mp4parser.muxer.container.mp4.MovieCreator;
67

78

89
public class InTestMovieCreator {
9-
public static Movie createMovieOnlyVideo(String... names) throws IOException {
10+
public static Movie createMovieOnlyVideo(String... names) throws IOException, URISyntaxException {
1011
Movie m = new Movie();
1112
for (String name : names) {
12-
Movie m1 = MovieCreator.build((InTestMovieCreator.class.getProtectionDomain().getCodeSource().getLocation().getFile() + name));
13+
Movie m1 = MovieCreator.build((InTestMovieCreator.class.getClassLoader().getResource(name).toURI().getPath()));
1314
for (Track track : m1.getTracks()) {
1415
if ("vide".equals(track.getHandler())) {
1516
m.addTrack(track);

muxer/src/test/java/org/mp4parser/muxer/builder/DefaultFragmenterTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
* Just check it works.
1010
*/
1111
public class DefaultFragmenterTest {
12-
long[] samples = new long[]{1, 87, 174, 261, 348, 435, 522, 609, 696, 783, 870, 957, 1044, 1131, 1218, 1305, 1392, 1479, 1566, 1653, 1740, 1827, 1914, 2001, 2088, 2175, 2262, 2349, 2436, 2523, 2610, 2697, 2784, 2871, 2958, 3045, 3132, 3219, 3306, 3393, 3480, 3567, 3654, 3741, 3828, 3915, 4002, 4089, 4176, 4263, 4350, 4437, 4524, 4611, 4698, 4785};
12+
long[] samples = new long[] { 1, 87, 174, 261, 348, 435, 522, 609, 696, 783, 870, 957, 1044, 1131, 1218, 1305, 1392, 1479, 1566, 1653, 1740, 1827, 1914,
13+
2001, 2088, 2175, 2262, 2349, 2436, 2523, 2610, 2697, 2784, 2871, 2958, 3045, 3132, 3219, 3306, 3393, 3480, 3567, 3654, 3741, 3828, 3915, 4002,
14+
4089, 4176, 4263, 4350, 4437, 4524, 4611, 4698, 4785 };
1315

1416
@Test
1517
public void testSampleNumbers() throws Exception {
16-
String f = DefaultFragmenterTest.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/Beethoven - Bagatelle op.119 no.11 i.m4a";
18+
String f = DefaultFragmenterTest.class.getClassLoader().getResource("Beethoven - Bagatelle op.119 no.11 i.m4a").toURI().getPath();
1719
Movie m = MovieCreator.build(f);
1820
DefaultFragmenterImpl intersectionFinder = new DefaultFragmenterImpl(2);
1921
long[] s = intersectionFinder.sampleNumbers(m.getTracks().get(0));

0 commit comments

Comments
 (0)