Skip to content

Commit

Permalink
Added tests for StyleLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Mar 28, 2016
1 parent 17e998d commit ab3ed5a
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/sf/jabref/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -1330,4 +1330,5 @@ private static void insertCleanupPreset(Map<String, Object> storage, CleanupPres
storage.put(CLEANUP_FIX_FILE_LINKS, preset.isFixFileLinks());
storage.put(CLEANUP_FORMATTERS, convertListToString(preset.getFormatterCleanups().convertToString()));
}

}
8 changes: 3 additions & 5 deletions src/main/java/net/sf/jabref/openoffice/AutoDetectPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private boolean autoDetectPaths() {
fileSearch.resetFileSearch();
if (OS.WINDOWS) {
List<File> progFiles = fileSearch.findWindowsProgramFilesDir();
List<File> sofficeFiles = new ArrayList<>(fileSearch.findFileDir(progFiles, SOFFICE_EXE));
List<File> sofficeFiles = new ArrayList<>(fileSearch.findFileInDirs(progFiles, SOFFICE_EXE));
if (fileSearchCancelled) {
return false;
}
Expand Down Expand Up @@ -138,7 +138,7 @@ public String getDescription() {
}
} else if (OS.OS_X) {
List<File> dirList = fileSearch.findOSXProgramFilesDir();
List<File> sofficeFiles = new ArrayList<>(fileSearch.findFileDir(dirList, SOFFICE_BIN));
List<File> sofficeFiles = new ArrayList<>(fileSearch.findFileInDirs(dirList, SOFFICE_BIN));

if (fileSearchCancelled) {
return false;
Expand All @@ -150,10 +150,8 @@ public String getDescription() {
return setupPreferencesForOO(rootdir, actualFile.get(), SOFFICE_BIN);
}
}
return false;
} else {
return false;
}
return false;
} else {
// Linux:
String usrRoot = "/usr/lib";
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/openoffice/OOBibBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1135,9 +1135,9 @@ public void combineCiteMarkers(List<BibDatabase> databases, OOBibStyle style) th
while (couldExpand && (compare.compareRegionEnds(mxDocCursor, r2) > 0)) {
couldExpand = mxDocCursor.goRight((short) 1, true);
}
String text = mxDocCursor.getString();
String cursorText = mxDocCursor.getString();
// Check if the string contains no line breaks and only whitespace:
if ((text.indexOf('\n') == -1) && text.trim().isEmpty()) {
if ((cursorText.indexOf('\n') == -1) && cursorText.trim().isEmpty()) {

// If we are supposed to set character format for citations, test this before
// making any changes. This way we can throw an exception before any reference
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/net/sf/jabref/openoffice/OOBibStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,23 +285,28 @@ private void readFormatFile(Reader in) throws IOException {
continue;
}
// Check if we should change mode:
if (line.equals(OOBibStyle.NAME_MARK)) {
switch (line) {
case NAME_MARK:
mode = BibStyleMode.NAME;
continue;
} else if (line.equals(OOBibStyle.LAYOUT_MRK)) {
case LAYOUT_MRK:
mode = BibStyleMode.LAYOUT;
continue;
} else if (line.equals(OOBibStyle.PROPERTIES_MARK)) {
case PROPERTIES_MARK:
mode = BibStyleMode.PROPERTIES;
continue;
} else if (line.equals(OOBibStyle.CITATION_MARK)) {
case CITATION_MARK:
mode = BibStyleMode.CITATION;
continue;
} else if (line.equals(OOBibStyle.JOURNALS_MARK)) {
case JOURNALS_MARK:
mode = BibStyleMode.JOURNALS;
continue;
default:
break;

}

// Handle line depending on mode
switch (mode) {
case NAME:
if (!line.trim().isEmpty()) {
Expand Down Expand Up @@ -361,7 +366,7 @@ private void handleStructureLine(String line) {
if (setDefault) {
defaultBibLayout = layout;
} else {
bibLayout.put(type.toLowerCase(), layout);
bibLayout.put(type.toLowerCase(Locale.ENGLISH), layout);
}

} catch (IOException ex) {
Expand Down Expand Up @@ -409,7 +414,7 @@ private void handleJournalsLine(String line) {
}

public Layout getReferenceFormat(String type) {
Layout l = bibLayout.get(type.toLowerCase());
Layout l = bibLayout.get(type.toLowerCase(Locale.ENGLISH));
if (l == null) {
return defaultBibLayout;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void cancelFileSearch() {
fileSearchCancelled = true;
}

public List<File> findFileDir(List<File> dirList, String filename) {
public List<File> findFileInDirs(List<File> dirList, String filename) {
List<File> sofficeFiles = new ArrayList<>();
for (File dir : dirList) {
if (fileSearchCancelled) {
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/net/sf/jabref/openoffice/StyleLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -32,13 +33,9 @@ public class StyleLoader {


public StyleLoader(OpenOfficePreferences preferences, JournalAbbreviationRepository repository, Charset encoding) {
this.repository = repository;
this.preferences = preferences;
this.encoding = encoding;
update();
}

public void update() {
this.repository = Objects.requireNonNull(repository);
this.preferences = Objects.requireNonNull(preferences);
this.encoding = Objects.requireNonNull(encoding);
loadInternalStyles();
loadExternalStyles();
}
Expand All @@ -50,6 +47,7 @@ public List<OOBibStyle> getStyles() {
}

public void addStyle(String filename) {
Objects.requireNonNull(filename);
try {
OOBibStyle newStyle = new OOBibStyle(new File(filename), repository, encoding);
if (externalStyles.contains(newStyle)) {
Expand Down Expand Up @@ -103,6 +101,7 @@ private void storeExternalStyles() {
}

public boolean removeStyle(OOBibStyle style) {
Objects.requireNonNull(style);
if (!style.isFromResource()) {
boolean result = externalStyles.remove(style);
storeExternalStyles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public void setVisible(boolean visible) {
* settings, and add the styles to the list of styles.
*/
private void updateStyles() {
table.clearSelection();
styles.getReadWriteLock().writeLock().lock();
styles.clear();
styles.addAll(loader.getStyles());
Expand Down
193 changes: 193 additions & 0 deletions src/test/java/net/sf/jabref/openoffice/StyleLoaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package net.sf.jabref.openoffice;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;

import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRef;
import net.sf.jabref.JabRefPreferences;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.logic.journals.JournalAbbreviationRepository;

public class StyleLoaderTest {

private JabRefPreferences backup;

private OpenOfficePreferences preferences;
@Before
public void setUp() {
backup = JabRefPreferences.getInstance();
if (Globals.prefs == null) {
Globals.prefs = JabRefPreferences.getInstance();
}
if (Globals.journalAbbreviationLoader == null) {
Globals.journalAbbreviationLoader = mock(JournalAbbreviationLoader.class);
}
preferences = new OpenOfficePreferences(Globals.prefs);
}

@After
public void tearDown() throws Exception {
Globals.prefs.overwritePreferences(backup);
}

@Test(expected = NullPointerException.class)
public void throwNPEWithNullPreferences() {
StyleLoader loader = new StyleLoader(null,
mock(JournalAbbreviationRepository.class), mock(Charset.class));
fail();
}

@Test(expected = NullPointerException.class)
public void throwNPEWithNullRepository() {
StyleLoader loader = new StyleLoader(mock(OpenOfficePreferences.class),
null, mock(Charset.class));
fail();
}

@Test(expected = NullPointerException.class)
public void throwNPEWithNullCharset() {
StyleLoader loader = new StyleLoader(mock(OpenOfficePreferences.class),
mock(JournalAbbreviationRepository.class), null);
fail();
}

@Test
public void testGetStylesWithEmptyExternal() {
preferences.setExternalStyles(Collections.emptyList());
StyleLoader loader = new StyleLoader(preferences,
mock(JournalAbbreviationRepository.class), Globals.prefs.getDefaultEncoding());

assertEquals(2, loader.getStyles().size());
}

@Test
public void testAddStyleLeadsToOneMoreStyle() throws URISyntaxException {
preferences.setExternalStyles(Collections.emptyList());
StyleLoader loader = new StyleLoader(preferences,
mock(JournalAbbreviationRepository.class), Globals.prefs.getDefaultEncoding());
int beforeAdding = loader.getStyles().size();
String filename = Paths.get(JabRef.class.getResource(StyleLoader.DEFAULT_AUTHORYEAR_STYLE_PATH).toURI())
.toFile().getPath();
loader.addStyle(filename);
assertEquals(beforeAdding + 1, loader.getStyles().size());
}

@Test
public void testAddInvalidStyleLeadsToNoMoreStyle() {
preferences.setExternalStyles(Collections.emptyList());
Globals.prefs.putStringList(JabRefPreferences.OO_EXTERNAL_STYLE_FILES, Collections.emptyList());
StyleLoader loader = new StyleLoader(preferences,
mock(JournalAbbreviationRepository.class), Globals.prefs.getDefaultEncoding());
int beforeAdding = loader.getStyles().size();
loader.addStyle("DefinitelyNotAValidFileNameOrWeAreExtremelyUnlucky");
assertEquals(beforeAdding, loader.getStyles().size());
}

@Test
public void testInitalizeWithOneExternalFile() throws URISyntaxException {
String filename = Paths.get(JabRef.class.getResource(StyleLoader.DEFAULT_AUTHORYEAR_STYLE_PATH).toURI())
.toFile().getPath();
preferences.setExternalStyles(Collections.singletonList(filename));
StyleLoader loader = new StyleLoader(preferences,
mock(JournalAbbreviationRepository.class), Globals.prefs.getDefaultEncoding());
assertEquals(3, loader.getStyles().size());
}

@Test
public void testInitalizeWithIncorrectExternalFile() {
preferences.setExternalStyles(Collections.singletonList("DefinitelyNotAValidFileNameOrWeAreExtremelyUnlucky"));

StyleLoader loader = new StyleLoader(new OpenOfficePreferences(Globals.prefs),
mock(JournalAbbreviationRepository.class), Globals.prefs.getDefaultEncoding());
assertEquals(2, loader.getStyles().size());
}

@Test
public void testInitalizeWithOneExternalFileRemoveStyle() throws URISyntaxException {
String filename = Paths.get(JabRef.class.getResource(StyleLoader.DEFAULT_AUTHORYEAR_STYLE_PATH).toURI())
.toFile().getPath();
preferences.setExternalStyles(Collections.singletonList(filename));

StyleLoader loader = new StyleLoader(new OpenOfficePreferences(Globals.prefs),
mock(JournalAbbreviationRepository.class), Globals.prefs.getDefaultEncoding());
List<OOBibStyle> toremove = new ArrayList<>();
int beforeRemoving = loader.getStyles().size();
for (OOBibStyle style : loader.getStyles()) {
if (!style.isFromResource()) {
toremove.add(style);
}
}

for (OOBibStyle style : toremove) {
assertTrue(loader.removeStyle(style));
}
assertEquals(beforeRemoving - 1, loader.getStyles().size());
}

@Test
public void testInitalizeWithOneExternalFileRemoveStyleUpdatesPreferences() throws URISyntaxException {
String filename = Paths.get(JabRef.class.getResource(StyleLoader.DEFAULT_AUTHORYEAR_STYLE_PATH).toURI())
.toFile().getPath();
preferences.setExternalStyles(Collections.singletonList(filename));

StyleLoader loader = new StyleLoader(preferences,
mock(JournalAbbreviationRepository.class), Globals.prefs.getDefaultEncoding());
List<OOBibStyle> toremove = new ArrayList<>();
for (OOBibStyle style : loader.getStyles()) {
if (!style.isFromResource()) {
toremove.add(style);
}
}

for (OOBibStyle style : toremove) {
assertTrue(loader.removeStyle(style));
}
assertTrue(preferences.getExternalStyles().isEmpty());
}

@Test
public void testAddSameStyleTwiceLeadsToOneMoreStyle() throws URISyntaxException {
preferences.setExternalStyles(Collections.emptyList());
StyleLoader loader = new StyleLoader(new OpenOfficePreferences(Globals.prefs),
mock(JournalAbbreviationRepository.class), Globals.prefs.getDefaultEncoding());
int beforeAdding = loader.getStyles().size();
String filename = Paths.get(JabRef.class.getResource(StyleLoader.DEFAULT_AUTHORYEAR_STYLE_PATH).toURI())
.toFile().getPath();
loader.addStyle(filename);
loader.addStyle(filename);
assertEquals(beforeAdding + 1, loader.getStyles().size());
}

@Test(expected = NullPointerException.class)
public void testAddNullStyleThrowsNPE() {
StyleLoader loader = new StyleLoader(new OpenOfficePreferences(Globals.prefs),
mock(JournalAbbreviationRepository.class), Globals.prefs.getDefaultEncoding());
loader.addStyle(null);
fail();
}


@Test
public void testGetDefaultUsedStyle() {
Globals.prefs.remove(JabRefPreferences.OO_BIBLIOGRAPHY_STYLE_FILE);
StyleLoader loader = new StyleLoader(new OpenOfficePreferences(Globals.prefs),
mock(JournalAbbreviationRepository.class), Globals.prefs.getDefaultEncoding());
OOBibStyle style = loader.getUsedStyle();
assertTrue(style.isValid());
assertEquals(StyleLoader.DEFAULT_AUTHORYEAR_STYLE_PATH, style.getPath());
assertEquals(StyleLoader.DEFAULT_AUTHORYEAR_STYLE_PATH, preferences.getCurrentStyle());
}

}

0 comments on commit ab3ed5a

Please sign in to comment.