aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/test/java/com/yahoo/vdslib/distribution/CrossPlatformTestFactory.java
blob: 6422bdf9ecccc0348842186c4ad269dcb1c4801e (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
// Copyright Vespa.ai. 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 void loadTestResults() throws Exception {
        File reference = new File(directory, name + ".reference.results");
        if (!reference.exists()) {
            return;
        }
        try (BufferedReader br = new BufferedReader(new FileReader(reference))) {
            StringBuilder sb = new StringBuilder();
            while (true) {
                String line = br.readLine();
                if (line == null) break;
                sb.append(line);
            }
            parse(sb.toString());
        }
    }

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

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