Skip to content

Commit

Permalink
Merge pull request #276 from crawljax/testing-conditions
Browse files Browse the repository at this point in the history
Tests + Fixes for Equals/Hashcode/toString of Conditions
  • Loading branch information
alexnederlof committed Jun 11, 2013
2 parents ffa9a92 + e708ae1 commit 572753d
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 128 deletions.
25 changes: 16 additions & 9 deletions core/src/main/java/com/crawljax/condition/CountCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CountCondition implements Condition {

private final Condition condition;
private final AtomicInteger count = new AtomicInteger(0);
private final AtomicInteger maxCount = new AtomicInteger(0);
private final int maxCount;

/**
* @param maxCount
Expand All @@ -29,16 +29,20 @@ public class CountCondition implements Condition {
* the condition.
*/
public CountCondition(int maxCount, Condition condition) {
this.maxCount.set(maxCount);
this.maxCount = maxCount;
this.condition = condition;
}

/**
* Note: Check has a side effect (it increments a counter).
* Invoking it multiple times may result in a different answer.
*/
@Override
public boolean check(EmbeddedBrowser browser) {
if (condition.check(browser)) {
count.getAndIncrement();
}
return count.get() <= maxCount.get();
return count.get() <= maxCount;
}

@Override
Expand All @@ -49,26 +53,29 @@ public NodeList getAffectedNodes() {
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("super", super.toString())
.add("condition", condition)
.add("count", count)
.add("maxCount", maxCount)
.toString();
}

/**
* Since "count" is a consequence of invoking "check",
* it is not included in the equality / hashCode computation.
*/
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), condition, count, maxCount);
return Objects.hashCode(getClass(), condition, maxCount);
}

/**
* Since "count" is a consequence of invoking "check",
* it is not included in the equality / hashCode computation.
*/
@Override
public boolean equals(Object object) {
if (object instanceof CountCondition) {
if (!super.equals(object))
return false;
CountCondition that = (CountCondition) object;
return Objects.equal(this.condition, that.condition)
&& Objects.equal(this.count, that.count)
&& Objects.equal(this.maxCount, that.maxCount);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,19 @@ public boolean check(EmbeddedBrowser browser) {
}
return object.toString().equals("1");
} catch (CrawljaxException e) {
// Exception is catched, check failed so return false;
// Exception is caught, check failed so return false;
return false;
}
}

@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), expression);
return Objects.hashCode(getClass(), expression);
}

@Override
public boolean equals(Object object) {
if (object instanceof JavaScriptCondition) {
if (!super.equals(object))
return false;
JavaScriptCondition that = (JavaScriptCondition) object;
return Objects.equal(this.expression, that.expression);
}
Expand All @@ -72,7 +70,6 @@ public boolean equals(Object object) {
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("super", super.toString())
.add("expression", expression)
.toString();
}
Expand Down
233 changes: 157 additions & 76 deletions core/src/main/java/com/crawljax/condition/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,172 @@
*/
package com.crawljax.condition;

import java.util.Arrays;

import net.jcip.annotations.Immutable;

import com.crawljax.browser.EmbeddedBrowser;
import com.google.common.base.Objects;

/**
* Logic operations for conditions.
*
* @author dannyroest@gmail.com (Danny Roest)
*/
@Immutable
public final class Logic {

/**
* @param condition
* the condition.
* @return the condition negated.
*/
public static Condition not(final Condition condition) {
return new AbstractCondition() {

@Override
public boolean check(EmbeddedBrowser browser) {
return !condition.check(browser);
}

};
}

/**
* @param conditions
* the conditions.
* @return AND of conditions
*/
public static Condition and(final Condition... conditions) {
return new AbstractCondition() {

@Override
public boolean check(EmbeddedBrowser browser) {
for (Condition condition : conditions) {
if (!condition.check(browser)) {
return false;
}
}
return true;
}
};
}

/**
* @param conditions
* the conditions.
* @return OR conditions
*/
public static Condition or(final Condition... conditions) {
return new AbstractCondition() {

@Override
public boolean check(EmbeddedBrowser browser) {
for (Condition condition : conditions) {
if (condition.check(browser)) {
return true;
}
}
return false;
}
};
}

/**
* @param conditions
* the conditions.
* @return NAND conditions
*/
public static Condition nand(final Condition... conditions) {
return new AbstractCondition() {

@Override
public boolean check(EmbeddedBrowser browser) {
return not(and(conditions)).check(browser);
}

};
}

private Logic() {
}
/**
* @param condition
* the condition.
* @return the condition negated.
*/
public static Condition not(final Condition condition) {
return new Not(condition);
}

/**
* @param conditions
* the conditions.
* @return AND of conditions
*/
public static Condition and(final Condition... conditions) {
return new And(conditions);
}

/**
* @param conditions
* the conditions.
* @return OR conditions
*/
public static Condition or(final Condition... conditions) {
return new Or(conditions);
}


/**
* @param conditions
* the conditions.
* @return NAND conditions
*/
public static Condition nand(final Condition... conditions) {
return not(and(conditions));
}

private Logic() {
}

private static class Not extends AbstractCondition {
private Condition condition;

public Not(Condition c) {
condition = c;
}

@Override
public boolean check(EmbeddedBrowser browser) {
return !condition.check(browser);
}

@Override
public String toString() {
return Objects.toStringHelper(this)
.add("condition", condition)
.toString();
}

@Override
public int hashCode() {
return Objects.hashCode(getClass(), condition);
}

@Override
public boolean equals(Object object) {
if (object instanceof Not) {
Not that = (Not) object;
return Objects.equal(this.condition, that.condition);
}
return false;
}
}

private static class And extends AbstractCondition {
private Condition[] conditions;

public And(Condition... cs) {
conditions = cs;
}

@Override
public boolean check(EmbeddedBrowser browser) {
for (Condition condition : conditions) {
if (!condition.check(browser)) {
return false;
}
}
return true;
}

@Override
public String toString() {
return Objects.toStringHelper(this)
.add("condition", Arrays.deepToString(conditions))
.toString();
}

@Override
public int hashCode() {
int args = Objects.hashCode((Object[]) conditions);
return Objects.hashCode(getClass(), args);
}

@Override
public boolean equals(Object object) {
if (object instanceof And) {
And that = (And) object;
return Arrays.equals(this.conditions, that.conditions);
}
return false;
}
}

private static class Or extends AbstractCondition {
private Condition[] conditions;

public Or(Condition... cs) {
conditions = cs;
}

@Override
public boolean check(EmbeddedBrowser browser) {
for (Condition condition : conditions) {
if (condition.check(browser)) {
return true;
}
}
return false;
}

@Override
public String toString() {
return Objects.toStringHelper(this)
.add("condition", Arrays.deepToString(conditions))
.toString();
}

@Override
public int hashCode() {
int args = Objects.hashCode((Object[]) conditions);
return Objects.hashCode(getClass(), args);
}

@Override
public boolean equals(Object object) {
if (object instanceof Or) {
Or that = (Or) object;
return Arrays.equals(this.conditions, that.conditions);
}
return false;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ public boolean check(EmbeddedBrowser browser) {

@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), regexCondition);
return Objects.hashCode(getClass(), regexCondition);
}

@Override
public boolean equals(Object object) {
if (object instanceof NotRegexCondition) {
if (!super.equals(object))
return false;
NotRegexCondition that = (NotRegexCondition) object;
return Objects.equal(this.regexCondition, that.regexCondition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ public boolean check(EmbeddedBrowser browser) {

@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), urlCondition);
return Objects.hashCode(getClass(), urlCondition);
}

@Override
public boolean equals(Object object) {
if (object instanceof NotUrlCondition) {
if (!super.equals(object))
return false;
NotUrlCondition that = (NotUrlCondition) object;
return Objects.equal(this.urlCondition, that.urlCondition);
}
Expand All @@ -48,8 +46,7 @@ public boolean equals(Object object) {
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("super", super.toString())
.add("urlCondition", urlCondition)
.add("urlCondition", urlCondition)
.toString();
}

Expand Down
Loading

0 comments on commit 572753d

Please sign in to comment.