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

Copacimportertest #310

Merged
merged 8 commits into from
Apr 15, 2016
Merged

Copacimportertest #310

merged 8 commits into from
Apr 15, 2016

Conversation

zesaro
Copy link
Contributor

@zesaro zesaro commented Nov 10, 2015

New PR on a new branch.
Edited Tests. No assignments. Better formatting. Cover DT (documenttype).
New Test in case an empty Text is imported.

Edit: Added the suggestions into the testfile. Checking the equality of Copac and RIS is ongoing.

@simonharrer
Copy link
Contributor

Can you please have a look at https://ondemand.coverity.com/jobs/uj1ep5hu8118n66eqa61rs5ofc/results/f/4/8/9/620b152144b541f48db807c7eed9.html which indicates issues in the test class. Please close the resources with a try-with-resources block.

Regarding the testIsRecognizedFormat, I think it would be better if there are two lists, one for the accepted and one for the rejected files which can then be iterated and check in a for loop each. This would allow to add all the other files from the other importers that will trickle in over the next month.

@zesaro
Copy link
Contributor Author

zesaro commented Nov 11, 2015

Am I assuming right that you mean something like that?

    @Test
    public void testIsRecognizedFormatAccept() throws IOException {
        CopacImporter importer = new CopacImporter();
            LinkedList<String> list = new LinkedList<>();
            list.add("CopacImporterTest1.txt");
            list.add("CopacImporterTest2.txt");

            for (String str : list) {
                try (InputStream is = CopacImporterTest.class.getResourceAsStream(str)) {
                    Assert.assertTrue(importer.isRecognizedFormat(is));
                    is.close();
                }
            }
    }

@lenhard
Copy link
Member

lenhard commented Nov 11, 2015

Almost :) As you are using try-with-resources, the line is.close() is no longer needed. The try block takes care of that automatically.

@oscargus
Copy link
Contributor

I read somewhere that it is still recommended (by many) to keep the close(), primarily for readability/understanding I guess. Now, I haven't followed it that carefully though...

@simonharrer
Copy link
Contributor

Do you have a link for that @oscargus ?

@oscargus
Copy link
Contributor

Maybe not. I tried to find it, but to no avail. I found something which might be what I referred to, but it didn't actually say exactly that. Rather, I found many examples without the close().

//CopacImporterTest3.txt is an empty file.
List<BibtexEntry> entries = importer.importEntries(
CopacImporterTest.class.getResourceAsStream("CopacImporterTest3.txt"), new OutputPrinterToNull());
Assert.assertEquals(0, entries.size());
Copy link
Contributor

Choose a reason for hiding this comment

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

When comparing lists, it is better to do an assert that way:

Assert.assertEquals(Collections.emptyList(), entries);

Then, the message when the assertion fails is much better, as it contains the entries in the list using their toString method.

Example:

List<String> values = new LinkedList<>();
values.add("asdf");

Assert.assertEquals(Collections.emptyList(), values);

results in

Exception in thread "main" java.lang.AssertionError: expected:<[]> but was:<[asdf]>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at Test.main(Test.java:13)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you want me to only edit the testImportEntries3() ?
Or should I create two Lists on every testImport() and only make one Assert at the end? For example

    @Test
    public void testImportEntries() throws IOException {
        Globals.prefs.put("defaultEncoding", "UTF8");

        CopacImporter importer = new CopacImporter();

        List<BibtexEntry> entries = importer.importEntries(
                CopacImporterTest.class.getResourceAsStream("CopacImporterTest1.txt"), new OutputPrinterToNull());
        Assert.assertEquals(1, entries.size());
        BibtexEntry entry = entries.get(0);

        List<String> idealValue = new LinkedList<>();
        idealValue.add("The SIS project : software reuse with a natural language approach");
        idealValue.add("Prechelt, Lutz and Universität Karlsruhe. Fakultät für Informatik");
        idealValue.add("Interner Bericht ; Nr.2/92");
        idealValue.add("1992");
        idealValue.add("Karlsruhe :  Universitat Karlsruhe, Fakultat fur Informatik");
        idealValue.add("Edinburgh");
        idealValue.add("TXT");

        List<String> isValue = new LinkedList<>();
        isValue.add(entry.getField("title"));
        isValue.add(entry.getField("author"));
        isValue.add(entry.getField("series"));
        isValue.add(entry.getField("year"));
        isValue.add(entry.getField("publisher"));
        isValue.add(entry.getField("HL"));
        isValue.add(entry.getField("documenttype"));

        Assert.assertEquals(idealValue, isValue);

        Assert.assertEquals(BibtexEntryTypes.BOOK, entry.getType());
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant only when checking that the size of the list should be 0. You do not get too much information that the list has size 1. It is much easier to debug when you know what values the element in the list has.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. Now I know what you mean. Thank you.
After I have changed testImportEntries() to the "two list model" I got a failure. I couldn't resolve it because it was one big Message and I didn't saw my mistake.
This is why I asked before I changed the other one and pushed it.

@lenhard
Copy link
Member

lenhard commented Nov 13, 2015

I just realized that a few fixes I implemented when going through coverty warnings this morning (see 5b66b98) conflict with this PR. I fixed the resource leaks and the remainder of this PR is still valuable.

@zellerdev To get it into master, please merge master into your branch. Sorry for the inconvenience, I'll keep my hands off the importers from now.

@koppor
Copy link
Member

koppor commented Nov 19, 2015

I noticed that line 53 reads

Globals.prefs.put("defaultEncoding", "UTF8");

Could you please change that string to UTF-8 and check if your test cases still work? - JabRef changed the strings to be more conforming to usual strings - see #155.

Furthermore, could you follow the pattern "If you modify preference, use following pattern:" stated at https://github.com/JabRef/jabref/wiki/Code-Howtos#test-cases?

@lenhard
Copy link
Member

lenhard commented Nov 19, 2015

I'd like to add that you should not refer to preferences via their string key, but rather via their static variable in JabRefPreferences, e.g.:

Globals.prefs.put(JabRefPreferences.DEFAULT_ENCODING, "UTF-8");

@koppor
Copy link
Member

koppor commented Nov 29, 2015

I think, it's easier for reviewing if you squash the whole commits into one commit. git reset master is your friend.

@zesaro zesaro force-pushed the copacimportertest branch 3 times, most recently from 15fd434 to f3bfd6e Compare December 21, 2015 14:30
@koppor koppor added the status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Dec 21, 2015
@Rule
public ExpectedException thrown = ExpectedException.none();


@Test
Copy link
Member

Choose a reason for hiding this comment

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

I think it is slightly better to understand if you use @Test(expect=IOException.class) instead of the thrown construction.

Copy link
Member

Choose a reason for hiding this comment

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

I agree. In this case, no the whole method may throw an exception and not a single line (as IMHO discussed with @bartsch-dev in another PR)

@koppor
Copy link
Member

koppor commented Dec 31, 2015

Coverage 100%. 👍

While wondering why the import source is so small, I checked the header comment http://copac.jisc.ac.uk/faq/#format to check whether there are more fields. There, I found "How do I import Copac records in reference management software? (e.g. Endnote/Zotero)" which contains "If you use Reference Manager you can use the RIS filter to import Copac records."

@obraliar @mairdl @zellerdev @ayanai1 Could you please check whether RIS is really equal to Copac? Can you read each other's code and test cases and report back here?

In case they are equal, I would propose to merge the importer code.

@zesaro zesaro force-pushed the copacimportertest branch from f3bfd6e to 41633a2 Compare January 8, 2016 15:44
@koppor koppor mentioned this pull request Jan 8, 2016
@koppor
Copy link
Member

koppor commented Jan 8, 2016

@zellerdev I can't see what you changed why in your commit. In this case, it would have been better to add another commit. Then, I can easily see, what you changed after my approval. Now, I have to review the whole commit again and I don't know what I have been approved and what not.

@zesaro
Copy link
Contributor Author

zesaro commented Jan 8, 2016

I apologize for that. Because of the problems building the project I had in the past this was a routine for me. I just included the suggestions you made on the top of the class.

Assert.assertEquals("TXT, PDF", one.getField("documenttype"));
Assert.assertEquals(
"Aberdeen ; Birmingham ; Edinburgh ; Trinity College Dublin ; UCL (University College London)",
one.getField("HL"));
Copy link
Member

Choose a reason for hiding this comment

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

What is HL? From the content, I would suspect it represents the location/city of the publisher. In this case it should be written to the address field in bibtex.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This implementation of CopacImporter knows the tags TI, AU, PY, PU, SE, IS, KW, NT, PD and DT. In this case Copac doesn't know what HL is. So to address this field you have to use the tag HL as field. This ressources already existed. In this case this is a test to watch whether the importer correctly handles with unknown tags.

@koppor koppor removed the status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Jan 15, 2016
@stefan-kolb
Copy link
Member

@koppor WDYT?

@zesaro zesaro force-pushed the copacimportertest branch from 2773bf5 to c82ff37 Compare April 7, 2016 10:18
@zesaro zesaro changed the title Copacimportertest v2 [WIP]Copacimportertest v2 Apr 7, 2016
@@ -25,7 +25,8 @@ public void setUp() throws Exception {
@Test
public final void testIsNotRecognizedFormat() throws Exception {
List<String> notAccept = Arrays.asList("emptyFile.xml", "IsiImporterTest1.isi",
"InspecSilverPlatterImporterTest.txt", "oai2.xml", "RisImporterTest1.ris");
"CopacImporterTest1.txt",
Copy link
Member

Choose a reason for hiding this comment

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

Please fix the formatting

@zesaro zesaro force-pushed the copacimportertest branch 2 times, most recently from f5d3c41 to c65e380 Compare April 13, 2016 13:27
@koppor koppor changed the title [WIP]Copacimportertest v2 Copacimportertest Apr 13, 2016
@simonharrer
Copy link
Contributor

Can you please rebase on current master?

@zesaro zesaro force-pushed the copacimportertest branch 2 times, most recently from 0fd9e5a to 73fe37a Compare April 13, 2016 14:52
@koppor
Copy link
Member

koppor commented Apr 13, 2016

Coverage 98,48%

LGTM

@zesaro zesaro force-pushed the copacimportertest branch from 73fe37a to 1e33511 Compare April 13, 2016 15:20
@koppor koppor added the status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Apr 13, 2016

int size = copacEntries.size();

// workaround because BibtexEntryAssert can only test 1 entry
Copy link
Contributor

Choose a reason for hiding this comment

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

Then provide a method in the BibtexEntryAssert class which can do this, please.

@zesaro
Copy link
Contributor Author

zesaro commented Apr 14, 2016

Using java.nio for getTestFiles(), removed InspecSilverPlatter and replaced it with another Inspec file which is not recognized by copac and added a new method for BibtexEntryAssert using InputStreams and an ImportFormat as parameter to generate to lists to compare. I hope I solved everything as expected.

*/
public List<String> getTestFiles() throws IOException {
List<String> files = new ArrayList<>();
Files.newDirectoryStream(Paths.get(FILEFORMAT_PATH)).forEach(n -> files.add(n.getFileName().toString()));
Copy link
Contributor

Choose a reason for hiding this comment

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

a directory stream is a resource which must be closed. use a try-with-resources construct here, please.

@simonharrer simonharrer merged commit 02cd696 into JabRef:master Apr 15, 2016
@zesaro zesaro deleted the copacimportertest branch April 15, 2016 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants