Merge "Bug 1386: Avoid commit deadlock"
[controller.git] / opendaylight / md-sal / sal-protocolbuffer-encoding / src / test / java / org / opendaylight / controller / protobuff / messages / AbstractMessagesTest.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.protobuff.messages;
12
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.FileOutputStream;
16
17 /**
18  * @author: syedbahm Date: 7/31/14
19  */
20 public abstract class AbstractMessagesTest {
21   public final String VERSION_COMPATIBILTY_TEST_DATA_PATH = "."
22       + File.separator + "src" + File.separator + "test" + File.separator
23       + "resources" + File.separator + "version-compatibility-serialized-data";
24   private File file;
25   private File testDataFile;
26
27   protected AbstractMessagesTest() {
28     init();
29   }
30
31   protected void init() {
32     file = new File(getTestFileName());
33     testDataFile =
34         new File(VERSION_COMPATIBILTY_TEST_DATA_PATH + File.separator
35             + getTestFileName() + "Data");
36   }
37
38
39
40   abstract public void verifySerialization() throws Exception;
41
42
43   protected void writeToFile(
44       com.google.protobuf.GeneratedMessage.Builder<?> builder) throws Exception {
45
46     FileOutputStream output = new FileOutputStream(file);
47     builder.build().writeTo(output);
48     output.close();;
49
50   }
51
52   protected com.google.protobuf.GeneratedMessage readFromFile(
53       com.google.protobuf.Parser<?> parser) throws Exception {
54     com.google.protobuf.GeneratedMessage message =
55         (com.google.protobuf.GeneratedMessage) parser
56             .parseFrom(new FileInputStream(file));
57
58     /*Note: we will delete only the test file -- comment below if you want to capture the
59        version-compatibility-serialized-data test data file.The file will be generated at root of the
60        sal-protocolbuffer-encoding
61        and you need to move it to test/resources/version-compatbility-serialized-data folder renaming the file to include suffix <TestFileName>"Data"
62     */
63      file.delete();
64     return message;
65   }
66
67   protected com.google.protobuf.GeneratedMessage readFromTestDataFile(
68       com.google.protobuf.Parser<?> parser) throws Exception {
69     return (com.google.protobuf.GeneratedMessage) parser
70         .parseFrom(new FileInputStream(testDataFile));
71   }
72
73
74   public abstract String getTestFileName();
75
76
77 }