Skip to content

Commit eeaf14f

Browse files
committed
fix encoding/decoding of IgnoredNames objects
fixes #1383
1 parent 5026156 commit eeaf14f

File tree

5 files changed

+120
-22
lines changed

5 files changed

+120
-22
lines changed

src/org/opensolaris/opengrok/index/IgnoredDirs.java

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

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

@@ -58,7 +58,7 @@ public IgnoredDirs() {
5858
* @return true if this file should be ignored, false otherwise
5959
*/
6060
public boolean ignore(File file) {
61-
return match(file) && file.isDirectory();
61+
return file.isDirectory() && match(file);
6262
}
6363

6464
/**

src/org/opensolaris/opengrok/index/IgnoredFiles.java

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

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

@@ -72,7 +72,7 @@ public IgnoredFiles() {
7272
* @return true if this file should be ignored, false otherwise
7373
*/
7474
public boolean ignore(File file) {
75-
return match(file) && file.isFile();
75+
return file.isFile() && match(file);
7676
}
7777

7878
/**

src/org/opensolaris/opengrok/index/IgnoredNames.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
*/
1919

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

2525
import java.io.File;
26+
import java.io.Serializable;
2627
import java.util.ArrayList;
2728
import java.util.List;
2829

@@ -31,10 +32,11 @@
3132
*
3233
* @author Vladimir Kotal
3334
*/
34-
public class IgnoredNames {
35+
public class IgnoredNames implements Serializable {
36+
private static final long serialVersionUID = 1L;
3537

36-
private final IgnoredFiles ignoredFiles;
37-
private final IgnoredDirs ignoredDirs;
38+
private IgnoredFiles ignoredFiles;
39+
private IgnoredDirs ignoredDirs;
3840

3941
public IgnoredNames() {
4042
ignoredFiles = new IgnoredFiles();
@@ -48,6 +50,13 @@ public List<String> getItems() {
4850
return twoLists;
4951
}
5052

53+
public void setItems(List<String> item) {
54+
clear();
55+
for (String s : item) {
56+
add(s);
57+
}
58+
}
59+
5160
public void add(String pattern) {
5261
if (pattern.startsWith("f:")) {
5362
ignoredFiles.add(pattern.substring(2));
@@ -59,13 +68,6 @@ public void add(String pattern) {
5968
}
6069
}
6170

62-
public void setItems(List<String> item) {
63-
clear();
64-
for (String s : item) {
65-
add(s);
66-
}
67-
}
68-
6971
/**
7072
* Should the file be ignored or not?
7173
*
@@ -94,4 +96,20 @@ public void clear() {
9496
ignoredFiles.clear();
9597
ignoredDirs.clear();
9698
}
99+
100+
public IgnoredDirs getIgnoredDirs() {
101+
return ignoredDirs;
102+
}
103+
104+
public IgnoredFiles getIgnoredFiles() {
105+
return ignoredFiles;
106+
}
107+
108+
public void setIgnoredDirs(IgnoredDirs i) {
109+
ignoredDirs = i;
110+
}
111+
112+
public void setIgnoredFiles(IgnoredFiles i) {
113+
ignoredFiles = i;
114+
}
97115
}

src/org/opensolaris/opengrok/index/Indexer.java

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

2020
/*
21-
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*
2323
* Portions Copyright 2011 Jens Elkner.
2424
*/
@@ -41,6 +41,7 @@
4141
import java.util.concurrent.TimeUnit;
4242
import java.util.logging.Level;
4343
import java.util.logging.Logger;
44+
import java.util.stream.Collectors;
4445
import org.opensolaris.opengrok.Info;
4546
import org.opensolaris.opengrok.analysis.AnalyzerGuru;
4647
import org.opensolaris.opengrok.configuration.Configuration;
@@ -775,11 +776,12 @@ public int compare(Project p1, Project p2) {
775776
LOGGER.info("Done...");
776777
}
777778

778-
if (refreshHistory) {
779-
if (repositories != null && !repositories.isEmpty()) {
780-
LOGGER.log(Level.INFO, "Generating history cache for specified repositories ...");
781-
HistoryGuru.getInstance().createCache(repositories);
782-
LOGGER.info("Done...");
779+
if (refreshHistory) {
780+
if (repositories != null && !repositories.isEmpty()) {
781+
LOGGER.log(Level.INFO, "Generating history cache for repositories: " +
782+
repositories.stream().collect(Collectors.joining(",")));
783+
HistoryGuru.getInstance().createCache(repositories);
784+
LOGGER.info("Done...");
783785
} else {
784786
LOGGER.log(Level.INFO, "Generating history cache for all repositories ...");
785787
HistoryGuru.getInstance().createCache();

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,34 @@
1818
*/
1919

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

25+
import java.beans.ExceptionListener;
26+
import java.beans.XMLDecoder;
27+
import java.beans.XMLEncoder;
28+
import java.io.BufferedOutputStream;
29+
import java.io.BufferedReader;
2530
import static org.junit.Assert.assertEquals;
2631
import static org.junit.Assert.assertFalse;
2732
import static org.junit.Assert.assertNotNull;
2833
import static org.junit.Assert.assertTrue;
2934

3035
import java.io.File;
36+
import java.io.FileInputStream;
37+
import java.io.FileNotFoundException;
38+
import java.io.FileOutputStream;
39+
import java.io.IOException;
3140
import java.util.ArrayList;
41+
import java.util.LinkedList;
3242
import java.util.List;
43+
import junit.framework.AssertionFailedError;
3344

3445
import org.junit.BeforeClass;
3546
import org.junit.Test;
3647
import org.opensolaris.opengrok.analysis.c.CAnalyzerFactoryTest;
48+
import org.opensolaris.opengrok.util.FileUtilities;
3749
import org.opensolaris.opengrok.util.TestRepository;
3850

3951
/**
@@ -111,4 +123,70 @@ public void testIgnoredPatterns() {
111123
names = instance.getItems();
112124
assertEquals(0, names.size());
113125
}
126+
127+
/**
128+
* Make sure that encoding and decoding IgnoredNames object is 1:1 operation.
129+
* @throws FileNotFoundException
130+
* @throws IOException
131+
*/
132+
@Test
133+
public void testEncodeDecode() throws FileNotFoundException, IOException {
134+
IgnoredNames in = new IgnoredNames();
135+
// Add file and directory to list of ignored items.
136+
in.add("f:foo.txt");
137+
in.add("d:bar");
138+
139+
// Create an exception listener to detect errors while encoding and decoding
140+
final LinkedList<Exception> exceptions = new LinkedList<Exception>();
141+
ExceptionListener listener = new ExceptionListener() {
142+
@Override
143+
public void exceptionThrown(Exception e) {
144+
exceptions.addLast(e);
145+
}
146+
};
147+
148+
// Actually create the file and directory for much better test coverage.
149+
File tmpdir = FileUtilities.createTemporaryDirectory("ignoredNames");
150+
File foo = new File(tmpdir, "foo.txt");
151+
foo.createNewFile();
152+
assertTrue(foo.isFile());
153+
File bar = new File(tmpdir, "bar");
154+
bar.mkdir();
155+
assertTrue(bar.isDirectory());
156+
157+
// Store the IgnoredNames object as XML file.
158+
File testXML = new File(tmpdir, "Test.xml");
159+
XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(testXML)));
160+
e.setExceptionListener(listener);
161+
e.writeObject(in);
162+
e.close();
163+
164+
// Restore the IgnoredNames object from XML file.
165+
XMLDecoder d = new XMLDecoder(new FileInputStream(testXML));
166+
IgnoredNames in2 = (IgnoredNames) d.readObject();
167+
d.close();
168+
169+
// Verify that the XML encoding/decoding did not fail.
170+
if (!exceptions.isEmpty()) {
171+
AssertionFailedError afe = new AssertionFailedError(
172+
"Got " + exceptions.size() + " exception(s)");
173+
// Can only chain one of the exceptions. Take the first one.
174+
afe.initCause(exceptions.getFirst());
175+
throw afe;
176+
}
177+
178+
// Make sure the complete list of items is equal after decoding.
179+
// This will is a simple casual test that cannot verify that sub-classes
180+
// are intact. For that there are the following tests.
181+
assertTrue(in.getItems().containsAll(in2.getItems()));
182+
183+
// Use the restored object to test the matching of file and directory.
184+
assertTrue(in2.ignore("foo.txt"));
185+
assertTrue(in2.ignore("bar"));
186+
assertTrue(in2.ignore(foo));
187+
assertTrue(in2.ignore(bar));
188+
189+
// Cleanup.
190+
FileUtilities.removeDirs(tmpdir);
191+
}
114192
}

0 commit comments

Comments
 (0)