summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-17 10:27:54 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-17 10:27:54 +0100
commitb21b53b919a741bc30ad835db14aefbeb4579f50 (patch)
treee8073be135ea9cd033f4b3e363d52178c486869d
parent1517ec87df466d9c2b0c38ff80b7160bb67c2ba6 (diff)
VariableFactory: Support set() operation.
-rw-r--r--same/src/main/java/com/orbekk/same/Variable.java2
-rw-r--r--same/src/main/java/com/orbekk/same/VariableFactory.java30
-rw-r--r--same/src/test/java/com/orbekk/same/VariableFactoryTest.java14
3 files changed, 41 insertions, 5 deletions
diff --git a/same/src/main/java/com/orbekk/same/Variable.java b/same/src/main/java/com/orbekk/same/Variable.java
index 63f8063..b6d3b7f 100644
--- a/same/src/main/java/com/orbekk/same/Variable.java
+++ b/same/src/main/java/com/orbekk/same/Variable.java
@@ -11,7 +11,7 @@ public interface Variable<T> {
}
T get();
- void set(T value);
+ void set(T value) throws UpdateConflict;
void update();
void setOnChangeListener(OnChangeListener<T> listener);
}
diff --git a/same/src/main/java/com/orbekk/same/VariableFactory.java b/same/src/main/java/com/orbekk/same/VariableFactory.java
index 81b7615..79c2cdc 100644
--- a/same/src/main/java/com/orbekk/same/VariableFactory.java
+++ b/same/src/main/java/com/orbekk/same/VariableFactory.java
@@ -1,17 +1,27 @@
package com.orbekk.same;
+import java.io.IOException;
+
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* TODO: Use WeakReference in order to make variables GC-able.
*/
public class VariableFactory {
- Client.ClientInterface client;
+ private Logger logger = LoggerFactory.getLogger(getClass());
+ private Client.ClientInterface client;
+ private ObjectMapper mapper = new ObjectMapper();
private class VariableImpl<T> implements Variable<T> {
String identifier;
TypeReference<T> type;
T value;
+ long revision = 0;
public VariableImpl(String identifier, TypeReference<T> type) {
this.identifier = identifier;
@@ -24,7 +34,23 @@ public class VariableFactory {
}
@Override
- public void set(T value) {
+ public void set(T value) throws UpdateConflict {
+ try {
+ String serializedValue = mapper.writeValueAsString(value);
+ client.set(identifier, serializedValue, revision);
+ } catch (JsonGenerationException e) {
+ logger.warn("Failed to convert to JSON: {}", value);
+ logger.warn("Parse exception.", e);
+ throw new RuntimeException(e);
+ } catch (JsonMappingException e) {
+ logger.warn("Failed to convert to JSON: {}", value);
+ logger.warn("Parse exception.", e);
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ logger.warn("Failed to cornvert to JSON: {}", value);
+ logger.warn("Parse exception.", e);
+ throw new RuntimeException(e);
+ }
}
@Override
diff --git a/same/src/test/java/com/orbekk/same/VariableFactoryTest.java b/same/src/test/java/com/orbekk/same/VariableFactoryTest.java
index eade799..7d20fa7 100644
--- a/same/src/test/java/com/orbekk/same/VariableFactoryTest.java
+++ b/same/src/test/java/com/orbekk/same/VariableFactoryTest.java
@@ -1,9 +1,11 @@
package com.orbekk.same;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verify;
import java.util.List;
@@ -18,12 +20,14 @@ public class VariableFactoryTest {
TypeReference<Integer> intType = new TypeReference<Integer>() {};
TypeReference<List<String>> listType = new TypeReference<List<String>>() {};
+ TypeReference<String> stringType = new TypeReference<String>() {};
@Before
public void setUp() {
client = mock(Client.ClientInterface.class);
vf = new VariableFactory(client);
initializeSampleState();
+ when(client.getState()).thenReturn(sampleState);
}
public void initializeSampleState() {
@@ -34,14 +38,12 @@ public class VariableFactoryTest {
@Test
public void getsInitialValue() {
- when(client.getState()).thenReturn(sampleState);
Variable<Integer> testVariable = vf.create("TestVariable", intType);
assertEquals(1, (int)testVariable.get());
}
@Test
public void updatesValue() {
- when(client.getState()).thenReturn(sampleState);
Variable<List<String>> list = vf.create("TestList", listType);
assertTrue(list.get().isEmpty());
sampleState.update("TestList", "[\"CONTENT\"]", 2);
@@ -49,4 +51,12 @@ public class VariableFactoryTest {
assertEquals(1, list.get().size());
assertEquals("CONTENT", list.get().get(0));
}
+
+ @Test
+ public void setsValue() throws Exception {
+ Variable<String> string = vf.create("X", stringType);
+ assertNull(string.get());
+ string.set("NewValue");
+ verify(client).set("X", "\"NewValue\"", 0);
+ }
}