]> git.sesse.net Git - vlc/blob - bindings/java/core/src/test/java/org/videolan/jvlc/internal/AbstractVLCInternalTest.java
jvlc: minor changes on unit tests
[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.InputStream;
31 import java.io.OutputStream;
32 import java.net.URL;
33 import java.net.URLConnection;
34
35 import junit.framework.Assert;
36
37 import org.apache.commons.io.IOUtils;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.videolan.jvlc.internal.LibVlc.LibVlcInstance;
43 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
44
45
46 public abstract class AbstractVLCInternalTest
47 {
48
49     protected LibVlc libvlc = LibVlc.SYNC_INSTANCE;
50
51     protected LibVlcInstance libvlcInstance;
52
53     protected String mrl;
54
55     protected libvlc_exception_t exception;
56
57     private String address = "http://streams.videolan.org/streams-videolan/reference/avi/Hero-Div3.avi";
58
59     /**
60      * Logger.
61      */
62     private Logger log = LoggerFactory.getLogger(AbstractVLCInternalTest.class);
63
64     @Before
65     public void testSetup()
66     {
67         exception = new libvlc_exception_t();
68         String[] args = new String[]{
69             "-vvv",
70             "--ignore-config",
71             "--reset-plugins-cache",
72             "--no-media-library",
73             "-I",
74             "dummy",
75             "-A",
76             "dummy",
77             "-V",
78             "dummy" };
79         libvlcInstance = libvlc.libvlc_new(args.length, args, exception);
80         libvlc.libvlc_exception_clear(exception);
81
82         downloadSample();
83     }
84
85     @After
86     public void tearDown()
87     {
88         libvlc.libvlc_release(libvlcInstance);
89         libvlc.libvlc_exception_clear(exception);
90     }
91
92     protected void catchException(libvlc_exception_t exception)
93     {
94         Assert.assertEquals(libvlc.libvlc_exception_get_message(exception), 0, libvlc
95             .libvlc_exception_raised(exception));
96     }
97
98     private void downloadSample()
99     {
100         OutputStream out = null;
101         URLConnection conn = null;
102         InputStream in = null;
103         URL sampleResource = this.getClass().getResource("/sample.avi");
104         if (sampleResource != null)
105         {
106             log.debug("Sample file already downloaded");
107             mrl = sampleResource.getPath();
108             return;
109         }
110         try
111         {
112             log.info("Downloading sample: {}", address);
113             String testResoucesPath = this.getClass().getResource("/sample").getPath();
114             URL url = new URL(address);
115             out = new BufferedOutputStream(new FileOutputStream(testResoucesPath + ".avi"));
116             conn = url.openConnection();
117             in = conn.getInputStream();
118             byte[] buffer = new byte[1024];
119             int numRead;
120             long numWritten = 0;
121             while ((numRead = in.read(buffer)) != -1)
122             {
123                 out.write(buffer, 0, numRead);
124                 numWritten += numRead;
125             }
126             log.info("Sample downloaded.");
127             mrl = testResoucesPath + ".avi";
128         }
129         catch (Exception e)
130         {
131             log.error("{}", e);
132         }
133         finally
134         {
135             IOUtils.closeQuietly(in);
136             IOUtils.closeQuietly(out);
137         }
138     }
139
140 }