aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/serialization/TestDocumentFactory.java
blob: cf26bde9f8c6a1bd84e4b22577f66c68b2045d09 (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.serialization;

import com.yahoo.document.Document;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentTypeManager;

/**
 * Helper class for creating a document for a given document type.
 *
 * @author geirst
 */
public class TestDocumentFactory {

    private final DocumentTypeManager typeManager;
    private final DocumentType docType;
    private final String defaultId;

    public TestDocumentFactory(DocumentType docType, String defaultId) {
        this.docType = docType;
        this.defaultId = defaultId;
        this.typeManager = new DocumentTypeManager();
        typeManager.register(docType);
    }

    /**
     * Utility constructor for setting up a factory with a preexisting document manager.
     * Does <em>not</em> automatically register docType in typeManager, but assumes it's already registered.
     */
    public TestDocumentFactory(DocumentTypeManager typeManager, DocumentType docType, String defaultId) {
        this.docType = docType;
        this.defaultId = defaultId;
        this.typeManager = typeManager;
    }

    public Document createDocument(String id) {
        return new Document(docType, id);
    }

    public Document createDocument() {
        return createDocument(defaultId);
    }

    public DocumentTypeManager typeManager() {
        return typeManager;
    }

}