Skip to content

Commit d76b660

Browse files
ahornaceVladimir Kotal
authored andcommitted
Remove redundant createTemporaryDirectory method from FileUtilities
1 parent 0288eee commit d76b660

File tree

9 files changed

+80
-168
lines changed

9 files changed

+80
-168
lines changed

test/org/opensolaris/opengrok/configuration/RuntimeEnvironmentTest.java

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opensolaris.opengrok.configuration;
@@ -32,7 +32,7 @@
3232
import java.net.InetSocketAddress;
3333
import java.net.SocketAddress;
3434
import java.nio.file.Files;
35-
import java.nio.file.Paths;
35+
import java.nio.file.Path;
3636
import java.util.ArrayList;
3737
import java.util.Arrays;
3838
import java.util.Date;
@@ -43,7 +43,6 @@
4343
import java.util.regex.PatternSyntaxException;
4444
import org.apache.tools.ant.filters.StringInputStream;
4545
import org.json.simple.parser.ParseException;
46-
import org.junit.After;
4746
import org.junit.AfterClass;
4847
import org.junit.Assert;
4948
import org.junit.Before;
@@ -65,7 +64,6 @@
6564
import static org.junit.Assert.assertSame;
6665
import static org.junit.Assert.assertTrue;
6766
import static org.junit.Assert.fail;
68-
import org.opensolaris.opengrok.util.FileUtilities;
6967
import org.opensolaris.opengrok.util.ForbiddenSymlinkException;
7068
import org.opensolaris.opengrok.util.IOUtils;
7169

@@ -97,16 +95,12 @@ public static void tearDownClass() throws Exception {
9795
}
9896

9997
@Before
100-
public void setUp() throws IOException {
98+
public void setUp() {
10199
// Create a default configuration
102100
Configuration config = new Configuration();
103101
RuntimeEnvironment.getInstance().setConfiguration(config);
104102
}
105103

106-
@After
107-
public void tearDown() throws IOException {
108-
}
109-
110104
@Test
111105
public void testDataRoot() throws IOException {
112106
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
@@ -160,7 +154,7 @@ public void testProjects() throws IOException {
160154
}
161155

162156
@Test
163-
public void testGroups() throws IOException {
157+
public void testGroups() {
164158
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
165159
assertFalse(instance.hasGroups());
166160
assertNotNull(instance.getGroups());
@@ -178,18 +172,14 @@ public void testGroups() throws IOException {
178172
}
179173

180174
@Test
181-
public void testRegister() throws InterruptedException, IOException {
175+
public void testRegister() throws InterruptedException {
182176
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
183177
String path = "/tmp/dataroot";
184178
instance.setDataRoot(path);
185179
instance.register();
186-
Thread t = new Thread(new Runnable() {
187-
188-
public void run() {
189-
Configuration c = new Configuration();
190-
RuntimeEnvironment.getInstance().setConfiguration(c);
191-
192-
}
180+
Thread t = new Thread(() -> {
181+
Configuration c = new Configuration();
182+
RuntimeEnvironment.getInstance().setConfiguration(c);
193183
});
194184
t.start();
195185
t.join();
@@ -232,17 +222,17 @@ public void testHistoryReaderTimeLimit() {
232222
@Test
233223
public void testFetchHistoryWhenNotInCache() {
234224
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
235-
assertEquals(true, instance.isFetchHistoryWhenNotInCache());
225+
assertTrue(instance.isFetchHistoryWhenNotInCache());
236226
instance.setFetchHistoryWhenNotInCache(false);
237-
assertEquals(false, instance.isFetchHistoryWhenNotInCache());
227+
assertFalse(instance.isFetchHistoryWhenNotInCache());
238228
}
239229

240230
@Test
241231
public void testUseHistoryCache() {
242232
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
243-
assertEquals(true, instance.useHistoryCache());
233+
assertTrue(instance.useHistoryCache());
244234
instance.setUseHistoryCache(false);
245-
assertEquals(false, instance.useHistoryCache());
235+
assertFalse(instance.useHistoryCache());
246236
}
247237

248238
@Test
@@ -1052,13 +1042,13 @@ public void testSaveStatistics() throws IOException {
10521042
}
10531043

10541044
@Test(expected = IOException.class)
1055-
public void testSaveNullStatistics() throws IOException, ParseException {
1045+
public void testSaveNullStatistics() throws IOException {
10561046
RuntimeEnvironment.getInstance().getConfiguration().setStatisticsFilePath(null);
10571047
RuntimeEnvironment.getInstance().saveStatistics();
10581048
}
10591049

10601050
@Test(expected = IOException.class)
1061-
public void testSaveNullStatisticsFile() throws IOException, ParseException {
1051+
public void testSaveNullStatisticsFile() throws IOException {
10621052
RuntimeEnvironment.getInstance().saveStatistics((File) null);
10631053
}
10641054

@@ -1085,7 +1075,7 @@ public void testGetPathRelativeToSourceRoot() throws IOException,
10851075
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
10861076

10871077
// Create and set source root.
1088-
File sourceRoot = FileUtilities.createTemporaryDirectory("src");
1078+
File sourceRoot = Files.createTempDirectory("src").toFile();
10891079
assertTrue(sourceRoot.exists());
10901080
assertTrue(sourceRoot.isDirectory());
10911081
env.setSourceRoot(sourceRoot.getPath());
@@ -1100,11 +1090,9 @@ public void testGetPathRelativeToSourceRoot() throws IOException,
11001090

11011091
// Create symlink underneath source root.
11021092
String symlinkName = "symlink";
1103-
File realDir = FileUtilities.createTemporaryDirectory("realdir");
1093+
Path realDir = Files.createTempDirectory("realdir");
11041094
File symlink = new File(sourceRoot, symlinkName);
1105-
Files.createSymbolicLink(
1106-
Paths.get(symlink.getPath()),
1107-
Paths.get(realDir.getPath()));
1095+
Files.createSymbolicLink(symlink.toPath(), realDir);
11081096
assertTrue(symlink.exists());
11091097
env.setAllowedSymlinks(new HashSet<>());
11101098
ForbiddenSymlinkException expex = null;
@@ -1113,8 +1101,8 @@ public void testGetPathRelativeToSourceRoot() throws IOException,
11131101
} catch (ForbiddenSymlinkException e) {
11141102
expex = e;
11151103
}
1116-
assertTrue("getPathRelativeToSourceRoot() should have thrown " +
1117-
"IOexception for symlink that is not allowed", expex != null);
1104+
assertNotNull("getPathRelativeToSourceRoot() should have thrown " +
1105+
"IOexception for symlink that is not allowed", expex);
11181106

11191107
// Allow the symlink and retest.
11201108
env.setAllowedSymlinks(new HashSet<>(Arrays.asList(symlink.getPath())));
@@ -1123,6 +1111,6 @@ public void testGetPathRelativeToSourceRoot() throws IOException,
11231111

11241112
// cleanup
11251113
IOUtils.removeRecursive(sourceRoot.toPath());
1126-
IOUtils.removeRecursive(realDir.toPath());
1114+
IOUtils.removeRecursive(realDir);
11271115
}
11281116
}

test/org/opensolaris/opengrok/index/IgnoredNamesTest.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,24 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.index;
2424

2525
import java.beans.ExceptionListener;
2626
import java.beans.XMLDecoder;
2727
import java.beans.XMLEncoder;
2828
import java.io.BufferedOutputStream;
29-
import java.io.BufferedReader;
3029
import static org.junit.Assert.assertEquals;
3130
import static org.junit.Assert.assertFalse;
3231
import static org.junit.Assert.assertNotNull;
3332
import static org.junit.Assert.assertTrue;
3433

3534
import java.io.File;
3635
import java.io.FileInputStream;
37-
import java.io.FileNotFoundException;
3836
import java.io.FileOutputStream;
3937
import java.io.IOException;
38+
import java.nio.file.Files;
4039
import java.util.ArrayList;
4140
import java.util.LinkedList;
4241
import java.util.List;
@@ -55,7 +54,8 @@
5554
* @author Trond Norbye
5655
*/
5756
public class IgnoredNamesTest {
58-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
57+
58+
private RuntimeEnvironment env = RuntimeEnvironment.getInstance();
5959
private static TestRepository repository;
6060

6161
@BeforeClass
@@ -155,27 +155,20 @@ public void testIgnoredPatterns() {
155155

156156
/**
157157
* Make sure that encoding and decoding IgnoredNames object is 1:1 operation.
158-
* @throws FileNotFoundException
159-
* @throws IOException
160158
*/
161159
@Test
162-
public void testEncodeDecode() throws FileNotFoundException, IOException {
160+
public void testEncodeDecode() throws IOException {
163161
IgnoredNames in = new IgnoredNames();
164162
// Add file and directory to list of ignored items.
165163
in.add("f:foo.txt");
166164
in.add("d:bar");
167165

168166
// Create an exception listener to detect errors while encoding and decoding
169-
final LinkedList<Exception> exceptions = new LinkedList<Exception>();
170-
ExceptionListener listener = new ExceptionListener() {
171-
@Override
172-
public void exceptionThrown(Exception e) {
173-
exceptions.addLast(e);
174-
}
175-
};
167+
final LinkedList<Exception> exceptions = new LinkedList<>();
168+
ExceptionListener listener = exceptions::addLast;
176169

177170
// Actually create the file and directory for much better test coverage.
178-
File tmpdir = FileUtilities.createTemporaryDirectory("ignoredNames");
171+
File tmpdir = Files.createTempDirectory("ignoredNames").toFile();
179172
File foo = new File(tmpdir, "foo.txt");
180173
foo.createNewFile();
181174
assertTrue(foo.isFile());

test/org/opensolaris/opengrok/index/IndexVersionTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.net.URL;
2828
import java.nio.file.Files;
2929
import java.nio.file.Path;
30-
import java.nio.file.Paths;
3130
import java.util.ArrayList;
3231
import org.junit.After;
3332
import static org.junit.Assert.assertNotNull;
@@ -55,9 +54,9 @@ public class IndexVersionTest {
5554
@Rule
5655
public ConditionalRunRule rule = new ConditionalRunRule();
5756

58-
TestRepository repository;
59-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
60-
private File oldIndexDataDir;
57+
private TestRepository repository;
58+
private RuntimeEnvironment env = RuntimeEnvironment.getInstance();
59+
private Path oldIndexDataDir;
6160

6261
@BeforeClass
6362
public static void setUpClass() {
@@ -78,7 +77,7 @@ public void tearDown() throws IOException {
7877
repository.destroy();
7978

8079
if (oldIndexDataDir != null) {
81-
IOUtils.removeRecursive(Paths.get(oldIndexDataDir.getPath()));
80+
IOUtils.removeRecursive(oldIndexDataDir);
8281
}
8382
}
8483

@@ -117,8 +116,8 @@ public void testIndexVersionProjects() throws Exception {
117116
@Test(expected = IndexVersion.IndexVersionException.class)
118117
public void testIndexVersionOldIndex() throws Exception {
119118
Configuration cfg = new Configuration();
120-
oldIndexDataDir = FileUtilities.createTemporaryDirectory("data");
121-
Path indexPath = Paths.get(oldIndexDataDir.getPath(), "index");
119+
oldIndexDataDir = Files.createTempDirectory("data");
120+
Path indexPath = oldIndexDataDir.resolve("index");
122121
Files.createDirectory(indexPath);
123122
File indexDir = new File(indexPath.toString());
124123
assertTrue("index directory check", indexDir.isDirectory());
@@ -127,7 +126,7 @@ public void testIndexVersionOldIndex() throws Exception {
127126
File archive = new File(oldindex.getPath());
128127
assertTrue("archive exists", archive.isFile());
129128
FileUtilities.extractArchive(archive, indexDir);
130-
cfg.setDataRoot(oldIndexDataDir.getPath());
129+
cfg.setDataRoot(oldIndexDataDir.toString());
131130
cfg.setProjectsEnabled(false);
132131
IndexVersion.check(cfg);
133132
}

test/org/opensolaris/opengrok/index/IndexerRepoTest.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@
2828

2929
import java.io.IOException;
3030
import java.nio.file.Files;
31+
import java.nio.file.Path;
3132
import java.nio.file.Paths;
3233
import java.util.ArrayList;
3334
import java.util.Arrays;
3435
import java.util.HashSet;
3536
import java.util.List;
3637

3738
import org.junit.After;
39+
40+
import static org.junit.Assert.assertFalse;
3841
import static org.junit.Assert.assertNotNull;
3942
import static org.junit.Assert.assertNull;
4043
import static org.junit.Assert.assertTrue;
@@ -51,7 +54,6 @@
5154
import org.opensolaris.opengrok.history.HistoryGuru;
5255
import org.opensolaris.opengrok.history.MercurialRepositoryTest;
5356
import org.opensolaris.opengrok.history.RepositoryInfo;
54-
import org.opensolaris.opengrok.util.FileUtilities;
5557
import org.opensolaris.opengrok.util.TestRepository;
5658
import org.opensolaris.opengrok.util.IOUtils;
5759

@@ -66,7 +68,7 @@ public class IndexerRepoTest {
6668
@Rule
6769
public ConditionalRunRule rule = new ConditionalRunRule();
6870

69-
TestRepository repository;
71+
private TestRepository repository;
7072

7173
@Before
7274
public void setUp() throws IOException {
@@ -94,15 +96,12 @@ private void checkNumberOfThreads() {
9496
if (threads[i] == null || threads[i].getName() == null) {
9597
continue;
9698
}
97-
assertEquals(false, threads[i].getName().contains("renamed-handling"));
99+
assertFalse(threads[i].getName().contains("renamed-handling"));
98100
}
99101
}
100102

101103
/**
102104
* Test it is possible to disable history per project.
103-
* @throws IndexerException
104-
* @throws IOException
105-
* @throws org.opensolaris.opengrok.history.HistoryException
106105
*/
107106
@ConditionalRun(RepositoryInstalled.MercurialInstalled.class)
108107
@ConditionalRun(RepositoryInstalled.GitInstalled.class)
@@ -113,9 +112,6 @@ public void testPerProjectHistoryGlobalOn() throws IndexerException, IOException
113112

114113
/**
115114
* Test it is possible to enable history per project.
116-
* @throws IndexerException
117-
* @throws IOException
118-
* @throws org.opensolaris.opengrok.history.HistoryException
119115
*/
120116
@ConditionalRun(RepositoryInstalled.MercurialInstalled.class)
121117
@ConditionalRun(RepositoryInstalled.GitInstalled.class)
@@ -128,7 +124,7 @@ private void testPerProjectHistory(boolean globalOn) throws IndexerException, IO
128124
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
129125

130126
// Make sure we start from scratch.
131-
File dataRoot = FileUtilities.createTemporaryDirectory("dataForPerProjectHistoryTest");
127+
File dataRoot = Files.createTempDirectory("dataForPerProjectHistoryTest").toFile();
132128
env.setDataRoot(dataRoot.getName());
133129
env.setProjectsEnabled(true);
134130
env.setHistoryEnabled(globalOn);
@@ -185,22 +181,22 @@ public void testSymlinks() throws IndexerException, IOException {
185181
// Set source root to pristine directory so that there is only one
186182
// repository to deal with (which makes this faster and easier to write)
187183
// and clone the mercurial repository outside of the source root.
188-
File realSource = FileUtilities.createTemporaryDirectory("real");
189-
File sourceRoot = FileUtilities.createTemporaryDirectory("src");
190-
MercurialRepositoryTest.runHgCommand(sourceRoot,
184+
Path realSource = Files.createTempDirectory("real");
185+
Path sourceRoot = Files.createTempDirectory("src");
186+
MercurialRepositoryTest.runHgCommand(sourceRoot.toFile(),
191187
"clone", repository.getSourceRoot() + File.separator + "mercurial",
192-
realSource.getPath());
188+
realSource.toString());
193189

194190
// Create symlink from source root to the real repository.
195191
String symlinkPath = sourceRoot.toString() + File.separator + SYMLINK;
196-
Files.createSymbolicLink(Paths.get(symlinkPath), Paths.get(realSource.getPath()));
192+
Files.createSymbolicLink(Paths.get(symlinkPath), realSource);
197193

198194
// Use alternative source root.
199195
env.setSourceRoot(sourceRoot.toString());
200196
// Need to have history cache enabled in order to perform scan of repositories.
201197
env.setHistoryEnabled(true);
202198
// Normally the Indexer would add the symlink automatically.
203-
env.setAllowedSymlinks(new HashSet<String>(Arrays.asList(symlinkPath)));
199+
env.setAllowedSymlinks(new HashSet<>(Arrays.asList(symlinkPath)));
204200

205201
// Do a rescan of the projects, and only that (we don't care about
206202
// the other aspects of indexing in this test case).
@@ -234,8 +230,8 @@ public void testSymlinks() throws IndexerException, IOException {
234230
assertTrue(HistoryGuru.getInstance().hasCacheForFile(fileInRepo));
235231

236232
// cleanup
237-
IOUtils.removeRecursive(realSource.toPath());
238-
IOUtils.removeRecursive(sourceRoot.toPath());
233+
IOUtils.removeRecursive(realSource);
234+
IOUtils.removeRecursive(sourceRoot);
239235
}
240236

241237
/**

0 commit comments

Comments
 (0)