Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Last active November 2, 2022 23:54
Show Gist options
  • Save mauricioaniche/e1bc1290ccc1fac4c3f1e04bdec89a29 to your computer and use it in GitHub Desktop.
Save mauricioaniche/e1bc1290ccc1fac4c3f1e04bdec89a29 to your computer and use it in GitHub Desktop.
package nl.tudelft.mocks;
public class Invoice {
private String customer;
private double value;
public Invoice(String customer, double value) {
this.customer = customer;
this.value = value;
}
public String getCustomer() {
return customer;
}
public double getValue() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Invoice invoice = (Invoice) o;
if (Double.compare(invoice.value, value) != 0) return false;
return customer != null ? customer.equals(invoice.customer) : invoice.customer == null;
}
@Override
public int hashCode() {
int result;
long temp;
result = customer != null ? customer.hashCode() : 0;
temp = Double.doubleToLongBits(value);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
package nl.tudelft.mocks;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class InvoiceDao {
private static final Connection c;
static {
try {
c = DriverManager.getConnection("jdbc:hsqldb:mem:mymemdb", "SA", "");
c.prepareStatement("create table invoice (name varchar(100), value double)").execute();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<Invoice> all() {
List<Invoice> allInvoices = new ArrayList<>();
try {
PreparedStatement ps = c.prepareStatement("select * from invoice");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
String name = rs.getString("name");
double value = rs.getDouble("value");
allInvoices.add(new Invoice(name, value));
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
return allInvoices;
}
}
public void save(Invoice inv) {
try {
PreparedStatement ps = c.prepareStatement("insert into invoice (name, value) values (?,?)");
ps.setString(1, inv.getCustomer());
ps.setDouble(2, inv.getValue());
ps.execute();
c.commit();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void close() {
try {
c.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
package nl.tudelft.mocks;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
public class InvoiceFilter {
public List<Invoice> filter() {
InvoiceDao invoiceDao = new InvoiceDao();
List<Invoice> filtered = new ArrayList<>();
List<Invoice> allInvoices = invoiceDao.all();
for(Invoice inv : allInvoices) {
if(inv.getValue() < 100.0)
filtered.add(inv);
}
return filtered;
}
}
package nl.tudelft.mocks;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import java.util.List;
public class InvoiceFilterTest {
@Test
void filterInvoices() {
InvoiceDao dao = new InvoiceDao();
Invoice mauricio = new Invoice("Mauricio", 20.0);
Invoice arie = new Invoice("Arie", 300.0);
dao.save(mauricio);
dao.save(arie);
InvoiceFilter filter = new InvoiceFilter();
List<Invoice> result = filter.filter();
assertThat(result).containsExactly(mauricio);
dao.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.tudelft</groupId>
<artifactId>mocks</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.7.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0-M4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.22</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment