Commits

Anonymous committed 35bcaac

Add tests.

  • Participants
  • Parent commits bf71cb6

Comments (0)

Files changed (3)

             <version>1.8</version>
         </dependency>
 
+        <dependency>
+            <groupId>postgresql</groupId>
+            <artifactId>postgresql</artifactId>
+            <version>9.0-801.jdbc4</version>
+            <scope>test</scope>
+        </dependency>
 
         <dependency>
             <groupId>junit</groupId>

File src/test/java/br/usp/ime/vision/dataset/ws/client/test/TestDatabaseSetup.java

+package br.usp.ime.vision.dataset.ws.client.test;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Scanner;
+
+
+public final class TestDatabaseSetup {
+    public static final Connection getConnection() throws SQLException {
+        try {
+            Class.forName("org.postgresql.Driver");
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+            return null;
+        }
+        String url = "jdbc:postgresql://127.0.0.1:5432/testvisionds?user=visionDataset&password=visionDataset";
+        Connection conn;
+        conn = DriverManager.getConnection(url);
+        return conn;
+    }
+
+    public static void setUp() throws SQLException {
+        Connection connection = getConnection();
+        Statement statement = connection.createStatement();
+
+        String sql = "SELECT pg_catalog.setval('tb_albums_id_seq', 1, true);"
+                + "SELECT pg_catalog.setval('tb_annotations_id_seq', 5, true);"
+                + "SELECT pg_catalog.setval('tb_image_attachment_id', 1, false);"
+                + "SELECT pg_catalog.setval('tb_image_attachments_id_seq', 32, true);"
+                + "SELECT pg_catalog.setval('tb_images_id_seq', 6, true);"
+                + "SELECT pg_catalog.setval('tb_tags_albums_id_seq', 1, true);"
+                + "SELECT pg_catalog.setval('tb_tags_images_id_seq', 4, true);"
+                + "SELECT pg_catalog.setval('tb_users_id_seq', 1, true);"
+                + "INSERT INTO tb_users VALUES (1, 'rafael', '741CBAEB4DFC59E110E13A4CD2134538CCF04FB3', 'Rafael', 'rafaellg@vision.ime.usp.br', true, true, '2011-06-13 13:34:53.042-03', '2011-06-13 13:34:53.042-03', 100, true);"
+                + "INSERT INTO tb_albums VALUES (1, 'Teste', 1, '2011-06-29 00:23:38.97-03', '2011-07-07 01:43:28.558-03', 10, 0, 0);"
+                + "INSERT INTO tb_images VALUES (3, 'image/png', 1, 1, '2011-07-07 01:43:27.587-03');"
+                + "INSERT INTO tb_images VALUES (4, 'image/png', 1, 1, '2011-07-07 01:43:28.286-03');"
+                + "INSERT INTO tb_images VALUES (5, 'image/jpeg', 1, 1, '2011-07-07 01:43:28.422-03');"
+                + "INSERT INTO tb_images VALUES (6, 'image/png', 1, 1, '2011-07-07 01:43:28.558-03');"
+                + "INSERT INTO tb_image_attachments VALUES (15, 3, '2011-07-07 17:52:06.173-03', 'teste1');"
+                + "INSERT INTO tb_image_attachments VALUES (16, 4, '2011-07-07 17:52:39.517-03', 'zxcv.txt');"
+                + "INSERT INTO tb_image_attachments VALUES (19, 3, '2011-07-12 19:23:46.03-03', 'qwer.png');"
+                + "INSERT INTO tb_image_attachments VALUES (22, 3, '2011-07-18 19:47:01.744-03', 'asdf.png');"
+                + "INSERT INTO tb_tags_albums VALUES (1, 'test', 1);"
+                + "INSERT INTO tb_tags_images VALUES (3, 'asdf', 3);"
+                + "INSERT INTO tb_tags_images VALUES (4, 'bdgdgbfg', 3);";
+
+        Scanner s = new Scanner(sql);
+        s.useDelimiter("(;(\r)?\n?)|(--\n)");
+        while (s.hasNext()) {
+            statement.execute(s.next() + ";");
+        }
+        statement.close();
+
+    }
+
+    public static void tearDown() throws SQLException {
+        Connection connection = getConnection();
+        Statement statement = connection.createStatement();
+        String sql = "delete from tb_tags_images ;" + "delete from tb_tags_albums ;"
+                + "delete from tb_annotations ;" + "delete from tb_image_attachments;"
+                + "delete from tb_images;" + "delete from tb_permissions ;" + "delete from tb_albums;"
+                + "delete from tb_users ;";
+        Scanner s = new Scanner(sql);
+        s.useDelimiter("(;(\r)?\n?)|(--\n)");
+        while (s.hasNext()) {
+            statement.execute(s.next());
+        }
+        statement.close();
+
+    }
+
+}

File src/test/java/br/usp/ime/vision/dataset/ws/client/test/VisionDatasetClientTest.java

+package br.usp.ime.vision.dataset.ws.client.test;
+
+import static org.junit.Assert.*;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.nio.CharBuffer;
+import java.util.List;
+import java.util.zip.CRC32;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import br.usp.ime.vision.dataset.ws.client.Attachment;
+import br.usp.ime.vision.dataset.ws.client.NotFoundException;
+import br.usp.ime.vision.dataset.ws.client.VisionDatasetClient;
+
+public class VisionDatasetClientTest {
+
+    private static final String URI_ATTACHMENT_19 = "http://localhost:8080/VisionDataset/ws/attachment/19";
+    private static final String URI_IMAGE_3 = "http://localhost:8080/VisionDataset/ws/image/3";
+    private VisionDatasetClient client;
+
+    @Before
+    public void setUp() throws Exception {
+        TestDatabaseSetup.setUp();
+        this.client = new VisionDatasetClient();
+        
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        TestDatabaseSetup.tearDown();
+    }
+
+    @Test
+    public void testAttachmentList() throws NotFoundException {
+        List<Attachment> list = this.client.attachmentList(URI_IMAGE_3);
+        assertEquals(3, list.size());
+        assertEquals("http://localhost:8080/VisionDataset/ws/attachment/22", list.get(0).getUri());
+        assertEquals("asdf.png", list.get(0).getName());
+        for (Attachment a: list){
+            assertEquals(3,a.getImageId());
+        }
+        
+        System.out.println(list);
+    }
+
+    @Test
+    public void testCreateAttachment() throws NotFoundException {
+        Attachment a = client.createAttachment(URI_IMAGE_3, "novo.tmp");
+        assertEquals("novo.tmp", a.getName());
+        assertEquals(3, a.getImageId());
+        assertEquals("http://localhost:8080/VisionDataset/ws/attachment/33", a.getUri());
+    }
+
+    @Test
+    public void testGetAttachmentFileAttachment() throws NotFoundException, IOException {
+        File attachmentFile = client.getAttachmentFile(URI_ATTACHMENT_19);
+        FileReader reader = new FileReader(attachmentFile);
+        char[] buf = new char[4];
+        reader.read(buf);
+        assertArrayEquals("1234".toCharArray(), buf);
+        System.out.print(buf);
+    }
+
+    @Test
+    public void testGetAttachmentFileString() throws NotFoundException, IOException {
+        Attachment attachment = new Attachment();
+        attachment.setUri(URI_ATTACHMENT_19);
+        File attachmentFile = client.getAttachmentFile(attachment);
+        FileReader reader = new FileReader(attachmentFile);
+        char[] buf = new char[4];
+        reader.read(buf);
+        assertArrayEquals("1234".toCharArray(), buf);
+    }
+
+    private long getFileCheckSum(File input) throws IOException {
+        FileInputStream file = new FileInputStream(input);
+    CheckedInputStream check = 
+            new CheckedInputStream(file, new CRC32());
+          BufferedInputStream in = 
+            new BufferedInputStream(check);
+          while (in.read() != -1) {
+              // Read file in completely
+          }
+          return check.getChecksum().getValue();
+        
+    }
+    
+    @Test
+    public void testGetImage() throws NotFoundException, IOException {
+        File image = client.getImage(URI_IMAGE_3);
+        long check = getFileCheckSum(image);
+        assertEquals(2755050192l, check);
+    }
+
+}