6 Commits

  1. 4
      src/main/java/Credential.java
  2. 4
      src/main/java/CredentialList.java
  3. 61
      src/main/java/CredentialRepository.java
  4. 35
      src/test/java/CredentialRepositoryTest.java

4
src/main/java/Credential.java

@ -1,4 +1,6 @@
public class Credential {
import java.io.Serializable;
public class Credential implements Serializable {
private String name, password; private String name, password;
private int id; private int id;

4
src/main/java/CredentialList.java

@ -0,0 +1,4 @@
import java.io.Serializable;
import java.util.ArrayList;
public class CredentialList extends ArrayList<Credential> implements Serializable{}

61
src/main/java/CredentialRepository.java

@ -1,4 +1,7 @@
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/** /**
* Credential Repository for handling user credentials * Credential Repository for handling user credentials
@ -8,7 +11,7 @@ import java.util.ArrayList;
public class CredentialRepository implements CredentialRepositoryInterface{ public class CredentialRepository implements CredentialRepositoryInterface{
private int idCounter = 0; private int idCounter = 0;
private ArrayList<Credential> credentials;
private CredentialList credentials;
/** /**
* Konstruktor * Konstruktor
@ -17,7 +20,7 @@ public class CredentialRepository implements CredentialRepositoryInterface{
*/ */
public CredentialRepository() public CredentialRepository()
{ {
this.credentials = new ArrayList<Credential>();
this.credentials = new CredentialList();
} }
@ -128,6 +131,48 @@ public class CredentialRepository implements CredentialRepositoryInterface{
} }
/**
* Function for serialization of all Credentials
* @param fileName
*/
public void serializeObject(String fileName)
{
if(fileName.equals("")) return;
try {
FileOutputStream file = new FileOutputStream(this.getWorkingDirectory() + fileName + ".claud11");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(this.credentials);
} catch (Exception fail) {
System.err.println("Serialization failed!");
fail.printStackTrace();
}
}
/**
* Function to load serialized Objects from hard drive by path
* @param fileName
*/
public void deserializeObjects(String fileName)
{
if(fileName.equals("")) return;
try {
FileInputStream file = new FileInputStream(this.getWorkingDirectory() + fileName + ".claud11");
ObjectInputStream in = new ObjectInputStream(file);
this.credentials = (CredentialList)in.readObject();
in.close();
file.close();
} catch(Exception fail)
{
System.err.println("Loading of CredentialRepository failed");
}
}
/** /**
* helper function to check list size after insertion / deletion * helper function to check list size after insertion / deletion
* @return * @return
@ -138,6 +183,16 @@ public class CredentialRepository implements CredentialRepositoryInterface{
return this.credentials.size(); return this.credentials.size();
} }
/**
* Helper function for serialization
* @return
*/
private String getWorkingDirectory()
{
return System.getProperty("user.dir");
}
} }

35
src/test/java/CredentialRepositoryTest.java

@ -81,4 +81,39 @@ class CredentialRepositoryTest {
Assertions.assertEquals(null, this.credentialRepository.getCredentialsViaId(0)); Assertions.assertEquals(null, this.credentialRepository.getCredentialsViaId(0));
} }
@Test
void testSerializeObject(){
this.credentialRepository.createNewCredential("mock", "mock");
this.credentialRepository.serializeObject("testName");
}
@Test
void testLoadingOfSerializedObject(){
this.credentialRepository.createNewCredential("mock1", "mock1");
this.credentialRepository.createNewCredential("mock2", "mock2");
this.credentialRepository.createNewCredential("mock3", "mock3");
Credential temporary1 = this.credentialRepository.getCredentialsViaId(0);
Credential temporary2 = this.credentialRepository.getCredentialsViaId(1);
Credential temporary3 = this.credentialRepository.getCredentialsViaId(2);
this.credentialRepository.serializeObject("unitTest_Deserialization");
//delete entries to proove we really load something
this.credentialRepository.delete(0);
this.credentialRepository.delete(0);
Assertions.assertEquals(null, this.credentialRepository.getCredentialsViaName("mock2") );
//load and deserialize object and proove objects are back
this.credentialRepository.deserializeObjects("unitTest_Deserialization");
Assertions.assertEquals(temporary1.getName(), this.credentialRepository.getCredentialsViaName("mock1").getName());
Assertions.assertEquals(temporary1.getPassword(), this.credentialRepository.getCredentialsViaName("mock1").getPassword());
Assertions.assertEquals(temporary2.getName(), this.credentialRepository.getCredentialsViaName("mock2").getName());
Assertions.assertEquals(temporary2.getPassword(), this.credentialRepository.getCredentialsViaName("mock2").getPassword());
Assertions.assertEquals(temporary3.getName(), this.credentialRepository.getCredentialsViaName("mock3").getName());
Assertions.assertEquals(temporary3.getPassword(), this.credentialRepository.getCredentialsViaName("mock3").getPassword());
}
} }
Loading…
Cancel
Save