Skip to content

Commit

Permalink
Added implementation and (mediocre) tests for editing an element
Browse files Browse the repository at this point in the history
  • Loading branch information
Vogel612 committed Jun 24, 2015
1 parent f16748a commit fe2349f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ repositories {
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group: 'org.jdom', name: 'jdom2', version: '2.0.6'
// xPath dependency
compile group: 'jaxen', name: 'jaxen', version: '1.1.6'
testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile 'org.mockito:mockito-all:2.0.2-beta'

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/de/vogel612/helper/ui/OverviewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public interface OverviewModel {
void loadFromDirectory(Path resxFolder, String targetLocale);

List<Translation> getTranslations();

void updateTranslation(String key, String newTranslation);

void saveTranslation();
}
26 changes: 26 additions & 0 deletions src/main/java/de/vogel612/helper/ui/impl/OverviewModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;

import de.vogel612.helper.data.Translation;
import de.vogel612.helper.ui.OverviewModel;
Expand Down Expand Up @@ -61,6 +64,7 @@ public class OverviewModelImpl implements OverviewModel {
this.presenter.onException(e, "Something went really wrong");
}
};
private final XPathFactory xPathFactory = XPathFactory.instance();

private OverviewPresenter presenter;
private Document translationDocument;
Expand Down Expand Up @@ -142,4 +146,26 @@ public List<Translation> getTranslations() {
}).collect(Collectors.toList());
}

@Override
public void updateTranslation(final String key, final String newTranslation) {
// XPathExpression<Element> expression =
// xPathFactory.compile("/parent::"
// + ELEMENT_NAME + "/@" + KEY_NAME + "='" + key + "'",
// Filters.element());

XPathExpression<Element> expression = xPathFactory.compile("/*/"
+ ELEMENT_NAME + "[@" + KEY_NAME + "='" + key + "']/"
+ VALUE_NAME, Filters.element());
// ohh damn that's so many assumptions
Element translationToUpdate = expression.evaluate(translationDocument)
.get(0);
translationToUpdate.setText(newTranslation);
}

@Override
public void saveTranslation() {
// TODO Auto-generated method stub

}

}
20 changes: 19 additions & 1 deletion src/test/java/de/vogel612/helper/ui/OverviewModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

Expand All @@ -29,6 +30,10 @@ public class OverviewModelTest {
new Translation("TestKey1", "TestValue", "Second Test"),
new Translation("TestKey2", "Another Test Value", "Another Test Value")
};
private static final Translation[] expectedAfterEdit = {
new Translation("TestKey1", "TestValue", "New Translation"),
new Translation("TestKey2", "Another Test Value", "Another Test Value")
};

private OverviewModelImpl cut;
private OverviewPresenter p;
Expand Down Expand Up @@ -76,7 +81,6 @@ public void loadFromFile_andSuccessiveGet_returnCorrectInformation() {
assertArrayEquals(expected, translations);
}

// FIXME: Test normalization!
@Test
public void loadFromFile_normalizationWorksAsExpected() {
final CountDownLatch latch = new CountDownLatch(1);
Expand Down Expand Up @@ -114,4 +118,18 @@ public void loadFromFile_normalizationWorksAsExpected() {

assertArrayEquals(expected2, translations);
}

@Test
public void editTranslation_updatesDocument() {
// abusing the loading test as a setup...
loadFromFile_andSuccessiveGet_returnCorrectInformation();
reset(p); // just to be sure

cut.updateTranslation("TestKey1", "New Translation");

Translation[] translations;
translations = cut.getTranslations().toArray(new Translation[0]);

assertArrayEquals(expectedAfterEdit, translations);
}
}

0 comments on commit fe2349f

Please sign in to comment.