package org.productivity.java.syslog4j.test.split;

import java.util.List;

import junit.framework.TestCase;

import org.productivity.java.syslog4j.Syslog;
import org.productivity.java.syslog4j.SyslogConfigIF;
import org.productivity.java.syslog4j.SyslogIF;
import org.productivity.java.syslog4j.test.split.base.SplitSyslog;
import org.productivity.java.syslog4j.test.split.base.SplitSyslogConfig;
import org.productivity.java.syslog4j.util.SyslogUtility;

public class SplitSyslogTest extends TestCase {
	protected int localNameLength = 0;
	
	public void setUp() {
		if (!Syslog.exists("split")) {
			SplitSyslogConfig config = new SplitSyslogConfig();
			
			Syslog.createInstance("split", config);
		}
	}
	
	protected void setMessageLength(int length) {
		SyslogConfigIF config = Syslog.getInstance("split").getConfig();
		
		String localName = SyslogUtility.getLocalName();
		this.localNameLength = localName.length();
		
		config.setMaxMessageLength(21 + this.localNameLength + length);
	}
	
	protected void assertSyslog(SyslogIF syslog, String[] expectedMessages) {
		SplitSyslog splitSyslog = (SplitSyslog) syslog;
		
		List lastMessages = splitSyslog.getLastMessages();
		
		if (lastMessages.size() < 1) {
			fail("No messages received");
		}
		
		for(int i=0; i<lastMessages.size(); i++) {
			String lastMessage = ((String) lastMessages.get(i)).substring(21 + this.localNameLength);
			
			assertEquals(expectedMessages[i],lastMessage);
		}
		
		syslog.flush();
	}
	
	public void testSplit() {
		setMessageLength(18);
		
		SyslogIF syslog = Syslog.getInstance("split");
		
		syslog.info("For now is the tim");
		assertSyslog(syslog,new String[] { "For now is the tim" });
		
		syslog.info("For now is the time");
		assertSyslog(syslog,new String[] {"For now is the ...", "...time" });
		
		syslog.info("For now is the time ");
		assertSyslog(syslog,new String[] {"For now is the ...", "...time " });
		
		syslog.info("------------------");
		syslog.flush();
		
		syslog.info("For now is the time for all g");
		assertSyslog(syslog,new String[] {"For now is the ...", "...time for all g" });
		
		syslog.info("For now is the time for all go");
		assertSyslog(syslog,new String[] {"For now is the ...", "...time for all go" });
		
		syslog.info("For now is the time for all goo");
		assertSyslog(syslog,new String[] {"For now is the ...", "...time for all...", "... goo" });
		
		syslog.flush();
	}
	
	public void testTruncate() {
		setMessageLength(10);
		
		SyslogIF syslog = Syslog.getInstance("split");
		syslog.getConfig().setTruncateMessage(true);
		
		syslog.info("T");
		syslog.info("Test 1234");
		syslog.info("Test 12345");
		syslog.info("Test 654321");
		syslog.info("Test 12345 For now is the time for all good men to come to the aid of their country");
		
		assertSyslog(syslog,new String[] {"T", "Test 1234", "Test 12345", "Test 65432", "Test 12345" } );
		
		syslog.flush();
	}
}

@@ -103,16 +103,64 @@
 tCase;%0A%0A
+import org.junit.Ignore;%0Aimport org.junit.Test;%0A
 import o
@@ -1507,34 +1507,33 @@
 log.flush();%0A%09%7D%0A
-%09
 %0A
+
 %09public void tes
@@ -1539,24 +1539,73 @@
 stSplit() %7B%0A
+%09%09// this doesn't appear to be implemented yet%0A//
 %09%09setMessage
@@ -1616,19 +1616,21 @@
 th(18);%0A
-%09%09%0A
+//%0A//
 %09%09Syslog
@@ -1670,19 +1670,21 @@
 plit%22);%0A
-%09%09%0A
+//%0A//
 %09%09syslog
@@ -1708,24 +1708,26 @@
  the tim%22);%0A
+//
 %09%09assertSysl
@@ -1772,27 +1772,29 @@
 he tim%22 %7D);%0A
-%09%09%0A
+//%0A//
 %09%09syslog.inf
@@ -1815,24 +1815,26 @@
 the time%22);%0A
+//
 %09%09assertSysl
@@ -1889,27 +1889,29 @@
 ..time%22 %7D);%0A
-%09%09%0A
+//%0A//
 %09%09syslog.inf
@@ -1933,24 +1933,26 @@
 he time %22);%0A
+//
 %09%09assertSysl
@@ -2008,27 +2008,29 @@
 .time %22 %7D);%0A
-%09%09%0A
+//%0A//
 %09%09syslog.inf
@@ -2050,24 +2050,26 @@
 --------%22);%0A
+//
 %09%09syslog.flu
@@ -2074,19 +2074,21 @@
 lush();%0A
-%09%09%0A
+//%0A//
 %09%09syslog
@@ -2123,24 +2123,26 @@
 or all g%22);%0A
+//
 %09%09assertSysl
@@ -2207,27 +2207,29 @@
  all g%22 %7D);%0A
-%09%09%0A
+//%0A//
 %09%09syslog.inf
@@ -2261,24 +2261,26 @@
 r all go%22);%0A
+//
 %09%09assertSysl
@@ -2346,27 +2346,29 @@
 all go%22 %7D);%0A
-%09%09%0A
+//%0A//
 %09%09syslog.inf
@@ -2405,16 +2405,18 @@
  goo%22);%0A
+//
 %09%09assert
@@ -2501,19 +2501,21 @@
 oo%22 %7D);%0A
-%09%09%0A
+//%0A//
 %09%09syslog

disable syslog split test