PreconditionsTest.java 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17
package org.apache.dolphinscheduler.common.utils;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;


public class PreconditionsTest {
    public static final Logger logger = LoggerFactory.getLogger(PreconditionsTest.class);

    /**
     * Test checkNotNull
     */
    @Test
    public void testCheckNotNull() throws Exception {
B
bao liang 已提交
36
        String testReference = "test object";
37
        Assert.assertEquals(testReference, Preconditions.checkNotNull(testReference));
B
bao liang 已提交
38
        Assert.assertEquals(testReference,Preconditions.checkNotNull(testReference,"object is null"));
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

        //test  reference is  null
        try {
            Preconditions.checkNotNull(null);
        } catch (NullPointerException ex) {
            assertNull(ex.getMessage());
        }

        try {
            Preconditions.checkNotNull("");
        } catch (NullPointerException ex) {
            assertNull(ex.getMessage());
        }

        try {
B
bao liang 已提交
54
            Preconditions.checkNotNull(null,"object is null");
55
        } catch (NullPointerException ex) {
B
bao liang 已提交
56
            assertThat(ex.getMessage(), containsString("object is null"));
57 58 59
        }

        try {
B
bao liang 已提交
60
            Preconditions.checkNotNull("","object is null");
61
        } catch (NullPointerException ex) {
B
bao liang 已提交
62
            assertThat(ex.getMessage(), containsString("object is null"));
63 64 65 66 67
        }

    }

}