Skip to content

Commit

Permalink
Added unit tests for tag classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jackrod committed Nov 18, 2011
1 parent 07b8052 commit c4c8ff6
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 153 deletions.
45 changes: 44 additions & 1 deletion src/main/java/org/bluemagic/config/decorator/tags/DoubleTag.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package org.bluemagic.config.decorator.tags;

/**
* This is a Java representation that encapsulates a doubletag.
* A doubletag consists of a key/value pair
* A simple example would be something like release=12.0_SNAPSHOT
* where the key is "release" and the value is "12.0_SNAPSHOT"
*
* This tag will be useful when needing to add some context to a singletag
*
*/
public class DoubleTag extends SingleTag {

private String key = "";
Expand All @@ -10,7 +19,41 @@ public DoubleTag(String key, String value) {
this.key = key;
this.value = value;
}


public DoubleTag(String key, String valuePrefix, String value, String valueSuffix) {

// SET THE KEY
this.key = key;

// SET THE VALUE
setPrefix(valuePrefix);
this.value = value;
setSuffix(valueSuffix);
}

@Override
public boolean equals(Object obj) {

boolean equals = false;

if (obj instanceof DoubleTag) {
DoubleTag other = (DoubleTag) obj;

String otherKey = other.getKey();
String otherValue = other.getValue();

if (otherKey.equals(this.getKey()) && otherValue.equals(this.getValue())) {
equals = true;
}
}
return equals;
}

@Override
public String toString() {
return this.key + "=" + getValue();
}

public void setKey(String key) {
this.key = key;
}
Expand Down
32 changes: 30 additions & 2 deletions src/main/java/org/bluemagic/config/decorator/tags/Hashtag.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
package org.bluemagic.config.decorator.tags;

/**
* Implements the common hashtags found on Twitter
* For example #SteveJobs or #test
*
*/
public class Hashtag extends SingleTag {

private static final String HASHTAG = "#";

public Hashtag() {
this.prefix = HASHTAG;
}

public Hashtag(String value) {
this.prefix = HASHTAG;
this.value = value;
}

public Hashtag(String prefix, String value, String suffix) {

setPrefix(prefix);
this.value = value;
setSuffix(suffix);
}

@Override
public String getPrefix() {
return "#";
public void setPrefix(String prefix) {

if ((prefix != null) && (prefix.startsWith(HASHTAG))) {
this.prefix = prefix;
} else {
this.prefix = HASHTAG + prefix;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public String getValue() {
throw new RuntimeException(t);
}
}
return value;
return prefix + value + suffix;
}
}
50 changes: 46 additions & 4 deletions src/main/java/org/bluemagic/config/decorator/tags/SingleTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import org.bluemagic.config.api.Tag;

/**
* This class is a Java representation of a singletag, also known as a meta-tag.
* Optional prefix and suffix can be applied as a convieniece to modifying the tag value
* to different types
*/
public class SingleTag implements Tag {

protected String prefix = "";
Expand All @@ -10,6 +15,39 @@ public class SingleTag implements Tag {

protected String value = "";

public SingleTag() { }

public SingleTag(String value) {
this.value = value;
}

public SingleTag(String prefix, String value, String suffix) {

setPrefix(prefix);
this.value = value;
setSuffix(suffix);
}

@Override
public boolean equals(Object obj) {

boolean equals = false;

if (obj instanceof SingleTag) {
SingleTag other = (SingleTag) obj;

if (other.getValue().equals(this.getValue())) {
equals = true;
}
}
return equals;
}

@Override
public String toString() {
return getValue();
}

public String getPrefix() {
return prefix;
}
Expand All @@ -25,19 +63,23 @@ public String getValue() {
public void setValue(String value) {

if ((prefix.length() > 0) && (value.startsWith(prefix))) {
value = value.substring(prefix.length() - 1, value.length() - prefix.length() - 1);
value = value.replaceFirst(prefix, "");
}
if ((suffix.length() > 0) && (value.endsWith(suffix))) {
value = value.substring(0, value.length() - suffix.length() - 1);
value = value.substring(0, value.length() - suffix.length());
}
this.value = value;
}

public void setSuffix(String suffix) {
this.suffix = suffix;
if (suffix != null) {
this.suffix = suffix;
}
}

public void setPrefix(String prefix) {
this.prefix = prefix;
if (prefix != null) {
this.prefix = prefix;
}
}
}
50 changes: 50 additions & 0 deletions src/main/java/org/bluemagic/config/decorator/tags/TripleTag.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package org.bluemagic.config.decorator.tags;

/**
* This is the Java representation of a tripletag or a machinetag
* It can be used to describe enhanced tagging information
*
* For example geo:latitude=83.3 where "geo" is the namespace, "latitude"
* id the predicate and "83.3" is the value
*
*/
public class TripleTag extends SingleTag {

private String namespace;
Expand All @@ -14,6 +22,48 @@ public TripleTag(String namespace, String predicate, String value) {
this.value = value;
}

public TripleTag(String namespace, String predicate, String valuePrefix, String value, String valueSuffix) {

this.namespace = namespace;
this.predicate = predicate;

setPrefix(valuePrefix);
this.value = value;
setSuffix(valueSuffix);
}

@Override
public boolean equals(Object obj) {

boolean equals = false;

if (obj instanceof TripleTag) {
TripleTag other = (TripleTag) obj;

String otherNamespace = other.getNamespace();
String otherPredicate = other.getPredicate();
String otherValue = other.getValue();

if (otherNamespace.equals(this.getNamespace()) && otherPredicate.equals(this.getPredicate()) && otherValue.equals(this.getValue())) {
equals = true;
}
}
return equals;
}

@Override
public String toString() {

StringBuilder b = new StringBuilder();
b.append(this.namespace);
b.append(":");
b.append(this.predicate);
b.append("=");
b.append(getValue());

return b.toString();
}

public void setPredicate(String predicate) {
this.predicate = predicate;
}
Expand Down
Loading

0 comments on commit c4c8ff6

Please sign in to comment.