From 645cc6101a5e833c2d30bf351f8648af75c844c3 Mon Sep 17 00:00:00 2001 From: PioDer Date: Sun, 15 Jun 2014 21:47:40 +0100 Subject: [PATCH] testy jednostkowe --- .../java/ksiazka/kucharska/CB_Controller.java | 38 +++++----- src/test/java/ksiazka/kucharska/AllTests.java | 2 +- src/test/java/ksiazka/kucharska/AppTest.java | 38 ---------- .../ksiazka/kucharska/RecipeBuilderTest.java | 23 ++++++ .../java/ksiazka/kucharska/SearchTest.java | 69 +++++++++++++++++ .../ksiazka/kucharska/ViewRecipeTest.java | 74 +++++++++++++++++++ 6 files changed, 188 insertions(+), 56 deletions(-) delete mode 100644 src/test/java/ksiazka/kucharska/AppTest.java create mode 100644 src/test/java/ksiazka/kucharska/RecipeBuilderTest.java create mode 100644 src/test/java/ksiazka/kucharska/SearchTest.java create mode 100644 src/test/java/ksiazka/kucharska/ViewRecipeTest.java diff --git a/src/main/java/ksiazka/kucharska/CB_Controller.java b/src/main/java/ksiazka/kucharska/CB_Controller.java index a654bbc..49d6a64 100644 --- a/src/main/java/ksiazka/kucharska/CB_Controller.java +++ b/src/main/java/ksiazka/kucharska/CB_Controller.java @@ -85,6 +85,26 @@ public class CB_Controller { myCb.deleteRecipe(name); myCb.closeConnection(); } + public ImageIcon resizeImage(File f) { + //File file = f; + ImageIcon iconLogo=null; + try { + BufferedImage srcImg = ImageIO.read(f); + int w = (int)Math.ceil(srcImg.getWidth()/(double)srcImg.getHeight()*theView.getImgRecipe().getHeight()); + + BufferedImage resizedImg = new BufferedImage(w, theView.getImgRecipe().getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics2D g2 = resizedImg.createGraphics(); + g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g2.drawImage(srcImg, 0, 0, w, theView.getImgRecipe().getHeight(), null); + g2.dispose(); + iconLogo = new ImageIcon(resizedImg); + + + } catch (IOException e1) { + e1.printStackTrace(); + } + return iconLogo; + } class AddListener implements ActionListener{ @@ -143,23 +163,7 @@ public class CB_Controller { JFileChooser fcImg = theView.getFileChooser(); int returnVal = fcImg.showOpenDialog(theView); if (returnVal == JFileChooser.APPROVE_OPTION) { - File file = fcImg.getSelectedFile(); - try { - BufferedImage srcImg = ImageIO.read(file); - JLabel imgRecipe = theView.getImgRecipe(); - int w = (int)Math.ceil(srcImg.getWidth()/(double)srcImg.getHeight()*imgRecipe.getHeight()); - - BufferedImage resizedImg = new BufferedImage(w, imgRecipe.getHeight(), BufferedImage.TYPE_INT_ARGB); - Graphics2D g2 = resizedImg.createGraphics(); - g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); - g2.drawImage(srcImg, 0, 0, w, imgRecipe.getHeight(), null); - g2.dispose(); - ImageIcon iconLogo = new ImageIcon(resizedImg); - imgRecipe.setIcon(iconLogo); - - } catch (IOException e1) { - e1.printStackTrace(); - } + theView.getImgRecipe().setIcon(resizeImage(fcImg.getSelectedFile())); } } catch(NumberFormatException ex){ diff --git a/src/test/java/ksiazka/kucharska/AllTests.java b/src/test/java/ksiazka/kucharska/AllTests.java index fd274d4..e31cb40 100644 --- a/src/test/java/ksiazka/kucharska/AllTests.java +++ b/src/test/java/ksiazka/kucharska/AllTests.java @@ -7,5 +7,5 @@ import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ AddRecipeTest.class, DelRecipeTestTest.class }) public class AllTests { - + } diff --git a/src/test/java/ksiazka/kucharska/AppTest.java b/src/test/java/ksiazka/kucharska/AppTest.java deleted file mode 100644 index 14ded89..0000000 --- a/src/test/java/ksiazka/kucharska/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package ksiazka.kucharska; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/src/test/java/ksiazka/kucharska/RecipeBuilderTest.java b/src/test/java/ksiazka/kucharska/RecipeBuilderTest.java new file mode 100644 index 0000000..8b94a24 --- /dev/null +++ b/src/test/java/ksiazka/kucharska/RecipeBuilderTest.java @@ -0,0 +1,23 @@ +package ksiazka.kucharska; + +import static org.junit.Assert.*; + +import ksiazka.kucharska.CB_View; + +import org.junit.Test; + +public class RecipeBuilderTest { + + @Test + public void testBuildRecipe() { + CB_View theView = new CB_View(); + RecipeManager rm = new RecipeManager(); + rm.setRecipeBuilder(new ExampleRecipeBuilder(theView)); + rm.constructRecipe(); + + CB_Model obj = rm.getRecipe(); + + assertEquals("Kanapka", obj.getName()); + } + +} diff --git a/src/test/java/ksiazka/kucharska/SearchTest.java b/src/test/java/ksiazka/kucharska/SearchTest.java new file mode 100644 index 0000000..3c37219 --- /dev/null +++ b/src/test/java/ksiazka/kucharska/SearchTest.java @@ -0,0 +1,69 @@ +package ksiazka.kucharska; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.List; + +import ksiazka.kucharska.CB_View; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import db.Cookbook; +import db.SearchRecipe; +import db.SearchRecipeAll; + +public class SearchTest { + + @BeforeClass + public static void addExamples() + { + File f1 = new File("cookbook.db"); + File f2 = new File("cookbook-bkp.db"); + if (f1.exists()) + f1.renameTo(f2); + + + Cookbook myCb = new Cookbook(); + CB_View theView = new CB_View(); + + RecipeManager rm = new RecipeManager(); + rm.setRecipeBuilder(new ExampleRecipeBuilder(theView)); + rm.constructRecipe(); + + CB_Model obj = rm.getRecipe(); + myCb.insertRecipe(obj); + myCb.insertRecipe(obj); + + myCb.closeConnection(); + } + @Test + public void testSearch() { + CB_View theView = new CB_View(); + + Cookbook myCb = new Cookbook(); + SearchRecipe sr; + sr = new SearchRecipeAll(theView); + List names = sr.obtainRecipeNames(); + + myCb.closeConnection(); + + assertEquals(2, names.size()); + } + + @AfterClass + public static void delExamples() + { + Cookbook myCb = new Cookbook(); + myCb.deleteRecipe("Kanapka"); + myCb.closeConnection(); + + File f1 = new File("cookbook.db"); + File f2 = new File("cookbook-bkp.db"); + f1.delete(); + if (f2.exists()) + f2.renameTo(f1); + } +} diff --git a/src/test/java/ksiazka/kucharska/ViewRecipeTest.java b/src/test/java/ksiazka/kucharska/ViewRecipeTest.java new file mode 100644 index 0000000..7dd8e6f --- /dev/null +++ b/src/test/java/ksiazka/kucharska/ViewRecipeTest.java @@ -0,0 +1,74 @@ +package ksiazka.kucharska; + +import static org.junit.Assert.*; + +import java.io.File; +import ksiazka.kucharska.CB_View; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import db.Cookbook; + +public class ViewRecipeTest { + static CB_View theView; + static CB_Model theModel; + static CB_Controller theController; + + @BeforeClass + public static void addExamples() + { + theView = new CB_View(); + theModel = new CB_Model(); + theController = new CB_Controller(theView, theModel); + File f1 = new File("cookbook.db"); + File f2 = new File("cookbook-bkp.db"); + if (f1.exists()) + f1.renameTo(f2); + + + Cookbook myCb = new Cookbook(); + + RecipeManager rm = new RecipeManager(); + rm.setRecipeBuilder(new ExampleRecipeBuilder(theView)); + rm.constructRecipe(); + + CB_Model obj = rm.getRecipe(); + + myCb.insertRecipe(obj); + theView.setRecipesListItem(obj.getName()); + myCb.closeConnection(); + } + @SuppressWarnings("static-access") + @Test + public void testView() { + theView.getRecipesList().setSelectedIndex(theView.getRecipesList().getModel().getSize()-1); + theController.fillComponents(theView.getRecipesList().getSelectedValue().toString()); + + RecipeManager rm = new RecipeManager(); + rm.setRecipeBuilder(new ExampleRecipeBuilder(theView)); + rm.constructRecipe(); + + CB_Model testObj = rm.getRecipe(); + + assertEquals(testObj.getName(), theView.getRecipeTitle()); + assertEquals(testObj.getIngredients(), theView.getRecipeIngredients()); + assertEquals(testObj.getDescription(), theView.getRecipeDescription()); + assertEquals(testObj.getTags(), theView.getRecipeTags()); + } + + @AfterClass + public static void delExamples() + { + Cookbook myCb = new Cookbook(); + myCb.deleteRecipe("Kanapka"); + myCb.closeConnection(); + + File f1 = new File("cookbook.db"); + File f2 = new File("cookbook-bkp.db"); + f1.delete(); + if (f2.exists()) + f2.renameTo(f1); + } +}