Skip to content

Commit

Permalink
ref torakiki#4: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
torakiki committed Dec 9, 2014
1 parent 9914d50 commit 22ddd4a
Show file tree
Hide file tree
Showing 20 changed files with 307 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/
package org.pdfsam.alternatemix;

import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.defaultString;

import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -53,8 +54,11 @@ class AlternateMixOptionsPane extends VBox implements TaskParametersBuildStep<Al

AlternateMixOptionsPane() {
super(Style.DEFAULT_SPACING);
this.reverseFirst.setId("reverseFirst");
this.reverseSecond.setId("reverseSecond");
this.reverseSecond.setSelected(true);
this.firstStep.setId("alternateMixFirstStep");
this.secondStep.setId("alternateMixSecondStep");
initialState();
getStyleClass().addAll(Style.CONTAINER.css());
HBox firstStepContainer = new HBox(new Label(DefaultI18nContext.getInstance().i18n(
Expand Down Expand Up @@ -94,16 +98,15 @@ private static ValidableTextField createValidableField() {
public void saveStateTo(Map<String, String> data) {
data.put("reverseFirst", Boolean.toString(reverseFirst.isSelected()));
data.put("reverseSecond", Boolean.toString(reverseSecond.isSelected()));
data.put("firstStep", defaultIfBlank(firstStep.getText(), null));
data.put("secondStep", defaultIfBlank(secondStep.getText(), null));
data.put("firstStep", defaultString(firstStep.getText()));
data.put("secondStep", defaultString(secondStep.getText()));
}

public void restoreStateFrom(Map<String, String> data) {
initialState();
Optional.ofNullable(data.get("reverseFirst")).map(Boolean::valueOf).ifPresent(reverseFirst::setSelected);
Optional.ofNullable(data.get("reverseSecond")).map(Boolean::valueOf).ifPresent(reverseSecond::setSelected);
Optional.ofNullable(data.get("firstStep")).ifPresent(firstStep::setText);
Optional.ofNullable(data.get("secondStep")).ifPresent(secondStep::setText);
reverseFirst.setSelected(Boolean.valueOf(data.get("reverseFirst")));
reverseSecond.setSelected(Boolean.valueOf(data.get("reverseSecond")));
firstStep.setText(Optional.ofNullable(data.get("firstStep")).orElse(EMPTY));
secondStep.setText(Optional.ofNullable(data.get("secondStep")).orElse(EMPTY));
}

private void initialState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
*/
package org.pdfsam.alternatemix;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import javafx.scene.Parent;
import javafx.scene.control.CheckBox;

import org.junit.Before;
import org.junit.ClassRule;
Expand All @@ -35,6 +41,7 @@
import org.loadui.testfx.categories.TestFX;
import org.loadui.testfx.utils.FXTestUtils;
import org.pdfsam.test.ClearEventStudioRule;
import org.pdfsam.ui.commons.ValidableTextField;

/**
* @author Andrea Vacondio
Expand Down Expand Up @@ -85,4 +92,36 @@ public void validSteps() throws Exception {
verify(builder).stepSecond(2);
verify(onError, never()).accept(anyString());
}

@Test
public void onSaveWorkspace() {
doubleClick("#alternateMixFirstStep").type('3');
doubleClick("#alternateMixSecondStep").type('2');
Map<String, String> data = new HashMap<>();
AlternateMixOptionsPane victim = find(".pdfsam-container");
victim.saveStateTo(data);
assertEquals("3", data.get("firstStep"));
assertEquals("2", data.get("secondStep"));
assertEquals(Boolean.TRUE.toString(), data.get("reverseSecond"));
assertEquals(Boolean.FALSE.toString(), data.get("reverseFirst"));
}

@Test
public void restoreStateFrom() throws Exception {
CheckBox reverseFirst = find("#reverseFirst");
CheckBox reverseSecond = find("#reverseSecond");
ValidableTextField firstStep = find("#alternateMixFirstStep");
ValidableTextField secondStep = find("#alternateMixSecondStep");
Map<String, String> data = new HashMap<>();
data.put("firstStep", "4");
data.put("secondStep", "3");
data.put("reverseFirst", Boolean.TRUE.toString());
data.put("reverseSecond", Boolean.FALSE.toString());
AlternateMixOptionsPane victim = find(".pdfsam-container");
FXTestUtils.invokeAndWait(() -> victim.restoreStateFrom(data), 2);
assertEquals("4", firstStep.getText());
assertEquals("3", secondStep.getText());
assertTrue(reverseFirst.isSelected());
assertFalse(reverseSecond.isSelected());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.pdfsam.ui.workspace;

import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.pdfsam.support.RequireUtils.requireNotNull;

import java.io.File;
Expand Down Expand Up @@ -57,7 +57,7 @@ public Map<String, Map<String, String>> getData() {
*/
public void addValue(String module, String key, String value) {
requireNotNull(key, "Destination file cannot be null");
getDataForModule(module).put(key, defaultIfBlank(value, null));
getDataForModule(module).put(key, defaultString(value));
}

/**
Expand Down
5 changes: 3 additions & 2 deletions pdfsam-fx/src/main/java/org/pdfsam/ui/io/BrowsableField.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.pdfsam.ui.io;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.defaultString;

import java.io.File;
Expand Down Expand Up @@ -82,11 +83,11 @@ public ValidableTextField getTextField() {
}

public void saveStateTo(Map<String, String> data) {
data.put(defaultString(getId()) + "browsableField", textField.getText());
data.put(defaultString(getId()) + "browsableField", defaultString(textField.getText()));
}

public void restoreStateFrom(Map<String, String> data) {
Optional.ofNullable(data.get(defaultString(getId()) + "browsableField")).ifPresent(textField::setText);
textField.setText(Optional.ofNullable(data.get(defaultString(getId()) + "browsableField")).orElse(EMPTY));
}

public final void setGraphic(Node value) {
Expand Down
5 changes: 3 additions & 2 deletions pdfsam-fx/src/main/java/org/pdfsam/ui/prefix/PrefixPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.pdfsam.ui.prefix;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.defaultString;

import java.util.Map;
Expand Down Expand Up @@ -68,10 +69,10 @@ public void apply(MultipleOutputTaskParametersBuilder<?> builder, Consumer<Strin
}

public void saveStateTo(Map<String, String> data) {
data.put(defaultString(getId()) + "prefix", field.getText());
data.put(defaultString(getId()) + "prefix", defaultString(field.getText()));
}

public void restoreStateFrom(Map<String, String> data) {
Optional.ofNullable(data.get(defaultString(getId()) + "prefix")).ifPresent(field::setText);
field.setText(Optional.ofNullable(data.get(defaultString(getId()) + "prefix")).orElse(EMPTY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -108,4 +110,26 @@ public void validSpecialCharsFolderExisting() throws IOException {
assertEquals(ValidationState.VALID, victim.getTextField().getValidationState());
assertEquals(inputFile.getAbsolutePath(), victim.getTextField().getText());
}

@Test
public void saveState() throws IOException {
BrowsableFileField victim = new BrowsableFileField(FileType.PDF, OpenType.SAVE);
victim.setId("fieldId");
victim.enforceValidation(true, true);
File inputFile = folder.newFile("test.pdf");
victim.setTextFromFile(inputFile);
Map<String, String> data = new HashMap<>();
victim.saveStateTo(data);
assertEquals(inputFile.getAbsolutePath(), data.get("fieldIdbrowsableField"));
}

@Test
public void restoreState() {
BrowsableFileField victim = new BrowsableFileField(FileType.PDF, OpenType.SAVE);
victim.setId("fieldId");
Map<String, String> data = new HashMap<>();
data.put("fieldIdbrowsableField", "/some/file/test.pdf");
victim.restoreStateFrom(data);
assertEquals("/some/file/test.pdf", victim.getTextField().getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
package org.pdfsam.ui.io;

import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
Expand All @@ -26,6 +30,8 @@

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Rule;
Expand All @@ -38,6 +44,7 @@
import org.pdfsam.test.ClearEventStudioRule;
import org.pdfsam.test.InitializeAndApplyJavaFxThreadRule;
import org.pdfsam.ui.commons.SetDestinationRequest;
import org.sejda.model.pdf.PdfVersion;
import org.springframework.test.annotation.DirtiesContext;

/**
Expand Down Expand Up @@ -102,4 +109,24 @@ public void dontSetFallbackDestinationIfNoSmartOutput() throws IOException {
victim.setDestination(event);
verify(destination, never()).setTextFromFile(any());
}

@Test
public void saveState() {
victim.overwrite().setSelected(true);
Map<String, String> data = new HashMap<>();
victim.saveStateTo(data);
assertEquals(Boolean.TRUE.toString(), data.get("overwrite"));
assertEquals(Boolean.FALSE.toString(), data.get("compress"));
assertTrue(isNotEmpty(data.get("version")));
}

@Test
public void restoreState() {
Map<String, String> data = new HashMap<>();
data.put("overwrite", Boolean.FALSE.toString());
data.put("compress", Boolean.TRUE.toString());
data.put("version", PdfVersion.VERSION_1_4.toString());
victim.restoreStateFrom(data);
assertFalse(victim.overwrite().isSelected());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
public class PrefixFieldTest extends GuiTest {

@Rule
public ClearEventStudioRule clearEventStudio = new ClearEventStudioRule("LogStage");
public ClearEventStudioRule clearEventStudio = new ClearEventStudioRule();

@Override
protected Parent getRootNode() {
Expand Down
94 changes: 94 additions & 0 deletions pdfsam-fx/src/test/java/org/pdfsam/ui/prefix/PrefixPaneTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* This file is part of the PDF Split And Merge source code
* Created on 09/dic/2014
* Copyright 2013-2014 by Andrea Vacondio ([email protected]).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.pdfsam.ui.prefix;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import javafx.scene.Parent;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.loadui.testfx.GuiTest;
import org.loadui.testfx.categories.TestFX;
import org.loadui.testfx.utils.FXTestUtils;
import org.pdfsam.support.params.MultipleOutputTaskParametersBuilder;
import org.pdfsam.test.ClearEventStudioRule;

/**
* @author Andrea Vacondio
*
*/
@Category(TestFX.class)
@SuppressWarnings({ "rawtypes", "unchecked" })
public class PrefixPaneTest extends GuiTest {

@Rule
public ClearEventStudioRule clearEventStudio = new ClearEventStudioRule();

private MultipleOutputTaskParametersBuilder builder;
private Consumer<String> onError;

@Before
public void setUp() {
builder = mock(MultipleOutputTaskParametersBuilder.class);
onError = mock(Consumer.class);
}

@Override
protected Parent getRootNode() {
PrefixPane victim = new PrefixPane();
victim.setId("victim");
return victim;
}

@Test
public void apply() throws Exception {
PrefixPane victim = find(".pdfsam-container");
FXTestUtils.invokeAndWait(() -> victim.apply(builder, onError), 2);
verify(onError, never()).accept(anyString());
verify(builder).prefix("PDFsam_");
}

@Test
public void saveState() {
PrefixPane victim = find(".pdfsam-container");
Map<String, String> data = new HashMap<>();
victim.saveStateTo(data);
assertEquals("PDFsam_", data.get("victimprefix"));
}

@Test
public void restoreState() throws Exception {
PrefixPane victim = find(".pdfsam-container");
Map<String, String> data = new HashMap<>();
data.put("victimprefix", "Chuck");
FXTestUtils.invokeAndWait(() -> victim.restoreStateFrom(data), 2);
assertEquals("Chuck", victim.getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.pdfsam.merge;

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.pdfsam.support.KeyStringValueItem.keyEmptyValue;
import static org.pdfsam.support.KeyStringValueItem.keyValue;

Expand Down Expand Up @@ -70,6 +71,8 @@ class MergeOptionsPane extends VBox implements TaskParametersBuildStep<MergePara
keyValue(OutlinePolicy.ONE_ENTRY_EACH_DOC,
DefaultI18nContext.getInstance().i18n("Create one entry for each merged document")));
outline.getSelectionModel().selectFirst();
outline.setId("outlineCombo");

HBox bookmarksPolicy = new HBox(new Label(DefaultI18nContext.getInstance().i18n("Bookmarks handling:")),
outline);
bookmarksPolicy.getStyleClass().addAll(Style.VITEM.css());
Expand All @@ -86,9 +89,9 @@ public void apply(MergeParametersBuilder builder, Consumer<String> onError) {
}

public void saveStateTo(Map<String, String> data) {
if (!outline.getSelectionModel().isEmpty()) {
data.put("outline", outline.getSelectionModel().getSelectedItem().getKey().toString());
}
data.put("outline",
Optional.ofNullable(outline.getSelectionModel().getSelectedItem()).map(i -> i.getKey().toString())
.orElse(EMPTY));
data.put("containsForms", Boolean.toString(containsForms.isSelected()));
data.put("blankIfOdd", Boolean.toString(blankIfOdd.isSelected()));
}
Expand Down
Loading

0 comments on commit 22ddd4a

Please sign in to comment.