2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.trace.test;
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;
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;
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.
30 * @author Michael Vorburger.ch
32 public class TracingBrokerTest {
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);
42 for (int i = 0; i < 3; i++) {
43 DOMDataTreeReadWriteTransaction tx = tracingBroker.newReadWriteTransaction();
45 DOMDataTreeReadWriteTransaction anotherTx = tracingBroker.newReadWriteTransaction();
47 DOMTransactionChain txChain = tracingBroker.createTransactionChain(null);
48 DOMDataTreeReadWriteTransaction txFromChain = txChain.newReadWriteTransaction();
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);
55 assertThat(printReturnValue).isTrue();
56 // Assert expectations about stack trace
57 assertThat(output).contains("testPrintOpenTransactions(TracingBrokerTest.java");
58 assertThat(output).doesNotContain(TracingBroker.class.getName());
60 String previousLine = "";
61 for (String line : output.split("\n")) {
62 if (line.contains("(...")) {
63 assertThat(previousLine.contains("(...)")).isFalse();
68 // assert that the sorting works - the x3 is shown before the x1
69 assertThat(output).contains(" DataBroker : newReadWriteTransaction()\n 3x");
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).