package test;
import static org.junit.Assert.*;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import org.junit.*;
import ui.WCViewer;
import webcam.ImageDownloadFailedException;
import webcam.WCVCam;
public class WCVCamJunitTest {
private final String TEST_IMG = "http://sourceforge.net/projects/wcviewer/screenshots/screenshot-0-wcviewer-0.1.jpg/182/137";
private WCVCam cam1, cam2;
private boolean testImgUp;
@Before
public void setUp() throws Exception {
this.testImgUp = true;
this.cam1 = new WCVCam("CAM1", "LOC1", TEST_IMG);
this.cam2 = new WCVCam("CAM2", "LOC2", "http://anonexistantsite/andnonexistantpng.png");
WCVCam.setPhotoDir(".");
cam1.setScaleDimension(new Dimension(WCViewer.WIDTH, WCViewer.HEIGHT));
cam2.setScaleDimension(new Dimension(WCViewer.WIDTH, WCViewer.HEIGHT));
//Check whether test image is reachable for testing
try {
//Make a URL to a reliable source
URL url = new URL(TEST_IMG);
//Open a connection to that source
HttpURLConnection urlConnect = (HttpURLConnection)url.openConnection();
//Trying to retrieve data from the source. If there
//is no connection, this line will fail
Object objData = urlConnect.getContent();
}
catch (UnknownHostException e) {
testImgUp = false;
}
catch (IOException e) {
testImgUp = false;
}
}
@After
public void tearDown() throws Exception {
}
@Test
public void testWCVCam() {
assert(cam1.getImgExtension() == ".jpg");
assert(cam2.getImgExtension() == ".png");
}
@Test
public void testRun() {
cam1.run(); cam2.run();
try{
assert(cam1.isReady());
//Test both scaled and raw images now exist
assert(cam1.getImage(true) != null);
assert(cam1.getImage(false) != null);
}
catch(Exception e) {
if(testImgUp) {
fail(e.getMessage());
}
}
try{
cam2.isReady();
fail("Image Download should have failed, but no exception thrown");
}
catch(ImageDownloadFailedException e) {
}
catch(InterruptedException e) {
}
}
@Test
public void testDeletePrevImg() {
File cam1ImgFile = new File(cam1.getImgFileName());
cam1.deletePrevImg();
assert(cam1ImgFile.exists() == false);
}
@Test
public void testCompareTo() {
assert(cam1.compareTo(cam2) < 0);
WCVCam cam3 = new WCVCam("CAM1", "LOC1", TEST_IMG);
assert(cam1.compareTo(cam3) == 0);
}
}