JAVA-27645 Check article code matches github (#15323)

This commit is contained in:
anuragkumawat 2023-12-01 12:14:25 -08:00 committed by GitHub
parent 6508331a74
commit 76a2939e7d
3 changed files with 30 additions and 30 deletions

View File

@ -11,55 +11,55 @@ import static org.junit.jupiter.api.Assertions.*;
public class InstanceOfUnitTest { public class InstanceOfUnitTest {
@Test @Test
void giveWhenInstanceIsCorrect_thenReturnTrue() { void givenWhenInstanceIsCorrect_thenReturnTrue() {
Ring ring = new Ring(); Ring ring = new Ring();
assertTrue(ring instanceof Round); assertTrue(ring instanceof Round);
} }
@Test @Test
void giveWhenObjectIsInstanceOfType_thenReturnTrue() { void givenWhenObjectIsInstanceOfType_thenReturnTrue() {
Circle circle = new Circle(); Circle circle = new Circle();
assertTrue(circle instanceof Circle); assertTrue(circle instanceof Circle);
} }
@Test @Test
void giveWhenInstanceIsOfSubtype_thenReturnTrue() { void givenWhenInstanceIsOfSubtype_thenReturnTrue() {
Circle circle = new Circle(); Circle circle = new Circle();
assertTrue(circle instanceof Round); assertTrue(circle instanceof Round);
} }
@Test @Test
void giveWhenTypeIsInterface_thenReturnTrue() { void givenWhenTypeIsInterface_thenReturnTrue() {
Circle circle = new Circle(); Circle circle = new Circle();
assertTrue(circle instanceof Shape); assertTrue(circle instanceof Shape);
} }
@Test @Test
void giveWhenTypeIsOfObjectType_thenReturnTrue() { void givenWhenTypeIsOfObjectType_thenReturnTrue() {
Thread thread = new Thread(); Thread thread = new Thread();
assertTrue(thread instanceof Object); assertTrue(thread instanceof Object);
} }
@Test @Test
void giveWhenInstanceValueIsNull_thenReturnFalse() { void givenWhenInstanceValueIsNull_thenReturnFalse() {
Circle circle = null; Circle circle = null;
assertFalse(circle instanceof Round); assertFalse(circle instanceof Round);
} }
@Test @Test
void giveWhenComparingClassInDiffHierarchy_thenCompilationError() { void givenWhenComparingClassInDiffHierarchy_thenCompilationError() {
//assertFalse( circle instanceof Triangle); //assertFalse( circle instanceof Triangle);
} }
@Test @Test
void giveWhenStream_whenCastWithoutInstanceOfChk_thenGetException() { void givenWhenStream_whenCastWithoutInstanceOfChk_thenGetException() {
Stream<Round> roundStream = Stream.of(new Ring(), new Ring(), new Circle()); Stream<Round> roundStream = Stream.of(new Ring(), new Ring(), new Circle());
assertThrows(ClassCastException.class, () -> roundStream.map(it -> (Ring) it).collect(Collectors.toList())); assertThrows(ClassCastException.class, () -> roundStream.map(it -> (Ring) it).collect(Collectors.toList()));
} }
@Test @Test
void giveWhenStream_whenCastAfterInstanceOfChk_thenGetExpectedResult() { void givenWhenStream_whenCastAfterInstanceOfChk_thenGetExpectedResult() {
Stream<Round> roundStream = Stream.of(new Ring(), new Ring(), new Circle()); Stream<Round> roundStream = Stream.of(new Ring(), new Ring(), new Circle());
List<Ring> ringList = roundStream.filter(it -> it instanceof Ring).map(it -> (Ring) it).collect(Collectors.toList()); List<Ring> ringList = roundStream.filter(it -> it instanceof Ring).map(it -> (Ring) it).collect(Collectors.toList());
assertEquals(2, ringList.size()); assertEquals(2, ringList.size());

View File

@ -14,52 +14,52 @@ import java.util.List;
public class Application { public class Application {
// CSV Reader Examples // CSV Reader Examples
public static List<String[]> readLineByLineSyncExample() throws Exception { public static List<String[]> readLineByLineExample() throws Exception {
Path path = Helpers.twoColumnCsvPath(); Path path = Helpers.twoColumnCsvPath();
return CsvReaderExamples.readLineByLine(path); return CsvReaderExamples.readLineByLine(path);
} }
public static List<String[]> readAllLinesSyncExample() throws Exception { public static List<String[]> readAllLinesExample() throws Exception {
Path path = Helpers.twoColumnCsvPath(); Path path = Helpers.twoColumnCsvPath();
return CsvReaderExamples.readAllLines(path); return CsvReaderExamples.readAllLines(path);
} }
// CSV Writer Examples // CSV Writer Examples
public static String writeLineByLineSyncExample() throws Exception { public static String writeLineByLineExample() throws Exception {
Path path = Helpers.fileOutOnePath(); Path path = Helpers.fileOutOnePath();
return CsvWriterExamples.writeLineByLine(Helpers.fourColumnCsvString(), path); return CsvWriterExamples.writeLineByLine(Helpers.fourColumnCsvString(), path);
} }
public static String writeAllLinesSyncExample() throws Exception { public static String writeAllLinesExample() throws Exception {
Path path = Helpers.fileOutAllPath(); Path path = Helpers.fileOutAllPath();
return CsvWriterExamples.writeAllLines(Helpers.fourColumnCsvString(), path); return CsvWriterExamples.writeAllLines(Helpers.fourColumnCsvString(), path);
} }
// Bean Examples // Bean Examples
public static List<CsvBean> simpleSyncPositionBeanExample() throws Exception { public static List<CsvBean> simplePositionBeanExample() throws Exception {
Path path = Helpers.twoColumnCsvPath(); Path path = Helpers.twoColumnCsvPath();
return BeanExamples.beanBuilderExample(path, SimplePositionBean.class); return BeanExamples.beanBuilderExample(path, SimplePositionBean.class);
} }
public static List<CsvBean> namedSyncColumnBeanExample() throws Exception { public static List<CsvBean> namedColumnBeanExample() throws Exception {
Path path = Helpers.namedColumnCsvPath(); Path path = Helpers.namedColumnCsvPath();
return BeanExamples.beanBuilderExample(path, NamedColumnBean.class); return BeanExamples.beanBuilderExample(path, NamedColumnBean.class);
} }
public static String writeSyncCsvFromBeanExample() throws Exception { public static String writeCsvFromBeanExample() throws Exception {
Path path = Helpers.fileOutBeanPath(); Path path = Helpers.fileOutBeanPath();
return BeanExamples.writeCsvFromBean(path); return BeanExamples.writeCsvFromBean(path);
} }
public static void main(String[] args) { public static void main(String[] args) {
try { try {
simpleSyncPositionBeanExample(); simplePositionBeanExample();
namedSyncColumnBeanExample(); namedColumnBeanExample();
writeSyncCsvFromBeanExample(); writeCsvFromBeanExample();
readLineByLineSyncExample(); readLineByLineExample();
readAllLinesSyncExample(); readAllLinesExample();
writeLineByLineSyncExample(); writeLineByLineExample();
writeAllLinesSyncExample(); writeAllLinesExample();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Error during csv processing", e); throw new RuntimeException("Error during csv processing", e);
} }

View File

@ -13,7 +13,7 @@ public class OpenCsvIntegrationTest {
@Test @Test
public void givenSampleData_whenReadUsingPosition_thenContentsRead() throws Exception { public void givenSampleData_whenReadUsingPosition_thenContentsRead() throws Exception {
List<CsvBean> values = Application.simpleSyncPositionBeanExample(); List<CsvBean> values = Application.simplePositionBeanExample();
assertThat(values) assertThat(values)
.extracting(Object::toString) .extracting(Object::toString)
@ -28,7 +28,7 @@ public class OpenCsvIntegrationTest {
@Test @Test
public void givenSampleData_whenReadUsingNamedColumn_thenContentsRead() throws Exception { public void givenSampleData_whenReadUsingNamedColumn_thenContentsRead() throws Exception {
List<CsvBean> values = Application.namedSyncColumnBeanExample(); List<CsvBean> values = Application.namedColumnBeanExample();
assertThat(values) assertThat(values)
.extracting(Object::toString) .extracting(Object::toString)
@ -41,7 +41,7 @@ public class OpenCsvIntegrationTest {
@Test @Test
public void givenSampleData_whenReadLineByLine_thenContentsRead() throws Exception { public void givenSampleData_whenReadLineByLine_thenContentsRead() throws Exception {
List<String[]> lineByLineContents = Application.readLineByLineSyncExample(); List<String[]> lineByLineContents = Application.readLineByLineExample();
assertThat(lineByLineContents) assertThat(lineByLineContents)
.containsExactly( .containsExactly(
@ -56,7 +56,7 @@ public class OpenCsvIntegrationTest {
@Test @Test
public void givenSampleData_whenReadAllLines_thenContentsRead() throws Exception { public void givenSampleData_whenReadAllLines_thenContentsRead() throws Exception {
List<String[]> contents = Application.readAllLinesSyncExample(); List<String[]> contents = Application.readAllLinesExample();
assertThat(contents) assertThat(contents)
.containsExactly( .containsExactly(
@ -70,7 +70,7 @@ public class OpenCsvIntegrationTest {
@Test @Test
public void givenSampleData_whenWriteCsvUsingBean_thenContentsWritten() throws Exception { public void givenSampleData_whenWriteCsvUsingBean_thenContentsWritten() throws Exception {
String contents = Application.writeSyncCsvFromBeanExample(); String contents = Application.writeCsvFromBeanExample();
assertThat(contents.split(NEW_LINE)) assertThat(contents.split(NEW_LINE))
.containsExactly( .containsExactly(
@ -82,7 +82,7 @@ public class OpenCsvIntegrationTest {
@Test @Test
public void givenSampleData_whenWriteCsvLineByLine_thenContentsWritten() throws Exception { public void givenSampleData_whenWriteCsvLineByLine_thenContentsWritten() throws Exception {
String contents = Application.writeLineByLineSyncExample(); String contents = Application.writeLineByLineExample();
assertThat(contents.split(NEW_LINE)) assertThat(contents.split(NEW_LINE))
.containsExactly( .containsExactly(
@ -94,7 +94,7 @@ public class OpenCsvIntegrationTest {
@Test @Test
public void givenSampleData_whenWriteCsvAllLines_thenContentsWritten() throws Exception { public void givenSampleData_whenWriteCsvAllLines_thenContentsWritten() throws Exception {
String contents = Application.writeAllLinesSyncExample(); String contents = Application.writeAllLinesExample();
assertThat(contents.split(NEW_LINE)) assertThat(contents.split(NEW_LINE))
.containsExactly( .containsExactly(