Do not use ListenerRegistration
[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 java.nio.charset.StandardCharsets.UTF_8;
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.hamcrest.CoreMatchers.not;
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
16 import static org.mockito.Mockito.mock;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.PrintStream;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
22 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
24 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
25 import org.opendaylight.mdsal.trace.impl.TracingBroker;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsaltrace.rev160908.Config;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsaltrace.rev160908.ConfigBuilder;
28
29 /**
30  * Test of {@link TracingBroker}. This test resides outside of org.opendaylight.mdsal.trace.impl package on purpose,
31  * as the package name is used to suppress stack entries.
32  *
33  * @author Michael Vorburger.ch
34  */
35 public class TracingBrokerTest {
36     @Test
37     @SuppressWarnings({ "resource", "unused" }) // Finding resource leaks is the point of this test
38     public void testPrintOpenTransactions() {
39         DOMDataBroker domDataBroker = mock(DOMDataBroker.class, RETURNS_DEEP_STUBS);
40         Config config = new ConfigBuilder().setTransactionDebugContextEnabled(true).build();
41         BindingCodecTree codec = mock(BindingCodecTree.class);
42         TracingBroker tracingBroker = new TracingBroker(domDataBroker, config, codec);
43
44         for (int i = 0; i < 3; i++) {
45             DOMDataTreeReadWriteTransaction tx = tracingBroker.newReadWriteTransaction();
46         }
47         DOMDataTreeReadWriteTransaction anotherTx = tracingBroker.newReadWriteTransaction();
48
49         DOMTransactionChain txChain = tracingBroker.createTransactionChain();
50         DOMDataTreeReadWriteTransaction txFromChain = txChain.newReadWriteTransaction();
51
52         ByteArrayOutputStream baos = new ByteArrayOutputStream();
53         PrintStream ps = new PrintStream(baos);
54         boolean printReturnValue = tracingBroker.printOpenTransactions(ps, 1);
55         String output = new String(baos.toByteArray(), UTF_8);
56
57         assertTrue(printReturnValue);
58         // Assert expectations about stack trace
59         assertThat(output, containsString("testPrintOpenTransactions(TracingBrokerTest.java"));
60         assertThat(output, not(containsString(TracingBroker.class.getName())));
61
62         String previousLine = "";
63         for (String line : output.split("\n")) {
64             if (line.contains("(...")) {
65                 assertThat(previousLine, not(containsString("(...)")));
66             }
67             previousLine = line;
68         }
69
70         // assert that the sorting works - the x3 is shown before the x1
71         assertThat(output, containsString("  DataBroker : newReadWriteTransaction()\n    3x"));
72
73         // We don't do any verify/times on the mocks,
74         // because the main point of the test is just to verify that
75         // printOpenTransactions runs through without any exceptions
76         // (e.g. it used to have a ClassCastException).
77     }
78 }