2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.yangtools.yang.parser.spi.source;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.assertThrows;
13 import static org.mockito.Mockito.doReturn;
15 import java.util.Optional;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.junit.MockitoJUnitRunner;
22 @RunWith(MockitoJUnitRunner.StrictStubs.class)
23 public class SourceExceptionTest {
25 public StatementSourceReference mock;
28 public void before() {
29 doReturn("mock").when(mock).toString();
33 public void testThrowIfFalse() {
34 SourceException.throwIf(false, mock, "");
38 public void testThrowIfTrueMockNull() {
39 assertThrows(NullPointerException.class, () -> SourceException.throwIf(true, mock, null));
43 public void testThrowIfTrueMockEmpty() {
44 assertThrows(SourceException.class, () -> SourceException.throwIf(true, mock, ""));
48 public void testThrowIfNullNullMockNull() {
49 assertThrows(NullPointerException.class, () -> SourceException.throwIfNull(null, mock, null));
53 public void testThrowIfNullNullMockEmpty() {
54 assertThrows(SourceException.class, () -> SourceException.throwIfNull(null, mock, ""));
58 public void testThrowIfNullMock() {
59 assertSame(mock, SourceException.throwIfNull(mock, mock, ""));
63 public void testUnwrapPresent() {
64 assertEquals("test", SourceException.unwrap(Optional.of("test"), mock, ""));
68 public void testUnwrapAbsent() {
69 assertThrows(SourceException.class, () -> SourceException.unwrap(Optional.empty(), mock, ""));