]> git.sesse.net Git - vlc/blob - bindings/java/core/src/test/java/org/videolan/jvlc/internal/AbstractVLCInternalTest.java
jvlc: download a sample file to run tests, if needed
[vlc] / bindings / java / core / src / test / java / org / videolan / jvlc / internal / AbstractVLCInternalTest.java
1 /*****************************************************************************
2  * VLMInternalTest.java: VLC Java Bindings
3  *****************************************************************************
4  * Copyright (C) 1998-2008 the VideoLAN team
5  *
6  * Authors: Filippo Carone <filippo@carone.org>
7  *
8  *
9  * $Id $
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 package org.videolan.jvlc.internal;
27
28 import java.io.BufferedOutputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.net.URL;
34 import java.net.URLConnection;
35
36 import junit.framework.Assert;
37
38 import org.apache.commons.io.IOUtils;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.videolan.jvlc.internal.LibVlc.LibVlcInstance;
44 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
45
46
47 public abstract class AbstractVLCInternalTest
48 {
49
50     protected LibVlc libvlc = LibVlc.SYNC_INSTANCE;
51
52     protected LibVlcInstance libvlcInstance;
53
54     protected String mrl;
55
56     protected libvlc_exception_t exception;
57
58     private String address = "http://streams.videolan.org/streams-videolan/reference/avi/Hero-Div3.avi";
59
60     /**
61      * Logger.
62      */
63     private Logger log = LoggerFactory.getLogger(AbstractVLCInternalTest.class);
64
65     @Before
66     public void testSetup()
67     {
68         exception = new libvlc_exception_t();
69         libvlcInstance = libvlc.libvlc_new(0, new String[]{
70             "-vvv",
71             "--ignore-config",
72             "--no-media-library",
73             "-I",
74             "dummy",
75             "--aout=dummy",
76             "--vout=dummy" }, exception);
77         libvlc.libvlc_exception_clear(exception);
78         downloadSample();
79     }
80
81     @After
82     public void tearDown()
83     {
84         libvlc.libvlc_release(libvlcInstance);
85         libvlc.libvlc_exception_clear(exception);
86     }
87
88     protected void catchException(libvlc_exception_t exception)
89     {
90         Assert.assertEquals(libvlc.libvlc_exception_get_message(exception), 0, libvlc
91             .libvlc_exception_raised(exception));
92     }
93
94     private void downloadSample()
95     {
96         OutputStream out = null;
97         URLConnection conn = null;
98         InputStream in = null;
99         URL sampleResource = this.getClass().getResource("/sample.avi");
100         if (sampleResource != null)
101         {
102             log.debug("Sample file already downloaded");
103             mrl = sampleResource.getPath();
104             return;
105         }
106         try
107         {
108             log.info("Downloading sample: {}", address);
109             String testResoucesPath = this.getClass().getResource("/sample").getPath();
110             URL url = new URL(address);
111             out = new BufferedOutputStream(new FileOutputStream(testResoucesPath + ".avi"));
112             conn = url.openConnection();
113             in = conn.getInputStream();
114             byte[] buffer = new byte[1024];
115             int numRead;
116             long numWritten = 0;
117             while ((numRead = in.read(buffer)) != -1)
118             {
119                 out.write(buffer, 0, numRead);
120                 numWritten += numRead;
121             }
122             log.info("Sample downloaded.");
123             mrl = testResoucesPath + ".avi";
124         }
125         catch (Exception e)
126         {
127             log.error("{}", e);
128         }
129         finally
130         {
131             IOUtils.closeQuietly(in);
132             IOUtils.closeQuietly(out);
133         }
134     }
135
136 }