[SubtitlesAdjuster]
[aprog.git] / SubtitlesAdjuster / test / net / sourceforge / aprog / subtitlesadjuster / SubtitlesTest.java
blob119b51fad0290188d293dee5fe687a1fbf66389e
1 /*
2 * The MIT License
3 *
4 * Copyright 2010 Codist Monk.
5 *
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
22 * THE SOFTWARE.
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.*;
34 import java.io.File;
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;
47 /**
49 * @author codistmonk (creation 2010-06-28)
51 public final class SubtitlesTest {
53 @Test
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)));
84 @Test
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
104 * <br>Not null
105 * @return
106 * <br>Not null
107 * <br>New
109 private static final InputStream getInputStream(final String resourcePath) {
110 return getCallerClass().getClassLoader().getResourceAsStream(resourcePath);
115 * @param resourcePath
116 * <br>Not null
117 * @return
118 * <br>Not null
119 * <br>New
121 private static final File getFile(final String resourcePath) {
122 try {
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.
133 * @param prefix
134 * <br>Not null
135 * @param suffix
136 * <br>Not null
137 * @param contentsSource
138 * <br>Not null
139 * <br>Input-output
140 * @return A file that will be deleted upon exit
141 * <br>Not null
142 * <br>New
144 private static final File createTemporaryFile(final String prefix, final String suffix, final InputStream contentsSource) {
145 OutputStream output = null;
147 try {
148 final File result = File.createTempFile(prefix, suffix);
150 result.deleteOnExit();
152 output = new FileOutputStream(result);
154 write(contentsSource, output);
156 return result;
157 } catch (final IOException exception) {
158 throw unchecked(exception);
159 } finally {
160 close(output);
161 close(contentsSource);
167 * @param source
168 * <br>Not null
169 * <br>Input-output
170 * @param destination
171 * <br>Not null
172 * <br>Input-output
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.
189 * @param closable
190 * <br>Maybe null
191 * <br>Input-output
193 private static final void close(final Object closable) {
194 if (closable == null) {
195 return;
198 try {
199 invoke(closable, "close");
200 } catch (final Throwable error) {
201 Logger.getLogger(getCallerClass().getName() + "." + getCallerMethodName())
202 .log(Level.WARNING, "", error);