[BAEL-13505] Move articles out of core-java part 1
This commit is contained in:
@@ -1,50 +0,0 @@
|
||||
package com.baeldung.curltojava;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaCurlExamplesLiveTest {
|
||||
|
||||
@Test
|
||||
public void givenCommand_whenCalled_thenProduceZeroExitCode() throws IOException {
|
||||
String command = "curl -X GET https://postman-echo.com/get?foo1=bar1&foo2=bar2";
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
|
||||
processBuilder.directory(new File("/home/"));
|
||||
Process process = processBuilder.start();
|
||||
InputStream inputStream = process.getInputStream();
|
||||
// Consume the inputStream so the process can exit
|
||||
JavaCurlExamples.consumeInputStream(inputStream);
|
||||
int exitCode = process.exitValue();
|
||||
|
||||
Assert.assertEquals(0, exitCode);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNewCommands_whenCalled_thenCheckIfIsAlive() throws IOException {
|
||||
String command = "curl -X GET https://postman-echo.com/get?foo1=bar1&foo2=bar2";
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
|
||||
processBuilder.directory(new File("/home/"));
|
||||
Process process = processBuilder.start();
|
||||
|
||||
// Re-use processBuilder
|
||||
processBuilder.command(new String[]{"newCommand", "arguments"});
|
||||
|
||||
Assert.assertEquals(true, process.isAlive());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestPost_thenCheckIfReturnContent() throws IOException {
|
||||
String command = "curl -X POST https://postman-echo.com/post --data foo1=bar1&foo2=bar2";
|
||||
Process process = Runtime.getRuntime().exec(command);
|
||||
|
||||
// Get the POST result
|
||||
String content = JavaCurlExamples.inputStreamToString(process.getInputStream());
|
||||
|
||||
Assert.assertTrue(null != content && !content.isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package com.baeldung.exceptions;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||
import ch.qos.logback.core.Appender;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GlobalExceptionHandlerUnitTest {
|
||||
|
||||
@Mock
|
||||
private Appender<ILoggingEvent> mockAppender;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.addAppender(mockAppender);
|
||||
|
||||
Handler globalExceptionHandler = new Handler();
|
||||
Thread.setDefaultUncaughtExceptionHandler(globalExceptionHandler);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.detachAppender(mockAppender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArithmeticException_thenUseUncaughtExceptionHandler() throws InterruptedException {
|
||||
|
||||
Thread globalExceptionHandlerThread = new Thread() {
|
||||
public void run() {
|
||||
GlobalExceptionHandler globalExceptionHandlerObj = new GlobalExceptionHandler();
|
||||
globalExceptionHandlerObj.performArithmeticOperation(99, 0);
|
||||
}
|
||||
};
|
||||
|
||||
globalExceptionHandlerThread.start();
|
||||
globalExceptionHandlerThread.join();
|
||||
|
||||
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||
LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||
|
||||
assertThat(loggingEvent.getLevel()).isEqualTo(Level.INFO);
|
||||
assertThat(loggingEvent.getFormattedMessage()).isEqualTo("Unhandled exception caught!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class BaeldungReflectionUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Exception {
|
||||
Customer customer = new Customer(1, "Himanshu", null, null);
|
||||
|
||||
List<String> result = BaeldungReflectionUtils.getNullPropertiesList(customer);
|
||||
List<String> expectedFieldNames = Arrays.asList("emailId", "phoneNumber");
|
||||
|
||||
assertTrue(result.size() == expectedFieldNames.size());
|
||||
assertTrue(result.containsAll(expectedFieldNames));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PersonAndEmployeeReflectionUnitTest {
|
||||
|
||||
// Fields names
|
||||
private static final String LAST_NAME_FIELD = "lastName";
|
||||
private static final String FIRST_NAME_FIELD = "firstName";
|
||||
private static final String EMPLOYEE_ID_FIELD = "employeeId";
|
||||
private static final String MONTH_EMPLOYEE_REWARD_FIELD = "reward";
|
||||
|
||||
@Test
|
||||
public void givenPersonClass_whenGetDeclaredFields_thenTwoFields() {
|
||||
// When
|
||||
Field[] allFields = Person.class.getDeclaredFields();
|
||||
|
||||
// Then
|
||||
assertEquals(2, allFields.length);
|
||||
|
||||
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||
field.getName().equals(LAST_NAME_FIELD)
|
||||
&& field.getType().equals(String.class))
|
||||
);
|
||||
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||
field.getName().equals(FIRST_NAME_FIELD)
|
||||
&& field.getType().equals(String.class))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeClass_whenGetDeclaredFields_thenOneField() {
|
||||
// When
|
||||
Field[] allFields = Employee.class.getDeclaredFields();
|
||||
|
||||
// Then
|
||||
assertEquals(1, allFields.length);
|
||||
|
||||
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||
field.getName().equals(EMPLOYEE_ID_FIELD)
|
||||
&& field.getType().equals(int.class))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeClass_whenSuperClassGetDeclaredFields_thenOneField() {
|
||||
// When
|
||||
Field[] allFields = Employee.class.getSuperclass().getDeclaredFields();
|
||||
|
||||
// Then
|
||||
assertEquals(2, allFields.length);
|
||||
|
||||
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||
field.getName().equals(LAST_NAME_FIELD)
|
||||
&& field.getType().equals(String.class))
|
||||
);
|
||||
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||
field.getName().equals(FIRST_NAME_FIELD)
|
||||
&& field.getType().equals(String.class))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeClass_whenGetDeclaredFieldsOnBothClasses_thenThreeFields() {
|
||||
// When
|
||||
Field[] personFields = Employee.class.getSuperclass().getDeclaredFields();
|
||||
Field[] employeeFields = Employee.class.getDeclaredFields();
|
||||
Field[] allFields = new Field[employeeFields.length + personFields.length];
|
||||
Arrays.setAll(allFields, i -> (i < personFields.length ? personFields[i] : employeeFields[i - personFields.length]));
|
||||
|
||||
// Then
|
||||
assertEquals(3, allFields.length);
|
||||
|
||||
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||
field.getName().equals(LAST_NAME_FIELD)
|
||||
&& field.getType().equals(String.class))
|
||||
);
|
||||
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||
field.getName().equals(FIRST_NAME_FIELD)
|
||||
&& field.getType().equals(String.class))
|
||||
);
|
||||
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||
field.getName().equals(EMPLOYEE_ID_FIELD)
|
||||
&& field.getType().equals(int.class))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeClass_whenGetDeclaredFieldsOnEmployeeSuperclassWithModifiersFilter_thenOneFields() {
|
||||
// When
|
||||
List<Field> personFields = Arrays.stream(Employee.class.getSuperclass().getDeclaredFields())
|
||||
.filter(f -> Modifier.isPublic(f.getModifiers()) || Modifier.isProtected(f.getModifiers()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Then
|
||||
assertEquals(1, personFields.size());
|
||||
|
||||
assertTrue(personFields.stream().anyMatch(field ->
|
||||
field.getName().equals(LAST_NAME_FIELD)
|
||||
&& field.getType().equals(String.class))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMonthEmployeeClass_whenGetAllFields_thenThreeFields() {
|
||||
// When
|
||||
List<Field> allFields = getAllFields(MonthEmployee.class);
|
||||
|
||||
// Then
|
||||
assertEquals(3, allFields.size());
|
||||
|
||||
assertTrue(allFields.stream().anyMatch(field ->
|
||||
field.getName().equals(LAST_NAME_FIELD)
|
||||
&& field.getType().equals(String.class))
|
||||
);
|
||||
assertTrue(allFields.stream().anyMatch(field ->
|
||||
field.getName().equals(EMPLOYEE_ID_FIELD)
|
||||
&& field.getType().equals(int.class))
|
||||
);
|
||||
assertTrue(allFields.stream().anyMatch(field ->
|
||||
field.getName().equals(MONTH_EMPLOYEE_REWARD_FIELD)
|
||||
&& field.getType().equals(double.class))
|
||||
);
|
||||
}
|
||||
|
||||
public List<Field> getAllFields(Class clazz) {
|
||||
if (clazz == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Field> result = new ArrayList<>(getAllFields(clazz.getSuperclass()));
|
||||
List<Field> filteredFields = Arrays.stream(clazz.getDeclaredFields())
|
||||
.filter(f -> Modifier.isPublic(f.getModifiers()) || Modifier.isProtected(f.getModifiers()))
|
||||
.collect(Collectors.toList());
|
||||
result.addAll(filteredFields);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user