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
.newContext();
58 assertNull(context
.get(FILE
));
60 ((Subtitles
) context
.get(SUBTITLES
)).load(file
);
62 assertSame(file
, context
.get(FILE
));
63 assertFalse((Boolean
) context
.get(FILE_MODIFIED
));
64 assertEquals("00:00:08,040", TIME_FORMAT
.format(context
.get(FIRST_TIME
)));
65 assertEquals("01:10:27,760", TIME_FORMAT
.format(context
.get(LAST_TIME
)));
67 context
.set(FIRST_TIME
, TIME_FORMAT
.parse("00:00:10,000"));
68 context
.set(LAST_TIME
, TIME_FORMAT
.parse("01:10:40,000"));
70 assertTrue((Boolean
) context
.get(FILE_MODIFIED
));
72 ((Subtitles
) context
.get(SUBTITLES
)).save();
74 assertFalse((Boolean
) context
.get(FILE_MODIFIED
));
76 ((Subtitles
) context
.get(SUBTITLES
)).load(file
);
78 assertSame(file
, context
.get(FILE
));
79 assertFalse((Boolean
) context
.get(FILE_MODIFIED
));
80 assertEquals("00:00:10,000", TIME_FORMAT
.format(context
.get(FIRST_TIME
)));
81 assertEquals("01:10:40,000", TIME_FORMAT
.format(context
.get(LAST_TIME
)));
85 public final void testLoad() {
86 final Context context
= SubtitlesAdjuster
.newContext();
88 assertNull(context
.get(FILE
));
90 ((Subtitles
) context
.get(SUBTITLES
)).load(SRT_FILE
);
92 assertSame(SRT_FILE
, context
.get(FILE
));
93 assertEquals("00:00:08,040", TIME_FORMAT
.format(context
.get(FIRST_TIME
)));
94 assertEquals("01:10:27,760", TIME_FORMAT
.format(context
.get(LAST_TIME
)));
97 private static final String SRT_RESOURCE_PATH
= getThisPackagePath() + "REC.en.srt";
99 private static final File SRT_FILE
= getFile(SRT_RESOURCE_PATH
);
103 * @param resourcePath
109 private static final InputStream
getInputStream(final String resourcePath
) {
110 return getCallerClass().getClassLoader().getResourceAsStream(resourcePath
);
115 * @param resourcePath
121 private static final File
getFile(final String resourcePath
) {
123 return new File(getCallerClass().getClassLoader().getResource(resourcePath
).toURI());
124 } catch (final URISyntaxException exception
) {
125 throw unchecked(exception
);
130 * Creates and initializes a temporary file with data read from {@code contentsSource};
131 * closes {@code contentsSource} when the initialization is done.
137 * @param contentsSource
140 * @return A file that will be deleted upon exit
144 private static final File
createTemporaryFile(final String prefix
, final String suffix
, final InputStream contentsSource
) {
145 OutputStream output
= null;
148 final File result
= File
.createTempFile(prefix
, suffix
);
150 result
.deleteOnExit();
152 output
= new FileOutputStream(result
);
154 write(contentsSource
, output
);
157 } catch (final IOException exception
) {
158 throw unchecked(exception
);
161 close(contentsSource
);
173 * @throws IOException if an I/O error occurs
175 private static final void write(final InputStream source
, final OutputStream destination
) throws IOException
{
176 final byte[] buffer
= new byte[1024];
177 int readBytes
= source
.read(buffer
);
179 while (readBytes
> 0) {
180 destination
.write(buffer
, 0, readBytes
);
181 readBytes
= source
.read(buffer
);
186 * Tries to close {@code closable} using reflection and logs the eventual error;
187 * does nothing if {@code closable} is null.
193 private static final void close(final Object closable
) {
194 if (closable
== null) {
199 invoke(closable
, "close");
200 } catch (final Throwable error
) {
201 Logger
.getLogger(getCallerClass().getName() + "." + getCallerMethodName())
202 .log(Level
.WARNING
, "", error
);