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 com.conwet.silbops.model.basic.Operator.EQ; import static com.conwet.silbops.model.basic.Operator.EXISTS; import static org.assertj.core.api.Assertions.assertThat; import java.util.EnumSet; import org.json.simple.JSONObject; import org.junit.Before; import org.junit.Test; import com.conwet.silbops.model.basic.Operator; import com.conwet.silbops.model.basic.Value; /** * This class test constraint behavior and its JSON form. * * @author sergio */ public class ConstraintTest { private Value oneString; private Constraint constr; @Before public void setUp() { oneString = Value.valueOf("1"); constr = new Constraint(EQ, oneString); } @SuppressWarnings("unused") @Test(expected = NullPointerException.class) public void shouldntAcceptNullOperator() { new Constraint(null, oneString); } @Test public void shouldAcceptNullValuesOnlyWithExistOperator() { Constraint exist = new Constraint(EXISTS, null); assertThat(exist).isEqualTo(Constraint.EXIST); assertThat(exist.hashCode()).isEqualTo(Constraint.EXIST.hashCode()); } @Test public void shouldDiscardValueWithExistOperator() { Constraint constr = new Constraint(EXISTS, oneString); assertThat(constr.getValue()).isNull(); } @Test public void shouldReturnOperatorAndValue() { assertThat(constr.getOperator()).isEqualTo(EQ); assertThat(constr.getValue()).isEqualTo(oneString); } @Test public void shouldTostringOperatorAndValue() { assertThat(constr.toString()).contains(EQ.toString()).contains(oneString.toString()); } @Test public void shouldDelegateMatching() { Constraint cons = new Constraint(Operator.GT, Value.valueOf(2)); assertThat(cons.match(Value.valueOf(3))).isTrue(); assertThat(cons.match(Value.valueOf(1))).isFalse(); } @Test @SuppressWarnings("unchecked") public void shouldJSONise() { // Exist constraint JSONObject json = new JSONObject(); json.put(EXISTS.toJSON(), ""); assertThat(Constraint.EXIST.toJSON()).isEqualTo(json); assertThat(Constraint.EXIST.toJSONString()).isEqualTo(json.toJSONString()); // verify Exist constraint is instantiated only once assertThat(Constraint.fromJSON(json)).isSameAs(Constraint.EXIST); // all others EnumSet<Operator> remaining = EnumSet.allOf(Operator.class); remaining.remove(EXISTS); for (Operator operator : remaining) { json = new JSONObject(); json.put(operator.toJSON(), oneString.toJSON()); Constraint cons = new Constraint(operator, oneString); assertThat(cons.toJSON()).isEqualTo(json); assertThat(cons.toJSONString()).isEqualTo(json.toJSONString()); assertThat(Constraint.fromJSON(json)).isEqualTo(cons); } } @SuppressWarnings("unchecked") @Test(expected = IllegalArgumentException.class) public void shouldntAcceptWrongJSON() { JSONObject json = new JSONObject(); json.put("=", "unknown"); json.put("<", "other"); Constraint.fromJSON(json); } // private Value zero; // private Value one; // private Value two; // private Value onef; // private Value onef2; // private Value pi; // private Value helloWorld; // private Value hello; // private Value world; // private Value unrelated; // private JSONObject jsonGt0; // private Constraint gt0, gt1, gt2; // // @Before // @SuppressWarnings({"unchecked", "serial"}) // public void setUp() { // // zero = new Value(0L); // one = new Value(1L); // two = new Value(2L); // onef = new Value(1D); // onef2 = new Value(1D); // pi = new Value(Math.PI); // // helloWorld = new Value("hello world"); // hello = new Value("hello"); // world = new Value("world"); // unrelated = new Value("unrelated"); // // gt0 = new Constraint(Operator.Gt, zero); // gt1 = new Constraint(Operator.Gt, one); // gt2 = new Constraint(Operator.Gt, two); // // jsonGt0 = new JSONObject() { // // { // put("gt", new JSONArray() { // // { // add("int"); // add(0L); // } // }); // } // }; // } // // @Test // public void existsCoverEverything() { // System.out.println("'exists' cover everything else"); // // Constraint exists = new Constraint(Operator.Exists, new Value(ValueType.INTEGER)); // assertThat(exists.covers(exists)).isTrue(); // assertThat(exists.covers(new Constraint(Operator.Ne, one))).isTrue(); // } // // @Test // public void shouldNotCoverDifferentTypes() { // System.out.println("should not cover different types"); // // Constraint eqInt = new Constraint(Operator.Eq, one), // eqFloat = new Constraint(Operator.Eq, onef), // eqString = new Constraint(Operator.Eq, new Value("1L")); // // assertThat(eqInt.covers(eqInt)).isTrue(); // assertThat(eqFloat.covers(eqFloat)).isTrue(); // assertThat(eqString.covers(eqString)).isTrue(); // // assertThat(eqInt.covers(eqFloat)).isFalse(); // assertThat(eqFloat.covers(eqString)).isFalse(); // } // // @Test // public void shouldCoverMoreSpecificConstraints() { // System.out.println("should cover more specific constraints"); // // Constraint eq1 = new Constraint(Operator.Eq, one), // eq2 = new Constraint(Operator.Eq, two), // eq0 = new Constraint(Operator.Eq, zero), // ne1 = new Constraint(Operator.Ne, one), // ge0 = new Constraint(Operator.Ge, zero), // ge1 = new Constraint(Operator.Ge, one), // ge2 = new Constraint(Operator.Ge, two); // // // EQ X covers EQ X // assertThat(eq1.covers(eq1)).isTrue(); // assertThat(eq1.covers(eq0)).isFalse(); // assertThat(eq1.covers(eq2)).isFalse(); // assertThat(eq1.covers(ge1)).isFalse(); // // // NE X covers NE X // // EQ Y, Y != X // // GT Y, Y >= X; GE Y, Y > X // // LT Y, Y <= X; LE Y, Y < X // assertThat(ne1.covers(ne1)).isTrue(); // assertThat(ne1.covers(eq1)).isFalse(); // assertThat(ne1.covers(ge2)).isTrue(); // assertThat(ne1.covers(ge1)).isFalse(); // // // GT X covers GT Y, Y>=X // // EQ Y, GE Y, Y>X // assertThat(gt1.covers(gt1)).isTrue(); // assertThat(gt1.covers(gt2)).isTrue(); // assertThat(gt1.covers(eq2)).isTrue(); // assertThat(gt1.covers(ge2)).isTrue(); // assertThat(gt1.covers(gt0)).isFalse(); // assertThat(gt1.covers(eq1)).isFalse(); // assertThat(gt1.covers(ge0)).isFalse(); // assertThat(gt1.covers(ge1)).isFalse(); // assertThat(gt1.covers(ne1)).isFalse(); // // // GE X covers GT Y, EQ Y, GE Y, Y>=X // assertThat(ge1.covers(ge1)).isTrue(); // assertThat(ge1.covers(ge2)).isTrue(); // assertThat(ge1.covers(ge0)).isFalse(); // assertThat(ge1.covers(gt1)).isTrue(); // assertThat(ge1.covers(gt2)).isTrue(); // } }