|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mongodb.internal.dns; |
| 18 | + |
| 19 | +import com.mongodb.MongoConfigurationException; |
| 20 | +import org.junit.jupiter.api.AfterEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import javax.naming.Context; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 27 | + |
| 28 | +public class DefaultDnsResolverTest { |
| 29 | + private static final String TEST_HOST = "test1.test.build.10gen.cc"; |
| 30 | + private static final String DEFAULT_PROVIDER_URL_VALUE = System.getProperty(Context.PROVIDER_URL); |
| 31 | + |
| 32 | + @AfterEach |
| 33 | + public void resetDefaultProviderUrl() { |
| 34 | + if (DEFAULT_PROVIDER_URL_VALUE != null) { |
| 35 | + System.setProperty(Context.PROVIDER_URL, DEFAULT_PROVIDER_URL_VALUE); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void nonDnsProviderUrlShouldBeIgnored() { |
| 41 | + System.setProperty(Context.PROVIDER_URL, "file:///tmp/provider.txt"); |
| 42 | + assertDoesNotThrow(() -> new DefaultDnsResolver().resolveHostFromSrvRecords(TEST_HOST, "mongodb")); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void dnsProviderUrlShouldNotBeIgnored() { |
| 47 | + System.setProperty(Context.PROVIDER_URL, "dns:///mongodb.unknown.server.com"); |
| 48 | + assertThrows(MongoConfigurationException.class, () -> new DefaultDnsResolver().resolveHostFromSrvRecords(TEST_HOST, "mongodb")); |
| 49 | + } |
| 50 | +} |
0 commit comments