From c6c79306a8ece27630352cf6eca3dadcc939c62a Mon Sep 17 00:00:00 2001 From: ainilife Date: Thu, 24 Oct 2013 16:00:00 +0800 Subject: [PATCH] Refactor unit tests for transaction and event --- .../com/dianping/cat/consumer/AllTests.java | 6 + .../cat/consumer/MockReportManager.java | 22 ++-- .../cat/consumer/event/Configurator.java | 37 ++++++ .../cat/consumer/event/EventAnalyzerTest.java | 26 ++-- .../consumer/transaction/Configurator.java | 37 ++++++ .../transaction/TransactionAnalyzerTest.java | 35 +++-- .../TransactionReportFilterTest.java | 2 +- .../transaction/TransactionReportTest.java | 3 +- .../cat/consumer/event/EventAnalyzerTest.xml | 44 +++---- .../cat/consumer/event/event_analyzer.xml | 21 +++ .../transaction/TransactionAnalyzerTest.xml | 124 +++--------------- .../cat/consumer/transaction/transaction.xml | 106 --------------- .../transaction/transaction_analyzer.xml | 86 ++++++++++++ ...ionReport.html => transaction_report.html} | 0 ...ionReport.json => transaction_report.json} | 0 ...ctionReport.xml => transaction_report.xml} | 0 ...lter.xml => transaction_report_filter.xml} | 0 17 files changed, 268 insertions(+), 281 deletions(-) create mode 100644 cat-consumer/src/test/java/com/dianping/cat/consumer/event/Configurator.java create mode 100644 cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/Configurator.java create mode 100644 cat-consumer/src/test/resources/com/dianping/cat/consumer/event/event_analyzer.xml delete mode 100644 cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction.xml create mode 100644 cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_analyzer.xml rename cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/{TransactionReport.html => transaction_report.html} (100%) rename cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/{TransactionReport.json => transaction_report.json} (100%) rename cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/{TransactionReport.xml => transaction_report.xml} (100%) rename cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/{transactionFilter.xml => transaction_report_filter.xml} (100%) diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/AllTests.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/AllTests.java index c6b082e5b..2960772c8 100644 --- a/cat-consumer/src/test/java/com/dianping/cat/consumer/AllTests.java +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/AllTests.java @@ -11,6 +11,7 @@ import com.dianping.cat.consumer.core.ProblemHandlerTest; import com.dianping.cat.consumer.core.ProblemReportAggregationTest; import com.dianping.cat.consumer.core.aggregation.CompositeFormatTest; import com.dianping.cat.consumer.core.aggregation.DefaultFormatTest; +import com.dianping.cat.consumer.event.EventAnalyzerTest; import com.dianping.cat.consumer.transaction.TransactionAnalyzerTest; import com.dianping.cat.consumer.transaction.TransactionReportFilterTest; import com.dianping.cat.consumer.transaction.TransactionReportTest; @@ -28,12 +29,17 @@ GsonTest.class, NumberFormatTest.class, +/* transaction */ + TransactionAnalyzerTest.class, TransactionReportTest.class, TransactionReportFilterTest.class, +/* event */ +EventAnalyzerTest.class, + CompositeFormatTest.class, DefaultFormatTest.class, diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/MockReportManager.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/MockReportManager.java index 0c692698f..8b9a0c3b7 100644 --- a/cat-consumer/src/test/java/com/dianping/cat/consumer/MockReportManager.java +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/MockReportManager.java @@ -4,6 +4,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.unidal.lookup.annotation.Inject; + import com.dianping.cat.Constants; import com.dianping.cat.service.DefaultReportManager.StoragePolicy; import com.dianping.cat.service.ReportDelegate; @@ -11,18 +13,12 @@ import com.dianping.cat.service.ReportManager; public class MockReportManager implements ReportManager { + @Inject private ReportDelegate m_delegate; private T m_report; - + private Set m_set; - - public MockReportManager(ReportDelegate delegate,String domain) { - m_delegate = delegate; - m_set = new HashSet(); - - m_set.add(domain); - } @Override public void cleanup() { @@ -34,15 +30,21 @@ public class MockReportManager implements ReportManager { @Override public Set getDomains(long startTime) { + if (m_set == null) { + m_set = new HashSet(); + + m_set.add("group"); + } + return m_set; } @Override public T getHourlyReport(long startTime, String domain, boolean createIfNotExist) { - if(m_report == null){ + if (m_report == null) { m_report = m_delegate.makeReport(domain, startTime, Constants.HOUR); } - + return m_report; } diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/event/Configurator.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/event/Configurator.java new file mode 100644 index 000000000..eb5de39e8 --- /dev/null +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/event/Configurator.java @@ -0,0 +1,37 @@ +package com.dianping.cat.consumer.event; + +import java.util.ArrayList; +import java.util.List; + +import org.unidal.lookup.configuration.AbstractResourceConfigurator; +import org.unidal.lookup.configuration.Component; + +import com.dianping.cat.consumer.MockReportManager; +import com.dianping.cat.service.ReportDelegate; +import com.dianping.cat.service.ReportManager; + +public class Configurator extends AbstractResourceConfigurator { + + public static void main(String[] args) { + generatePlexusComponentsXmlFile(new Configurator()); + } + + protected Class getTestClass() { + return EventAnalyzerTest.class; + } + + @Override + public List defineComponents() { + List all = new ArrayList(); + final String ID = EventAnalyzer.ID; + + all.add(C(ReportManager.class, ID, MockReportManager.class)// + .req(ReportDelegate.class, ID, "m_delegate")); + all.add(C(ReportDelegate.class, ID, ExtendedEventDelegate.class)); + + return all; + } + + public static class ExtendedEventDelegate extends EventDelegate{ + } +} diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/event/EventAnalyzerTest.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/event/EventAnalyzerTest.java index db70b440d..14290fb11 100644 --- a/cat-consumer/src/test/java/com/dianping/cat/consumer/event/EventAnalyzerTest.java +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/event/EventAnalyzerTest.java @@ -7,15 +7,13 @@ import org.junit.Test; import org.unidal.helper.Files; import org.unidal.lookup.ComponentTestCase; -import com.dianping.cat.consumer.MockReportManager; +import com.dianping.cat.analysis.MessageAnalyzer; import com.dianping.cat.consumer.event.model.entity.EventReport; import com.dianping.cat.message.Message; import com.dianping.cat.message.internal.DefaultEvent; import com.dianping.cat.message.internal.DefaultTransaction; import com.dianping.cat.message.spi.MessageTree; import com.dianping.cat.message.spi.internal.DefaultMessageTree; -import com.dianping.cat.service.ReportDelegate; -import com.dianping.cat.service.ReportManager; public class EventAnalyzerTest extends ComponentTestCase { @@ -26,32 +24,28 @@ public class EventAnalyzerTest extends ComponentTestCase { private String m_domain = "group"; @Before - public void init() { + public void setUp() throws Exception { + super.setUp(); m_timestamp = System.currentTimeMillis() - System.currentTimeMillis() % (3600 * 1000); - ReportDelegate deleaate = new EventDelegate(); - ReportManager reportManager = new MockReportManager(deleaate, m_domain); - - m_analyzer = new EventAnalyzer(); - m_analyzer.setReportManager(reportManager); + m_analyzer = (EventAnalyzer) lookup(MessageAnalyzer.class, EventAnalyzer.ID); } @Test - public void testProcessTransaction() throws Exception { - + public void testProcess() throws Exception { for (int i = 1; i <= 1000; i++) { - MessageTree tree = newMessageTree(i); + MessageTree tree = generateMessageTree(i); m_analyzer.process(tree); } EventReport report = m_analyzer.getReport(m_domain); - - String expected = Files.forIO().readFrom(getClass().getResourceAsStream("EventAnalyzerTest.xml"), "utf-8"); - Assert.assertEquals(expected.replaceAll("\\s*", ""), report.toString().replaceAll("\\s*", "")); + + String expected = Files.forIO().readFrom(getClass().getResourceAsStream("event_analyzer.xml"), "utf-8"); + Assert.assertEquals(expected.replaceAll("\r", ""), report.toString().replaceAll("\r", "")); } - protected MessageTree newMessageTree(int i) { + protected MessageTree generateMessageTree(int i) { MessageTree tree = new DefaultMessageTree(); tree.setMessageId("" + i); diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/Configurator.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/Configurator.java new file mode 100644 index 000000000..c90ca019a --- /dev/null +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/Configurator.java @@ -0,0 +1,37 @@ +package com.dianping.cat.consumer.transaction; + +import java.util.ArrayList; +import java.util.List; + +import org.unidal.lookup.configuration.AbstractResourceConfigurator; +import org.unidal.lookup.configuration.Component; + +import com.dianping.cat.consumer.MockReportManager; +import com.dianping.cat.service.ReportDelegate; +import com.dianping.cat.service.ReportManager; + +public class Configurator extends AbstractResourceConfigurator { + + public static void main(String[] args) { + generatePlexusComponentsXmlFile(new Configurator()); + } + + protected Class getTestClass() { + return TransactionAnalyzerTest.class; + } + + @Override + public List defineComponents() { + List all = new ArrayList(); + final String ID = TransactionAnalyzer.ID; + + all.add(C(ReportManager.class, ID, MockReportManager.class)// + .req(ReportDelegate.class, ID, "m_delegate")); + all.add(C(ReportDelegate.class, ID, ExtendedTransactionDelegate.class)); + + return all; + } + + public static class ExtendedTransactionDelegate extends TransactionDelegate { + } +} diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionAnalyzerTest.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionAnalyzerTest.java index f3a730557..21c98ff38 100644 --- a/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionAnalyzerTest.java +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionAnalyzerTest.java @@ -7,15 +7,12 @@ import org.junit.Test; import org.unidal.helper.Files; import org.unidal.lookup.ComponentTestCase; -import com.dianping.cat.ServerConfigManager; -import com.dianping.cat.consumer.MockReportManager; +import com.dianping.cat.analysis.MessageAnalyzer; import com.dianping.cat.consumer.transaction.model.entity.TransactionReport; import com.dianping.cat.message.Message; import com.dianping.cat.message.internal.DefaultTransaction; import com.dianping.cat.message.spi.MessageTree; import com.dianping.cat.message.spi.internal.DefaultMessageTree; -import com.dianping.cat.service.ReportDelegate; -import com.dianping.cat.service.ReportManager; public class TransactionAnalyzerTest extends ComponentTestCase { private long m_timestamp; @@ -25,25 +22,22 @@ public class TransactionAnalyzerTest extends ComponentTestCase { private String m_domain = "group"; @Before - public void init() { - m_timestamp = System.currentTimeMillis() - System.currentTimeMillis() % (3600 * 1000); + public void setUp() throws Exception { + super.setUp(); - ReportDelegate transactionDelegate = new TransactionDelegate(); - ServerConfigManager serverConfigManager = new ServerConfigManager(); - ReportManager reportManager = new MockReportManager(transactionDelegate, - m_domain); + m_timestamp = System.currentTimeMillis() - System.currentTimeMillis() % (3600 * 1000); - m_analyzer = new TransactionAnalyzer(); - m_analyzer.setDelegate((TransactionDelegate) transactionDelegate); - m_analyzer.setReportManager(reportManager); - m_analyzer.setServerConfigManager(serverConfigManager); + try { + m_analyzer = (TransactionAnalyzer) lookup(MessageAnalyzer.class, TransactionAnalyzer.ID); + } catch (Exception e) { + e.printStackTrace(); + } } @Test - public void testProcessTransaction() throws Exception { - + public void testProcess() throws Exception { for (int i = 1; i <= 1000; i++) { - MessageTree tree = newMessageTree(i); + MessageTree tree = generateMessageTree(i); m_analyzer.process(tree); } @@ -52,11 +46,12 @@ public class TransactionAnalyzerTest extends ComponentTestCase { report.accept(new TransactionStatisticsComputer()); - String expected = Files.forIO().readFrom(getClass().getResourceAsStream("TransactionAnalyzerTest.xml"), "utf-8"); - Assert.assertEquals(expected.replaceAll("\\s*", ""), report.toString().replaceAll("\\s*", "")); + String expected = Files.forIO().readFrom(getClass().getResourceAsStream("transaction_analyzer.xml"), + "utf-8"); + Assert.assertEquals(expected.replaceAll("\r", ""), report.toString().replaceAll("\r", "")); } - protected MessageTree newMessageTree(int i) { + protected MessageTree generateMessageTree(int i) { MessageTree tree = new DefaultMessageTree(); tree.setMessageId("" + i); diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionReportFilterTest.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionReportFilterTest.java index 31bd933db..92239c43d 100644 --- a/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionReportFilterTest.java +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionReportFilterTest.java @@ -16,7 +16,7 @@ public class TransactionReportFilterTest { @Test public void whether_url_has_max_names() throws Exception { - String source = Files.forIO().readFrom(getClass().getResourceAsStream("transaction.xml"), "utf-8"); + String source = Files.forIO().readFrom(getClass().getResourceAsStream("transaction_report_filter.xml"), "utf-8"); TransactionReport report = DefaultSaxParser.parse(source); TransactionType type = report.findMachine("10.1.77.193").findType("URL"); diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionReportTest.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionReportTest.java index eb54fe1e4..66f693abc 100644 --- a/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionReportTest.java +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/transaction/TransactionReportTest.java @@ -11,12 +11,11 @@ import com.dianping.cat.consumer.transaction.model.transform.DefaultXmlBuilder; public class TransactionReportTest { @Test public void testXml() throws Exception { - String source = Files.forIO().readFrom(getClass().getResourceAsStream("TransactionReport.xml"), "utf-8"); + String source = Files.forIO().readFrom(getClass().getResourceAsStream("transaction_report.xml"), "utf-8"); TransactionReport report = DefaultSaxParser.parse(source); String xml = new DefaultXmlBuilder().buildXml(report); String expected = source; Assert.assertEquals("XML is not well parsed!", expected.replace("\r", ""), xml.replace("\r", "")); } - } diff --git a/cat-consumer/src/test/resources/com/dianping/cat/consumer/event/EventAnalyzerTest.xml b/cat-consumer/src/test/resources/com/dianping/cat/consumer/event/EventAnalyzerTest.xml index e78ba7eee..98ed25627 100644 --- a/cat-consumer/src/test/resources/com/dianping/cat/consumer/event/EventAnalyzerTest.xml +++ b/cat-consumer/src/test/resources/com/dianping/cat/consumer/event/EventAnalyzerTest.xml @@ -1,23 +1,21 @@ - - - group - 192.168.1.1 - - - 1 - - 1 - - - - - 1 - - 1 - - - - - - + + + + com.dianping.cat.service.ReportManager + event + com.dianping.cat.consumer.MockReportManager + + + com.dianping.cat.service.ReportDelegate + event + m_delegate + + + + + com.dianping.cat.service.ReportDelegate + event + com.dianping.cat.consumer.event.Configurator$ExtendedEventDelegate + + + diff --git a/cat-consumer/src/test/resources/com/dianping/cat/consumer/event/event_analyzer.xml b/cat-consumer/src/test/resources/com/dianping/cat/consumer/event/event_analyzer.xml new file mode 100644 index 000000000..936d31f9b --- /dev/null +++ b/cat-consumer/src/test/resources/com/dianping/cat/consumer/event/event_analyzer.xml @@ -0,0 +1,21 @@ + + + group + 192.168.1.1 + + + 1 + + 1 + + + + + 1 + + 1 + + + + + diff --git a/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/TransactionAnalyzerTest.xml b/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/TransactionAnalyzerTest.xml index 12c8ac996..cc05c1e41 100644 --- a/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/TransactionAnalyzerTest.xml +++ b/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/TransactionAnalyzerTest.xml @@ -1,103 +1,21 @@ - - - group - 192.168.1.1 - - - 1 - 2 - - 1 - - - - - - - - - - - - - - 2 - - - - - - - - - - - - - - - 1 - 2 - - 1 - 4 - - - - - - - - - - - - - - 5 - 2 - - - - - - - - - - - - - 3 - 6 - - - - - - - - - - - - - - \ No newline at end of file + + + + com.dianping.cat.service.ReportManager + transaction + com.dianping.cat.consumer.MockReportManager + + + com.dianping.cat.service.ReportDelegate + transaction + m_delegate + + + + + com.dianping.cat.service.ReportDelegate + transaction + com.dianping.cat.consumer.transaction.Configurator$ExtendedTransactionDelegate + + + diff --git a/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction.xml b/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction.xml deleted file mode 100644 index a3e2ac7e6..000000000 --- a/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction.xml +++ /dev/null @@ -1,106 +0,0 @@ - - Cat - MobileApi - 10.1.77.193 - - - MobileApi-0a014dc1-1339999254922-0 - - - MobileApi-0a014dc1-1340002657975-0 - - - - - - MobileApi-0a014dc1-1340002658121-0 - - - - - - - MobileApi-0a014dc1-1339999960694-0 - - - MobileApi-0a014dc1-1339999960694-0 - - - - - - MobileApi-0a014dc1-1339999960753-0 - - - - - - - MobileApi-0a014dc1-1339999960794-0 - - - - - - MobileApi-0a014dc1-1339999961753-0 - - - - - - MobileApi-0a014dc1-1339999968913-0 - - - - - - MobileApi-0a014dc1-1339999994494-0 - - - - - - MobileApi-0a014dc1-1340000001993-0 - - - - - - - - - \ No newline at end of file diff --git a/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_analyzer.xml b/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_analyzer.xml new file mode 100644 index 000000000..28782043a --- /dev/null +++ b/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_analyzer.xml @@ -0,0 +1,86 @@ + + + group + 192.168.1.1 + + + 1 + 2 + + 1 + + + + + + + + + + + + + + 2 + + + + + + + + + + + + + + + 1 + 2 + + 1 + 4 + + + + + + + + + + + + + + 5 + 2 + + + + + + + + + + + + + 3 + 6 + + + + + + + + + + + + + + diff --git a/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/TransactionReport.html b/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_report.html similarity index 100% rename from cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/TransactionReport.html rename to cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_report.html diff --git a/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/TransactionReport.json b/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_report.json similarity index 100% rename from cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/TransactionReport.json rename to cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_report.json diff --git a/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/TransactionReport.xml b/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_report.xml similarity index 100% rename from cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/TransactionReport.xml rename to cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_report.xml diff --git a/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transactionFilter.xml b/cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_report_filter.xml similarity index 100% rename from cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transactionFilter.xml rename to cat-consumer/src/test/resources/com/dianping/cat/consumer/transaction/transaction_report_filter.xml -- GitLab