]> git.sesse.net Git - vlc/blob - bindings/java/core/src/test/java/org/videolan/jvlc/AbstractJVLCTest.java
jvlc: abstract test class added to not-internal tests too
[vlc] / bindings / java / core / src / test / java / org / videolan / jvlc / AbstractJVLCTest.java
1 /*****************************************************************************
2  * AbstractJVLCTest.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;
27
28 import java.io.BufferedOutputStream;
29 import java.io.FileOutputStream;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.net.URL;
33 import java.net.URLConnection;
34
35 import org.apache.commons.io.IOUtils;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.videolan.jvlc.internal.AbstractVLCInternalTest;
41
42
43 public abstract class AbstractJVLCTest
44 {
45     
46     protected JVLC jvlc;
47
48     protected String mrl;
49
50     private String address = "http://streams.videolan.org/streams-videolan/reference/avi/Hero-Div3.avi";
51
52     /**
53      * Logger.
54      */
55     private Logger log = LoggerFactory.getLogger(AbstractVLCInternalTest.class);
56
57     @Before
58     public void testSetup()
59     {
60         jvlc = new JVLC("-vvv --ignore-config --no-media-library -I dummy --aout=dummy --vout=dummy");
61         downloadSample();
62     }
63
64     @After
65     public void tearDown()
66     {
67         jvlc.release();
68     }
69
70     private void downloadSample()
71     {
72         OutputStream out = null;
73         URLConnection conn = null;
74         InputStream in = null;
75         URL sampleResource = this.getClass().getResource("/sample.avi");
76         if (sampleResource != null)
77         {
78             log.debug("Sample file already downloaded");
79             mrl = sampleResource.getPath();
80             return;
81         }
82         try
83         {
84             log.info("Downloading sample: {}", address);
85             String testResoucesPath = this.getClass().getResource("/sample").getPath();
86             URL url = new URL(address);
87             out = new BufferedOutputStream(new FileOutputStream(testResoucesPath + ".avi"));
88             conn = url.openConnection();
89             in = conn.getInputStream();
90             byte[] buffer = new byte[1024];
91             int numRead;
92             long numWritten = 0;
93             while ((numRead = in.read(buffer)) != -1)
94             {
95                 out.write(buffer, 0, numRead);
96                 numWritten += numRead;
97             }
98             log.info("Sample downloaded.");
99             mrl = testResoucesPath + ".avi";
100         }
101         catch (Exception e)
102         {
103             log.error("{}", e);
104         }
105         finally
106         {
107             IOUtils.closeQuietly(in);
108             IOUtils.closeQuietly(out);
109         }
110     }
111
112 }