diff --git a/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/ConstraintsBuilder.java b/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/ConstraintsBuilder.java new file mode 100644 index 0000000000..ce437fac6d --- /dev/null +++ b/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/ConstraintsBuilder.java @@ -0,0 +1,15 @@ +package reminderapplication; + +import java.awt.GridBagConstraints; +import java.awt.Insets; + +public class ConstraintsBuilder { + + static GridBagConstraints constraint(int x, int y) { + final GridBagConstraints gridBagConstraints = new GridBagConstraints(); + gridBagConstraints.gridx = x; + gridBagConstraints.gridy = y; + gridBagConstraints.insets = new Insets(5, 5, 5, 5); + return gridBagConstraints; + } +} diff --git a/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/EditReminderFrame.java b/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/EditReminderFrame.java new file mode 100644 index 0000000000..818cea403e --- /dev/null +++ b/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/EditReminderFrame.java @@ -0,0 +1,194 @@ +package reminderapplication; + +import static reminderapplication.ConstraintsBuilder.*; + +import java.awt.GridBagLayout; +import java.awt.HeadlessException; +import java.lang.reflect.InvocationTargetException; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.TimeUnit; +import javax.swing.DefaultComboBoxModel; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JTextField; +import javax.swing.SwingUtilities; + +public class EditReminderFrame extends JFrame { + + private static Timer TIMER = new Timer(); + + private final TimeReminderApplication reminderApplication; + private final JLabel reminderTextLabel; + private final JLabel repeatPeriodLabel; + private final JLabel setDelayLabel; + private final JComboBox delay; + private final JComboBox period; + private final JButton cancelButton; + private final JButton okButton; + private final JTextField textField; + private final JLabel delaysLabel; + private final JLabel periodLabel; + + private final int reminderIndex; + + public EditReminderFrame(TimeReminderApplication reminderApp, String reminderText, int delayInSeconds, int periodInSeconds, int index) throws HeadlessException { + this.reminderApplication = reminderApp; + reminderIndex = index; + textField = createTextField(reminderText); + delay = createDelayComboBox(delayInSeconds); + period = createPeriodComboBox(periodInSeconds); + cancelButton = createCancelButton(); + okButton = createOkButton(); + reminderTextLabel = createReminderTextLabel(); + repeatPeriodLabel = createRepeatPeriodLabel(); + setDelayLabel = createSetDelayLabel(); + delaysLabel = createDelaysLabel(); + periodLabel = createPeriodLabel(); + configureVisualRepresentation(); + configureActions(); + } + + private void configureActions() { + updateReminder(); + } + + private void configureVisualRepresentation() { + configureFrame(); + setLocationRelativeTo(null); + setLayout(new GridBagLayout()); + add(reminderTextLabel, constraint(0,0)); + add(repeatPeriodLabel, constraint(1,0)); + add(setDelayLabel, constraint(2,0)); + add(textField, constraint(0, 1)); + add(delay, constraint(1, 1)); + add(period, constraint(2, 1)); + add(delaysLabel, constraint(1,3)); + add(periodLabel, constraint(2,3)); + add(okButton, constraint(1, 4)); + add(cancelButton, constraint(2, 4)); + pack(); + setVisible(true); + } + + private void configureFrame() { + setTitle("Set Reminder"); + setName("Set Reminder"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + } + + private static JLabel createSetDelayLabel() { + return createLabel("Set Delay", "Set Delay Label"); + } + + private static JLabel createRepeatPeriodLabel() { + return createLabel("Set Period", "Set Repeat Period Label"); + } + + private static JLabel createReminderTextLabel() { + return createLabel("Reminder Text", "Reminder Text Label"); + } + + private JLabel createPeriodLabel() { + return createLabel("0", "Period label"); + } + + private JLabel createDelaysLabel() { + return createLabel("30", "Delays Label"); + } + + private JComboBox createPeriodComboBox(final int periodInSeconds) { + final JComboBox comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{0, 5, 10, 20})); + comboBox.setSelectedItem(periodInSeconds); + comboBox.setName("set Period"); + comboBox.addActionListener(e -> periodLabel.setText(comboBox.getSelectedItem().toString())); + return comboBox; + } + + private JComboBox createDelayComboBox(final int delayInSeconds) { + final JComboBox comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{30, 25, 15, 5})); + comboBox.setSelectedItem(delayInSeconds); + comboBox.setName("set Delay"); + comboBox.addActionListener(e -> delaysLabel.setText(comboBox.getSelectedItem().toString())); + return comboBox; + } + + private JTextField createTextField(final String reminderText) { + final JTextField textField = new JTextField(20); + textField.setName("Field"); + textField.setText(reminderText); + return textField; + } + + private JButton createOkButton() { + final JButton button = new JButton("ok"); + button.setName("OK"); + return button; + } + + private void updateReminder() { + okButton.addActionListener(e -> this.dispose()); + okButton.addActionListener(e -> { + final int periodInSeconds = getTimeInSeconds(period); + final int delayInSeconds = getTimeInSeconds(delay); + final Reminder reminder = new Reminder(textField.getText(), delayInSeconds, periodInSeconds); + ((DefaultListModel) reminderApplication.getReminders()).set(reminderIndex, reminder); + }); + okButton.addActionListener(e -> scheduleReminder(textField, delay, period)); + } + + private void scheduleReminder(final JTextField textField, final JComboBox delay, final JComboBox period) { + final int periodInSeconds = getTimeInSeconds(period); + if (periodInSeconds == 0) + scheduleNonRepeatedReminder(textField, delay); + else + scheduleRepeatedReminder(textField, delay, period); + } + + private void scheduleRepeatedReminder(final JTextField textField, final JComboBox delay, final JComboBox period) { + final int delayInSeconds = getTimeInSeconds(delay); + final int periodInSeconds = getTimeInSeconds(period); + final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds); + TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds), TimeUnit.SECONDS.toMillis(periodInSeconds)); + } + + private void scheduleNonRepeatedReminder(final JTextField textField, final JComboBox delay) { + final int delayInSeconds = getTimeInSeconds(delay); + final int periodInSeconds = 0; + final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds); + TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds)); + + } + + private int getTimeInSeconds(final JComboBox comboBox) { + if (comboBox != null && comboBox.getSelectedItem() != null) + return ((Integer) comboBox.getSelectedItem()); + else + return 0; + } + + private TimerTask getTimerTask(final String reminderText, final Integer delayInSeconds, final Integer periodInSeconds) { + return new TimerTask() { + @Override + public void run() { + new ReminderPopupFrame(reminderApplication, reminderText, delayInSeconds, periodInSeconds); + } + }; + } + + private JButton createCancelButton() { + final JButton button = new JButton("cancel"); + button.setName("Cancel"); + button.addActionListener(e -> this.dispose()); + return button; + } + + private static JLabel createLabel(final String text, final String name) { + JLabel label = new JLabel(text); + label.setName(name); + return label; + } +} diff --git a/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/Reminder.java b/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/Reminder.java new file mode 100644 index 0000000000..8f6ff336ed --- /dev/null +++ b/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/Reminder.java @@ -0,0 +1,33 @@ +package reminderapplication; + +public class Reminder { + + private static String REMINDER_FORMAT = "Reminder Text: %s; Delay: %d; Period: %d;"; + + private final String name; + private final int delay; + private final int period; + + public Reminder(final String name, final int delay, final int period) { + this.name = name; + this.delay = delay; + this.period = period; + } + + public String getName() { + return name; + } + + public int getDelay() { + return delay; + } + + public int getPeriod() { + return period; + } + + @Override + public String toString() { + return REMINDER_FORMAT.formatted(name, delay, period); + } +} diff --git a/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/ReminderFrame.java b/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/ReminderFrame.java new file mode 100644 index 0000000000..3a1623219c --- /dev/null +++ b/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/ReminderFrame.java @@ -0,0 +1,186 @@ +package reminderapplication; + +import static reminderapplication.ConstraintsBuilder.*; + +import java.awt.GridBagLayout; +import java.awt.HeadlessException; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.TimeUnit; +import javax.swing.DefaultComboBoxModel; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JTextField; + +public class ReminderFrame extends JFrame { + + private static Timer TIMER = new Timer(); + private final TimeReminderApplication reminderApplication; + private final JLabel reminderTextLabel; + private final JLabel repeatPeriodLabel; + private final JLabel setDelayLabel; + private final JComboBox delay; + private final JComboBox period; + private final JButton cancelButton; + private final JButton okButton; + private final JTextField textField; + private final JLabel delaysLabel; + private final JLabel periodLabel; + + public ReminderFrame(TimeReminderApplication reminderApp) throws HeadlessException { + this.reminderApplication = reminderApp; + textField = createTextField(); + delay = createDelayComboBox(); + period = createPeriodComboBox(); + cancelButton = createCancelButton(); + okButton = createOkButton(); + reminderTextLabel = createReminderTextLabel(); + repeatPeriodLabel = createRepeatPeriodLabel(); + setDelayLabel = createSetDelayLabel(); + delaysLabel = createDelaysLabel(); + periodLabel = createPeriodLabel(); + configureVisualRepresentation(); + configureActions(); + } + + private void configureActions() { + createNewReminder(); + } + + private void configureVisualRepresentation() { + configureFrame(); + setLocationRelativeTo(null); + setLayout(new GridBagLayout()); + add(reminderTextLabel, constraint(0,0)); + add(repeatPeriodLabel, constraint(1,0)); + add(setDelayLabel, constraint(2,0)); + add(textField, constraint(0, 1)); + add(delay, constraint(1, 1)); + add(period, constraint(2, 1)); + add(delaysLabel, constraint(1,3)); + add(periodLabel, constraint(2,3)); + add(okButton, constraint(1, 4)); + add(cancelButton, constraint(2, 4)); + pack(); + setVisible(true); + } + + private void configureFrame() { + setTitle("Set Reminder"); + setName("Set Reminder"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + } + + private static JLabel createSetDelayLabel() { + return createLabel("Set Delay", "Set Delay Label"); + } + + private static JLabel createRepeatPeriodLabel() { + return createLabel("Set Period", "Set Repeat Period Label"); + } + + private static JLabel createReminderTextLabel() { + return createLabel("Reminder Text", "Reminder Text Label"); + } + + private JLabel createPeriodLabel() { + return createLabel("0", "Period label"); + } + + private JLabel createDelaysLabel() { + return createLabel("30", "Delays Label"); + } + + private JComboBox createPeriodComboBox() { + final JComboBox comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{0, 5, 10, 20})); + comboBox.setName("set Period"); + comboBox.addActionListener(e -> periodLabel.setText(comboBox.getSelectedItem().toString())); + return comboBox; + } + + private JComboBox createDelayComboBox() { + final JComboBox comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{30, 25, 15, 5})); + comboBox.setName("set Delay"); + comboBox.addActionListener(e -> delaysLabel.setText(comboBox.getSelectedItem().toString())); + return comboBox; + } + + private JTextField createTextField() { + final JTextField textField = new JTextField(20); + textField.setName("Field"); + return textField; + } + + private JButton createOkButton() { + final JButton button = new JButton("ok"); + button.setName("OK"); + return button; + } + + private void createNewReminder() { + + okButton.addActionListener(e -> this.dispose()); + okButton.addActionListener(e -> { + final int periodInSeconds = getTimeInSeconds(period); + final int delayInSeconds = getTimeInSeconds(delay); + final Reminder reminder = new Reminder(textField.getText(), delayInSeconds, periodInSeconds); + ((DefaultListModel) reminderApplication.getReminders()).addElement(reminder); + }); + okButton.addActionListener(e -> scheduleReminder(textField, delay, period)); + } + + private void scheduleReminder(final JTextField textField, final JComboBox delay, final JComboBox period) { + final int periodInSeconds = getTimeInSeconds(period); + if (periodInSeconds == 0) + scheduleNonRepeatedReminder(textField, delay); + else + scheduleRepeatedReminder(textField, delay, period); + } + + private void scheduleRepeatedReminder(final JTextField textField, final JComboBox delay, final JComboBox period) { + final int delayInSeconds = getTimeInSeconds(delay) + 200; + final int periodInSeconds = getTimeInSeconds(period); + final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds); + TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds), TimeUnit.SECONDS.toMillis(periodInSeconds)); + } + + private void scheduleNonRepeatedReminder(final JTextField textField, final JComboBox delay) { + final int delayInSeconds = getTimeInSeconds(delay); + final int periodInSeconds = 0; + final TimerTask timerTask = getTimerTask(textField.getText(), delayInSeconds, periodInSeconds); + TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(delayInSeconds)); + + } + + private int getTimeInSeconds(final JComboBox comboBox) { + if (comboBox != null && comboBox.getSelectedItem() != null) + return ((Integer) comboBox.getSelectedItem()); + else + return 0; + } + + private TimerTask getTimerTask(final String reminderText, final Integer delayInSeconds, final Integer periodInSeconds) { + return new TimerTask() { + @Override + public void run() { + new ReminderPopupFrame(reminderApplication, reminderText, delayInSeconds, periodInSeconds); + } + }; + } + + private JButton createCancelButton() { + final JButton button = new JButton("cancel"); + button.setName("Cancel"); + button.addActionListener(e -> this.dispose()); + return button; + } + + private static JLabel createLabel(final String text, final String name) { + JLabel label = new JLabel(text); + label.setName(name); + return label; + } +} diff --git a/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/ReminderPopupFrame.java b/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/ReminderPopupFrame.java new file mode 100644 index 0000000000..d41343cb6d --- /dev/null +++ b/core-java-modules/Reminder Application/Reminder Application/task/src/reminderapplication/ReminderPopupFrame.java @@ -0,0 +1,151 @@ +package reminderapplication; + +import static reminderapplication.ConstraintsBuilder.*; + +import java.awt.GridBagLayout; +import java.awt.HeadlessException; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.TimeUnit; +import javax.swing.DefaultComboBoxModel; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JTextField; + +public class ReminderPopupFrame extends JFrame { + + private static final Timer TIMER = new Timer(); + private final int AUTOMATIC_CLOSE_TIME_IN_SECONDS = 10; + private final TimeReminderApplication reminderApplication; + private final JLabel reminderTextLabel; + private final JLabel repeatPeriodLabel; + private final JLabel setDelayLabel; + private final JComboBox delay; + private final JComboBox period; + private final JButton cancelButton; + private final JButton okButton; + private final JTextField textField; + private final JLabel delaysLabel; + private final JLabel periodLabel; + + public ReminderPopupFrame(TimeReminderApplication reminderApp, final String text, final Integer delayInSeconds, final Integer periodInSeconds) throws HeadlessException { + this.reminderApplication = reminderApp; + textField = createTextField(text); + delay = createDelayComboBox(delayInSeconds); + period = createPeriodComboBox(periodInSeconds); + cancelButton = createCancelButton(); + okButton = createDisabledOkButton(); + reminderTextLabel = createReminderTextLabel(); + repeatPeriodLabel = createRepeatPeriodLabel(); + setDelayLabel = createSetDelayLabel(); + delaysLabel = createDelaysLabel(); + periodLabel = createPeriodLabel(); + configureVisualRepresentation(); + configureActions(); + } + + private void configureActions() { + scheduleClosing(); + } + + private void scheduleClosing() { + final TimerTask timerTask = new TimerTask() { + @Override + public void run() { + ReminderPopupFrame.this.dispose(); + } + }; + TIMER.schedule(timerTask, TimeUnit.SECONDS.toMillis(AUTOMATIC_CLOSE_TIME_IN_SECONDS)); + } + + private void configureVisualRepresentation() { + configureFrame(); + setLocationRelativeTo(null); + setLayout(new GridBagLayout()); + add(reminderTextLabel, constraint(0,0)); + add(repeatPeriodLabel, constraint(1,0)); + add(setDelayLabel, constraint(2,0)); + add(textField, constraint(0, 1)); + add(delay, constraint(1, 1)); + add(period, constraint(2, 1)); + add(delaysLabel, constraint(1,3)); + add(periodLabel, constraint(2,3)); + add(okButton, constraint(1, 4)); + add(cancelButton, constraint(2, 4)); + pack(); + setVisible(true); + } + + private void configureFrame() { + setTitle("Set Reminder"); + setName("Set Reminder"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + } + + private static JLabel createSetDelayLabel() { + return createLabel("Set Delay", "Set Delay Label"); + } + + private static JLabel createRepeatPeriodLabel() { + return createLabel("Set Period", "Set Repeat Period Label"); + } + + private static JLabel createReminderTextLabel() { + return createLabel("Reminder Text", "Reminder Text Label"); + } + + private JLabel createPeriodLabel() { + return createLabel("0", "Period label"); + } + + private JLabel createDelaysLabel() { + return createLabel("30", "Delays Label"); + } + + private JComboBox createPeriodComboBox(final Integer periodInSeconds) { + final JComboBox comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{0, 5, 10, 20})); + comboBox.setName("set Period"); + comboBox.setSelectedItem(periodInSeconds); + comboBox.addActionListener(e -> periodLabel.setText(comboBox.getSelectedItem().toString())); + return comboBox; + } + + private JComboBox createDelayComboBox(Integer delay) { + final JComboBox comboBox = new JComboBox<>(new DefaultComboBoxModel<>(new Integer[]{30, 25, 15, 5})); + comboBox.setSelectedItem(delay); + comboBox.setName("set Delay"); + comboBox.addActionListener(e -> delaysLabel.setText(comboBox.getSelectedItem().toString())); + return comboBox; + } + + private JTextField createTextField(final String text) { + final JTextField textField = new JTextField(20); + textField.setName("Field"); + textField.setText(text); + return textField; + } + + private JButton createDisabledOkButton() { + final JButton button = new JButton("ok"); + button.setName("OK"); + button.setEnabled(false); + return button; + } + + private JButton createCancelButton() { + final JButton button = new JButton("cancel"); + button.setName("Cancel"); + button.addActionListener(e -> this.dispose()); + return button; + } + + private static JLabel createLabel(final String text, final String name) { + JLabel label = new JLabel(text); + label.setName(name); + return label; + } + +} diff --git a/core-java-modules/core-java-20/src/main/java/com/baeldung/convertpaths/RelativePathConverter.java b/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/convertpaths/RelativePathConverter.java similarity index 100% rename from core-java-modules/core-java-20/src/main/java/com/baeldung/convertpaths/RelativePathConverter.java rename to core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/convertpaths/RelativePathConverter.java diff --git a/core-java-modules/core-java-20/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java similarity index 100% rename from core-java-modules/core-java-20/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java rename to core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java diff --git a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/trustedcert/CertificatesUnitTest.java b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/trustedcert/CertificatesUnitTest.java index 4f40c3c195..b013ff4ed1 100644 --- a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/trustedcert/CertificatesUnitTest.java +++ b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/trustedcert/CertificatesUnitTest.java @@ -28,6 +28,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Disabled; + +@Disabled //fixing in https://team.baeldung.com/browse/JAVA-23897 public class CertificatesUnitTest { private static final String GODADDY_CA_ALIAS = "godaddyrootg2ca [jdk]"; diff --git a/di-modules/avaje/README.md b/di-modules/avaje/README.md new file mode 100644 index 0000000000..ea914e551f --- /dev/null +++ b/di-modules/avaje/README.md @@ -0,0 +1,7 @@ +# Introduction to Avaje Inject + +This module contains articles about Avaje + +### Relevant articles: + +- [Introduction to Avaje Inject](https://www.baeldung.com/avaje-inject/intro) \ No newline at end of file diff --git a/di-modules/avaje/pom.xml b/di-modules/avaje/pom.xml new file mode 100644 index 0000000000..7ffe14bd72 --- /dev/null +++ b/di-modules/avaje/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + com.baeldung + inject-intro + 0.0.1-SNAPSHOT + avaje-inject-intro + + 11 + 11 + 9.5 + + + + io.avaje + avaje-inject + ${avaje.inject.version} + + + + io.avaje + avaje-inject-test + ${avaje.inject.version} + test + + + + + io.avaje + avaje-inject-generator + ${avaje.inject.version} + provided + true + + + \ No newline at end of file diff --git a/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/ArmsFactory.java b/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/ArmsFactory.java new file mode 100644 index 0000000000..381b390ad0 --- /dev/null +++ b/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/ArmsFactory.java @@ -0,0 +1,18 @@ +package com.baeldung.avaje.intro; + +import io.avaje.inject.Bean; +import io.avaje.inject.Factory; + +@Factory +public class ArmsFactory { + + @Bean + public Sword provideSword() { + return new Sword(); + } + + @Bean + public Shield provideShield() { + return new Shield(25); + } +} diff --git a/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Knight.java b/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Knight.java new file mode 100644 index 0000000000..c78c60b858 --- /dev/null +++ b/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Knight.java @@ -0,0 +1,34 @@ +package com.baeldung.avaje.intro; + +import jakarta.inject.Inject; +import jakarta.inject.Singleton; + +@Singleton +public class Knight { + + private Sword sword; + + private Shield shield; + + @Inject + public Knight(Sword sword, Shield shield) { + this.sword = sword; + this.shield = shield; + } + + public Sword sword() { + return sword; + } + + public void sword(Sword engine) { + this.sword = engine; + } + + public Shield shield() { + return shield; + } + + public void shield(Shield brand) { + this.shield = brand; + } +} diff --git a/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Ninja.java b/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Ninja.java new file mode 100644 index 0000000000..e4d893ffef --- /dev/null +++ b/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Ninja.java @@ -0,0 +1,30 @@ +package com.baeldung.avaje.intro; + +import io.avaje.inject.BeanScope; +import io.avaje.inject.PostConstruct; +import io.avaje.inject.PreDestroy; +import jakarta.inject.Singleton; + +@Singleton +public class Ninja { + + private Sword sword; + + @PostConstruct + void equip(BeanScope scope) { + sword = scope.get(Sword.class); + } + + @PreDestroy + void dequip() { + sword = null; + } + + public Sword sword() { + return sword; + } + + public void sword(Sword engine) { + this.sword = engine; + } +} diff --git a/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Shield.java b/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Shield.java new file mode 100644 index 0000000000..c5d5a3e999 --- /dev/null +++ b/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Shield.java @@ -0,0 +1,18 @@ +package com.baeldung.avaje.intro; + +public class Shield { + + private int defense; + + public Shield(int defense) { + this.defense = defense; + } + + public int defense() { + return defense; + } + + public void defense(int defense) { + this.defense = defense; + } +} diff --git a/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Sword.java b/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Sword.java new file mode 100644 index 0000000000..6eceed082e --- /dev/null +++ b/di-modules/avaje/src/main/java/com/baeldung/avaje/intro/Sword.java @@ -0,0 +1,8 @@ +package com.baeldung.avaje.intro; + +public class Sword { + + public void attack() { + System.out.println("swing"); + } +} diff --git a/di-modules/avaje/src/test/java/com/baeldung/avaje/intro/AvajeUnitTest.java b/di-modules/avaje/src/test/java/com/baeldung/avaje/intro/AvajeUnitTest.java new file mode 100644 index 0000000000..a0b71a228a --- /dev/null +++ b/di-modules/avaje/src/test/java/com/baeldung/avaje/intro/AvajeUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.avaje.intro; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +import io.avaje.inject.BeanScope; + +class AvajeUnitTest { + + @Test + void givenBeanScope_whenScopeGetsKnight_thenKnightShouldHaveDependencies() { + + final var scope = BeanScope.builder().build(); + final var knight = scope.get(Knight.class); + + assertNotNull(knight); + assertNotNull(knight.sword()); + assertNotNull(knight.shield()); + assertEquals(25, knight.shield().defense()); + } +} diff --git a/di-modules/avaje/src/test/java/com/baeldung/avaje/intro/ExampleInjectTest.java b/di-modules/avaje/src/test/java/com/baeldung/avaje/intro/ExampleInjectTest.java new file mode 100644 index 0000000000..6eada4cb6d --- /dev/null +++ b/di-modules/avaje/src/test/java/com/baeldung/avaje/intro/ExampleInjectTest.java @@ -0,0 +1,29 @@ +package com.baeldung.avaje.intro; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +import io.avaje.inject.test.InjectTest; +import jakarta.inject.Inject; + +@InjectTest +class ExampleInjectTest { + + @Mock Shield shield; + + @Inject Knight knight; + + @Test + void givenMockedShield_whenGetShield_thenShieldShouldHaveMockedValue() { + + Mockito.when(shield.defense()).thenReturn(0); + assertNotNull(knight); + assertNotNull(knight.sword()); + assertNotNull(knight.shield()); + assertEquals(0, knight.shield().defense()); + } +} diff --git a/parent-boot-3/pom.xml b/parent-boot-3/pom.xml index dcb5e179bb..dabcd7850b 100644 --- a/parent-boot-3/pom.xml +++ b/parent-boot-3/pom.xml @@ -230,7 +230,7 @@ 3.3.0 2.22.2 - 3.0.0 + 3.1.2 5.8.2 0.9.17 17 diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/QueryService.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/QueryService.java index 4c1e73e530..7e2dbd93e9 100644 --- a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/QueryService.java +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/QueryService.java @@ -27,14 +27,15 @@ public class QueryService { return entityManager.createQuery(query).getResultList(); } - public BookRecord findBookById(Long id) { + public BookRecord findBookByTitle(String title) { TypedQuery query = entityManager - .createQuery("SELECT new com.baeldung.recordswithjpa.records.BookRecord(b.id, b.title, b.author, b.isbn) " + - "FROM Book b WHERE b.id = :id", BookRecord.class); - query.setParameter("id", id); + .createQuery("SELECT new com.baeldung.recordswithjpa.records.BookRecord(b.id, b.title, b.author, b.isbn) " + + "FROM Book b WHERE b.title = :title", BookRecord.class); + query.setParameter("title", title); return query.getSingleResult(); } + public List findAllBooksUsingMapping() { Query query = entityManager.createNativeQuery("SELECT * FROM book", "BookRecordMapping"); return query.getResultList(); diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/embeddable/Author.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/embeddable/Author.java new file mode 100644 index 0000000000..4f2fa664db --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/embeddable/Author.java @@ -0,0 +1,9 @@ +package com.baeldung.recordswithjpa.embeddable; + +import jakarta.persistence.Embeddable; + +@Embeddable +public record Author( + String firstName, + String lastName +) {} diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/embeddable/AuthorInstallator.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/embeddable/AuthorInstallator.java new file mode 100644 index 0000000000..7debfb5071 --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/embeddable/AuthorInstallator.java @@ -0,0 +1,23 @@ +package com.baeldung.recordswithjpa.embeddable; + +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.metamodel.spi.EmbeddableInstantiator; +import org.hibernate.metamodel.spi.ValueAccess; + +public class AuthorInstallator implements EmbeddableInstantiator { + + public boolean isInstance(Object object, SessionFactoryImplementor sessionFactory) { + return object instanceof Author; + } + + public boolean isSameClass(Object object, SessionFactoryImplementor sessionFactory) { + return object.getClass().equals(Author.class); + } + + @Override + public Object instantiate(final ValueAccess valueAccess, final SessionFactoryImplementor sessionFactoryImplementor) { + final String firstName = valueAccess.getValue(0, String.class); + final String secondName = valueAccess.getValue(1, String.class); + return new Author(firstName, secondName); + } +} diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/embeddable/EmbeddableBook.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/embeddable/EmbeddableBook.java new file mode 100644 index 0000000000..74bd7d78ae --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/embeddable/EmbeddableBook.java @@ -0,0 +1,65 @@ +package com.baeldung.recordswithjpa.embeddable; + +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import org.hibernate.annotations.EmbeddableInstantiator; + +@Entity +@Table(name = "embeadable_author_book") +public class EmbeddableBook { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String title; + @Embedded + @EmbeddableInstantiator(AuthorInstallator.class) + private Author author; + private String isbn; + + public EmbeddableBook() { + } + + public EmbeddableBook(Long id, String title, Author author, String isbn) { + this.id = id; + this.title = title; + this.author = author; + this.isbn = isbn; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Author getAuthor() { + return author; + } + + public void setAuthor(Author author) { + this.author = author; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/repository/EmbeddableBookRepository.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/repository/EmbeddableBookRepository.java new file mode 100644 index 0000000000..3d6e558050 --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/recordswithjpa/repository/EmbeddableBookRepository.java @@ -0,0 +1,14 @@ +package com.baeldung.recordswithjpa.repository; + +import com.baeldung.recordswithjpa.embeddable.EmbeddableBook; +import com.baeldung.recordswithjpa.embeddable.Author; +import java.util.List; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; + +public interface EmbeddableBookRepository extends CrudRepository { + @Query("SELECT b FROM EmbeddableBook b WHERE b.author = :author") + List findBookByAuthor(@Param("author") Author author); + +} diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/recordswithjpa/QueryServiceIntegrationTest.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/recordswithjpa/QueryServiceIntegrationTest.java index 011895e7fa..09c6805544 100644 --- a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/recordswithjpa/QueryServiceIntegrationTest.java +++ b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/recordswithjpa/QueryServiceIntegrationTest.java @@ -1,13 +1,8 @@ package com.baeldung.recordswithjpa; -import com.baeldung.recordswithjpa.entity.Book; import com.baeldung.recordswithjpa.records.BookRecord; -import com.baeldung.recordswithjpa.repository.BookRepository; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @@ -27,8 +22,8 @@ public class QueryServiceIntegrationTest extends RecordsAsJpaIntegrationTest { @Test void findBookById() { - BookRecord bookById = queryService.findBookById(1L); - assertEquals("The Lord of the Rings", bookById.title()); + BookRecord bookByTitle = queryService.findBookByTitle("The Lord of the Rings"); + assertNotNull(bookByTitle); } @Test diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/recordswithjpa/RecordsAsJpaEmbeddableIntegrationTest.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/recordswithjpa/RecordsAsJpaEmbeddableIntegrationTest.java new file mode 100644 index 0000000000..2d0b834def --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/recordswithjpa/RecordsAsJpaEmbeddableIntegrationTest.java @@ -0,0 +1,31 @@ +package com.baeldung.recordswithjpa; + +import com.baeldung.recordswithjpa.embeddable.Author; +import com.baeldung.recordswithjpa.embeddable.EmbeddableBook; +import com.baeldung.recordswithjpa.repository.EmbeddableBookRepository; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class RecordsAsJpaEmbeddableIntegrationTest { + @Autowired + protected EmbeddableBookRepository bookRepository; + + @BeforeEach + void setUp() { + + Author author = new Author("J.R.R.", "Tolkien"); + EmbeddableBook book1 = new EmbeddableBook(null, "The Lord of the Rings", author, "978-0544003415"); + EmbeddableBook book2 = new EmbeddableBook(null, "The Hobbit", author, "978-0547928227"); + + bookRepository.save(book1); + bookRepository.save(book2); + } + + @AfterEach + void tearDown() { + bookRepository.deleteAll(); + } +} diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/recordswithjpa/repository/EmbeddableBookRepositoryIntegrationTest.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/recordswithjpa/repository/EmbeddableBookRepositoryIntegrationTest.java new file mode 100644 index 0000000000..8f9e9680ba --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/recordswithjpa/repository/EmbeddableBookRepositoryIntegrationTest.java @@ -0,0 +1,16 @@ +package com.baeldung.recordswithjpa.repository; + +import com.baeldung.recordswithjpa.RecordsAsJpaEmbeddableIntegrationTest; +import com.baeldung.recordswithjpa.embeddable.Author; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class EmbeddableBookRepositoryIntegrationTest extends RecordsAsJpaEmbeddableIntegrationTest { + + @Test + void findBookByAuthor() { + assertEquals(2, bookRepository.findBookByAuthor(new Author("J.R.R.", "Tolkien")).size()); + } + +}