4 * Copyright 2010 Codist Monk.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package net
.sourceforge
.aprog
.subtitlesadjuster
;
27 import java
.text
.ParseException
;
28 import static net
.sourceforge
.aprog
.subtitlesadjuster
.Constants
.Variables
.*;
29 import static net
.sourceforge
.aprog
.subtitlesadjuster
.Subtitles
.TIME_FORMAT
;
30 import static net
.sourceforge
.aprog
.tools
.Tools
.*;
32 import static org
.junit
.Assert
.*;
35 import java
.io
.FileOutputStream
;
36 import java
.io
.InputStream
;
37 import java
.io
.IOException
;
38 import java
.io
.OutputStream
;
39 import java
.util
.logging
.Level
;
40 import java
.util
.logging
.Logger
;
41 import java
.net
.URISyntaxException
;
43 import net
.sourceforge
.aprog
.context
.Context
;
45 import org
.junit
.Test
;
49 * @author codistmonk (creation 2010-06-28)
51 public final class SubtitlesTest
{
54 public final void testSave() throws ParseException
{
55 final File file
= createTemporaryFile("tmp", ".srt", getInputStream(SRT_RESOURCE_PATH
));
56 final Context context
= SubtitlesAdjuster
.createContext();
58 assertNull(context
.get(FILE
));
60 ((Subtitles
) context
.get(SUBTITLES
)).load(file
);
62 assertSame(file
, context
.get(FILE
));
63 assertEquals("00:00:08,040", TIME_FORMAT
.format(context
.get(FIRST_TIME
)));
64 assertEquals("01:10:27,760", TIME_FORMAT
.format(context
.get(LAST_TIME
)));
66 context
.set(FIRST_TIME
, TIME_FORMAT
.parse("00:00:10,000"));
67 context
.set(LAST_TIME
, TIME_FORMAT
.parse("01:10:40,000"));
69 ((Subtitles
) context
.get(SUBTITLES
)).save();
70 ((Subtitles
) context
.get(SUBTITLES
)).load(file
);
72 assertSame(file
, context
.get(FILE
));
73 assertEquals("00:00:10,040", TIME_FORMAT
.format(context
.get(FIRST_TIME
)));
74 assertEquals("01:10:40,000", TIME_FORMAT
.format(context
.get(LAST_TIME
)));
78 public final void testLoad() {
79 final Context context
= SubtitlesAdjuster
.createContext();
81 assertNull(context
.get(FILE
));
83 ((Subtitles
) context
.get(SUBTITLES
)).load(SRT_FILE
);
85 assertSame(SRT_FILE
, context
.get(FILE
));
86 assertEquals("00:00:08,040", TIME_FORMAT
.format(context
.get(FIRST_TIME
)));
87 assertEquals("01:10:27,760", TIME_FORMAT
.format(context
.get(LAST_TIME
)));
90 private static final String SRT_RESOURCE_PATH
= getCallerPackagePath() + "REC.en.srt";
92 private static final File SRT_FILE
= getFile(SRT_RESOURCE_PATH
);
102 private static final InputStream
getInputStream(final String resourcePath
) {
103 return getCallerClass().getClassLoader().getResourceAsStream(resourcePath
);
108 * @param resourcePath
114 private static final File
getFile(final String resourcePath
) {
116 return new File(getCallerClass().getClassLoader().getResource(resourcePath
).toURI());
117 } catch (final URISyntaxException exception
) {
118 throw unchecked(exception
);
123 * Creates and initializes a temporary file with data read from {@code contentsSource};
124 * closes {@code contentsSource} when the initialization is done.
130 * @param contentsSource
133 * @return A file that will be deleted upon exit
137 private static final File
createTemporaryFile(final String prefix
, final String suffix
, final InputStream contentsSource
) {
138 OutputStream output
= null;
141 final File result
= File
.createTempFile(prefix
, suffix
);
143 result
.deleteOnExit();
145 output
= new FileOutputStream(result
);
147 write(contentsSource
, output
);
150 } catch (final IOException exception
) {
151 throw unchecked(exception
);
154 close(contentsSource
);
166 * @throws IOException if an I/O error occurs
168 private static final void write(final InputStream source
, final OutputStream destination
) throws IOException
{
169 final byte[] buffer
= new byte[1024];
170 int readBytes
= source
.read(buffer
);
172 while (readBytes
> 0) {
173 destination
.write(buffer
, 0, readBytes
);
174 readBytes
= source
.read(buffer
);
179 * Tries to close {@code closable} using reflection and logs the eventual error;
180 * does nothing if {@code closable} is null.
186 private static final void close(final Object closable
) {
187 if (closable
== null) {
192 invoke(closable
, "close");
193 } catch (final Throwable error
) {
194 Logger
.getLogger(getCallerClass().getName() + "." + getCallerMethodName())
195 .log(Level
.WARNING
, "", error
);