Merge pull request #8137 from amit2103/BAEL-16640
[BAEL-16640] - Split or move core-java-modules/core-java-lang-syntax …
This commit is contained in:
@@ -2,5 +2,8 @@
|
||||
|
||||
This module contains articles about Object-oriented programming (OOP) in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-oop-3)
|
||||
### Relevant Articles:
|
||||
- [Static and Dynamic Binding in Java](https://www.baeldung.com/java-static-dynamic-binding)
|
||||
- [Methods in Java](https://www.baeldung.com/java-methods)
|
||||
- [Java ‘private’ Access Modifier](https://www.baeldung.com/java-private-keyword)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-oop-3)
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.basicmethods;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class PersonName {
|
||||
|
||||
public String getName(String firstName, String lastName) throws RuntimeException {
|
||||
return firstName + " " + lastName;
|
||||
}
|
||||
|
||||
public String getName(String firstName, String middleName, String lastName) {
|
||||
if (!middleName.equals("")) {
|
||||
return firstName + " " + lastName;
|
||||
}
|
||||
return firstName + " " + middleName + " " + lastName;
|
||||
}
|
||||
|
||||
public void printFullName(String firstName, String lastName) {
|
||||
System.out.println(firstName + " " + lastName);
|
||||
}
|
||||
|
||||
public void writeName(String name) throws IOException {
|
||||
PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"));
|
||||
out.println("Name: " + name);
|
||||
out.close();
|
||||
}
|
||||
|
||||
public static String getNameStatic(String firstName, String lastName) {
|
||||
return firstName + " " + lastName;
|
||||
}
|
||||
|
||||
public static void callToStaticMethod() {
|
||||
System.out.println("Name is: " + PersonName.getNameStatic("Alan", "Turing"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.binding;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Created by madhumita.g on 25-07-2018.
|
||||
*/
|
||||
public class Animal {
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger(Animal.class);
|
||||
|
||||
public void makeNoise() {
|
||||
logger.info("generic animal noise");
|
||||
}
|
||||
|
||||
public void makeNoise(Integer repetitions) {
|
||||
while(repetitions != 0) {
|
||||
logger.info("generic animal noise countdown " + repetitions);
|
||||
repetitions -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.binding;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Created by madhumita.g on 25-07-2018.
|
||||
*/
|
||||
public class AnimalActivity {
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger(AnimalActivity.class);
|
||||
|
||||
|
||||
public static void sleep(Animal animal) {
|
||||
logger.info("Animal is sleeping");
|
||||
}
|
||||
|
||||
public static void sleep(Cat cat) {
|
||||
logger.info("Cat is sleeping");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Animal animal = new Animal();
|
||||
|
||||
//calling methods of animal object
|
||||
animal.makeNoise();
|
||||
|
||||
animal.makeNoise(3);
|
||||
|
||||
|
||||
//assigning a dog object to reference of type Animal
|
||||
Animal catAnimal = new Cat();
|
||||
|
||||
catAnimal.makeNoise();
|
||||
|
||||
// calling static function
|
||||
AnimalActivity.sleep(catAnimal);
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.binding;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Created by madhumita.g on 25-07-2018.
|
||||
*/
|
||||
public class Cat extends Animal {
|
||||
|
||||
final static Logger logger = LoggerFactory.getLogger(Cat.class);
|
||||
|
||||
public void makeNoise() {
|
||||
|
||||
logger.info("meow");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.core.privatemodifier;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String privateId;
|
||||
private String name;
|
||||
private boolean manager;
|
||||
|
||||
public Employee(String id, String name) {
|
||||
setPrivateId(id);
|
||||
setName(name);
|
||||
}
|
||||
|
||||
private Employee(String id, String name, boolean managerAttribute) {
|
||||
this.privateId = id;
|
||||
this.name = name;
|
||||
this.privateId = id + "_ID-MANAGER";
|
||||
}
|
||||
|
||||
public void setPrivateId(String customId) {
|
||||
if (customId.endsWith("_ID")) {
|
||||
this.privateId = customId;
|
||||
} else {
|
||||
this.privateId = customId + "_ID";
|
||||
}
|
||||
}
|
||||
|
||||
public String getPrivateId() {
|
||||
return privateId;
|
||||
}
|
||||
|
||||
public boolean isManager() {
|
||||
return manager;
|
||||
}
|
||||
|
||||
public void elevateToManager() {
|
||||
if ("Carl".equals(this.name)) {
|
||||
setManager(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void setManager(boolean manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static Employee buildManager(String id, String name) {
|
||||
return new Employee(id, name, true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.core.privatemodifier;
|
||||
|
||||
public class ExampleClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Employee employee = new Employee("Bob","ABC123");
|
||||
employee.setPrivateId("BCD234");
|
||||
System.out.println(employee.getPrivateId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.core.privatemodifier;
|
||||
|
||||
public class PublicOuterClass {
|
||||
|
||||
public PrivateInnerClass getInnerClassInstance() {
|
||||
PrivateInnerClass myPrivateClassInstance = this.new PrivateInnerClass();
|
||||
myPrivateClassInstance.id = "ID1";
|
||||
myPrivateClassInstance.name = "Bob";
|
||||
return myPrivateClassInstance;
|
||||
}
|
||||
|
||||
private class PrivateInnerClass {
|
||||
public String name;
|
||||
public String id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.baeldung.binding;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||
import ch.qos.logback.core.Appender;
|
||||
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 static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
*https://gist.github.com/bloodredsun/a041de13e57bf3c6c040
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
|
||||
public class AnimalActivityUnitTest {
|
||||
|
||||
@Mock
|
||||
private Appender mockAppender;
|
||||
@Captor
|
||||
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.addAppender(mockAppender);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.detachAppender(mockAppender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnimalReference__whenRefersAnimalObject_shouldCallFunctionWithAnimalParam() {
|
||||
|
||||
Animal animal = new Animal();
|
||||
|
||||
AnimalActivity.sleep(animal);
|
||||
|
||||
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||
|
||||
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||
|
||||
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||
|
||||
assertThat(loggingEvent.getFormattedMessage(),
|
||||
is("Animal is sleeping"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDogReference__whenRefersCatObject_shouldCallFunctionWithAnimalParam() {
|
||||
|
||||
Cat cat = new Cat();
|
||||
|
||||
AnimalActivity.sleep(cat);
|
||||
|
||||
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||
|
||||
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||
|
||||
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||
|
||||
assertThat(loggingEvent.getFormattedMessage(),
|
||||
is("Cat is sleeping"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnimaReference__whenRefersDogObject_shouldCallFunctionWithAnimalParam() {
|
||||
|
||||
Animal cat = new Cat();
|
||||
|
||||
AnimalActivity.sleep(cat);
|
||||
|
||||
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||
|
||||
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||
|
||||
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||
|
||||
assertThat(loggingEvent.getFormattedMessage(),
|
||||
is("Animal is sleeping"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.binding;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||
import ch.qos.logback.core.Appender;
|
||||
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 java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Created by madhumita.g on 01-08-2018.
|
||||
*/
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AnimalUnitTest {
|
||||
@Mock
|
||||
private Appender mockAppender;
|
||||
@Captor
|
||||
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.addAppender(mockAppender);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.detachAppender(mockAppender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalledWithoutParameters_shouldCallFunctionMakeNoiseWithoutParameters() {
|
||||
|
||||
Animal animal = new Animal();
|
||||
|
||||
animal.makeNoise();
|
||||
|
||||
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||
|
||||
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||
|
||||
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||
|
||||
assertThat(loggingEvent.getFormattedMessage(),
|
||||
is("generic animal noise"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalledWithParameters_shouldCallFunctionMakeNoiseWithParameters() {
|
||||
|
||||
Animal animal = new Animal();
|
||||
|
||||
int testValue = 3;
|
||||
animal.makeNoise(testValue);
|
||||
|
||||
verify(mockAppender,times(3)).doAppend(captorLoggingEvent.capture());
|
||||
|
||||
final List<LoggingEvent> loggingEvents = captorLoggingEvent.getAllValues();
|
||||
|
||||
for(LoggingEvent loggingEvent : loggingEvents)
|
||||
{
|
||||
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||
|
||||
assertThat(loggingEvent.getFormattedMessage(),
|
||||
is("generic animal noise countdown "+testValue));
|
||||
|
||||
testValue--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.binding;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||
import ch.qos.logback.core.Appender;
|
||||
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 static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Created by madhumita.g on 01-08-2018.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CatUnitTest {
|
||||
|
||||
@Mock
|
||||
private Appender mockAppender;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.addAppender(mockAppender);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.detachAppender(mockAppender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void makeNoiseTest() {
|
||||
|
||||
Cat cat = new Cat();
|
||||
|
||||
cat.makeNoise();
|
||||
|
||||
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||
|
||||
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||
|
||||
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||
|
||||
assertThat(loggingEvent.getFormattedMessage(),
|
||||
is("meow"));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user