Use the new constructor of 'Disruptor' instead of a deperecated one
[mdsal.git] / trace / mdsal-trace-impl / src / test / java / org / opendaylight / mdsal / trace / test / TracingBrokerTest.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.trace.test;
9
10 import static com.google.common.truth.Truth.assertThat;
11 import static java.nio.charset.StandardCharsets.UTF_8;
12 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
13 import static org.mockito.Mockito.mock;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.PrintStream;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
19 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
21 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
22 import org.opendaylight.mdsal.trace.impl.TracingBroker;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsaltrace.rev160908.Config;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsaltrace.rev160908.ConfigBuilder;
25
26 /**
27  * Test of {@link TracingBroker}. This test resides outside of org.opendaylight.mdsal.trace.impl package on purpose,
28  * as the package name is used to suppress stack entries.
29  *
30  * @author Michael Vorburger.ch
31  */
32 public class TracingBrokerTest {
33
34     @Test
35     @SuppressWarnings({ "resource", "unused" }) // Finding resource leaks is the point of this test
36     public void testPrintOpenTransactions() {
37         DOMDataBroker domDataBroker = mock(DOMDataBroker.class, RETURNS_DEEP_STUBS);
38         Config config = new ConfigBuilder().setTransactionDebugContextEnabled(true).build();
39         BindingNormalizedNodeSerializer codec = mock(BindingNormalizedNodeSerializer.class);
40         TracingBroker tracingBroker = new TracingBroker("mock", domDataBroker, config, codec);
41
42         for (int i = 0; i < 3; i++) {
43             DOMDataTreeReadWriteTransaction tx = tracingBroker.newReadWriteTransaction();
44         }
45         DOMDataTreeReadWriteTransaction anotherTx = tracingBroker.newReadWriteTransaction();
46
47         DOMTransactionChain txChain = tracingBroker.createTransactionChain(null);
48         DOMDataTreeReadWriteTransaction txFromChain = txChain.newReadWriteTransaction();
49
50         ByteArrayOutputStream baos = new ByteArrayOutputStream();
51         PrintStream ps = new PrintStream(baos);
52         boolean printReturnValue = tracingBroker.printOpenTransactions(ps, 1);
53         String output = new String(baos.toByteArray(), UTF_8);
54
55         assertThat(printReturnValue).isTrue();
56         // Assert expectations about stack trace
57         assertThat(output).contains("testPrintOpenTransactions(TracingBrokerTest.java");
58         assertThat(output).doesNotContain(TracingBroker.class.getName());
59
60         String previousLine = "";
61         for (String line : output.split("\n")) {
62             if (line.contains("(...")) {
63                 assertThat(previousLine.contains("(...)")).isFalse();
64             }
65             previousLine = line;
66         }
67
68         // assert that the sorting works - the x3 is shown before the x1
69         assertThat(output).contains("  DataBroker : newReadWriteTransaction()\n    3x");
70
71         // We don't do any verify/times on the mocks,
72         // because the main point of the test is just to verify that
73         // printOpenTransactions runs through without any exceptions
74         // (e.g. it used to have a ClassCastException).
75     }
76
77 }