Search in sources :

Example 6 with DirectoryScannerConfig

use of com.sun.jmx.examples.scandir.config.DirectoryScannerConfig in project jdk8u_jdk by JetBrains.

the class ScanDirConfigTest method testGetXmlConfigString.

/**
     * Test of saveTo method, of class com.sun.jmx.examples.scandir.ScanProfile.
     */
/*
    public void testSaveTo() throws Exception {
        System.out.println("saveTo");

        String filename = "";
        ScanDirConfig instance = null;

        instance.saveTo(filename);

        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }
    */
/**
     * Test of getXmlConfigString method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
     */
public void testGetXmlConfigString() throws Exception {
    System.out.println("getXmlConfigString");
    try {
        final File file = File.createTempFile("testconf", ".xml");
        final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
        final ScanManagerConfig bean = new ScanManagerConfig("testGetXmlConfigString");
        final DirectoryScannerConfig dir = new DirectoryScannerConfig("tmp");
        dir.setRootDirectory(file.getParent());
        bean.putScan(dir);
        instance.setConfiguration(bean);
        System.out.println("Expected: " + XmlConfigUtils.toString(bean));
        System.out.println("Received: " + instance.getConfiguration().toString());
        assertEquals(XmlConfigUtils.toString(bean), instance.getConfiguration().toString());
    } catch (Exception x) {
        x.printStackTrace();
        throw x;
    }
}
Also used : ScanManagerConfig(com.sun.jmx.examples.scandir.config.ScanManagerConfig) DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig) File(java.io.File) IOException(java.io.IOException)

Example 7 with DirectoryScannerConfig

use of com.sun.jmx.examples.scandir.config.DirectoryScannerConfig in project jdk8u_jdk by JetBrains.

the class ScanDirConfigTest method testLoad.

/**
     * Test of load method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
     */
public void testLoad() throws Exception {
    System.out.println("load");
    final File file = File.createTempFile("testconf", ".xml");
    final ScanDirConfig instance = new ScanDirConfig(file.getAbsolutePath());
    final ScanManagerConfig bean = new ScanManagerConfig("testLoad");
    final DirectoryScannerConfig dir = new DirectoryScannerConfig("tmp");
    dir.setRootDirectory(file.getParent());
    bean.putScan(dir);
    XmlConfigUtils.write(bean, new FileOutputStream(file), false);
    instance.load();
    assertEquals(bean, instance.getConfiguration());
    bean.removeScan(dir.getName());
    XmlConfigUtils.write(bean, new FileOutputStream(file), false);
    assertNotSame(bean, instance.getConfiguration());
    instance.load();
    assertEquals(bean, instance.getConfiguration());
}
Also used : ScanManagerConfig(com.sun.jmx.examples.scandir.config.ScanManagerConfig) DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 8 with DirectoryScannerConfig

use of com.sun.jmx.examples.scandir.config.DirectoryScannerConfig in project jdk8u_jdk by JetBrains.

the class ScanDirConfigTest method testSave.

/**
     * Test of save method, of class com.sun.jmx.examples.scandir.ScanDirConfig.
     */
public void testSave() throws Exception {
    System.out.println("save");
    final File file = File.createTempFile("testconf", ".xml");
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ScanManagerMXBean manager = ScanManager.register(mbs);
    try {
        final ScanDirConfigMXBean instance = manager.createOtherConfigurationMBean("testSave", file.getAbsolutePath());
        assertTrue(mbs.isRegistered(ScanManager.makeScanDirConfigName("testSave")));
        final ScanManagerConfig bean = new ScanManagerConfig("testSave");
        final DirectoryScannerConfig dir = new DirectoryScannerConfig("tmp");
        dir.setRootDirectory(file.getParent());
        bean.putScan(dir);
        instance.setConfiguration(bean);
        instance.save();
        final ScanManagerConfig loaded = new XmlConfigUtils(file.getAbsolutePath()).readFromFile();
        assertEquals(instance.getConfiguration(), loaded);
        assertEquals(bean, loaded);
        instance.getConfiguration().removeScan("tmp");
        instance.save();
        assertNotSame(loaded, instance.getConfiguration());
        final ScanManagerConfig loaded2 = new XmlConfigUtils(file.getAbsolutePath()).readFromFile();
        assertEquals(instance.getConfiguration(), loaded2);
    } finally {
        manager.close();
        mbs.unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
    }
    final ObjectName all = new ObjectName(ScanManager.SCAN_MANAGER_NAME.getDomain() + ":*");
    assertEquals(0, mbs.queryNames(all, null).size());
}
Also used : ScanManagerConfig(com.sun.jmx.examples.scandir.config.ScanManagerConfig) DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig) XmlConfigUtils(com.sun.jmx.examples.scandir.config.XmlConfigUtils) File(java.io.File)

Example 9 with DirectoryScannerConfig

use of com.sun.jmx.examples.scandir.config.DirectoryScannerConfig in project jdk8u_jdk by JetBrains.

the class DirectoryScannerTest method testScan.

/**
     * Test of scan method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
     */
public void testScan() throws Exception {
    System.out.println("scan");
    final ScanManagerMXBean manager = ScanManager.register();
    try {
        final String tmpdir = System.getProperty("java.io.tmpdir");
        final ScanDirConfigMXBean config = manager.getConfigurationMBean();
        final DirectoryScannerConfig bean = config.addDirectoryScanner("test1", tmpdir, ".*", 0, 0);
        config.addDirectoryScanner("test2", tmpdir, ".*", 0, 0);
        config.addDirectoryScanner("test3", tmpdir, ".*", 0, 0);
        manager.applyConfiguration(true);
        final DirectoryScannerMXBean proxy = manager.getDirectoryScanners().get("test1");
        final Call op = new Call() {

            public void call() throws Exception {
                final BlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
                final NotificationListener listener = new NotificationListener() {

                    public void handleNotification(Notification notification, Object handback) {
                        try {
                            queue.put(notification);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
                manager.start();
                while (true) {
                    final Notification n = queue.poll(10, TimeUnit.SECONDS);
                    if (n == null)
                        break;
                    final AttributeChangeNotification at = (AttributeChangeNotification) n;
                    if (RUNNING == ScanState.valueOf((String) at.getNewValue()))
                        break;
                    else {
                        System.err.println("New state: " + (String) at.getNewValue() + " isn't " + RUNNING);
                    }
                }
                assertContained(EnumSet.of(SCHEDULED, RUNNING, COMPLETED), proxy.getState());
            }

            public void cancel() throws Exception {
                manager.stop();
            }
        };
        doTestOperation(proxy, op, EnumSet.of(RUNNING, SCHEDULED, COMPLETED), "scan");
    } catch (Exception x) {
        x.printStackTrace();
        throw x;
    } finally {
        try {
            manager.stop();
        } catch (Exception x) {
            System.err.println("Failed to stop: " + x);
        }
        try {
            ManagementFactory.getPlatformMBeanServer().unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
        } catch (Exception x) {
            System.err.println("Failed to cleanup: " + x);
        }
    }
}
Also used : Call(com.sun.jmx.examples.scandir.ScanManagerTest.Call) AttributeChangeNotification(javax.management.AttributeChangeNotification) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Notification(javax.management.Notification) AttributeChangeNotification(javax.management.AttributeChangeNotification) DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig) NotificationListener(javax.management.NotificationListener)

Example 10 with DirectoryScannerConfig

use of com.sun.jmx.examples.scandir.config.DirectoryScannerConfig in project jdk8u_jdk by JetBrains.

the class DirectoryScannerTest method testGetState.

/**
     * Test of getState method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
     */
public void testGetState() {
    System.out.println("getState");
    final DirectoryScannerConfig bean = new DirectoryScannerConfig("test");
    bean.setRootDirectory(System.getProperty("java.io.tmpdir"));
    final ResultLogManager log = new ResultLogManager();
    DirectoryScanner instance = new DirectoryScanner(bean, log);
    ScanState expResult = STOPPED;
    ScanState result = instance.getState();
    assertEquals(STOPPED, result);
    instance.scan();
    result = instance.getState();
    assertEquals(COMPLETED, result);
}
Also used : DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig) ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)

Aggregations

DirectoryScannerConfig (com.sun.jmx.examples.scandir.config.DirectoryScannerConfig)12 File (java.io.File)6 ScanManagerConfig (com.sun.jmx.examples.scandir.config.ScanManagerConfig)5 XmlConfigUtils (com.sun.jmx.examples.scandir.config.XmlConfigUtils)3 Call (com.sun.jmx.examples.scandir.ScanManagerTest.Call)2 FileMatch (com.sun.jmx.examples.scandir.config.FileMatch)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 ScanState (com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Date (java.util.Date)1 AttributeChangeNotification (javax.management.AttributeChangeNotification)1 Notification (javax.management.Notification)1 NotificationListener (javax.management.NotificationListener)1