Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/maven-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CI (Maven)

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
checks: write

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '21'
cache: maven
server-id: github
server-username: GITHUB_ACTOR
server-password: GITHUB_TOKEN

- name: Build & test (use wrapper if present)
run: |
if [ -f mvnw ]; then
./mvnw -B -ntp -DskipITs=true verify
else
mvn -B -ntp -DskipITs=true verify
fi

- name: Upload surefire test report (on failure)
if: failure()
uses: actions/upload-artifact@v4
with:
name: surefire-reports
path: |
**/target/surefire-reports/*.txt
**/target/surefire-reports/*.xml
**/target/surefire-reports/*.dumpstream

- name: Publish JUnit results
if: always()
uses: dorny/test-reporter@v1
with:
name: Unit tests
path: "**/target/surefire-reports/*.xml"
reporter: java-junit
13 changes: 13 additions & 0 deletions .maven-settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- For GitHub Packages auth. Safe to have even if you don't use packages. -->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>github</id>
<username>${env.GITHUB_ACTOR}</username>
<password>${env.GITHUB_TOKEN}</password>
</server>
</servers>
</settings>
8 changes: 7 additions & 1 deletion jhotdraw-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@
<artifactId>jhotdraw-actions</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jhotdraw.geom.Bezier;
import org.jhotdraw.geom.BezierPath;
import org.jhotdraw.geom.Geom;
import org.jhotdraw.undo.CompositeEdit;
import org.jhotdraw.util.*;

/**
Expand Down Expand Up @@ -225,7 +226,10 @@ protected void fireUndoEvent(Figure createdFigure, DrawingView creationView) {
final Figure addedFigure = createdFigure;
final Drawing addedDrawing = creationView.getDrawing();
final DrawingView addedView = creationView;
getDrawing().fireUndoableEditHappened(new AbstractUndoableEdit() {

CompositeEdit compositeEdit = new CompositeEdit(presentationName);

UndoableEdit undoableEdit = new AbstractUndoableEdit() {
private static final long serialVersionUID = 1L;

@Override
Expand All @@ -246,7 +250,10 @@ public void redo() throws CannotRedoException {
addedDrawing.add(addedFigure);
addedView.addToSelection(addedFigure);
}
});
};
compositeEdit.addEdit(undoableEdit);
compositeEdit.end();
getDrawing().fireUndoableEditHappened(compositeEdit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.swing.undo.*;
import org.jhotdraw.draw.*;
import org.jhotdraw.draw.connector.Connector;
import org.jhotdraw.undo.CompositeEdit;
import org.jhotdraw.util.*;

/**
Expand Down Expand Up @@ -283,7 +284,9 @@ public void mouseReleased(MouseEvent e) {
createdFigure.changed();
final Figure addedFigure = createdFigure;
final Drawing addedDrawing = getDrawing();
getDrawing().fireUndoableEditHappened(new AbstractUndoableEdit() {

CompositeEdit compositeEdit = new CompositeEdit(presentationName);
UndoableEdit undoableEdit = new AbstractUndoableEdit() {
private static final long serialVersionUID = 1L;

@Override
Expand All @@ -302,7 +305,11 @@ public void redo() throws CannotRedoException {
super.redo();
addedDrawing.add(addedFigure);
}
});
};
compositeEdit.addEdit(undoableEdit);
compositeEdit.end();
getDrawing().fireUndoableEditHappened(compositeEdit);

targetFigure = null;
Point2D.Double anchor = startConnector.getAnchor();
Rectangle r = new Rectangle(getView().drawingToView(anchor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.*;
import javax.swing.undo.*;
import org.jhotdraw.draw.*;
import org.jhotdraw.undo.CompositeEdit;
import org.jhotdraw.util.*;

/**
Expand Down Expand Up @@ -60,7 +61,7 @@ public class CreationTool extends AbstractTool {
* Attributes to be applied to the created ConnectionFigure. These attributes override the
* default attributes of the DrawingEditor.
*/
protected Map<AttributeKey<?>, Object> prototypeAttributes;
protected transient Map<AttributeKey<?>, Object> prototypeAttributes;
/**
* A localized name for this tool. The presentationName is displayed by the UndoableEdit.
*/
Expand Down Expand Up @@ -239,7 +240,9 @@ public void mouseReleased(MouseEvent evt) {
}
final Figure addedFigure = createdFigure;
final Drawing addedDrawing = getDrawing();
getDrawing().fireUndoableEditHappened(new AbstractUndoableEdit() {
CompositeEdit compositeEdit = new CompositeEdit(presentationName);

UndoableEdit edit = new AbstractUndoableEdit() {
private static final long serialVersionUID = 1L;

@Override
Expand All @@ -258,7 +261,12 @@ public void redo() throws CannotRedoException {
super.redo();
addedDrawing.add(addedFigure);
}
});
};

compositeEdit.addEdit(edit);
compositeEdit.end();
getDrawing().fireUndoableEditHappened(compositeEdit);

Rectangle r = new Rectangle(anchor.x, anchor.y, 0, 0);
r.add(evt.getX(), evt.getY());
maybeFireBoundsInvalidated(r);
Expand Down
117 changes: 117 additions & 0 deletions jhotdraw-core/src/test/java/org/jhotdraw/draw/tool/BezierToolTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.jhotdraw.draw.tool;

import org.jhotdraw.undo.UndoRedoManager;
import org.junit.*;
import javax.swing.undo.*;
import java.util.Locale;
import static org.junit.Assert.*;

public class BezierToolTest {

private UndoRedoManager undoManager;

@BeforeClass
public static void setUpClass() {
Locale.setDefault(Locale.ENGLISH);
}

@Before
public void setUp() {
undoManager = new UndoRedoManager();
}

@Test
public void testEditAddedToHistory() {
MockEdit edit = new MockEdit("Create Bezier Figure");
undoManager.addEdit(edit);

assertTrue(undoManager.canUndo());
assertEquals("Create Bezier Figure", edit.getPresentationName());
}

@Test
public void testMultipleCreationsPreserveHistory() {
MockEdit edit1 = new MockEdit("Create Bezier 1");
undoManager.addEdit(edit1);

MockEdit edit2 = new MockEdit("Create Bezier 2");
undoManager.addEdit(edit2);

assertTrue(undoManager.canUndo());

undoManager.undo();
assertTrue(edit2.wasUndone());

assertTrue(undoManager.canUndo());
undoManager.undo();
assertTrue(edit1.wasUndone());
}

@Test
public void testChronologicalUndoOrder() {
MockEdit[] edits = new MockEdit[3];
for (int i = 0; i < 3; i++) {
edits[i] = new MockEdit("Bezier Edit " + (i + 1));
undoManager.addEdit(edits[i]);
}

for (int i = 2; i >= 0; i--) {
assertTrue(undoManager.canUndo());
undoManager.undo();
assertTrue(edits[i].wasUndone());
}

assertFalse(undoManager.canUndo());
}

@Test
public void testRedoAfterUndo() {
MockEdit edit = new MockEdit("Bezier Undo Test");
undoManager.addEdit(edit);

undoManager.undo();
assertTrue(edit.wasUndone());
assertTrue(undoManager.canRedo());

undoManager.redo();
assertTrue(edit.wasRedone());
}

private static class MockEdit extends AbstractUndoableEdit {
private static final long serialVersionUID = 1L;
private final String name;
private boolean undone = false;
private boolean redone = false;

public MockEdit(String name) {
this.name = name;
}

@Override
public String getPresentationName() {
return name;
}

@Override
public void undo() throws CannotUndoException {
super.undo();
undone = true;
redone = false;
}

@Override
public void redo() throws CannotRedoException {
super.redo();
redone = true;
undone = false;
}

public boolean wasUndone() {
return undone;
}

public boolean wasRedone() {
return redone;
}
}
}
Loading