Skip to content

Commit 3804568

Browse files
authored
Merge branch 'eugenp:master' into master
2 parents 73b99b7 + 59dc3b2 commit 3804568

File tree

159 files changed

+4753
-156
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+4753
-156
lines changed

apache-poi-4/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CellStyleTest_output.xlsx
-6.26 KB
Binary file not shown.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.baeldung.spark.dataframeconcat;
2+
3+
import org.apache.spark.sql.Dataset;
4+
import org.apache.spark.sql.Row;
5+
import org.apache.spark.sql.SparkSession;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
public class ConcatRowsExample {
13+
14+
private static final Logger logger = LoggerFactory.getLogger(ConcatRowsExample.class);
15+
16+
public static void main(String[] args) {
17+
SparkSession spark = SparkSession.builder()
18+
.appName("Row-wise Concatenation Example")
19+
.master("local[*]")
20+
.getOrCreate();
21+
22+
try {
23+
// Create sample data
24+
List<Person> data1 = Arrays.asList(
25+
new Person(1, "Alice"),
26+
new Person(2, "Bob")
27+
);
28+
29+
List<Person> data2 = Arrays.asList(
30+
new Person(3, "Charlie"),
31+
new Person(4, "Diana")
32+
);
33+
34+
Dataset<Row> df1 = spark.createDataFrame(data1, Person.class);
35+
Dataset<Row> df2 = spark.createDataFrame(data2, Person.class);
36+
37+
logger.info("First DataFrame:");
38+
df1.show();
39+
40+
logger.info("Second DataFrame:");
41+
df2.show();
42+
43+
// Row-wise concatenation using reusable method
44+
Dataset<Row> combined = concatenateDataFrames(df1, df2);
45+
46+
logger.info("After row-wise concatenation:");
47+
combined.show();
48+
} finally {
49+
spark.stop();
50+
}
51+
}
52+
53+
/**
54+
* Concatenates two DataFrames row-wise using unionByName.
55+
* This method is extracted for reusability and testing.
56+
*/
57+
public static Dataset<Row> concatenateDataFrames(Dataset<Row> df1, Dataset<Row> df2) {
58+
return df1.unionByName(df2);
59+
}
60+
61+
public static class Person implements java.io.Serializable {
62+
private int id;
63+
private String name;
64+
65+
public Person() {
66+
}
67+
68+
public Person(int id, String name) {
69+
this.id = id;
70+
this.name = name;
71+
}
72+
73+
public int getId() {
74+
return id;
75+
}
76+
77+
public void setId(int id) {
78+
this.id = id;
79+
}
80+
81+
public String getName() {
82+
return name;
83+
}
84+
85+
public void setName(String name) {
86+
this.name = name;
87+
}
88+
}
89+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.spark.dataframeconcat;
2+
3+
import org.apache.spark.sql.Dataset;
4+
import org.apache.spark.sql.Row;
5+
import org.apache.spark.sql.SparkSession;
6+
import org.junit.jupiter.api.*;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
class ConcatRowsExampleUnitTest {
13+
14+
private static SparkSession spark;
15+
private Dataset<Row> df1;
16+
private Dataset<Row> df2;
17+
18+
@BeforeAll
19+
static void setupClass() {
20+
spark = SparkSession.builder()
21+
.appName("Row-wise Concatenation Test")
22+
.master("local[*]")
23+
.getOrCreate();
24+
}
25+
26+
@BeforeEach
27+
void setup() {
28+
df1 = spark.createDataFrame(
29+
Arrays.asList(
30+
new ConcatRowsExample.Person(1, "Alice"),
31+
new ConcatRowsExample.Person(2, "Bob")
32+
),
33+
ConcatRowsExample.Person.class
34+
);
35+
36+
df2 = spark.createDataFrame(
37+
Arrays.asList(
38+
new ConcatRowsExample.Person(3, "Charlie"),
39+
new ConcatRowsExample.Person(4, "Diana")
40+
),
41+
ConcatRowsExample.Person.class
42+
);
43+
}
44+
45+
@AfterAll
46+
static void tearDownClass() {
47+
spark.stop();
48+
}
49+
50+
@Test
51+
void givenTwoDataFrames_whenConcatenated_thenRowCountMatches() {
52+
Dataset<Row> combined = ConcatRowsExample.concatenateDataFrames(df1, df2);
53+
54+
assertEquals(
55+
4,
56+
combined.count(),
57+
"The combined DataFrame should have 4 rows"
58+
);
59+
}
60+
61+
@Test
62+
void givenTwoDataFrames_whenConcatenated_thenSchemaRemainsSame() {
63+
Dataset<Row> combined = ConcatRowsExample.concatenateDataFrames(df1, df2);
64+
65+
assertEquals(
66+
df1.schema(),
67+
combined.schema(),
68+
"Schema should remain consistent after concatenation"
69+
);
70+
}
71+
72+
@Test
73+
void givenTwoDataFrames_whenConcatenated_thenDataContainsExpectedName() {
74+
Dataset<Row> combined = ConcatRowsExample.concatenateDataFrames(df1, df2);
75+
76+
assertTrue(
77+
combined
78+
.filter("name = 'Charlie'")
79+
.count() > 0,
80+
"Combined DataFrame should contain Charlie"
81+
);
82+
}
83+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.objectarray;
2+
3+
public class ObjectArray {
4+
5+
public static Object[] createSampleArray() {
6+
Object[] values = new Object[5];
7+
values[0] = "Hello"; // String
8+
values[1] = 42; // Autoboxed Integer
9+
values[2] = 3.14; // Autoboxed Double
10+
values[3] = new int[]{1, 2, 3}; // int[] array
11+
values[4] = new Person("Alice", 30); // Custom class
12+
return values;
13+
}
14+
15+
// Nested static Person class
16+
public static class Person {
17+
private final String name;
18+
private final int age;
19+
20+
public Person(String name, int age) {
21+
this.name = name;
22+
this.age = age;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public int getAge() {
30+
return age;
31+
}
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.objectarray;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ObjectArrayUnitTest {
8+
9+
@Test
10+
void givenObjectArray_whenHoldingDifferentTypes_thenCorrect() {
11+
Object[] values = ObjectArray.createSampleArray();
12+
13+
assertEquals("Hello", values[0]); // String stored
14+
assertEquals(42, values[1]); // Integer stored
15+
assertEquals(3.14, values[2]); // Double stored
16+
assertTrue(values[3] instanceof int[]); // Array stored
17+
assertTrue(values[4] instanceof ObjectArray.Person);// Custom class stored
18+
}
19+
20+
@Test
21+
void givenObjectArray_whenAccessingPerson_thenCorrect() {
22+
Object[] values = ObjectArray.createSampleArray();
23+
24+
ObjectArray.Person person = (ObjectArray.Person) values[4];
25+
assertEquals("Alice", person.getName()); // Name matches
26+
assertEquals(30, person.getAge()); // Age matches
27+
}
28+
29+
@Test
30+
void givenObjectArray_whenCastingIncorrectly_thenClassCastException() {
31+
Object[] values = ObjectArray.createSampleArray();
32+
33+
assertThrows(ClassCastException.class, () -> {
34+
String wrongCast = (String) values[1]; // values[1] is actually Integer
35+
});
36+
}
37+
}

core-java-modules/core-java-datetime-string-2/src/test/java/com/baeldung/formatduration/FormatDurationUnitTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class FormatDurationUnitTest {
1313

1414

1515
@Test
16-
public void givenInterval_WhenFormatInterval_formatDuration() {
16+
public void givenInterval_whenFormatInterval_thenFormatDuration() {
1717
long HH = TimeUnit.MILLISECONDS.toHours(38114000);
1818
long MM = TimeUnit.MILLISECONDS.toMinutes(38114000) % 60;
1919
long SS = TimeUnit.MILLISECONDS.toSeconds(38114000) % 60;
@@ -23,7 +23,16 @@ public void givenInterval_WhenFormatInterval_formatDuration() {
2323
}
2424

2525
@Test
26-
public void givenInterval_WhenFormatUsingDuration_formatDuration() {
26+
public void givenIntMinutes_whenConvertUsingTimeUnit_thenFormatHHMM() {
27+
int totalMinutes = 155;
28+
long hours = TimeUnit.MINUTES.toHours(totalMinutes);
29+
long remainingMinutes = totalMinutes - TimeUnit.HOURS.toMinutes(hours);
30+
String timeInHHMM = String.format("%02d:%02d", hours, remainingMinutes);
31+
assertThat(timeInHHMM).isEqualTo("02:35");
32+
}
33+
34+
@Test
35+
public void givenInterval_whenFormatUsingDuration_thenFormatDuration() {
2736
Duration duration = Duration.ofMillis(38114000);
2837
long seconds = duration.getSeconds();
2938
long HH = seconds / 3600;
@@ -32,16 +41,15 @@ public void givenInterval_WhenFormatUsingDuration_formatDuration() {
3241
String timeInHHMMSS = String.format("%02d:%02d:%02d", HH, MM, SS);
3342
assertThat(timeInHHMMSS).isEqualTo("10:35:14");
3443
}
35-
36-
44+
3745
@Test
38-
public void givenInterval_WhenFormatDurationUsingApacheCommons_formatDuration() {
46+
public void givenInterval_whenFormatDurationUsingApacheCommons_thenFormatDuration() {
3947
assertThat(DurationFormatUtils.formatDuration(38114000, "HH:mm:ss"))
4048
.isEqualTo("10:35:14");
4149
}
4250

4351
@Test
44-
public void givenInterval_WhenFormatDurationUsingJodaTime_formatDuration() {
52+
public void givenInterval_whenFormatDurationUsingJodaTime_thenFormatDuration() {
4553
org.joda.time.Duration duration = new org.joda.time.Duration(38114000);
4654
Period period = duration.toPeriod();
4755
long HH = period.getHours();
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
test-link*
2-
0.*
2+
0.*
3+
src/test/resources/iostreams/TestFile.txt

core-java-modules/core-java-io-4/src/test/resources/iostreams/TestFile.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/test/resources/data_output.txt

0 commit comments

Comments
 (0)