ZEPPELIN-25 add unittest

This commit is contained in:
Lee moon soo 2015-04-05 21:36:28 +09:00
parent 6ce8f364d2
commit d4d270e431
3 changed files with 324 additions and 0 deletions

View file

@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package com.nflabs.zeppelin.display;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
public class AngularObjectRegistryTest {
@Test
public void testBasic() {
final AtomicInteger onAdd = new AtomicInteger(0);
final AtomicInteger onUpdate = new AtomicInteger(0);
final AtomicInteger onRemove = new AtomicInteger(0);
AngularObjectRegistry registry = new AngularObjectRegistry("intpId",
new AngularObjectRegistryListener() {
@Override
public void onAdd(String interpreterGroupId, AngularObject object) {
onAdd.incrementAndGet();
}
@Override
public void onUpdate(String interpreterGroupId, AngularObject object) {
onUpdate.incrementAndGet();
}
@Override
public void onRemove(String interpreterGroupId, AngularObject object) {
onRemove.incrementAndGet();
}
});
registry.add("name1", "value1");
assertEquals(1, registry.getAll().size());
assertEquals(1, onAdd.get());
assertEquals(0, onUpdate.get());
registry.get("name1").set("newValue");
assertEquals(1, onUpdate.get());
registry.remove("name1");
assertEquals(0, registry.getAll().size());
assertEquals(1, onRemove.get());
assertEquals(null, registry.get("name1"));
}
}

View file

@ -0,0 +1,142 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package com.nflabs.zeppelin.interpreter.remote;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.util.HashMap;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.nflabs.zeppelin.display.AngularObject;
import com.nflabs.zeppelin.display.AngularObjectRegistry;
import com.nflabs.zeppelin.display.AngularObjectRegistryListener;
import com.nflabs.zeppelin.display.GUI;
import com.nflabs.zeppelin.interpreter.InterpreterContext;
import com.nflabs.zeppelin.interpreter.InterpreterGroup;
import com.nflabs.zeppelin.interpreter.InterpreterResult;
import com.nflabs.zeppelin.interpreter.remote.mock.MockInterpreterAngular;
public class RemoteAngularObjectTest implements AngularObjectRegistryListener {
private InterpreterGroup intpGroup;
private HashMap<String, String> env;
private RemoteInterpreter intp;
private InterpreterContext context;
private RemoteAngularObjectRegistry localRegistry;
private AtomicInteger onAdd;
private AtomicInteger onUpdate;
private AtomicInteger onRemove;
@Before
public void setUp() throws Exception {
onAdd = new AtomicInteger(0);
onUpdate = new AtomicInteger(0);
onRemove = new AtomicInteger(0);
intpGroup = new InterpreterGroup("intpId");
localRegistry = new RemoteAngularObjectRegistry("intpId", this, intpGroup);
intpGroup.setAngularObjectRegistry(localRegistry);
env = new HashMap<String, String>();
env.put("ZEPPELIN_CLASSPATH", new File("./target/test-classes").getAbsolutePath());
Properties p = new Properties();
intp = new RemoteInterpreter(
p,
MockInterpreterAngular.class.getName(),
new File("../bin/interpreter.sh").getAbsolutePath(),
"fake",
env
);
intpGroup.add(intp);
intp.setInterpreterGroup(intpGroup);
context = new InterpreterContext(
"id",
"title",
"text",
new HashMap<String, Object>(),
new GUI(),
new AngularObjectRegistry(intpGroup.getId(), null));
intp.open();
}
@After
public void tearDown() throws Exception {
intp.close();
intpGroup.clone();
intpGroup.destroy();
}
@Test
public void testAngularObjectCRUD() throws InterruptedException {
InterpreterResult ret = intp.interpret("get", context);
Thread.sleep(500); // waitFor eventpoller pool event
String[] result = ret.message().split(" ");
assertEquals("0", result[0]); // size of registry
assertEquals("0", result[1]); // num watcher called
// create object
ret = intp.interpret("add n1 v1", context);
Thread.sleep(500);
result = ret.message().split(" ");
assertEquals("1", result[0]); // size of registry
assertEquals("0", result[1]); // num watcher called
assertEquals("v1", localRegistry.get("n1").get());
// update object
ret = intp.interpret("update n1 v11", context);
result = ret.message().split(" ");
Thread.sleep(500);
assertEquals("1", result[0]); // size of registry
assertEquals("1", result[1]); // num watcher called
assertEquals("v11", localRegistry.get("n1").get());
// remove object
ret = intp.interpret("remove n1", context);
result = ret.message().split(" ");
Thread.sleep(500);
assertEquals("0", result[0]); // size of registry
assertEquals("1", result[1]); // num watcher called
assertEquals(null, localRegistry.get("n1"));
}
@Override
public void onAdd(String interpreterGroupId, AngularObject object) {
onAdd.incrementAndGet();
}
@Override
public void onUpdate(String interpreterGroupId, AngularObject object) {
onUpdate.incrementAndGet();
}
@Override
public void onRemove(String interpreterGroupId, AngularObject object) {
onRemove.incrementAndGet();
}
}

View file

@ -0,0 +1,115 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package com.nflabs.zeppelin.interpreter.remote.mock;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import com.nflabs.zeppelin.display.AngularObjectRegistry;
import com.nflabs.zeppelin.display.AngularObjectWatcher;
import com.nflabs.zeppelin.interpreter.Interpreter;
import com.nflabs.zeppelin.interpreter.InterpreterContext;
import com.nflabs.zeppelin.interpreter.InterpreterPropertyBuilder;
import com.nflabs.zeppelin.interpreter.InterpreterResult;
import com.nflabs.zeppelin.interpreter.InterpreterResult.Code;
public class MockInterpreterAngular extends Interpreter implements AngularObjectWatcher {
static {
Interpreter.register(
"angularTest",
"angular",
MockInterpreterA.class.getName(),
new InterpreterPropertyBuilder()
.add("p1", "v1", "property1").build());
}
AtomicInteger numWatch = new AtomicInteger(0);
public MockInterpreterAngular(Properties property) {
super(property);
}
@Override
public void open() {
}
@Override
public void close() {
}
@Override
public InterpreterResult interpret(String st, InterpreterContext context) {
String[] stmt = st.split(" ");
String cmd = stmt[0];
String name = null;
if (stmt.length >= 2) {
name = stmt[1];
}
String value = null;
if (stmt.length == 3) {
value = stmt[2];
}
AngularObjectRegistry registry = context.getAngularObjectRegistry();
if (cmd.equals("add")) {
registry.add(name, value);
registry.get(name).addWatcher(this);
} else if (cmd.equalsIgnoreCase("update")) {
registry.get(name).set(value);
} else if (cmd.equals("remove")) {
registry.remove(name);
}
try {
Thread.sleep(500); // wait for watcher executed
} catch (InterruptedException e) {
}
String msg = registry.getAll().size() + " " + Integer.toString(numWatch.get());
return new InterpreterResult(Code.SUCCESS, msg);
}
@Override
public void cancel(InterpreterContext context) {
}
@Override
public FormType getFormType() {
return FormType.NATIVE;
}
@Override
public int getProgress(InterpreterContext context) {
return 0;
}
@Override
public List<String> completion(String buf, int cursor) {
return null;
}
@Override
public void watch(Object oldObject, Object newObject) {
numWatch.incrementAndGet();
}
}