1 package org.bitbucket.jrsofty.parser.logging.api; 2 3 import org.bitbucket.jrsofty.parser.logging.api.LogParserFactory.DefaultLogParserFactory; 4 import org.bitbucket.jrsofty.parser.logging.util.LogLineFormatException; 5 import org.junit.Assert; 6 import org.junit.Test; 7 8 import test.util.LogElementDupAnno; 9 import test.util.LogElementNoAnnotations; 10 import test.util.LogElementNoInterface; 11 import test.util.LogElementOtherAnnotations; 12 import test.util.LogElementTestObject; 13 import test.util.LogElementWithParameterConstructor; 14 15 public class LogParserFactoryTest { 16 17 private final String intialFormat = "%dtm{dd/MM/yyyy:HH:mm:ss Z} %ip4"; 18 private final String intialFormatExample = "[30/01/1972:11:42:00 +0200] 192.168.172.200"; 19 private final String mismatchedFormat = "%dtm{dd/MMM/yyyy:HH:mm:ss Z} %ip4 %msg"; 20 21 private final String duplicateFormat = "%url %ip4 %url"; 22 private final String duplicateFormatExample = "http://www.test1.com 192.168.172.200 http://www.test2.org"; 23 24 @Test 25 public void testDuplicateTokenParse2() 26 throws LogLineFormatException, TokenParseException, IllegalClassException { 27 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 28 final LogParser parser = factory.createParserForFormat(this.duplicateFormat, 29 LogElementDupAnno.class); 30 final LogEntry entry = parser.parseLogString(this.duplicateFormatExample); 31 final String result = ((LogElementDupAnno) entry).getUrl2(); 32 Assert.assertTrue("http://www.test2.org".equals(result)); 33 } 34 35 @Test 36 public void testDuplicateTokenParse3() 37 throws LogLineFormatException, TokenParseException, IllegalClassException { 38 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 39 final LogParser parser = factory.createParserForFormat(this.duplicateFormat, 40 LogElementDupAnno.class); 41 final LogEntry entry = parser.parseLogString(this.duplicateFormatExample); 42 final String result = ((LogElementDupAnno) entry).getIp4(); 43 Assert.assertTrue("192.168.172.200".equals(result)); 44 } 45 46 @Test 47 public void testDuplicateTokenParse1() 48 throws LogLineFormatException, TokenParseException, IllegalClassException { 49 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 50 final LogParser parser = factory.createParserForFormat(this.duplicateFormat, 51 LogElementDupAnno.class); 52 final LogEntry entry = parser.parseLogString(this.duplicateFormatExample); 53 final String result = ((LogElementDupAnno) entry).getUrl1(); 54 Assert.assertTrue("http://www.test1.com".equals(result)); 55 } 56 57 @Test(expected = TokenParseException.class) 58 public void testFailedParse() 59 throws LogLineFormatException, TokenParseException, IllegalClassException { 60 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 61 final LogParser parser = factory.createParserForFormat(this.intialFormat, 62 LogElementTestObject.class); 63 parser.parseLogString("[30-01-1972 11:42:00 +0200] 192.168.172.200"); 64 } 65 66 @Test 67 public void testParserParsing() 68 throws LogLineFormatException, TokenParseException, IllegalClassException { 69 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 70 final LogParser parser = factory.createParserForFormat(this.intialFormat, 71 LogElementTestObject.class); 72 final LogEntry entry = parser.parseLogString(this.intialFormatExample); 73 Assert.assertTrue(entry instanceof LogElementTestObject); 74 } 75 76 @Test 77 public void testParserFactoryInstance() { 78 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 79 Assert.assertTrue(factory instanceof DefaultLogParserFactory); 80 } 81 82 @Test 83 public void testDefaultParserInstance() 84 throws LogLineFormatException, ClassNotFoundException, IllegalClassException { 85 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 86 final LogParser parser = factory.createParserForFormat(this.intialFormat, 87 Class.forName(LogElementTestObject.class.getName())); 88 89 Assert.assertTrue(parser instanceof DefaultLogParser); 90 91 } 92 93 @Test(expected = IllegalClassException.class) 94 public void testMismatch() 95 throws ClassNotFoundException, LogLineFormatException, IllegalClassException { 96 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 97 factory.createParserForFormat(this.mismatchedFormat, 98 Class.forName(LogElementTestObject.class.getName())); 99 } 100 101 @Test(expected = IllegalClassException.class) 102 public void testBadClass() 103 throws ClassNotFoundException, LogLineFormatException, IllegalClassException { 104 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 105 factory.createParserForFormat(this.mismatchedFormat, 106 Class.forName(LogElementWithParameterConstructor.class.getName())); 107 } 108 109 @Test(expected = IllegalClassException.class) 110 public void testNoInterface() 111 throws ClassNotFoundException, LogLineFormatException, IllegalClassException { 112 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 113 factory.createParserForFormat(this.intialFormat, 114 Class.forName(LogElementNoInterface.class.getName())); 115 } 116 117 @Test(expected = IllegalClassException.class) 118 public void testNoAnnotations() 119 throws ClassNotFoundException, LogLineFormatException, IllegalClassException { 120 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 121 factory.createParserForFormat(this.intialFormat, 122 Class.forName(LogElementNoAnnotations.class.getName())); 123 } 124 125 @Test(expected = IllegalClassException.class) 126 public void testNullCatch() 127 throws ClassNotFoundException, LogLineFormatException, IllegalClassException { 128 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 129 factory.createParserForFormat(this.mismatchedFormat, 130 null); 131 } 132 133 @Test(expected = LogLineFormatException.class) 134 public void testNullString() 135 throws ClassNotFoundException, LogLineFormatException, IllegalClassException { 136 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 137 factory.createParserForFormat(null, 138 Class.forName(LogElementOtherAnnotations.class.getName())); 139 } 140 141 @Test(expected = LogLineFormatException.class) 142 public void testEmptyString() 143 throws ClassNotFoundException, LogLineFormatException, IllegalClassException { 144 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 145 factory.createParserForFormat("", 146 Class.forName(LogElementOtherAnnotations.class.getName())); 147 } 148 149 @Test 150 public void testOtherAnnotations() 151 throws ClassNotFoundException, LogLineFormatException, IllegalClassException { 152 final LogParserFactory factory = LogParserFactory.createDefaultInstance(); 153 final LogParser parser = factory.createParserForFormat(this.intialFormat, 154 Class.forName(LogElementOtherAnnotations.class.getName())); 155 156 Assert.assertTrue(parser instanceof DefaultLogParser); 157 } 158 159 }