Skip to content
Snippets Groups Projects
Commit ed16c1a5 authored by Thiemo Belmega's avatar Thiemo Belmega
Browse files

Implement DatabaseController and Application

* Implement the Application class
* Implement the DatabaseController, that loads an Application with gived
ID to database
parent fc844841
No related branches found
No related tags found
No related merge requests found
package de.unipotsdam.cs.toolup.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import de.unipotsdam.cs.toolup.model.Application;
public class DatabaseController {
public static Application load(String uuid) throws SQLException {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&pw=");
Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("select * from application;");
res.first();
String id = res.getString("uuid");
String title = res.getString("title");
String description = res.getString("description");
return new Application(id,title,description);
}
}
package de.unipotsdam.cs.toolup.model;
public class Application {
private String uuid;
private String title;
private String description;
public Application(String uuid, String title, String description) {
this.uuid = uuid;
this.title = title;
this.description = description;
}
private Object getUuid() {
return this.uuid;
}
public String getTitle() {
return title;
}
public String getDescription() {
return this.description;
}
public boolean equals(Application anOtherApp){
if (this.uuid.equals(anOtherApp.getUuid())){
return true;
} else {
return false;
}
}
}
package de.unipotsdam.cs.toolup.database;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.testng.annotations.Test;
import de.unipotsdam.cs.toolup.model.Application;
public class DatabaseControllerTest {
@Test
public void testThatJDBCcanExecuteQuery() throws SQLException{
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&pw=");
Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("select * from application;");
res.first();
String title = res.getString("title");
assertEquals("Dropbox", title);
}
@Test
public void testThatDBCLoadsApplicationFromDB() throws SQLException {
//arrange
Application expectedApp = new Application("test_id_1","Dropbox",null);
//act
Application app = DatabaseController.load("test_id_1");
//assert
assertTrue(expectedApp.equals(app));
}
@Test
public void testThatLoadedApplicationHasExpectedValues() throws SQLException {
//arrange
String expectedTitle = "Dropbox";
String expectedDescription = "Dropbox Description";
//act
Application app = DatabaseController.load("test_id_1");
//assert
assertEquals(expectedTitle, app.getTitle());
assertEquals(expectedDescription, app.getDescription());
}
}
package de.unipotsdam.cs.toolup.model;
import org.testng.annotations.Test;
import static org.testng.AssertJUnit.*;
public class ApplicationTest {
private static final String TEST_ID_2 = "test_id_2";
private static final String TEST_ID_1 = "test_id_1";
@Test
public void testThatAnApplicationDoesNotEqualAnOtherApplication() {
//arrange
Application anOtherApp = new Application(TEST_ID_1,null,null);
//act
Application newApp = new Application(TEST_ID_2,null,null);
//assert
assertFalse(anOtherApp.equals(newApp));
}
@Test
public void testThatAnApplicationDoesEqualItself() {
//arrange
Application anOtherApp = new Application(TEST_ID_1,null,null);
//act
Application newApp = new Application(TEST_ID_1,null,null);
//assert
assertTrue(anOtherApp.equals(newApp));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment