add some generics (removes warnings) and bump junit to 4.11
This commit is contained in:
parent
053d532655
commit
12945ed615
3 changed files with 41 additions and 24 deletions
2
pom.xml
2
pom.xml
|
|
@ -133,7 +133,7 @@
|
|||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.7</version>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -32,6 +32,13 @@ public class FlvMetadata {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void merge(Attr<?> other) {
|
||||
if (other != null && other.getClass() == getClass()) {
|
||||
set((T)other.value);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void write(OutputStream out) throws IOException;
|
||||
|
||||
public boolean isPresent() {
|
||||
|
|
@ -249,7 +256,7 @@ public class FlvMetadata {
|
|||
|
||||
private LinkedList<Keyframe> keyframes = new LinkedList<Keyframe>();
|
||||
|
||||
private ArrayList<Attr> attrs = new ArrayList<Attr>();
|
||||
private ArrayList<Attr<?>> attrs = new ArrayList<Attr<?>>();
|
||||
|
||||
public FlvMetadata() {
|
||||
|
||||
|
|
@ -304,7 +311,7 @@ public class FlvMetadata {
|
|||
|
||||
private int length() {
|
||||
int i = 0;
|
||||
for (Attr a : attrs) {
|
||||
for (Attr<?> a : attrs) {
|
||||
if (a.isPresent()) {
|
||||
i++;
|
||||
}
|
||||
|
|
@ -355,7 +362,7 @@ public class FlvMetadata {
|
|||
out.write(2); // string
|
||||
writeFlvString(out, "onMetaData");
|
||||
writeFlvEcmaArray(out, length());
|
||||
for (Attr a : attrs) {
|
||||
for (Attr<?> a : attrs) {
|
||||
if (a.isPresent()) {
|
||||
if (a.name != null) {
|
||||
writeFlvString(out, a.name);
|
||||
|
|
@ -499,10 +506,10 @@ public class FlvMetadata {
|
|||
public void merge(FlvMetadata other) {
|
||||
|
||||
for (int i = 0; i < attrs.size(); i++) {
|
||||
Attr their = other.attrs.get(i);
|
||||
Attr<?> their = other.attrs.get(i);
|
||||
if (their.isPresent()) {
|
||||
Attr our = this.attrs.get(i);
|
||||
our.set(their.value);
|
||||
Attr<?> our = this.attrs.get(i);
|
||||
our.merge(their);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -512,7 +519,7 @@ public class FlvMetadata {
|
|||
if (prop == null) {
|
||||
return null;
|
||||
}
|
||||
for (Attr a : attrs) {
|
||||
for (Attr<?> a : attrs) {
|
||||
if (prop.equals(a.name) && a.isPresent()) {
|
||||
return a.value;
|
||||
}
|
||||
|
|
@ -637,7 +644,7 @@ public class FlvMetadata {
|
|||
protected void readProperty(InputStream in) throws FlvFormatException, IOException {
|
||||
|
||||
String name = readString(in);
|
||||
Attr attr = findAttrByName(name);
|
||||
Attr<?> attr = findAttrByName(name);
|
||||
if (attr != null) {
|
||||
attr.read(in);
|
||||
} else {
|
||||
|
|
@ -660,8 +667,8 @@ public class FlvMetadata {
|
|||
}
|
||||
}
|
||||
|
||||
private Attr findAttrByName(String name) {
|
||||
for (Attr a : this.attrs) {
|
||||
private Attr<?> findAttrByName(String name) {
|
||||
for (Attr<?> a : this.attrs) {
|
||||
if (name.equals(a.name)) {
|
||||
return a;
|
||||
}
|
||||
|
|
@ -716,8 +723,8 @@ public class FlvMetadata {
|
|||
}
|
||||
FlvMetadata other = (FlvMetadata)o;
|
||||
for (int i = 0; i < attrs.size(); i++) {
|
||||
Attr their = other.attrs.get(i);
|
||||
Attr our = this.attrs.get(i);
|
||||
Attr<?> their = other.attrs.get(i);
|
||||
Attr<?> our = this.attrs.get(i);
|
||||
if (our.isPresent() && their.isPresent()) {
|
||||
if (!our.value.equals(their.value)) {
|
||||
return false;
|
||||
|
|
@ -733,7 +740,7 @@ public class FlvMetadata {
|
|||
@Override
|
||||
public int hashCode() {
|
||||
int x = 0;
|
||||
for (Attr a : attrs) {
|
||||
for (Attr<?> a : attrs) {
|
||||
x *= 13;
|
||||
x += a.present ? a.value.hashCode() : 7;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,10 +5,14 @@ import java.io.ByteArrayOutputStream;
|
|||
import java.io.InputStream;
|
||||
|
||||
import org.forkalsrud.album.exif.Dimension;
|
||||
import org.junit.Test;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.hamcrest.core.IsInstanceOf.instanceOf;
|
||||
|
||||
public class FlvMetadataTest extends TestCase {
|
||||
public class FlvMetadataTest {
|
||||
|
||||
private ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
private ByteArrayInputStream in;
|
||||
|
|
@ -19,6 +23,7 @@ public class FlvMetadataTest extends TestCase {
|
|||
out.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMerge() throws Exception {
|
||||
|
||||
FlvMetadata md1 = new FlvMetadata();
|
||||
|
|
@ -27,19 +32,20 @@ public class FlvMetadataTest extends TestCase {
|
|||
|
||||
FlvMetadata md2 = new FlvMetadata();
|
||||
md2.merge(md1);
|
||||
assertEquals(Double.valueOf("3.14"), md2.get("duration"));
|
||||
assertEquals(Double.valueOf("5"), md2.get("width"));
|
||||
assertEquals(Double.valueOf("6"), md2.get("height"));
|
||||
assertEquals(3.14d, md2.get("duration"));
|
||||
assertEquals(5d, md2.get("width"));
|
||||
assertEquals(6d, md2.get("height"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadWriteDouble() throws Exception {
|
||||
|
||||
double value = 3.14;
|
||||
md0.writeDouble(out, value);
|
||||
flip();
|
||||
assertEquals(value, md0.readDouble(in));
|
||||
assertThat(md0.readDouble(in), equalTo(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadWriteInt() throws Exception {
|
||||
int value = 45;
|
||||
// uint16
|
||||
|
|
@ -63,6 +69,7 @@ public class FlvMetadataTest extends TestCase {
|
|||
assertEquals(value, md0.readTimestamp(in));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadWriteString() throws Exception {
|
||||
String value = "awsome";
|
||||
md0.writeFlvString(out, value);
|
||||
|
|
@ -70,6 +77,7 @@ public class FlvMetadataTest extends TestCase {
|
|||
assertEquals(value, md0.readString(in));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteReadMetadata() throws Exception {
|
||||
|
||||
FlvMetadata md1 = new FlvMetadata();
|
||||
|
|
@ -88,6 +96,7 @@ public class FlvMetadataTest extends TestCase {
|
|||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testReadWriteMetadata() throws Exception {
|
||||
|
||||
InputStream is = getClass().getResourceAsStream("/VideoAd.flv");
|
||||
|
|
@ -105,12 +114,13 @@ public class FlvMetadataTest extends TestCase {
|
|||
assertEquals(md1, md2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetDuration() {
|
||||
FlvMetadata m = new FlvMetadata();
|
||||
m.setDuration("1:02:03");
|
||||
Object o = m.get("duration");
|
||||
assertTrue(o.getClass() == Double.class);
|
||||
assertThat(o, instanceOf(Double.class));
|
||||
Double durationSeconds = (Double) o;
|
||||
assertEquals(3600 + 2*60 + 3, durationSeconds.intValue());
|
||||
assertThat(durationSeconds.intValue(), equalTo(3600 + 2*60 + 3));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue