Java tutorial
package com.conwet.silbops.model; /* * #%L * SilboPS API * %% * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import static org.assertj.core.api.Assertions.assertThat; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import org.json.simple.JSONObject; import org.junit.Before; import org.junit.Test; import com.conwet.silbops.model.basic.Attribute; import com.conwet.silbops.model.basic.Type; /** * * @author sergio */ public class ContextFunctionTest { private Attribute pubAttr; private Attribute subAttr; private ContextFunction function; @Before public void setUp() { pubAttr = new Attribute("pub1", Type.DOUBLE); subAttr = new Attribute("sub1", Type.DOUBLE); function = new ContextFunction().addConstraint(pubAttr, subAttr, 10.0D); } @Test @SuppressWarnings({ "unchecked" }) public void shouldJSONize() { JSONObject key1 = new JSONObject(); key1.put("notAttr", "pub1:double"); key1.put("subscriptionAttr", "sub1:double"); JSONObject json = new JSONObject(); json.put(key1.toJSONString(), 10.0D); assertThat(function.toJSONString()).isEqualTo(json.toJSONString()); assertThat(ContextFunction.fromJSON(json)).isEqualTo(function); } @Test public void shouldExternalize() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutput output = new ObjectOutputStream(baos); output.writeObject(function); output.close(); ObjectInput input = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); ContextFunction cxtFun = (ContextFunction) input.readObject(); assertThat(cxtFun).isEqualTo(function); } @Test public void shouldMatchContext() { Context cxtPub = new Context().attribute(pubAttr, 30.0D); Context cxtSub = new Context().attribute(subAttr, 31.0D); assertThat(function.match(cxtPub, cxtSub)).isTrue(); } @Test public void shouldntMatchContext() { Context cxtPub = new Context().attribute(pubAttr, 42.0D); Context cxtSub = new Context().attribute(subAttr, 31.0D); assertThat(function.match(cxtPub, cxtSub)).isFalse(); } @Test public void shuldntMatchEmptyContext() { assertThat(function.match(new Context(), new Context())).isFalse(); } }