Search in sources :

Example 76 with LogChannelInterface

use of org.pentaho.di.core.logging.LogChannelInterface in project pentaho-kettle by pentaho.

the class GetJobStatusServletTest method testGetJobStatus.

@Test
@PrepareForTest({ Job.class })
public void testGetJobStatus() throws ServletException, IOException {
    KettleLogStore.init();
    CarteStatusCache cacheMock = mock(CarteStatusCache.class);
    getJobStatusServlet.cache = cacheMock;
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    HttpServletResponse mockHttpServletResponse = mock(HttpServletResponse.class);
    Job mockJob = PowerMockito.mock(Job.class);
    JobMeta mockJobMeta = mock(JobMeta.class);
    LogChannelInterface mockLogChannelInterface = mock(LogChannelInterface.class);
    ServletOutputStream outMock = mock(ServletOutputStream.class);
    StringWriter out = new StringWriter();
    PrintWriter printWriter = new PrintWriter(out);
    String id = "123";
    String logId = "logId";
    String useXml = "Y";
    String name = "dummyName";
    when(mockHttpServletRequest.getContextPath()).thenReturn(GetJobStatusServlet.CONTEXT_PATH);
    when(mockHttpServletRequest.getParameter("id")).thenReturn(id);
    when(mockHttpServletRequest.getParameter("xml")).thenReturn(useXml);
    when(mockHttpServletRequest.getParameter("name")).thenReturn(ServletTestUtils.BAD_STRING_TO_TEST);
    when(mockHttpServletResponse.getOutputStream()).thenReturn(outMock);
    when(mockHttpServletResponse.getWriter()).thenReturn(printWriter);
    when(mockJobMap.getJob((CarteObjectEntry) any())).thenReturn(mockJob);
    PowerMockito.when(mockJob.getJobname()).thenReturn(ServletTestUtils.BAD_STRING_TO_TEST);
    PowerMockito.when(mockJob.getLogChannel()).thenReturn(mockLogChannelInterface);
    PowerMockito.when(mockJob.getJobMeta()).thenReturn(mockJobMeta);
    PowerMockito.when(mockJob.isFinished()).thenReturn(true);
    PowerMockito.when(mockJob.getLogChannelId()).thenReturn(logId);
    PowerMockito.when(mockJobMeta.getMaximum()).thenReturn(new Point(10, 10));
    when(mockJob.getStatus()).thenReturn("Finished");
    getJobStatusServlet.doGet(mockHttpServletRequest, mockHttpServletResponse);
    when(cacheMock.get(logId, 0)).thenReturn(new byte[] { 0, 1, 2 });
    getJobStatusServlet.doGet(mockHttpServletRequest, mockHttpServletResponse);
    verify(cacheMock, times(2)).get(logId, 0);
    verify(cacheMock, times(1)).put(eq(logId), anyString(), eq(0));
    verify(mockJob, times(1)).getLogChannel();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JobMeta(org.pentaho.di.job.JobMeta) StringWriter(java.io.StringWriter) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) CarteStatusCache(org.pentaho.di.www.cache.CarteStatusCache) Matchers.anyString(org.mockito.Matchers.anyString) Point(org.pentaho.di.core.gui.Point) Job(org.pentaho.di.job.Job) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) PrintWriter(java.io.PrintWriter) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 77 with LogChannelInterface

use of org.pentaho.di.core.logging.LogChannelInterface in project pentaho-kettle by pentaho.

the class GetJobStatusServletTest method testGetJobStatusServletEscapesHtmlWhenTransFound.

@Test
@PrepareForTest({ Encode.class, Job.class })
public void testGetJobStatusServletEscapesHtmlWhenTransFound() throws ServletException, IOException {
    KettleLogStore.init();
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    HttpServletResponse mockHttpServletResponse = mock(HttpServletResponse.class);
    Job mockJob = PowerMockito.mock(Job.class);
    JobMeta mockJobMeta = mock(JobMeta.class);
    LogChannelInterface mockLogChannelInterface = mock(LogChannelInterface.class);
    StringWriter out = new StringWriter();
    PrintWriter printWriter = new PrintWriter(out);
    PowerMockito.spy(Encode.class);
    when(mockHttpServletRequest.getContextPath()).thenReturn(GetJobStatusServlet.CONTEXT_PATH);
    when(mockHttpServletRequest.getParameter(anyString())).thenReturn(ServletTestUtils.BAD_STRING_TO_TEST);
    when(mockHttpServletResponse.getWriter()).thenReturn(printWriter);
    when(mockJobMap.getJob(any(CarteObjectEntry.class))).thenReturn(mockJob);
    PowerMockito.when(mockJob.getJobname()).thenReturn(ServletTestUtils.BAD_STRING_TO_TEST);
    PowerMockito.when(mockJob.getLogChannel()).thenReturn(mockLogChannelInterface);
    PowerMockito.when(mockJob.getJobMeta()).thenReturn(mockJobMeta);
    PowerMockito.when(mockJobMeta.getMaximum()).thenReturn(new Point(10, 10));
    getJobStatusServlet.doGet(mockHttpServletRequest, mockHttpServletResponse);
    assertFalse(out.toString().contains(ServletTestUtils.BAD_STRING_TO_TEST));
    PowerMockito.verifyStatic(atLeastOnce());
    Encode.forHtml(anyString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JobMeta(org.pentaho.di.job.JobMeta) StringWriter(java.io.StringWriter) HttpServletResponse(javax.servlet.http.HttpServletResponse) Point(org.pentaho.di.core.gui.Point) Job(org.pentaho.di.job.Job) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) PrintWriter(java.io.PrintWriter) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 78 with LogChannelInterface

use of org.pentaho.di.core.logging.LogChannelInterface in project pentaho-kettle by pentaho.

the class ExtensionPointHandlerTest method callExtensionPointTest.

@Test
public void callExtensionPointTest() throws Exception {
    PluginMockInterface pluginInterface = mock(PluginMockInterface.class);
    when(pluginInterface.getName()).thenReturn(TEST_NAME);
    when(pluginInterface.getMainType()).thenReturn((Class) ExtensionPointInterface.class);
    when(pluginInterface.getIds()).thenReturn(new String[] { "testID" });
    ExtensionPointInterface extensionPoint = mock(ExtensionPointInterface.class);
    when(pluginInterface.loadClass(ExtensionPointInterface.class)).thenReturn(extensionPoint);
    PluginRegistry.addPluginType(ExtensionPointPluginType.getInstance());
    PluginRegistry.getInstance().registerPlugin(ExtensionPointPluginType.class, pluginInterface);
    final LogChannelInterface log = mock(LogChannelInterface.class);
    ExtensionPointHandler.callExtensionPoint(log, "noPoint", null);
    verify(extensionPoint, never()).callExtensionPoint(any(LogChannelInterface.class), any());
    ExtensionPointHandler.callExtensionPoint(log, TEST_NAME, null);
    verify(extensionPoint, times(1)).callExtensionPoint(eq(log), isNull());
}
Also used : LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) Test(org.junit.Test)

Example 79 with LogChannelInterface

use of org.pentaho.di.core.logging.LogChannelInterface in project pentaho-kettle by pentaho.

the class LogBrowser method installLogSniffer.

public void installLogSniffer() {
    // Create a new buffer appender to the log and capture that directly...
    // 
    final AtomicInteger lastLogId = new AtomicInteger(-1);
    final AtomicBoolean busy = new AtomicBoolean(false);
    final KettleLogLayout logLayout = new KettleLogLayout(true);
    final Color colorBlue = GUIResource.getInstance().getColorBlue();
    final Color colorRed = GUIResource.getInstance().getColorRed();
    final int LOG_LINE_ESTIMATED_SIZE = 150;
    final int MAX_NR_LOG_LINES_CHUNK = 150;
    // Refresh the log every second or so
    // 
    final Timer logRefreshTimer = new Timer("log sniffer Timer");
    TimerTask timerTask = new TimerTask() {

        public void run() {
            if (text.isDisposed() || text.getDisplay().isDisposed()) {
                return;
            }
            text.getDisplay().asyncExec(new Runnable() {

                public void run() {
                    HasLogChannelInterface provider = logProvider.getLogChannelProvider();
                    if (provider != null && !text.isDisposed() && !busy.get() && !paused.get() && text.isVisible()) {
                        busy.set(true);
                        LogChannelInterface logChannel = provider.getLogChannel();
                        String parentLogChannelId = logChannel.getLogChannelId();
                        LoggingRegistry registry = LoggingRegistry.getInstance();
                        Date registryModDate = registry.getLastModificationTime();
                        if (childIds == null || lastLogRegistryChange == null || registryModDate.compareTo(lastLogRegistryChange) > 0) {
                            lastLogRegistryChange = registry.getLastModificationTime();
                            childIds = LoggingRegistry.getInstance().getLogChannelChildren(parentLogChannelId);
                        }
                        // See if we need to log any lines...
                        // 
                        int lastNr = KettleLogStore.getLastBufferLineNr();
                        if (lastNr > lastLogId.get()) {
                            // Sometimes there're so many lines to log at one time that the application goes unresponsive
                            // until all log entries are handled...
                            // Let's limit the lines handled, leaving the rest for the next time the timer fires...
                            lastNr = Math.min(lastNr, lastLogId.get() + MAX_NR_LOG_LINES_CHUNK);
                            List<KettleLoggingEvent> logLines = KettleLogStore.getLogBufferFromTo(childIds, true, lastLogId.get(), lastNr);
                            synchronized (text) {
                                for (KettleLoggingEvent event : logLines) {
                                    String line = logLayout.format(event).trim();
                                    int start = text.getText().length();
                                    int length = line.length();
                                    if (length > 0) {
                                        Format format = TextFormatter.getInstance().execute(line);
                                        String theRealText = format.getText();
                                        // The formatting may have change the original text and its length
                                        length = theRealText.length();
                                        text.append(theRealText);
                                        text.append(Const.CR);
                                        for (StyleRange styleRange : format.getStyleRanges()) {
                                            styleRange.start += start;
                                            text.setStyleRange(styleRange);
                                        }
                                        StyleRange styleRange = new StyleRange();
                                        styleRange.start = start;
                                        if (event.getLevel() == LogLevel.ERROR) {
                                            styleRange.foreground = colorRed;
                                            styleRange.length = length;
                                        } else {
                                            styleRange.foreground = colorBlue;
                                            styleRange.length = Math.min(20, length);
                                        }
                                        text.setStyleRange(styleRange);
                                    }
                                }
                            }
                            // The maximum size of the log buffer
                            // 
                            int maxSize = Props.getInstance().getMaxNrLinesInLog() * LOG_LINE_ESTIMATED_SIZE;
                            // Erase it all in one go
                            // This makes it a bit more efficient
                            // 
                            int size = text.getText().length();
                            if (maxSize > 0 && size > maxSize) {
                                int dropIndex = (text.getText().indexOf(Const.CR, size - maxSize)) + Const.CR.length();
                                text.replaceTextRange(0, dropIndex, "");
                            }
                            text.setSelection(text.getText().length());
                            lastLogId.set(lastNr);
                        }
                        busy.set(false);
                    }
                }
            });
        }
    };
    // Refresh every often enough
    // 
    logRefreshTimer.schedule(timerTask, Const.toInt(EnvUtil.getSystemProperty(Const.KETTLE_LOG_TAB_REFRESH_DELAY), 1000), Const.toInt(EnvUtil.getSystemProperty(Const.KETTLE_LOG_TAB_REFRESH_PERIOD), 1000));
    text.addListener(SWT.MouseDown, e -> {
        try {
            int offset = text.getOffsetAtLocation(new Point(e.x, e.y));
            StyleRange style = text.getStyleRangeAtOffset(offset);
            if (style != null && style.underline && style.underlineStyle == SWT.UNDERLINE_LINK) {
                if (Desktop.isDesktopSupported()) {
                    Desktop.getDesktop().browse(new URI((String) style.data));
                }
            }
        } catch (Exception ex) {
        // no character under event.x, event.y
        }
    });
    // Make sure the timer goes down when the widget is disposed
    // 
    text.addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent event) {
            logRefreshTimer.cancel();
        }
    });
    final Menu menu = new Menu(text);
    MenuItem item = new MenuItem(menu, SWT.NONE);
    item.setText(BaseMessages.getString(PKG, "LogBrowser.CopySelectionToClipboard.MenuItem"));
    item.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            String selection = text.getSelectionText();
            if (!Utils.isEmpty(selection)) {
                GUIResource.getInstance().toClipboard(selection);
            }
        }
    });
    text.setMenu(menu);
    text.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDown(MouseEvent event) {
            if (event.button == 3) {
                ConstUI.displayMenu(menu, text);
            }
        }
    });
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) LoggingRegistry(org.pentaho.di.core.logging.LoggingRegistry) StyleRange(org.eclipse.swt.custom.StyleRange) HasLogChannelInterface(org.pentaho.di.core.logging.HasLogChannelInterface) DisposeEvent(org.eclipse.swt.events.DisposeEvent) URI(java.net.URI) Format(org.pentaho.di.ui.core.widget.text.Format) TimerTask(java.util.TimerTask) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) List(java.util.List) KettleLoggingEvent(org.pentaho.di.core.logging.KettleLoggingEvent) Menu(org.eclipse.swt.widgets.Menu) MouseEvent(org.eclipse.swt.events.MouseEvent) Color(org.eclipse.swt.graphics.Color) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MouseAdapter(org.eclipse.swt.events.MouseAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) KettleLogLayout(org.pentaho.di.core.logging.KettleLogLayout) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) Date(java.util.Date) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Timer(java.util.Timer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HasLogChannelInterface(org.pentaho.di.core.logging.HasLogChannelInterface) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface)

Example 80 with LogChannelInterface

use of org.pentaho.di.core.logging.LogChannelInterface in project pentaho-kettle by pentaho.

the class RegisterPackageServletIT method setup.

@Before
public void setup() {
    servlet = new RegisterPackageServlet();
    LogChannelInterface logChannelInterface = mock(LogChannelInterface.class);
    servlet.setup(new TransformationMap(), new JobMap(), new SocketRepository(logChannelInterface), new ArrayList<>());
}
Also used : LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) Before(org.junit.Before)

Aggregations

LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)85 Test (org.junit.Test)53 PrintWriter (java.io.PrintWriter)29 TransMeta (org.pentaho.di.trans.TransMeta)29 HttpServletRequest (javax.servlet.http.HttpServletRequest)27 HttpServletResponse (javax.servlet.http.HttpServletResponse)27 StringWriter (java.io.StringWriter)26 Point (org.pentaho.di.core.gui.Point)26 Trans (org.pentaho.di.trans.Trans)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 Job (org.pentaho.di.job.Job)17 JobMeta (org.pentaho.di.job.JobMeta)15 KettleException (org.pentaho.di.core.exception.KettleException)12 ArrayList (java.util.ArrayList)9 Matchers.anyString (org.mockito.Matchers.anyString)7 LogChannel (org.pentaho.di.core.logging.LogChannel)6 AbstractMeta (org.pentaho.di.base.AbstractMeta)5 Repository (org.pentaho.di.repository.Repository)5 RepositoryDirectoryInterface (org.pentaho.di.repository.RepositoryDirectoryInterface)5 ExtensionPointWrapper (org.pentaho.di.ui.core.events.dialog.extension.ExtensionPointWrapper)5