Normalize tests. Rewrite Presenter.extractBodyContent and add tests.

This commit is contained in:
Christian P. MOMON 2013-07-30 02:34:33 +02:00
parent 9d9493a2a0
commit 29bf8c03a2
8 changed files with 103 additions and 30 deletions

View file

@ -15,5 +15,7 @@
<classpathentry kind="lib" path="lib/slf4j-log4j12-1.7.5.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="lib/junit-4.11.jar"/>
<classpathentry kind="lib" path="lib/fest-assert-1.4.jar"/>
<classpathentry kind="lib" path="lib/fest-util-1.1.6.jar"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>

Binary file not shown.

BIN
lib/fest-assert-1.4.jar Normal file

Binary file not shown.

Binary file not shown.

BIN
lib/fest-util-1.1.6.jar Normal file

Binary file not shown.

View file

@ -1,5 +1,8 @@
package fr.devinsy.xidyn.presenters;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.devinsy.xidyn.data.TagDataListById;
import fr.devinsy.xidyn.data.TagDataManager;
@ -8,6 +11,8 @@ import fr.devinsy.xidyn.data.TagDataManager;
*/
public abstract class Presenter<E>
{
public static final Pattern BODY_PATTERN = Pattern.compile("^.*<[bB][oO][dD][yY]>\\s*(.*\\S)\\s*</[bB][oO][dD][yY]>.*$");
/**
*
* @param datas
@ -65,27 +70,32 @@ public abstract class Presenter<E>
}
/**
* TODO implement a trim action.
*
*/
static public String extractBodyContent(final StringBuffer data)
static public String extractBodyContent(final CharSequence source)
{
String result = null;
String result;
// Extract the body content.
String dataLowerCase = data.toString().toLowerCase();
int startBody = dataLowerCase.indexOf("<body>");
int endBody = dataLowerCase.indexOf("</body>");
// Note: as failed search is improbable, no care about complexity
// in failed search case.
if ((startBody == -1) || (endBody == -1))
if (source == null)
{
result = null;
result = "";
}
else
{
result = data.substring(startBody + 6, endBody);
Matcher matcher = BODY_PATTERN.matcher(source);
if ((matcher.find()) && (matcher.groupCount() == 1))
{
// for (int i = 0; i <= matcher.groupCount(); i++)
// {
// System.out.println(i + " " + matcher.group(i));
// }
result = matcher.group(1);
}
else
{
result = "";
}
}
//

View file

@ -5,13 +5,11 @@ import fr.devinsy.xidyn.presenters.Presenter;
/**
*
*/
public class View
{
public class View {
/**
*
*/
public StringBuffer getHtml() throws Exception
{
public StringBuffer getHtml() {
StringBuffer result;
result = null;
@ -23,26 +21,19 @@ public class View
/**
*
*/
public StringBuffer getHtmlBody() throws Exception
{
public StringBuffer getHtmlBody() {
StringBuffer result;
StringBuffer html = this.getHtml();
if (html == null)
{
if (html == null) {
result = new StringBuffer();
}
else
{
} else {
String body = Presenter.extractBodyContent(html);
if (body == null)
{
if (body == null) {
result = new StringBuffer();
}
else
{
} else {
result = new StringBuffer(body);
}
}
@ -50,6 +41,18 @@ public class View
//
return (result);
}
/**
*
*/
public boolean isDeprecated() {
boolean result;
result = true;
//
return result;
}
}
// ////////////////////////////////////////////////////////////////////////

View file

@ -0,0 +1,58 @@
package fr.devinsy.xidyn.presenters;
import org.fest.assertions.Assertions;
import org.junit.Test;
/**
*
*/
public class PresenterTest
{
/**
*
*/
@Test
public void testExtractBodyContent01()
{
String source = "aaaaa<boDY>hello</Body>zzzzz";
String target = Presenter.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo("hello");
}
/**
*
*/
@Test
public void testExtractBodyContent02()
{
String source = "aaaaaaa<boDY>hello</Bod>zzzzz";
String target = Presenter.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo("");
}
/**
*
*/
@Test
public void testExtractBodyContent03()
{
String source = "aaaaa<boDY></BodY>zzzzz";
String target = Presenter.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo("");
}
/**
*
*/
@Test
public void testExtractBodyContent04()
{
String source = "aaaaa<boDY> hello </BodY>zzzzz";
String target = Presenter.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo("hello");
}
}