[SubtitlesAdjuster]
[aprog.git] / SubtitlesAdjuster / test / net / sourceforge / aprog / subtitlesadjuster / SubtitlesTest.java
blob4fa83af37dc1f3e347d720f3c7f3b20126b70e7f
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.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)));
77 @Test
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);
94 /**
96 * @param resourcePath
97 * <br>Not null
98 * @return
99 * <br>Not null
100 * <br>New
102 private static final InputStream getInputStream(final String resourcePath) {
103 return getCallerClass().getClassLoader().getResourceAsStream(resourcePath);
108 * @param resourcePath
109 * <br>Not null
110 * @return
111 * <br>Not null
112 * <br>New
114 private static final File getFile(final String resourcePath) {
115 try {
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.
126 * @param prefix
127 * <br>Not null
128 * @param suffix
129 * <br>Not null
130 * @param contentsSource
131 * <br>Not null
132 * <br>Input-output
133 * @return A file that will be deleted upon exit
134 * <br>Not null
135 * <br>New
137 private static final File createTemporaryFile(final String prefix, final String suffix, final InputStream contentsSource) {
138 OutputStream output = null;
140 try {
141 final File result = File.createTempFile(prefix, suffix);
143 result.deleteOnExit();
145 output = new FileOutputStream(result);
147 write(contentsSource, output);
149 return result;
150 } catch (final IOException exception) {
151 throw unchecked(exception);
152 } finally {
153 close(output);
154 close(contentsSource);
160 * @param source
161 * <br>Not null
162 * <br>Input-output
163 * @param destination
164 * <br>Not null
165 * <br>Input-output
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.
182 * @param closable
183 * <br>Maybe null
184 * <br>Input-output
186 private static final void close(final Object closable) {
187 if (closable == null) {
188 return;
191 try {
192 invoke(closable, "close");
193 } catch (final Throwable error) {
194 Logger.getLogger(getCallerClass().getName() + "." + getCallerMethodName())
195 .log(Level.WARNING, "", error);