Skip to content

Commit

Permalink
vähän kattavammin yksikkötestiä
Browse files Browse the repository at this point in the history
  • Loading branch information
sjsarsa committed Apr 20, 2016
1 parent 6287687 commit cdb8411
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ public void execute() {
BookReference newRef = new BookReference(referenceName);
editorOrAuthorAsObligatoryField(newRef);
super.fillReferenceFields(newRef);

//save the reference
app.newReference(newRef);
}

public void editorOrAuthorAsObligatoryField(BookReference reference) {String command = io.readLine(
private void editorOrAuthorAsObligatoryField(BookReference reference) {String command = io.readLine(
"\nDo you want author or editor as obligatory field?" +
"\n(a)uthor (default)" +
"\n(e)ditor\n\n");

//switch (reference type)
switch(command){
case "a":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package referencelibrary.ui.command;

import org.junit.After;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import referencelibrary.App;
import referencelibrary.data.StubDao;
import referencelibrary.io.StubIO;

/**
*
* @author sjsarsa
*/
public class AddArticleCommandTest {

AddReferenceCommand addRefCmd = null;
App app = null;

@Before
public void setUp() {
this.app = new App(new StubDao());
this.addRefCmd = new AddArticleCommand(this.app, createStubIO());
}

/**
* Create StubIO to simulate add new inproceedings -functionality.
*/
private StubIO createStubIO() {
StubIO stubIo = new StubIO(
"someId",
"someAuthor",
"someTitle",
"someJournal",
"someYear",
"someVolume",
"y", //answers: "Would you like to add some optional fields?"
"a", //answers: (a)dd a field
"note",
"someNote"
);
return stubIo; //author, title, booktitle, year
}

@After
public void tearDown() {
}

/**
* Test of execute method, of class AddReferenceCommand.
*/
@Test
public void testExecute() {
this.addRefCmd.execute(); //this.app.listReferences().get(1).getField("author")
assertEquals("someAuthor", this.app.listReferences().get(1).getField("author"));
assertEquals("someTitle", this.app.listReferences().get(1).getField("title"));
assertEquals("someJournal", this.app.listReferences().get(1).getField("journal"));
assertEquals("someYear", this.app.listReferences().get(1).getField("year"));
assertEquals("someVolume", this.app.listReferences().get(1).getField("volume"));
assertEquals("someNote", this.app.listReferences().get(1).getField("note"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package referencelibrary.ui.command;

import org.junit.After;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import referencelibrary.App;
import referencelibrary.data.StubDao;
import referencelibrary.io.StubIO;

/**
*
* @author sjsarsa
*/
public class AddBookCommandTest {

AddReferenceCommand addRefCmd = null;
App app = null;

@Before
public void setUp() {
this.app = new App(new StubDao());
}

/**
* Create StubIO to simulate add new inproceedings -functionality.
*/
private StubIO createStubIO() {
StubIO stubIo = new StubIO(
"someId",
"a",
"someAuthor",
"someTitle",
"somePublisher",
"someYear",
"y", //answers: "Would you like to add some optional fields?"
"a", //answers: (a)dd a field
"note",
"someNote",
"s", // just for linecoverage, testing with easyB
"d"
);
return stubIo; //author, title, booktitle, year
}

private StubIO createStubIOWithEditor() {
StubIO stubIo = new StubIO(
"someId",
"e",
"someEditor",
"someTitle",
"somePublisher",
"someYear",
"n"
);
return stubIo; //author, title, booktitle, year
}

@After
public void tearDown() {
}

/**
* Test of execute method, of class AddReferenceCommand.
*/
@Test
public void testExecuteWithAuthorSelected() {
this.addRefCmd = new AddBookCommand(this.app, createStubIO());
this.addRefCmd.execute(); //this.app.listReferences().get(1).getField("author")
assertEquals("someAuthor", this.app.listReferences().get(1).getField("author"));
assertEquals("someTitle", this.app.listReferences().get(1).getField("title"));
assertEquals("somePublisher", this.app.listReferences().get(1).getField("publisher"));
assertEquals("someYear", this.app.listReferences().get(1).getField("year"));
assertEquals("someNote", this.app.listReferences().get(1).getField("note"));
}

@Test
public void testExecuteWithEditor() {
this.addRefCmd = new AddBookCommand(this.app, createStubIOWithEditor());
this.addRefCmd.execute(); //this.app.listReferences().get(1).getField("author")
assertEquals("someEditor", this.app.listReferences().get(1).getField("editor"));
assertEquals("someTitle", this.app.listReferences().get(1).getField("title"));
assertEquals("somePublisher", this.app.listReferences().get(1).getField("publisher"));
assertEquals("someYear", this.app.listReferences().get(1).getField("year"));
}

@Test
public void testEditorOrAuthorAsObligatoryField() {
this.addRefCmd = new AddBookCommand(this.app, createStubIO());
this.addRefCmd.execute();
assertEquals(null, this.app.listReferences().get(1).getField("editor"));
assertEquals("someAuthor", this.app.listReferences().get(1).getField("author"));
}

@Test
public void testEditorOrAuthorAsDefaultObligatoryField() {
StubIO stubIO = new StubIO("id", "something", "someAuthor", "2", "3", "4", "n");
this.addRefCmd = new AddBookCommand(this.app, stubIO);
this.addRefCmd.execute();
assertEquals(null, this.app.listReferences().get(1).getField("editor"));
assertEquals("someAuthor", this.app.listReferences().get(1).getField("author"));
assertEquals(true, stubIO.getPrints().contains("author selected as default"));
}

@Test
public void testEditorAsObligatoryField() {
this.addRefCmd = new AddBookCommand(this.app, new StubIO("id", "e", "someEditor", "2", "3", "4", "n"));
this.addRefCmd.execute();
assertEquals(null, this.app.listReferences().get(1).getField("author"));
assertEquals("someEditor", this.app.listReferences().get(1).getField("editor"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public void tearDown() {
public void testExecute() {
this.addRefCmd.execute(); //this.app.listReferences().get(1).getField("author")
assertEquals("someAuthor", this.app.listReferences().get(1).getField("author"));
//TODO check all fields
assertEquals("someTitle", this.app.listReferences().get(1).getField("title"));
assertEquals("someBooktitle", this.app.listReferences().get(1).getField("booktitle"));
assertEquals("someYear", this.app.listReferences().get(1).getField("year"));
assertEquals("someNote", this.app.listReferences().get(1).getField("note"));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package referencelibrary.ui.command;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import referencelibrary.App;
import referencelibrary.data.StubDao;
import referencelibrary.io.StubIO;
import referencelibrary.reference.BookReference;
import static referencelibrary.reference.ReferenceType.REFERENCE_BOOK;

/**
*
* @author sjsarsa
*/
public class ShowReferencesCommandTest {

ShowReferencesCommand showRefCmd;
App app;
StubIO stubIO;

public ShowReferencesCommandTest() {
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
this.app = new App(new StubDao());
this.stubIO = new StubIO("s");
}

@After
public void tearDown() {
}

@Test
public void testExecute() {
this.showRefCmd = new ShowReferencesCommand(this.app, stubIO);
this.showRefCmd.execute();
assertEquals("[Book: REF]", stubIO.getPrints().get(0));
assertEquals(1, stubIO.getPrints().size());
}

@Test
public void testShowNewReference() {
this.showRefCmd = new ShowReferencesCommand(this.app, stubIO);
app.newReference(new BookReference("asdasd"));
this.showRefCmd.execute();
assertEquals(true, stubIO.getPrints().contains("[Book: asdasd]"));
assertEquals(2, stubIO.getPrints().size());
}



}

0 comments on commit cdb8411

Please sign in to comment.