hebdobot/src/test/java/org/april/hebdobot/irc/BotTest.java

145 lines
4.3 KiB
Java

/**
* Copyright (C) 2011-2013,2017 Nicolas Vinot <aeris@imirhil.fr>
* Copyright (C) 2017 Christian Pierre MOMON <cmomon@april.org>
*
* This file is part of (April) Hebdobot.
*
* Hebdobot 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.
*
* Hebdobot 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
*/
package org.april.hebdobot.irc;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.april.hebdobot.review.Review;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* The Class BotTest.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/org/april/hebdobot/conf.xml")
public class BotTest implements ReviewListener
{
/**
* The Class BotMock.
*/
private static class BotMock extends Hebdobot
{
/**
* Instantiates a new bot mock.
*
* @throws Exception
* the exception
*/
public BotMock() throws Exception
{
super("", 0, "bot", "channel");
}
/* (non-Javadoc)
* @see org.april.hebdobot.irc.Bot#onMessage(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public void onMessage(final String channel, final String sender, final String login, final String hostname, final String message)
{
super.onMessage(channel, sender, login, hostname, message);
}
}
/* (non-Javadoc)
* @see org.april.hebdobot.irc.ReviewListener#onEnd(org.april.hebdobot.review.Review)
*/
@Override
public void onEnd(final Review review)
{
try
{
final String date = ISODateTimeFormat.basicDate().print(new DateTime());
final String text = review.toString();
final File file = new File("target/" + date + "_revue.txt");
FileUtils.writeStringToFile(file, text);
}
catch (final IOException exception)
{
throw new RuntimeException(exception);
}
}
/**
* Redo.
*
* @throws Exception
* the exception
*/
@Test
public void redo() throws Exception
{
final Hebdobot bot = new BotMock();
bot.add(this);
final InputStream is = BotTest.class.getResourceAsStream("/org/april/hebdobot/review.log");
if (is == null)
{
return;
}
try
{
final Reader isr = new InputStreamReader(is);
try
{
final BufferedReader isbr = new BufferedReader(isr);
try
{
String line;
final Pattern pattern = Pattern.compile(".*\\s+<([^>]+)>\\s+(.*)");
while ((line = isbr.readLine()) != null)
{
final Matcher matcher = pattern.matcher(line);
if (matcher.matches())
{
bot.onMessage("channel", matcher.group(1), "", "", matcher.group(2));
}
}
}
finally
{
isbr.close();
}
}
finally
{
isr.close();
}
}
finally
{
is.close();
}
}
}