Skip to content

Commit

Permalink
[WAYANG-32] Tests and fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ro-pardo committed Aug 30, 2021
1 parent 5344336 commit a1c1ac8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@ public class ProfileDB {
*/
private Gson gson;

public void save(Experiment... experiments) throws IOException {
this.storage.save(experiments);
}

public void save(Collection<Experiment> experiments) throws IOException {
this.storage.save(experiments);
}

public void save(Collection<Experiment> experiments, OutputStream outputStream) throws IOException {
this.storage.save(experiments, outputStream);
}

public void append(Experiment... experiments) throws IOException {
this.storage.append(experiments);
}

public void append(Collection<Experiment> experiments) throws IOException {
this.storage.append(experiments);
}

public Collection<Experiment> load() throws IOException {
return this.storage.load();
}

public Collection<Experiment> load(InputStream inputStream) throws IOException {
return this.storage.load(inputStream);
}

/**
* Creates a new instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public void changeLocation(URI uri){
this.storageFile = uri;
}

public void save(Experiment... experiments) throws IOException {}
public void save(Experiment... experiments) throws IOException {
System.out.println("llegue");
}

public void save(Collection<Experiment> experiments) throws IOException {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class ProfileDBTest {
Expand Down Expand Up @@ -52,7 +53,7 @@ public void testPolymorphSaveAndLoad() throws IOException {
*/
byte[] buffer;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
profileDB.getStorage().save(Collections.singleton(experiment), bos);
profileDB.save(Collections.singleton(experiment), bos);
bos.close();
buffer = bos.toByteArray();
System.out.println("Buffer contents: " + new String(buffer, "UTF-8"));
Expand All @@ -62,7 +63,7 @@ public void testPolymorphSaveAndLoad() throws IOException {
* Lee el experimento desde el buffer en memoria
*/
ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
Collection<Experiment> loadedExperiments = profileDB.getStorage().load(bis);
Collection<Experiment> loadedExperiments = profileDB.load(bis);

// Compare the experiments.
Assert.assertEquals(1, loadedExperiments.size());
Expand Down Expand Up @@ -102,14 +103,14 @@ public void testRecursiveSaveAndLoad() throws IOException {
// Save the experiment.
byte[] buffer;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
profileDB.getStorage().save(Collections.singleton(experiment), bos);
profileDB.save(Collections.singleton(experiment), bos);
bos.close();
buffer = bos.toByteArray();
System.out.println("Buffer contents: " + new String(buffer, "UTF-8"));

// Load the experiment.
ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
Collection<Experiment> loadedExperiments = profileDB.getStorage().load(bis);
Collection<Experiment> loadedExperiments = profileDB.load(bis);

// Compare the experiments.
Assert.assertEquals(1, loadedExperiments.size());
Expand Down Expand Up @@ -146,14 +147,18 @@ public void testFileOperations() throws IOException {

// Save the experiments.
File tempDir = Files.createTempDirectory("profiledb").toFile();
//File dir = Files.createTempDirectory(Paths.get("/Users/rodrigopardomeza/Desktop/random/"), "profiledb").toFile();
//File dir = Paths.get("/Users/rodrigopardomeza/Desktop/random/").toFile();
File file = new File(tempDir, "profiledb.json");
profileDB.getStorage().save(experiment1);
profileDB.getStorage().append(experiment2, experiment3);
file.createNewFile();
profileDB.save(experiment1);
profileDB.append(experiment2, experiment3);

System.out.println("File plat" + file.toPath().toUri().toString());
Files.lines(file.toPath()).forEach(System.out::println);

// Load and compare.
final Set<Experiment> loadedExperiments = new HashSet<>(profileDB.getStorage().load());
final Set<Experiment> loadedExperiments = new HashSet<>(profileDB.load());
final List<Experiment> expectedExperiments = Arrays.asList(experiment1, experiment2, experiment3);
Assert.assertEquals(expectedExperiments.size(), loadedExperiments.size());
Assert.assertEquals(new HashSet<>(expectedExperiments), new HashSet<>(loadedExperiments));
Expand Down Expand Up @@ -181,13 +186,14 @@ public void testAppendOnNonExistentFile() throws IOException {
// Save the experiments.
File tempDir = Files.createTempDirectory("profiledb").toFile();
File file = new File(tempDir, "new-profiledb.json");
file.createNewFile();
Assert.assertTrue(!file.exists() || file.delete());
profileDB.getStorage().append(experiment1);
profileDB.append(experiment1);

Files.lines(file.toPath()).forEach(System.out::println);

// Load and compare.
final Set<Experiment> loadedExperiments = new HashSet<>(profileDB.getStorage().load());
final Set<Experiment> loadedExperiments = new HashSet<>(profileDB.load());
final List<Experiment> expectedExperiments = Collections.singletonList(experiment1);
Assert.assertEquals(expectedExperiments.size(), loadedExperiments.size());
Assert.assertEquals(new HashSet<>(expectedExperiments), new HashSet<>(loadedExperiments));
Expand Down

0 comments on commit a1c1ac8

Please sign in to comment.