View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package cz.cuni.amis.pogamut.base.utils.logging.jmx;
6   
7   import static org.junit.Assert.assertTrue;
8   
9   import java.io.IOException;
10  import java.net.MalformedURLException;
11  import java.util.concurrent.CountDownLatch;
12  import java.util.concurrent.TimeUnit;
13  
14  import javax.management.AttributeNotFoundException;
15  import javax.management.InstanceAlreadyExistsException;
16  import javax.management.InstanceNotFoundException;
17  import javax.management.MBeanException;
18  import javax.management.MBeanRegistrationException;
19  import javax.management.MBeanServerConnection;
20  import javax.management.MalformedObjectNameException;
21  import javax.management.NotCompliantMBeanException;
22  import javax.management.Notification;
23  import javax.management.NotificationListener;
24  import javax.management.ObjectName;
25  import javax.management.ReflectionException;
26  
27  import junit.framework.Assert;
28  
29  import org.junit.After;
30  import org.junit.AfterClass;
31  import org.junit.Before;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  
35  import cz.cuni.amis.pogamut.base.utils.Pogamut;
36  import cz.cuni.amis.pogamut.base.utils.jmx.PogamutJMX;
37  import cz.cuni.amis.pogamut.base.utils.logging.ILogCategories;
38  import cz.cuni.amis.pogamut.base.utils.logging.LogCategories;
39  import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
40  import cz.cuni.amis.utils.exception.PogamutException;
41  import java.util.logging.Level;
42  
43  /**
44   * Exports ILogCategories through JMX and then tests receiving messages over the
45   * JMX.
46   * 
47   * @author Ik
48   */
49  import cz.cuni.amis.tests.BaseTest;
50  				
51  public class Test01_JMXLogCategories extends BaseTest {
52  
53  	public Test01_JMXLogCategories() {
54  	}
55  
56  	@BeforeClass
57  	public static void setUpClass() throws Exception {
58  	}
59  
60  	@AfterClass
61  	public static void tearDownClass() throws Exception {
62  	}
63  
64  	@Before
65  	public void setUp() {
66  	}
67  
68  	@After
69  	public void tearDown() {
70  	}
71  
72  	CountDownLatch messageReceivedLatch = new CountDownLatch(1);
73  	String receivedMessage = null;
74  
75  	// TODO add test methods here.
76  	// The methods must be annotated with annotation @Test. For example:
77  	//
78  	@Test
79  	public void receiveLogEventThroughJMX()
80  			throws InstanceAlreadyExistsException, MBeanRegistrationException,
81  			NotCompliantMBeanException, MalformedObjectNameException,
82  			PogamutException, MalformedURLException, IOException,
83  			InterruptedException, MBeanException, InstanceNotFoundException,
84  			AttributeNotFoundException, ReflectionException {
85  		
86  		final String testMsg = "TEST LOG MESSAGE FROM receiveLogEventThroughJMX()";
87  		
88  		ObjectName parentName = PogamutJMX.getObjectName("testDomain", "root", "test");
89  		
90  		
91  		// export the log on the MBean server
92  		ILogCategories logCategories = new LogCategories();
93  		
94  		String testLogCategoryNameStr = "testLogCategory";
95  		LogCategory testLog = logCategories.getCategory(testLogCategoryNameStr);
96  				
97  		JMXLogCategories jmxLogCategories = new JMXLogCategories(
98  				logCategories,
99  				Pogamut.getPlatform().getMBeanServer(),
100 				parentName
101 		);
102 
103 		// connect through RMI and get the proxy
104 		MBeanServerConnection mbsc = Pogamut.getPlatform().getMBeanServerConnection();
105 		ObjectName logCatsName = jmxLogCategories.getJMXLogCategoriesName();
106 		
107 		// get the name of all log category names
108 		String[] catNames = (String[]) mbsc.getAttribute(logCatsName, "CategoryNames");		
109 		boolean found = false;
110 		for (String catName : catNames) {
111 			if (catName.equals(testLogCategoryNameStr)) {
112 				found = true;
113 				break;
114 			}
115 		}
116 		Assert.assertTrue(testLogCategoryNameStr + " must be among exported log category names", found);
117 		
118 		// get the object name for the test log category
119 		ObjectName testCategoryName = 
120 			(ObjectName) 
121 			mbsc.invoke(
122 				logCatsName, 
123 				"getJMXLogCategoryName", 
124 				new Object[]{ testLogCategoryNameStr }, 
125 				new String[] { "java.lang.String" }
126 			);
127 		
128 		// add the listener
129 		mbsc.addNotificationListener(testCategoryName,
130 			new NotificationListener() {
131 
132 				public void handleNotification(Notification notification,
133 						Object handback) {
134 					receivedMessage = notification.getMessage();
135 					messageReceivedLatch.countDown();
136 				}
137 			}, null, this
138 		);
139 
140 		// send log event
141 		if (testLog.isLoggable(Level.INFO)) testLog.info(testMsg);
142 		// wait
143 		messageReceivedLatch.await(30000, TimeUnit.MILLISECONDS);
144 		// compare
145 		assertTrue("Received message must contain testMsg", receivedMessage.contains(testMsg));
146 		
147 		System.out.println("---/// TEST OK ///---");
148 		
149 		Pogamut.getPlatform().close();
150 	}
151 }