Browse Source

testy jednostkowe

master
Piotr Dergun 10 years ago
parent
commit
645cc6101a
6 changed files with 188 additions and 56 deletions
  1. +21
    -17
      src/main/java/ksiazka/kucharska/CB_Controller.java
  2. +1
    -1
      src/test/java/ksiazka/kucharska/AllTests.java
  3. +0
    -38
      src/test/java/ksiazka/kucharska/AppTest.java
  4. +23
    -0
      src/test/java/ksiazka/kucharska/RecipeBuilderTest.java
  5. +69
    -0
      src/test/java/ksiazka/kucharska/SearchTest.java
  6. +74
    -0
      src/test/java/ksiazka/kucharska/ViewRecipeTest.java

+ 21
- 17
src/main/java/ksiazka/kucharska/CB_Controller.java View File

@ -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){

+ 1
- 1
src/test/java/ksiazka/kucharska/AllTests.java View File

@ -7,5 +7,5 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ AddRecipeTest.class, DelRecipeTestTest.class })
public class AllTests {
}

+ 0
- 38
src/test/java/ksiazka/kucharska/AppTest.java View File

@ -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 );
}
}

+ 23
- 0
src/test/java/ksiazka/kucharska/RecipeBuilderTest.java View File

@ -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());
}
}

+ 69
- 0
src/test/java/ksiazka/kucharska/SearchTest.java View File

@ -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<String> 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);
}
}

+ 74
- 0
src/test/java/ksiazka/kucharska/ViewRecipeTest.java View File

@ -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);
}
}

Loading…
Cancel
Save