summaryrefslogtreecommitdiffstats
path: root/vdslib/src/test/java/com/yahoo/vdslib/distribution/CrossPlatformTestFactory.java
blob: 7dadd9560b53e610f735dea8649260ed1d61b1f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vdslib.distribution;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * Helper class to implement unit tests that should produce the same result in different implementations.
 */
public abstract class CrossPlatformTestFactory {
    private final String directory;
    private final String name;

    public CrossPlatformTestFactory(String directory, String name) {
        this.directory = directory;
        this.name = name;
    }

    public String getName() { return name; }

    public boolean loadTestResults() throws Exception {
        File reference = new File(directory, name + ".reference.results");
        if (!reference.exists()) {
            return false;
        }
        BufferedReader br = new BufferedReader(new FileReader(reference));
        StringBuilder sb = new StringBuilder();
        try{
            while(true) {
                String line = br.readLine();
                if (line == null) break;
                sb.append(line);
            }
            parse(sb.toString());
        } finally {
            br.close();
        }
        return true;
    }

    public void recordTestResults() throws Exception {
        File results = new File(directory, name + ".java.results");
        FileWriter fw = new FileWriter(results);
        try{
            fw.write(serialize());
        } finally {
            fw.close();
        }
    }

    public abstract String serialize() throws Exception;
    public abstract void parse(String serialized) throws Exception;
}