From 33c0f44a7320b27763d9133dc85a2aa766437c1d Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 14 Jan 2016 10:13:12 +0100 Subject: [PATCH] migration test added --- .../android/calculator/history/History.java | 33 ++++++---- .../calculator/history/HistoryState.java | 19 ++++-- .../history/OldBaseHistoryState.java | 65 ------------------- .../history/OldDisplayHistoryState.java | 51 --------------- .../history/OldEditorHistoryState.java | 37 ----------- .../calculator/history/OldHistory.java | 35 +--------- .../calculator/history/OldHistoryState.java | 41 ++++++------ .../calculator/history/HistoryTest.java | 39 ++++++++++- 8 files changed, 91 insertions(+), 229 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/history/OldBaseHistoryState.java diff --git a/app/src/main/java/org/solovyev/android/calculator/history/History.java b/app/src/main/java/org/solovyev/android/calculator/history/History.java index 34771bea..f75bf5f4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/History.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/History.java @@ -26,20 +26,24 @@ import android.app.Application; import android.content.SharedPreferences; import android.os.Handler; import android.support.annotation.NonNull; + import com.google.common.base.Strings; import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; + import org.json.JSONArray; import org.json.JSONException; import org.solovyev.android.Check; -import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.AppModule; +import org.solovyev.android.calculator.Display; +import org.solovyev.android.calculator.DisplayState; +import org.solovyev.android.calculator.Editor; +import org.solovyev.android.calculator.EditorState; +import org.solovyev.android.calculator.Locator; import org.solovyev.android.io.FileLoader; import org.solovyev.android.io.FileSaver; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import javax.inject.Inject; -import javax.inject.Named; import java.io.File; import java.util.ArrayList; import java.util.Collections; @@ -47,6 +51,11 @@ import java.util.LinkedList; import java.util.List; import java.util.concurrent.Executor; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.inject.Inject; +import javax.inject.Named; + import static android.text.TextUtils.isEmpty; public class History { @@ -95,14 +104,12 @@ public class History { } final List states = new ArrayList<>(); for (OldHistoryState state : history.getItems()) { - final OldEditorHistoryState oldEditorState = state.getEditorState(); - final OldDisplayHistoryState oldDisplayState = state.getDisplayState(); - final String editorText = oldEditorState.getText(); - final EditorState editor = EditorState.create(Strings.nullToEmpty(editorText), oldEditorState.getCursorPosition()); - final DisplayState display = oldDisplayState.isValid() - ? DisplayState.createValid(oldDisplayState.getJsclOperation(), null, Strings.nullToEmpty(oldDisplayState.getEditorState().getText()), EditorState.NO_SEQUENCE) - : DisplayState.createError(oldDisplayState.getJsclOperation(), "", EditorState.NO_SEQUENCE); - states.add(HistoryState.newBuilder(editor, display).build()); + final OldEditorHistoryState oldEditor = state.getEditorState(); + final OldDisplayHistoryState oldDisplay = state.getDisplayState(); + final String editorText = oldEditor.getText(); + final EditorState editor = EditorState.create(Strings.nullToEmpty(editorText), oldEditor.getCursorPosition()); + final DisplayState display = DisplayState.createValid(oldDisplay.getJsclOperation(), null, Strings.nullToEmpty(oldDisplay.getEditorState().getText()), EditorState.NO_SEQUENCE); + states.add(HistoryState.newBuilder(editor, display).withTime(state.getTime()).withComment(state.getComment()).build()); } return states; } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java index 9a48d7d4..771921ec 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryState.java @@ -1,7 +1,5 @@ package org.solovyev.android.calculator.history; -import android.text.TextUtils; - import org.json.JSONException; import org.json.JSONObject; import org.solovyev.android.Check; @@ -9,6 +7,9 @@ import org.solovyev.android.calculator.DisplayState; import org.solovyev.android.calculator.EditorState; import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import static android.text.TextUtils.isEmpty; public class HistoryState { @@ -52,7 +53,7 @@ public class HistoryState { json.put(JSON_EDITOR, editor.toJson()); json.put(JSON_DISPLAY, display.toJson()); json.put(JSON_TIME, time); - if (!TextUtils.isEmpty(comment)) { + if (!isEmpty(comment)) { json.put(JSON_COMMENT, comment); } return json; @@ -97,17 +98,21 @@ public class HistoryState { private Builder(@Nonnull EditorState editor, @Nonnull DisplayState display) { super(editor, display); - setTime(System.currentTimeMillis()); + withTime(System.currentTimeMillis()); } - public void setTime(long time) { + @Nonnull + public Builder withTime(long time) { Check.isTrue(!built); this.time = time; + return this; } - public void setComment(@Nonnull String comment) { + @Nonnull + public Builder withComment(@Nullable String comment) { Check.isTrue(!built); - this.comment = comment; + this.comment = comment == null ? "" : comment; + return this; } @Nonnull diff --git a/app/src/main/java/org/solovyev/android/calculator/history/OldBaseHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/OldBaseHistoryState.java deleted file mode 100644 index 4710a5f4..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/history/OldBaseHistoryState.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator.history; - -import org.simpleframework.xml.Element; - -import javax.annotation.Nullable; -import java.util.Date; - -class OldBaseHistoryState implements Cloneable { - - @Element - private long time = new Date().getTime(); - - @Element(required = false) - @Nullable - private String comment; - - public long getTime() { - return time; - } - - public void setTime(long time) { - this.time = time; - } - - @Nullable - public String getComment() { - return comment; - } - - public void setComment(@Nullable String comment) { - this.comment = comment; - } - - @SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException") - @Override - protected OldBaseHistoryState clone() { - try { - return (OldBaseHistoryState) super.clone(); - } catch (CloneNotSupportedException e) { - throw new UnsupportedOperationException(e); - } - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/history/OldDisplayHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/OldDisplayHistoryState.java index d70db64b..7e573115 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/OldDisplayHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/OldDisplayHistoryState.java @@ -24,7 +24,6 @@ package org.solovyev.android.calculator.history; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; -import org.simpleframework.xml.Transient; import org.solovyev.android.calculator.jscl.JsclOperation; import javax.annotation.Nonnull; @@ -32,25 +31,16 @@ import javax.annotation.Nonnull; @Root(name = "DisplayHistoryState") class OldDisplayHistoryState implements Cloneable { - @Transient - private boolean valid = true; - @Element - @Nonnull private OldEditorHistoryState editorState; @Element - @Nonnull private JsclOperation jsclOperation; private OldDisplayHistoryState() { // for xml } - public boolean isValid() { - return valid; - } - @Nonnull public OldEditorHistoryState getEditorState() { return editorState; @@ -60,45 +50,4 @@ class OldDisplayHistoryState implements Cloneable { public JsclOperation getJsclOperation() { return jsclOperation; } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - OldDisplayHistoryState that = (OldDisplayHistoryState) o; - - if (!editorState.equals(that.editorState)) return false; - if (jsclOperation != that.jsclOperation) return false; - - return true; - } - - @Override - public int hashCode() { - int result = editorState.hashCode(); - result = 31 * result + jsclOperation.hashCode(); - return result; - } - - @Override - public String toString() { - return "CalculatorDisplayHistoryState{" + - "editorHistoryState=" + editorState + - ", jsclOperation=" + jsclOperation + - '}'; - } - - @Override - protected OldDisplayHistoryState clone() { - try { - final OldDisplayHistoryState clone = (OldDisplayHistoryState) super.clone(); - - clone.editorState = this.editorState.clone(); - - return clone; - } catch (CloneNotSupportedException e) { - throw new RuntimeException(e); - } - } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/OldEditorHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/OldEditorHistoryState.java index 14bf4713..d09f3460 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/OldEditorHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/OldEditorHistoryState.java @@ -61,41 +61,4 @@ class OldEditorHistoryState implements Cloneable { public int getCursorPosition() { return cursorPosition; } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof OldEditorHistoryState)) return false; - - OldEditorHistoryState that = (OldEditorHistoryState) o; - - if (cursorPosition != that.cursorPosition) return false; - if (text != null ? !text.equals(that.text) : that.text != null) return false; - - return true; - } - - @Override - public int hashCode() { - int result = cursorPosition; - result = 31 * result + (text != null ? text.hashCode() : 0); - return result; - } - - @Override - public String toString() { - return "EditorHistoryState{" + - "cursorPosition=" + cursorPosition + - ", text='" + text + '\'' + - '}'; - } - - @Override - protected OldEditorHistoryState clone() { - try { - return (OldEditorHistoryState) super.clone(); - } catch (CloneNotSupportedException e) { - throw new UnsupportedOperationException(e); - } - } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/OldHistory.java b/app/src/main/java/org/solovyev/android/calculator/history/OldHistory.java index 3eac7988..d5c1ad5e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/OldHistory.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/OldHistory.java @@ -27,12 +27,11 @@ import org.simpleframework.xml.Root; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; +import java.util.ArrayList; +import java.util.List; + import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; @Root(name = "History") class OldHistory { @@ -57,36 +56,8 @@ class OldHistory { } } - @Nonnull - public String toXml() { - final StringWriter xml = new StringWriter(); - final Serializer serializer = new Persister(); - try { - serializer.write(this, xml); - } catch (Exception e) { - return ""; - } - return xml.toString(); - } - @Nonnull public List getItems() { return items; } - - public void add(@Nonnull OldHistoryState state) { - items.add(state); - } - - public void addAll(@Nonnull Collection states) { - items.addAll(states); - } - - public void clear() { - items.clear(); - } - - public void remove(@Nonnull OldHistoryState state) { - items.remove(state); - } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java b/app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java index 5317bcf1..0350734d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/OldHistoryState.java @@ -25,23 +25,40 @@ package org.solovyev.android.calculator.history; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; +import java.util.Date; + import javax.annotation.Nonnull; +import javax.annotation.Nullable; @Root(name = "HistoryState") -class OldHistoryState extends OldBaseHistoryState { +class OldHistoryState { @Element - @Nonnull private OldEditorHistoryState editorState; @Element - @Nonnull private OldDisplayHistoryState displayState; + @Element + private long time = new Date().getTime(); + + @Element(required = false) + @Nullable + private String comment; + private OldHistoryState() { // for xml } + public long getTime() { + return time; + } + + @Nullable + public String getComment() { + return comment; + } + @Nonnull public OldEditorHistoryState getEditorState() { return editorState; @@ -51,22 +68,4 @@ class OldHistoryState extends OldBaseHistoryState { public OldDisplayHistoryState getDisplayState() { return displayState; } - - @Override - public String toString() { - return "HistoryState{" + - "editorState=" + editorState + - ", displayState=" + displayState + - '}'; - } - - @Override - protected OldHistoryState clone() { - final OldHistoryState that = (OldHistoryState) super.clone(); - - that.editorState = this.editorState.clone(); - that.displayState = this.displayState.clone(); - - return that; - } } diff --git a/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java b/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java index ff962636..a7d31fff 100644 --- a/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java +++ b/app/src/test/java/org/solovyev/android/calculator/history/HistoryTest.java @@ -24,7 +24,9 @@ package org.solovyev.android.calculator.history; import android.content.SharedPreferences; import android.os.Handler; + import com.squareup.otto.Bus; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -38,12 +40,14 @@ import org.solovyev.android.calculator.DisplayState; import org.solovyev.android.calculator.Editor; import org.solovyev.android.calculator.EditorState; -import javax.annotation.Nonnull; import java.util.List; import java.util.concurrent.Executor; +import javax.annotation.Nonnull; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; @Config(constants = BuildConfig.class, sdk = CalculatorTestRunner.SUPPORTED_SDK) @RunWith(RobolectricGradleTestRunner.class) @@ -124,9 +128,10 @@ public class HistoryTest { " \n" + " \n" + " \n" + + " boom\n" + " \n" + " 3\n" + - " 1+1\n" + + " 1+11\n" + " \n" + " \n" + " \n" + @@ -165,7 +170,7 @@ public class HistoryTest { " \n" + " \n" + " \n" + - " \n" + + " \n" + " \n" + " 0\n" + " 4+5/35sin(41)+dfdsfsdfs\n" + @@ -189,5 +194,33 @@ public class HistoryTest { HistoryState state = states.get(0); assertEquals(100000000, state.time); + assertEquals("", state.comment); + assertEquals("1+1", state.editor.getTextString()); + assertEquals(3, state.editor.selection); + assertEquals("Error", state.display.text); + assertEquals(true, state.display.valid); + assertNull(state.display.getResult()); + + states = History.convertOldHistory(oldXml2); + assertNotNull(states); + assertEquals(4, states.size()); + + state = states.get(0); + assertEquals(100000000, state.time); + assertEquals("boom", state.comment); + assertEquals("1+11", state.editor.getTextString()); + assertEquals(3, state.editor.selection); + assertEquals("Error", state.display.text); + assertEquals(true, state.display.valid); + assertNull(state.display.getResult()); + + state = states.get(3); + assertEquals(1, state.time); + assertEquals("", state.comment); + assertEquals("4+5/35sin(41)+dfdsfsdfs", state.editor.getTextString()); + assertEquals(0, state.editor.selection); + assertEquals("4+5/35sin(41)+dfdsfsdfs", state.display.text); + assertEquals(true, state.display.valid); + assertNull(state.display.getResult()); } }