@Test public void testAdd() throws DatabaseException { Project census = new Project("census", 2, 3, 4, 5); Project births = new Project("births", 6, 7, 8, 9); dbProjects.add(census); dbProjects.add(births); List<Project> all = dbProjects.getAll(); assertEquals(2, all.size()); boolean foundAge = false; boolean foundPlace = false; for (Project p : all) { assertFalse(p.getId() == -1); if (!foundAge) { foundAge = areEqual(p, census, false); } if (!foundPlace) { foundPlace = areEqual(p, births, false); } } assertTrue(foundAge && foundPlace); }
/** update a project */ public void update(Project project) { PreparedStatement stmt = null; try { String sql = "update Projects " + "set Title = ?, RecordSperImage = ?, FirstYCoord = ?, RecordHeight = ? " + "where ProjectID = ?"; stmt = db.getConnection().prepareStatement(sql); stmt.setString(1, project.getTitle()); stmt.setInt(2, project.getRecordsPerImage()); stmt.setInt(3, project.getFirstYCoord()); stmt.setInt(4, project.getRecordHeight()); stmt.setInt(5, project.getProjectID()); if (stmt.executeUpdate() == 1) { System.out.println("Update success"); } else { System.out.println("Update fail"); } } catch (SQLException e) { System.out.println("Can't execute update"); e.printStackTrace(); } finally { try { if (stmt != null) stmt.close(); } catch (SQLException e) { System.out.println("Can't execute connect"); e.printStackTrace(); } } }
@Test public void testGetNumFields() throws DatabaseException { Project census = new Project("census", 2, 3, 4, 5); dbProjects.add(census); assertEquals(dbProjects.getNumFields(census.getId()), 5); }
@Test public void testGetRecsPerImage() throws DatabaseException { Project census = new Project("census", 2, 3, 4, 5); dbProjects.add(census); assertEquals(dbProjects.getRecsPerImage(census.getId()), 2); }
/** add a project */ public void add(Project project) { PreparedStatement stmt = null; Statement keyStmt = null; ResultSet keyRS = null; try { String sql = "insert into Projects " + "(Title, RecordSperImage, " + "FirstYCoord, RecordHeight) " + "values (?, ?, ?, ?)"; stmt = db.getConnection().prepareStatement(sql); stmt.setString(1, project.getTitle()); stmt.setInt(2, project.getRecordsPerImage()); stmt.setInt(3, project.getFirstYCoord()); stmt.setInt(4, project.getRecordHeight()); if (stmt.executeUpdate() == 1) { keyStmt = db.getConnection().createStatement(); keyRS = keyStmt.executeQuery("select last_insert_rowid()"); keyRS.next(); int id = keyRS.getInt(1); project.setProjectID(id); } else { System.out.println("Add fail"); } } catch (SQLException e) { System.out.println("Can't execute add"); e.printStackTrace(); } finally { try { if (keyRS != null) keyRS.close(); if (stmt != null) stmt.close(); if (keyStmt != null) keyStmt.close(); } catch (SQLException e) { System.out.println("Can't execute connect"); e.printStackTrace(); } } }
public DownloadBatchDialog(final IndexerFrame parentFrame, final List<Project> projectList) { setModal(true); setTitle("Download Batch"); setResizable(false); setSize(300, 125); setLocationRelativeTo(null); JPanel panel = new JPanel(); panel.add(new JLabel("Project:")); final JComboBox<String> projectMenu = new JComboBox<String>(); for (Project p : projectList) { projectMenu.addItem(p.getTitle()); } panel.add(projectMenu); JButton sample = new JButton("Sample"); JButton cancel = new JButton("Cancel"); JButton download = new JButton("Download"); sample.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { Project tmpProject = null; for (Project p : projectList) { if (p.getTitle().equals(projectMenu.getSelectedItem())) { tmpProject = p; break; } } SampleBatchDialog d = new SampleBatchDialog(tmpProject); d.setVisible(true); repaint(); } }); cancel.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { dispose(); } }); download.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { String selected = (String) projectMenu.getSelectedItem(); Project tmpProject = null; for (Project p : projectList) { if (p.getTitle().equals(selected)) { tmpProject = p; break; } } DownloadBatchOutput batchData = null; try { batchData = ClientFacade.getBatch( new GetBatchInput( new ValidateUserInput(Client.getUsername(), Client.getPassword()), tmpProject.getId())); parentFrame.getImagePanel().loadBatch(batchData); parentFrame.repaint(); } catch (ClientException e) { System.out.println("Could not load batch"); e.printStackTrace(); } parentFrame.createBottom(batchData); parentFrame.getMainPanel().setRightComponent(parentFrame.getBottomPanel()); parentFrame.add(parentFrame.getMainPanel()); parentFrame.repaint(); dispose(); } }); panel.add(sample); panel.add(cancel); panel.add(download); add(panel); }
private boolean areEqual(Project a, Project b, boolean compareIDs) { if (compareIDs) { if (a.getId() != b.getId()) { return false; } } return (safeEquals(a.getTitle(), b.getTitle()) && safeEquals(a.getRecordsperimage(), b.getRecordsperimage()) && safeEquals(a.getFirstycoord(), b.getFirstycoord()) && safeEquals(a.getRecordheight(), b.getRecordheight()) && safeEquals(a.getNumfields(), b.getNumfields())); }
@Test public void testUpdate() throws DatabaseException { Project census = new Project("census", 2, 3, 4, 5); Project births = new Project("births", 6, 7, 8, 9); dbProjects.add(census); dbProjects.add(births); census.setTitle("CENSUS"); census.setRecordsperimage(3); census.setFirstycoord(4); census.setRecordheight(5); census.setNumfields(6); births.setTitle("BIRTHS"); births.setRecordsperimage(7); births.setFirstycoord(8); births.setRecordheight(9); births.setNumfields(10); dbProjects.update(census); dbProjects.update(births); List<Project> all = dbProjects.getAll(); assertEquals(2, all.size()); boolean foundAge = false; boolean foundPlace = false; for (Project p : all) { if (!foundAge) { foundAge = areEqual(p, census, false); } if (!foundPlace) { foundPlace = areEqual(p, births, false); } } assertTrue(foundAge && foundPlace); }