From 712e04b7ccbf1b0339eeadbfd21ee781bf28c0df Mon Sep 17 00:00:00 2001 From: ANNIE Date: Thu, 7 Apr 2016 16:41:03 -0400 Subject: [PATCH 1/4] 1 & 2 DONE --- .classpath | 7 +++ src/debug/ContainerArray.java | 36 +++++++++---- src/debug/ContainerArrayTest.java | 50 +++++++++++++++++- src/tdd/Sheet.java | 34 +++++++++++++ src/tdd/TestSheet.java | 85 +++++++++++++++++++++++++++++++ 5 files changed, 200 insertions(+), 12 deletions(-) create mode 100644 .classpath create mode 100644 src/tdd/Sheet.java create mode 100644 src/tdd/TestSheet.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3e0fb27 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/debug/ContainerArray.java b/src/debug/ContainerArray.java index 124ece6..da8f9a0 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -2,32 +2,48 @@ public class ContainerArray { - private int initialCapacity = 10; + private static final int LIMIT = 10; private int currentSize = 0; private Object[] internalArray; public ContainerArray () { - this(10); + this(LIMIT); } - public ContainerArray (int initialCapacity) { - internalArray = new Object[initialCapacity]; + public ContainerArray (int limit) { + internalArray = new Object[limit]; } - public void add (E element) { - internalArray[currentSize++] = element; + public void add (E element) { //1: handling array out of bounds; don't allow addition when full + if(currentSize < internalArray.length){ + internalArray[currentSize++] = element; + } } public int size () { return currentSize; } - public void remove (E objectToRemove) { - currentSize--; + public void remove (E objectToRemove) { //2: Remove object (first instance) and shift rightmost elements to left + int index = -1; + for(int i = 0; i < internalArray.length; i++){ + if(objectToRemove.equals(internalArray[i])){ + index = i; + break; + } + } + if(index != -1){ + for(;index < internalArray.length-1; index++){ + internalArray[index] = internalArray[index+1]; + internalArray[index+1] = null; + } + currentSize--; //3: only decrement currentSize if something is removed + } } @SuppressWarnings("unchecked") - public E get (int index) { - return (E)internalArray[index]; + public E get (int index) { //4: handle index out of bounds + if(index>=0 && index < LIMIT) return (E)internalArray[index]; + return null; } } diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index c566d50..5393be0 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -20,28 +20,74 @@ public void testSizeChangeWithAdd () { myContainer.add("Camel"); assertEquals("Add size", 3, myContainer.size()); } + + @Test // + public void testStoreLimit () { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.add("Camel"); + assertEquals("Add size", 3, myContainer.size()); + myContainer.add("4"); + myContainer.add("5"); + myContainer.add("6"); + myContainer.add("7"); + myContainer.add("8"); + myContainer.add("9"); + myContainer.add("10"); + myContainer.add("11"); + myContainer.add("12"); + myContainer.add("13"); + myContainer.add("14"); + assertEquals("Add size", 10, myContainer.size()); + } @Test public void testObjectIsStored () { String alligator = "Alligator"; myContainer.add(alligator); + myContainer.add("Alligator"); assertEquals("Add should be same reference", alligator, myContainer.get(0)); } - @Test + @Test public void testSizeChangeWithRemove () { myContainer.add("Alligator"); myContainer.add("Bear"); myContainer.remove("Alligator"); assertEquals("Remove size", 1, myContainer.size()); } + + @Test // + public void testRemoveNonExistentObject () { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.remove("Falafel"); + assertEquals("Remove size", 2, myContainer.size()); + } + + @Test // + public void testRemoveModifiesAndShiftsArray () { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.remove("Alligator"); + assertEquals("Bear", myContainer.get(0)); + } + @Test // + public void testGetOutOfBounds () { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.add("Loofah"); + assertEquals(null, myContainer.get(-1)); + assertEquals(null, myContainer.get(2568)); + } @Test - public void testObjectIsRemoved () { + public void testObjectIsRemoved () { String alligator = "Alligator"; myContainer.add("Alligator"); myContainer.add("Bear"); myContainer.remove("Bear"); + myContainer.remove("Bear"); assertEquals("Remove should be same reference", alligator, myContainer.get(0)); } } diff --git a/src/tdd/Sheet.java b/src/tdd/Sheet.java new file mode 100644 index 0000000..bd9e234 --- /dev/null +++ b/src/tdd/Sheet.java @@ -0,0 +1,34 @@ +package tdd; + +import java.util.HashMap; + +public class Sheet { + HashMap sheetMap; + HashMap literalMap; + public Sheet() { + this.sheetMap = new HashMap<>(); + this.literalMap = new HashMap<>(); + } + + public String get(String key) { + String toReturn = sheetMap.get(key); + if(toReturn!=null) return toReturn; + return ""; + } + + public void put(String theCell, String string) { + try{ + int testInt = Integer.parseInt(string.trim()); + sheetMap.put(theCell, string.trim()); + } + catch(Exception e){ + sheetMap.put(theCell, string); + } + literalMap.put(theCell, string); + } + + public Object getLiteral(String theCell) { + return literalMap.get(theCell); + } + +} diff --git a/src/tdd/TestSheet.java b/src/tdd/TestSheet.java new file mode 100644 index 0000000..78d2347 --- /dev/null +++ b/src/tdd/TestSheet.java @@ -0,0 +1,85 @@ +package tdd; + +//static allows to import all static methods from this class, Assert +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestSheet { + + @Test + public void testThatCellsAreEmptyByDefault() { + Sheet sheet = new Sheet(); + assertEquals("", sheet.get("A1")); + assertEquals("", sheet.get("ZX347")); + } + + @Test + public void testThatTextCellsAreStored() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "A string"); + assertEquals("A string", sheet.get(theCell)); + + sheet.put(theCell, "A different string"); + assertEquals("A different string", sheet.get(theCell)); + + sheet.put(theCell, ""); + assertEquals("", sheet.get(theCell)); + } + + @Test + public void testThatManyCellsExist() { + Sheet sheet = new Sheet(); + sheet.put("A1", "First"); + sheet.put("X27", "Second"); + sheet.put("ZX901", "Third"); + + assertEquals("A1", "First", sheet.get("A1")); + assertEquals("X27", "Second", sheet.get("X27")); + assertEquals("ZX901", "Third", sheet.get("ZX901")); + + sheet.put("A1", "Fourth"); + assertEquals("A1 after", "Fourth", sheet.get("A1")); + assertEquals("X27 same", "Second", sheet.get("X27")); + assertEquals("ZX901 same", "Third", sheet.get("ZX901")); + } + + @Test + public void testThatNumericCellsAreIdentifiedAndStored() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "X99"); // "Obvious" string + assertEquals("X99", sheet.get(theCell)); + + sheet.put(theCell, "14"); // "Obvious" number + assertEquals("14", sheet.get(theCell)); + + sheet.put(theCell, " 99 X"); // Whole string must be numeric + assertEquals(" 99 X", sheet.get(theCell)); + + sheet.put(theCell, " 1234 "); // Blanks ignored + assertEquals("1234", sheet.get(theCell)); + + sheet.put(theCell, " "); // Just a blank + assertEquals(" ", sheet.get(theCell)); + } + + @Test + public void testThatWeHaveAccessToCellLiteralValuesForEditing() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "Some string"); + assertEquals("Some string", sheet.getLiteral(theCell)); + + sheet.put(theCell, " 1234 "); + assertEquals(" 1234 ", sheet.getLiteral(theCell)); + + sheet.put(theCell, "=7"); // Foreshadowing formulas:) + assertEquals("=7", sheet.getLiteral(theCell)); + } + +} From 1064c4e12260e1f934daa8a7a050290de5924ca5 Mon Sep 17 00:00:00 2001 From: ANNIE Date: Thu, 7 Apr 2016 17:06:52 -0400 Subject: [PATCH 2/4] minor changes --- src/debug/ContainerArray.java | 7 +++++++ src/debug/ContainerArrayTest.java | 19 ++++--------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/debug/ContainerArray.java b/src/debug/ContainerArray.java index da8f9a0..918f633 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -1,5 +1,6 @@ package debug; +import java.util.List; public class ContainerArray { private static final int LIMIT = 10; @@ -19,6 +20,12 @@ public ContainerArray (int limit) { internalArray[currentSize++] = element; } } + + public void addAll(List toAdd){ + for(E e: toAdd){ + add(e); + } + } public int size () { return currentSize; diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index 5393be0..d42c491 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -1,6 +1,9 @@ package debug; import static org.junit.Assert.*; + +import java.util.Arrays; + import org.junit.Before; import org.junit.Test; @@ -23,21 +26,7 @@ public void testSizeChangeWithAdd () { @Test // public void testStoreLimit () { - myContainer.add("Alligator"); - myContainer.add("Bear"); - myContainer.add("Camel"); - assertEquals("Add size", 3, myContainer.size()); - myContainer.add("4"); - myContainer.add("5"); - myContainer.add("6"); - myContainer.add("7"); - myContainer.add("8"); - myContainer.add("9"); - myContainer.add("10"); - myContainer.add("11"); - myContainer.add("12"); - myContainer.add("13"); - myContainer.add("14"); + myContainer.addAll(Arrays.asList("aaaaaaaaaaaaaaaaaaaaaa".split(""))); assertEquals("Add size", 10, myContainer.size()); } From 02074209c688196bc886434b67e4fc6c013c8642 Mon Sep 17 00:00:00 2001 From: ANNIE Date: Thu, 7 Apr 2016 17:29:53 -0400 Subject: [PATCH 3/4] junit for loopsgoatcheesesalad akt16 --- .classpath | 3 +- src/resource/invalidbasegiven.properties | 1 + src/resource/validbasegiven.properties | 1 + src/voogasalad/TabAttributes.java | 98 ++++++++++++++++++++++++ src/voogasalad/TabAttributesTest.java | 23 ++++++ 5 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/resource/invalidbasegiven.properties create mode 100644 src/resource/validbasegiven.properties create mode 100644 src/voogasalad/TabAttributes.java create mode 100644 src/voogasalad/TabAttributesTest.java diff --git a/.classpath b/.classpath index 3e0fb27..3b7b654 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,7 @@ - + + diff --git a/src/resource/invalidbasegiven.properties b/src/resource/invalidbasegiven.properties new file mode 100644 index 0000000..985e493 --- /dev/null +++ b/src/resource/invalidbasegiven.properties @@ -0,0 +1 @@ +Test=test \ No newline at end of file diff --git a/src/resource/validbasegiven.properties b/src/resource/validbasegiven.properties new file mode 100644 index 0000000..985e493 --- /dev/null +++ b/src/resource/validbasegiven.properties @@ -0,0 +1 @@ +Test=test \ No newline at end of file diff --git a/src/voogasalad/TabAttributes.java b/src/voogasalad/TabAttributes.java new file mode 100644 index 0000000..fe23ef4 --- /dev/null +++ b/src/voogasalad/TabAttributes.java @@ -0,0 +1,98 @@ +package voogasalad; + +import java.util.Arrays; +import java.util.List; +import java.util.ResourceBundle; + +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Node; +import javafx.scene.control.Label; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; + +/** + * Abstract Tab for setting attributes to go in the Inspector Pane in either the Level or Actor Editing Environment GUI. + * @author AnnieTang + * + */ +public class TabAttributes { + private static final String DEFAULT_BASE = "invalidbasegiven"; + private static final int PADDING = 10; + private static final double TEXTFIELD_WIDTH = 150; + private static final String EDITOR_ELEMENTS = "EditorElements"; + private static final String PROMPT = "Select"; + private static final String DELIMITER = ","; + private static final String LABEL = "Label"; + private static final String OPTIONS = "Options"; + private static final String TEXTFIELD = "Textfields"; + private static final String COMBOBOXES = "Comboboxes"; + private static final String TEST = "TEST"; + private static final String GO = "GO"; + private ResourceBundle myAttributesResources; + private String levelOptionsResource; + + public TabAttributes(String tabText, String levelOptionsResource) { + this.levelOptionsResource = levelOptionsResource; + setResources(); + } + + private void setResources(){ + try{ + this.myAttributesResources = ResourceBundle.getBundle(levelOptionsResource); + }catch(Exception e){ + this.myAttributesResources = ResourceBundle.getBundle(DEFAULT_BASE); + } + } + + public String getResourcesBundleBaseName(){ + return myAttributesResources.getBaseBundleName(); + } + + Node getContent() { + VBox vbox = new VBox(PADDING); + vbox.setPadding(new Insets(PADDING, PADDING, PADDING, PADDING)); + + + String[] elements = myAttributesResources.getString(EDITOR_ELEMENTS).split(DELIMITER); + for (int i = 0; i < elements.length; i++) { + HBox hbox = new HBox(PADDING); + hbox.setAlignment(Pos.CENTER_LEFT); + Label label = new Label(myAttributesResources.getString(elements[i] + LABEL)); + label.setWrapText(true); +// IGUIElement elementToCreate = null; +// elementToCreate = createNewGUIObject(elements[i]); +// hbox.getChildren().addAll(label, elementToCreate.createNode()); + vbox.getChildren().add(hbox); + } + return vbox; + } + +// private IGUIElement createNewGUIObject(String nodeType) { +// if (isTextField(nodeType)) { +// return createTextField(nodeType); +// } else if (isComboBox(nodeType)) { +// return createComboBox(nodeType); +// } +// return null; +// } + + private boolean isTextField(String nodeType) { + return Arrays.asList(myAttributesResources.getString(TEXTFIELD).split(",")).contains(nodeType); + } + + private boolean isComboBox(String nodeType) { + return Arrays.asList(myAttributesResources.getString(COMBOBOXES).split(",")).contains(nodeType); + } + +// private IGUIElement createTextField(String nodeType) { +// IGUIElement textField = new TextFieldWithButton("", TEST, TEXTFIELD_WIDTH, GO, null); +// return textField; +// } + +// private IGUIElement createComboBox(String nodeType) { +// String options = myAttributesResources.getString(nodeType + OPTIONS); +// List optionsList = Arrays.asList(options.split(DELIMITER)); +// return (IGUIElement) new ComboBoxOptions(myAttributesResources,PROMPT,optionsList); +// } +} diff --git a/src/voogasalad/TabAttributesTest.java b/src/voogasalad/TabAttributesTest.java new file mode 100644 index 0000000..71dd435 --- /dev/null +++ b/src/voogasalad/TabAttributesTest.java @@ -0,0 +1,23 @@ +package voogasalad; + +import static org.junit.Assert.*; + +import java.util.ResourceBundle; +import org.junit.Test; + + +public class TabAttributesTest { + private TabAttributes ta = null; + private String rbValidBaseBundle; + + @Test + public void testHandleInvalidBaseBundle() { + rbValidBaseBundle = "validbasegiven"; + ta = new TabAttributes("tabTest", rbValidBaseBundle); + assertEquals("validbasegiven", ta.getResourcesBundleBaseName()); + ta = new TabAttributes("tabTest", "randomword"); + assertEquals("invalidbasegiven", ta.getResourcesBundleBaseName()); + + } + +} From c4d2c3f6289f0a6c0fff14fc4ed7d39ab920be6b Mon Sep 17 00:00:00 2001 From: ANNIE Date: Thu, 7 Apr 2016 17:34:37 -0400 Subject: [PATCH 4/4] junit for other project --- src/voogasalad/TabAttributesTest.java | 1 - src/voogasalad/ToDo.java | 27 +++++++++++++++++++++++++++ src/voogasalad/ToDoTest.java | 21 +++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/voogasalad/ToDo.java create mode 100644 src/voogasalad/ToDoTest.java diff --git a/src/voogasalad/TabAttributesTest.java b/src/voogasalad/TabAttributesTest.java index 71dd435..e1fa41f 100644 --- a/src/voogasalad/TabAttributesTest.java +++ b/src/voogasalad/TabAttributesTest.java @@ -2,7 +2,6 @@ import static org.junit.Assert.*; -import java.util.ResourceBundle; import org.junit.Test; diff --git a/src/voogasalad/ToDo.java b/src/voogasalad/ToDo.java new file mode 100644 index 0000000..393cea0 --- /dev/null +++ b/src/voogasalad/ToDo.java @@ -0,0 +1,27 @@ +package voogasalad; + +import java.util.ResourceBundle; + +public class ToDo { + private static final String DEFAULT_BASE = "invalidbasegiven"; + private ResourceBundle myAttributesResources; + private String bundleBase; + + public ToDo(String bundleBase) { + this.bundleBase = bundleBase; + setResources(); + } + + private void setResources(){ + try{ + this.myAttributesResources = ResourceBundle.getBundle(bundleBase); + }catch(Exception e){ + this.myAttributesResources = ResourceBundle.getBundle(DEFAULT_BASE); + } + } + + public String getResourcesBundleBaseName(){ + return myAttributesResources.getBaseBundleName(); + } + +} diff --git a/src/voogasalad/ToDoTest.java b/src/voogasalad/ToDoTest.java new file mode 100644 index 0000000..45f4f08 --- /dev/null +++ b/src/voogasalad/ToDoTest.java @@ -0,0 +1,21 @@ +package voogasalad; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ToDoTest { + private ToDo td = null; + private String rbValidBaseBundle; + + @Test + public void testHandleInvalidBaseBundle() { + rbValidBaseBundle = "validbasegiven"; + td = new ToDo(rbValidBaseBundle); + assertEquals("validbasegiven", td.getResourcesBundleBaseName()); + td = new ToDo("randomword"); + assertEquals("invalidbasegiven", td.getResourcesBundleBaseName()); + + } + +}