kezhenxu94
3 years ago
committed by
GitHub
77 changed files with 633 additions and 2191 deletions
@ -1,95 +0,0 @@ |
|||||||
/* |
|
||||||
* 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
package org.apache.dolphinscheduler.api.utils; |
|
||||||
|
|
||||||
|
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
import java.io.BufferedReader; |
|
||||||
import java.io.IOException; |
|
||||||
import java.io.InputStreamReader; |
|
||||||
import java.io.OutputStream; |
|
||||||
import java.net.InetAddress; |
|
||||||
import java.net.InetSocketAddress; |
|
||||||
import java.net.Socket; |
|
||||||
import java.net.SocketTimeoutException; |
|
||||||
import java.util.Objects; |
|
||||||
|
|
||||||
public class FourLetterWordMain { |
|
||||||
|
|
||||||
private static final int DEFAULT_SOCKET_TIMEOUT = 5000; |
|
||||||
protected static final Logger LOG = LoggerFactory.getLogger(FourLetterWordMain.class); |
|
||||||
|
|
||||||
private FourLetterWordMain() { |
|
||||||
throw new IllegalStateException("FourLetterWordMain class"); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Send the 4letterword |
|
||||||
* @param host the destination host |
|
||||||
* @param port the destination port |
|
||||||
* @param cmd the 4letterword |
|
||||||
* @return server response |
|
||||||
* @throws java.io.IOException io exceptions |
|
||||||
*/ |
|
||||||
public static String send4LetterWord(String host, int port, String cmd) |
|
||||||
throws IOException { |
|
||||||
return send4LetterWord(host, port, cmd, DEFAULT_SOCKET_TIMEOUT); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Send the 4letterword |
|
||||||
* @param host the destination host |
|
||||||
* @param port the destination port |
|
||||||
* @param cmd the 4letterword |
|
||||||
* @param timeout in milliseconds, maximum time to wait while connecting/reading data |
|
||||||
* @return server response |
|
||||||
* @throws java.io.IOException io exceptions |
|
||||||
*/ |
|
||||||
public static String send4LetterWord(String host, int port, String cmd, int timeout) |
|
||||||
throws IOException { |
|
||||||
Objects.requireNonNull(cmd, "cmd must not be null"); |
|
||||||
LOG.info("connecting to {} {}", host, port); |
|
||||||
InetSocketAddress hostaddress= host != null ? new InetSocketAddress(host, port) : |
|
||||||
new InetSocketAddress(InetAddress.getByName(null), port); |
|
||||||
|
|
||||||
try (Socket sock = new Socket()) { |
|
||||||
sock.setSoTimeout(timeout); |
|
||||||
sock.connect(hostaddress, timeout); |
|
||||||
OutputStream outstream = sock.getOutputStream(); |
|
||||||
outstream.write(cmd.getBytes()); |
|
||||||
outstream.flush(); |
|
||||||
// this replicates NC - close the output stream before reading
|
|
||||||
sock.shutdownOutput(); |
|
||||||
|
|
||||||
try (BufferedReader reader = |
|
||||||
new BufferedReader( |
|
||||||
new InputStreamReader(sock.getInputStream()))) { |
|
||||||
StringBuilder sb = new StringBuilder(); |
|
||||||
String line; |
|
||||||
while ((line = reader.readLine()) != null) { |
|
||||||
sb.append(line + "\n"); |
|
||||||
} |
|
||||||
return sb.toString(); |
|
||||||
} |
|
||||||
} catch (SocketTimeoutException e) { |
|
||||||
throw new IOException("Exception while executing four letter word: " + cmd, e); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,217 +0,0 @@ |
|||||||
/* |
|
||||||
* 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
package org.apache.dolphinscheduler.api.utils; |
|
||||||
|
|
||||||
import org.junit.Assert; |
|
||||||
import org.junit.Before; |
|
||||||
import org.junit.Test; |
|
||||||
import org.junit.runner.RunWith; |
|
||||||
import org.mockito.InjectMocks; |
|
||||||
import org.mockito.Mock; |
|
||||||
import org.mockito.Mockito; |
|
||||||
import org.powermock.api.mockito.PowerMockito; |
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest; |
|
||||||
import org.powermock.modules.junit4.PowerMockRunner; |
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream; |
|
||||||
import java.io.ByteArrayOutputStream; |
|
||||||
import java.io.InputStream; |
|
||||||
import java.net.InetSocketAddress; |
|
||||||
import java.net.Socket; |
|
||||||
import java.net.SocketTimeoutException; |
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals; |
|
||||||
import static org.mockito.ArgumentMatchers.any; |
|
||||||
import static org.mockito.Mockito.doThrow; |
|
||||||
import static org.mockito.Mockito.when; |
|
||||||
|
|
||||||
@RunWith(PowerMockRunner.class) |
|
||||||
@PrepareForTest({FourLetterWordMain.class, Socket.class}) |
|
||||||
public class FourLetterWordMainTest { |
|
||||||
|
|
||||||
private static final Logger logger = |
|
||||||
LoggerFactory.getLogger(FourLetterWordMainTest.class); |
|
||||||
private static final String NEW_LINE = "\n"; |
|
||||||
|
|
||||||
@InjectMocks |
|
||||||
private FourLetterWordMain fourLetterWord; |
|
||||||
@Mock |
|
||||||
private Socket socket; |
|
||||||
@Mock |
|
||||||
private InetSocketAddress socketAddress; |
|
||||||
|
|
||||||
private final String localHost = "127.0.0.1"; |
|
||||||
private final int zkPort = 2181; |
|
||||||
private ByteArrayOutputStream byteArrayOutputStream; |
|
||||||
private InputStream inputStream; |
|
||||||
|
|
||||||
private String cmd; |
|
||||||
private String testResult; |
|
||||||
private String expectedStr; |
|
||||||
|
|
||||||
@Before |
|
||||||
public void setUp() { |
|
||||||
// mock socket class
|
|
||||||
PowerMockito.mockStatic(Socket.class); |
|
||||||
try { |
|
||||||
PowerMockito.whenNew(Socket.class).withNoArguments() |
|
||||||
.thenReturn(socket); |
|
||||||
} catch (Exception e) { |
|
||||||
e.printStackTrace(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* None mock test method, just to check zookeeper status. |
|
||||||
* Comment @Before notation to run this test. |
|
||||||
* Zookeeper status will be as: |
|
||||||
* Zookeeper version: 3.4.11 ... |
|
||||||
* Received: 6739707 |
|
||||||
* Sent: 6739773 |
|
||||||
* Connections: 20 |
|
||||||
* Outstanding: 0 |
|
||||||
* Zxid: 0x9ba |
|
||||||
* Mode: standalone |
|
||||||
* Node count: 263 |
|
||||||
*/ |
|
||||||
public void testCmd() { |
|
||||||
// "192.168.64.11"
|
|
||||||
// final String zkHost = localHost;
|
|
||||||
final String zkHost = "192.168.64.11"; |
|
||||||
cmd = "srvr"; |
|
||||||
try { |
|
||||||
// Change localhost to right zk host ip.
|
|
||||||
final String result = FourLetterWordMain |
|
||||||
.send4LetterWord(zkHost, zkPort, cmd); |
|
||||||
logger.info(cmd + ": " + result + "<<<"); |
|
||||||
} catch (Exception e) { |
|
||||||
e.printStackTrace(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testEmptyCmd() { |
|
||||||
cmd = ""; |
|
||||||
expectedStr = ""; |
|
||||||
testSend4LetterWord(cmd, expectedStr); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testNullCmd() { |
|
||||||
cmd = null; |
|
||||||
|
|
||||||
try { |
|
||||||
testResult = FourLetterWordMain |
|
||||||
.send4LetterWord(localHost, zkPort, cmd); |
|
||||||
} catch (Exception e) { |
|
||||||
testResult = e.getMessage(); |
|
||||||
} |
|
||||||
|
|
||||||
logger.info("testNullCmd result: " + testResult); |
|
||||||
assertEquals("cmd must not be null", testResult); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testNullSocketOutput() { |
|
||||||
cmd = "test null socket output"; |
|
||||||
expectedStr = null; |
|
||||||
testSend4LetterWord(cmd, expectedStr); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testOneLineOutput() { |
|
||||||
cmd = "line 1"; |
|
||||||
|
|
||||||
// line end without \n
|
|
||||||
expectedStr = "line 1" + NEW_LINE; |
|
||||||
testSend4LetterWord(cmd, expectedStr); |
|
||||||
|
|
||||||
// line end with \n
|
|
||||||
expectedStr = "line 1\n" + NEW_LINE; |
|
||||||
testSend4LetterWord(cmd, expectedStr); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testMultiline() { |
|
||||||
cmd = "line 1 " + NEW_LINE + |
|
||||||
"line 2 " + NEW_LINE + |
|
||||||
"line 3 " + NEW_LINE; |
|
||||||
|
|
||||||
expectedStr = cmd + NEW_LINE; |
|
||||||
testSend4LetterWord(cmd, expectedStr); |
|
||||||
|
|
||||||
expectedStr = NEW_LINE + NEW_LINE + NEW_LINE; |
|
||||||
testSend4LetterWord(cmd, expectedStr); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testSocketTimeOut() { |
|
||||||
cmd = "test socket time out"; |
|
||||||
|
|
||||||
try { |
|
||||||
doThrow(new SocketTimeoutException()) |
|
||||||
.when(socket) |
|
||||||
.connect(any(InetSocketAddress.class), Mockito.anyInt()); |
|
||||||
testResult = FourLetterWordMain |
|
||||||
.send4LetterWord(localHost, zkPort, cmd); |
|
||||||
} catch (Exception e) { |
|
||||||
testResult = e.getMessage(); |
|
||||||
} |
|
||||||
|
|
||||||
logger.info("testSocketTimeOut result: " + testResult); |
|
||||||
assertEquals( |
|
||||||
"Exception while executing four letter word: " + cmd, |
|
||||||
testResult |
|
||||||
); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Test FourLetterWordMain.send4LetterWord() with input cmd and output |
|
||||||
* string. |
|
||||||
* @param cmd |
|
||||||
* @param expectedStr |
|
||||||
*/ |
|
||||||
public void testSend4LetterWord(String cmd, String expectedStr) { |
|
||||||
try { |
|
||||||
final byte[] strBytes = cmd.getBytes(); |
|
||||||
byteArrayOutputStream = new ByteArrayOutputStream(strBytes.length); |
|
||||||
byteArrayOutputStream.write(strBytes, 0, strBytes.length); |
|
||||||
|
|
||||||
inputStream = new ByteArrayInputStream(expectedStr.getBytes()); |
|
||||||
|
|
||||||
when(socket.getOutputStream()) |
|
||||||
.thenReturn(byteArrayOutputStream); |
|
||||||
when(socket.getInputStream()).thenReturn(inputStream); |
|
||||||
|
|
||||||
final String result = FourLetterWordMain |
|
||||||
.send4LetterWord(localHost, zkPort, cmd); |
|
||||||
logger.info( |
|
||||||
"testSend4LetterWord: " + |
|
||||||
"cmd: " + cmd + |
|
||||||
", expectedStr: " + expectedStr + |
|
||||||
", result: " + result + "." |
|
||||||
); |
|
||||||
Assert.assertEquals(expectedStr, result); |
|
||||||
} catch (Exception e) { |
|
||||||
e.printStackTrace(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,38 +0,0 @@ |
|||||||
/* |
|
||||||
* 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
package org.apache.dolphinscheduler.common.utils; |
|
||||||
|
|
||||||
public class ArrayUtils { |
|
||||||
|
|
||||||
public static byte[] clone(byte[] array) { |
|
||||||
return array == null ? null : (byte[])((byte[])array.clone()); |
|
||||||
} |
|
||||||
|
|
||||||
public static byte[] addAll(byte[] array1, byte[] array2) { |
|
||||||
if (array1 == null) { |
|
||||||
return clone(array2); |
|
||||||
} else if (array2 == null) { |
|
||||||
return clone(array1); |
|
||||||
} else { |
|
||||||
byte[] joinedArray = new byte[array1.length + array2.length]; |
|
||||||
System.arraycopy(array1, 0, joinedArray, 0, array1.length); |
|
||||||
System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); |
|
||||||
return joinedArray; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,48 +0,0 @@ |
|||||||
/* |
|
||||||
* 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
package org.apache.dolphinscheduler.common.utils; |
|
||||||
|
|
||||||
public class EnumUtils { |
|
||||||
|
|
||||||
private EnumUtils() { |
|
||||||
throw new UnsupportedOperationException("Construct EnumUtils"); |
|
||||||
} |
|
||||||
|
|
||||||
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) { |
|
||||||
if (enumName == null) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
try { |
|
||||||
return Enum.valueOf(enumClass, enumName); |
|
||||||
} catch (final IllegalArgumentException ex) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public static <E extends Enum<E>> boolean isValidEnum(final Class<E> enumClass, final String enumName) { |
|
||||||
if (enumName == null) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
try { |
|
||||||
Enum.valueOf(enumClass, enumName); |
|
||||||
return true; |
|
||||||
} catch (final IllegalArgumentException ex) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,45 +0,0 @@ |
|||||||
/* |
|
||||||
* 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
package org.apache.dolphinscheduler.common.utils; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.Constants; |
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils; |
|
||||||
|
|
||||||
/** |
|
||||||
* sensitive log Util |
|
||||||
*/ |
|
||||||
public class SensitiveLogUtils { |
|
||||||
|
|
||||||
private SensitiveLogUtils() { |
|
||||||
throw new UnsupportedOperationException("Construct SensitiveLogUtils"); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param dataSourcePwd data source password |
|
||||||
* @return String |
|
||||||
*/ |
|
||||||
public static String maskDataSourcePwd(String dataSourcePwd) { |
|
||||||
|
|
||||||
if (!StringUtils.isEmpty(dataSourcePwd)) { |
|
||||||
dataSourcePwd = Constants.PASSWORD_DEFAULT; |
|
||||||
} |
|
||||||
return dataSourcePwd; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,161 +0,0 @@ |
|||||||
/* |
|
||||||
* 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. |
|
||||||
*/ |
|
||||||
package org.apache.dolphinscheduler.common.utils; |
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.Constants; |
|
||||||
import org.junit.Assert; |
|
||||||
import org.junit.Test; |
|
||||||
|
|
||||||
import java.util.*; |
|
||||||
|
|
||||||
|
|
||||||
public class CollectionUtilsTest { |
|
||||||
|
|
||||||
@Test |
|
||||||
public void equalLists() { |
|
||||||
Assert.assertTrue(CollectionUtils.equalLists(null,null)); |
|
||||||
Assert.assertTrue(CollectionUtils.equalLists(new ArrayList<Integer>(),new ArrayList<Integer>())); |
|
||||||
List<Integer> a = new ArrayList<Integer>(); |
|
||||||
a.add(1); |
|
||||||
a.add(2); |
|
||||||
List<Integer> b = new ArrayList<Integer>(); |
|
||||||
b.add(1); |
|
||||||
b.add(2); |
|
||||||
Assert.assertTrue(CollectionUtils.equalLists(a, b)); |
|
||||||
a.add(1); |
|
||||||
Assert.assertFalse(CollectionUtils.equalLists(a, b)); |
|
||||||
b.add(2); |
|
||||||
Assert.assertFalse(CollectionUtils.equalLists(a, b)); |
|
||||||
a.add(2); |
|
||||||
b.add(1); |
|
||||||
a.add(4); |
|
||||||
b.add(2); |
|
||||||
Assert.assertFalse(CollectionUtils.equalLists(a, b)); |
|
||||||
Assert.assertFalse(CollectionUtils.equalLists(null, new ArrayList<Integer>())); |
|
||||||
Assert.assertFalse(CollectionUtils.equalLists(new ArrayList<Integer>(), null)); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void subtract() { |
|
||||||
Set<Integer> a = new HashSet<Integer>(); |
|
||||||
a.add(1); |
|
||||||
a.add(2); |
|
||||||
a.add(3); |
|
||||||
Set<Integer> b = new HashSet<Integer>(); |
|
||||||
b.add(0); |
|
||||||
b.add(2); |
|
||||||
b.add(4); |
|
||||||
Assert.assertArrayEquals(new Integer[]{1,3},CollectionUtils.subtract(a,b).toArray()); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void stringToMap() { |
|
||||||
Map<String, String> a = CollectionUtils.stringToMap("a=b;c=d;", ";"); |
|
||||||
Assert.assertNotNull(a); |
|
||||||
Assert.assertTrue(a.size() == 2); |
|
||||||
a = CollectionUtils.stringToMap(null, ";"); |
|
||||||
Assert.assertTrue(a.isEmpty()); |
|
||||||
a = CollectionUtils.stringToMap("", ";"); |
|
||||||
Assert.assertTrue(a.isEmpty()); |
|
||||||
a = CollectionUtils.stringToMap("a=b;c=d", ""); |
|
||||||
Assert.assertTrue(a.isEmpty()); |
|
||||||
a = CollectionUtils.stringToMap("a=b;c=d", null); |
|
||||||
Assert.assertTrue(a.isEmpty()); |
|
||||||
a = CollectionUtils.stringToMap("a=b;c=d;e=f", ";"); |
|
||||||
Assert.assertEquals(3, a.size()); |
|
||||||
a = CollectionUtils.stringToMap("a;b=f", ";"); |
|
||||||
Assert.assertTrue(a.isEmpty()); |
|
||||||
a = CollectionUtils.stringToMap("a=b;c=d;e=f;", ";", "test"); |
|
||||||
Assert.assertEquals(3, a.size()); |
|
||||||
Assert.assertNotNull(a.get("testa")); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void getListByExclusion() { |
|
||||||
Assert.assertNotNull(CollectionUtils.getListByExclusion(null, null)); |
|
||||||
List<Integer> originList = new ArrayList<>(); |
|
||||||
originList.add(1); |
|
||||||
originList.add(2); |
|
||||||
List<Map<String, Object>> ret = CollectionUtils.getListByExclusion(originList, null); |
|
||||||
Assert.assertEquals(2, ret.size()); |
|
||||||
ret = CollectionUtils.getListByExclusion(originList, new HashSet<>()); |
|
||||||
Assert.assertEquals(2, ret.size()); |
|
||||||
Assert.assertFalse(ret.get(0).isEmpty()); |
|
||||||
Set<String> exclusion = new HashSet<>(); |
|
||||||
exclusion.add(Constants.CLASS); |
|
||||||
ret = CollectionUtils.getListByExclusion(originList, exclusion); |
|
||||||
Assert.assertEquals(2, ret.size()); |
|
||||||
Assert.assertTrue(ret.get(0).isEmpty()); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void isNotEmpty() { |
|
||||||
List<Integer> list = new ArrayList<>(); |
|
||||||
Assert.assertFalse(CollectionUtils.isNotEmpty(list)); |
|
||||||
Assert.assertFalse(CollectionUtils.isNotEmpty(null)); |
|
||||||
} |
|
||||||
@Test |
|
||||||
public void isEmpty(){ |
|
||||||
List<Integer> list = new ArrayList<>(); |
|
||||||
Assert.assertTrue(CollectionUtils.isEmpty(list)); |
|
||||||
Assert.assertTrue(CollectionUtils.isEmpty(null)); |
|
||||||
list.add(1); |
|
||||||
Assert.assertFalse(CollectionUtils.isEmpty(list)); |
|
||||||
} |
|
||||||
@Test |
|
||||||
public void isEqualCollection() { |
|
||||||
List<Integer> a = new ArrayList<>(); |
|
||||||
a.add(1); |
|
||||||
List<Integer> b = new ArrayList<>(); |
|
||||||
b.add(1); |
|
||||||
Assert.assertTrue(CollectionUtils.isEqualCollection(a,b)); |
|
||||||
b.add(2); |
|
||||||
Assert.assertFalse(CollectionUtils.isEqualCollection(a,b)); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void getCardinalityMap(){ |
|
||||||
List<Integer> a = new ArrayList<>(); |
|
||||||
a.add(1); |
|
||||||
a.add(2); |
|
||||||
a.add(2); |
|
||||||
a.add(3); |
|
||||||
a.add(3); |
|
||||||
a.add(3); |
|
||||||
Map<Integer,Integer> cardinalityMap = CollectionUtils.getCardinalityMap(a); |
|
||||||
Assert.assertEquals(3, cardinalityMap.size()); |
|
||||||
Assert.assertEquals(1, cardinalityMap.get(1).intValue()); |
|
||||||
Assert.assertEquals(2, cardinalityMap.get(2).intValue()); |
|
||||||
Assert.assertEquals(3, cardinalityMap.get(3).intValue()); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void transformToList() { |
|
||||||
List<String> stringList = new ArrayList<>(); |
|
||||||
stringList.add("1"); |
|
||||||
List<Integer> integers = CollectionUtils.transformToList(stringList, String::length); |
|
||||||
Assert.assertFalse(integers.isEmpty()); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void collectionToMap() { |
|
||||||
List<String> stringList = new ArrayList<>(); |
|
||||||
stringList.add("1"); |
|
||||||
Map<Integer, String> lengthStringMap = CollectionUtils.collectionToMap(stringList, String::length); |
|
||||||
Assert.assertFalse(lengthStringMap.isEmpty()); |
|
||||||
} |
|
||||||
} |
|
@ -1,85 +0,0 @@ |
|||||||
/* |
|
||||||
* 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
package org.apache.dolphinscheduler.common.utils; |
|
||||||
|
|
||||||
import java.io.BufferedReader; |
|
||||||
import java.io.FileInputStream; |
|
||||||
import java.io.IOException; |
|
||||||
import java.io.InputStreamReader; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Optional; |
|
||||||
|
|
||||||
import org.junit.Assert; |
|
||||||
import org.junit.Test; |
|
||||||
import org.junit.Test.None; |
|
||||||
import org.junit.runner.RunWith; |
|
||||||
import org.powermock.api.mockito.PowerMockito; |
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest; |
|
||||||
import org.powermock.modules.junit4.PowerMockRunner; |
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
@RunWith(PowerMockRunner.class) |
|
||||||
@PrepareForTest({LoggerUtils.class}) |
|
||||||
public class LoggerUtilsTest { |
|
||||||
private Logger logger = LoggerFactory.getLogger(LoggerUtilsTest.class); |
|
||||||
|
|
||||||
@Test |
|
||||||
public void buildTaskId() { |
|
||||||
|
|
||||||
String taskId = LoggerUtils.buildTaskId(LoggerUtils.TASK_LOGGER_INFO_PREFIX, 798L,1,4084, 15210); |
|
||||||
|
|
||||||
Assert.assertEquals(" - [taskAppId=TASK-798_1-4084-15210]", taskId); |
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void getAppIds() { |
|
||||||
List<String> appIdList = LoggerUtils.getAppIds("Running job: application_1_1", logger); |
|
||||||
Assert.assertEquals("application_1_1", appIdList.get(0)); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testReadWholeFileContent() throws Exception { |
|
||||||
BufferedReader bufferedReader = PowerMockito.mock(BufferedReader.class); |
|
||||||
PowerMockito.whenNew(BufferedReader.class).withAnyArguments().thenReturn(bufferedReader); |
|
||||||
PowerMockito.when(bufferedReader.readLine()).thenReturn("").thenReturn(null); |
|
||||||
FileInputStream fileInputStream = PowerMockito.mock(FileInputStream.class); |
|
||||||
PowerMockito.whenNew(FileInputStream.class).withAnyArguments().thenReturn(fileInputStream); |
|
||||||
|
|
||||||
InputStreamReader inputStreamReader = PowerMockito.mock(InputStreamReader.class); |
|
||||||
PowerMockito.whenNew(InputStreamReader.class).withAnyArguments().thenReturn(inputStreamReader); |
|
||||||
|
|
||||||
String log = LoggerUtils.readWholeFileContent("/tmp/log"); |
|
||||||
Assert.assertNotNull(log); |
|
||||||
|
|
||||||
PowerMockito.when(bufferedReader.readLine()).thenThrow(new IOException()); |
|
||||||
log = LoggerUtils.readWholeFileContent("/tmp/log"); |
|
||||||
Assert.assertNotNull(log); |
|
||||||
} |
|
||||||
|
|
||||||
@Test(expected = None.class) |
|
||||||
public void testLogError() { |
|
||||||
Optional<Logger> loggerOptional = Optional.of(this.logger); |
|
||||||
|
|
||||||
LoggerUtils.logError(loggerOptional, "error message"); |
|
||||||
LoggerUtils.logError(loggerOptional, new RuntimeException("error message")); |
|
||||||
LoggerUtils.logError(loggerOptional, "error message", new RuntimeException("runtime exception")); |
|
||||||
LoggerUtils.logInfo(loggerOptional, "info message"); |
|
||||||
} |
|
||||||
} |
|
@ -1,37 +0,0 @@ |
|||||||
/* |
|
||||||
* 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. |
|
||||||
*/ |
|
||||||
package org.apache.dolphinscheduler.common.utils; |
|
||||||
|
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.common.Constants; |
|
||||||
import org.junit.Assert; |
|
||||||
import org.junit.Test; |
|
||||||
|
|
||||||
|
|
||||||
public class SensitiveLogUtilsTest { |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testMaskDataSourcePwd() { |
|
||||||
|
|
||||||
String password = "123456"; |
|
||||||
String emptyPassword = ""; |
|
||||||
|
|
||||||
Assert.assertEquals(Constants.PASSWORD_DEFAULT, SensitiveLogUtils.maskDataSourcePwd(password)); |
|
||||||
Assert.assertEquals("", SensitiveLogUtils.maskDataSourcePwd(emptyPassword)); |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
@ -1,30 +0,0 @@ |
|||||||
/* |
|
||||||
* 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
package org.apache.dolphinscheduler.server.utils; |
|
||||||
|
|
||||||
public class ArgsUtils { |
|
||||||
|
|
||||||
private ArgsUtils() throws IllegalStateException { |
|
||||||
throw new IllegalStateException("Utility class"); |
|
||||||
} |
|
||||||
|
|
||||||
public static String escape(String arg) { |
|
||||||
return arg.replace(" ", "\\ ").replace("\"", "\\\"").replace("'", "\\'"); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
Loading…
Reference in new issue