Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial tests for ExportFormat #1010

Merged
merged 4 commits into from
Mar 23, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/test/java/net/sf/jabref/exporter/ExportFormatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package net.sf.jabref.exporter;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Charsets;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefPreferences;
import net.sf.jabref.MetaData;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.BibEntry;

public class ExportFormatTest {

@Before
public void setUp() {
Globals.prefs = JabRefPreferences.getInstance();
ExportFormats.initAllExports();
Globals.journalAbbreviationLoader = new JournalAbbreviationLoader(Globals.prefs);

}

@Test
public void testExportingEmptyDatabaseLayoutBasedFormat() throws Exception {
BibDatabase db = new BibDatabase();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how strict we are, but db is an abbreviation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably copies that line from the exactly same code that was later
commented on. I'll fix it. :-)
Den 21 mar 2016 18:03 skrev "Tobias Diez" notifications@github.com:

In src/test/java/net/sf/jabref/exporter/ExportFormatTest.java
#1010 (comment):

+import net.sf.jabref.model.database.BibDatabase;
+import net.sf.jabref.model.entry.BibEntry;
+
+public class ExportFormatTest {
+

  • @before
  • public void setUp() {
  •    Globals.prefs = JabRefPreferences.getInstance();
    
  •    ExportFormats.initAllExports();
    
  •    Globals.journalAbbreviationLoader = new JournalAbbreviationLoader(Globals.prefs);
    
  • }
  • @test
  • public void testExportingEmptyDatabaseLayoutBasedFormat() throws Exception {
  •    BibDatabase db = new BibDatabase();
    

Not sure how strict we are, but db is an abbreviation.


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/JabRef/jabref/pull/1010/files/7e0bd7c2e6254337ce04085bd5eb3ebd86479c43#r56858544

Map<String, IExportFormat> exportFormats = ExportFormats.getExportFormats();
IExportFormat exportFormat = exportFormats.get("html");
try {
File tmpFile = File.createTempFile("exporttest", "html");
tmpFile.deleteOnExit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a slightly more preferable way to deal with temporary files in form of a rule, see the wiki.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary. Will be deleted after the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sort of assumed it, but better safe than sorry. :-) Will remove.

String filename = tmpFile.getCanonicalPath();
List<BibEntry> entries = Collections.emptyList();
Charset charset = Charsets.UTF_8;
MetaData metaData = new MetaData();
exportFormat.performExport(db, metaData, filename, charset, entries);
} catch (IOException e) {
Assert.fail("Exception caught: " + e.toString() + e.getMessage());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be more readable code if you just skip the try-catch here. The tests will fail anyway if the method is terminated by an exception and we will get all the exception info we need. There is no need to explicitly build an exception string as you do here.

Some code quality tools might unnecessarily complain about missing assertions, though...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lenhard That should be a general rule in my opinion, not to catch Exceptions ins Tests.
Edit// At the moment there are tests that fail but omit stacktrace. I'lll create a new issue for this

}
}

@Test
public void testExportingEmptyDatabaseClassBasedFormat() throws Exception {
BibDatabase db = new BibDatabase();
Map<String, IExportFormat> exportFormats = ExportFormats.getExportFormats();
IExportFormat exportFormat = exportFormats.get("oocalc");
try {
File tmpFile = File.createTempFile("exporttest", "oocalc");
tmpFile.deleteOnExit();
String filename = tmpFile.getCanonicalPath();
List<BibEntry> entries = Collections.emptyList();
Charset charset = Charsets.UTF_8;
MetaData metaData = new MetaData();
exportFormat.performExport(db, metaData, filename, charset, entries);
} catch (IOException e) {
Assert.fail("Exception caught: " + e.toString() + e.getMessage());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here the try-catch can be omitted from my point of view.

}
}

}