Search in sources :

Example 66 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project indy by Commonjava.

the class StoreAndVerifyJarViaDirectDownloadTest method storeFileThenDownloadAndVerifyContentViaDirectDownload.

@Test
public void storeFileThenDownloadAndVerifyContentViaDirectDownload() throws Exception {
    final String content = "This is a test: " + System.nanoTime();
    String entryName = "org/something/foo.class";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JarOutputStream jarOut = new JarOutputStream(out);
    jarOut.putNextEntry(new JarEntry(entryName));
    jarOut.write(content.getBytes());
    jarOut.close();
    // Used to visually inspect the jars moving up...
    //        String userDir = System.getProperty( "user.home" );
    //        File dir = new File( userDir, "temp" );
    //        dir.mkdirs();
    //
    //        FileUtils.writeByteArrayToFile( new File( dir, name.getMethodName() + "-in.jar" ), out.toByteArray() );
    final InputStream stream = new ByteArrayInputStream(out.toByteArray());
    final String path = "/path/to/" + getClass().getSimpleName() + "-" + name.getMethodName() + ".jar";
    assertThat(client.content().exists(hosted, STORE, path), equalTo(false));
    client.content().store(hosted, STORE, path, stream);
    assertThat(client.content().exists(hosted, STORE, path), equalTo(true));
    final URL url = new URL(client.content().contentUrl(hosted, STORE, path));
    final InputStream is = url.openStream();
    byte[] result = IOUtils.toByteArray(is);
    is.close();
    assertThat(result, equalTo(out.toByteArray()));
    // ...and down
    //        FileUtils.writeByteArrayToFile( new File( dir, name.getMethodName() + "-out.jar" ), result );
    JarInputStream jarIn = new JarInputStream(new ByteArrayInputStream(result));
    JarEntry jarEntry = jarIn.getNextJarEntry();
    assertThat(jarEntry.getName(), equalTo(entryName));
    String contentResult = IOUtils.toString(jarIn);
    assertThat(contentResult, equalTo(content));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Example 67 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project hadoop by apache.

the class TestHsJobBlock method testHsJobBlockForNormalSizeJobShouldNotDisplayWarningMessage.

@Test
public void testHsJobBlockForNormalSizeJobShouldNotDisplayWarningMessage() {
    Configuration config = new Configuration();
    config.setInt(JHAdminConfig.MR_HS_LOADED_JOBS_TASKS_MAX, -1);
    JobHistory jobHistory = new JobHitoryStubWithAllNormalSizeJobs();
    jobHistory.init(config);
    HsJobBlock jobBlock = new HsJobBlock(jobHistory) {

        // override this so that the job block can fetch a job id.
        @Override
        public Map<String, String> moreParams() {
            Map<String, String> map = new HashMap<>();
            map.put(AMParams.JOB_ID, "job_0000_0001");
            return map;
        }

        // override this to avoid view context lookup in render()
        @Override
        public ResponseInfo info(String about) {
            return new ResponseInfo().about(about);
        }

        // override this to avoid view context lookup in render()
        @Override
        public String url(String... parts) {
            return StringHelper.ujoin("", parts);
        }
    };
    // set up the test block to render HsJobBLock to
    OutputStream outputStream = new ByteArrayOutputStream();
    HtmlBlock.Block block = createBlockToCreateTo(outputStream);
    jobBlock.render(block);
    block.getWriter().flush();
    String out = outputStream.toString();
    Assert.assertTrue("Should display job overview for the job.", out.contains("ApplicationMaster"));
}
Also used : ResponseInfo(org.apache.hadoop.yarn.webapp.ResponseInfo) Configuration(org.apache.hadoop.conf.Configuration) JobHistory(org.apache.hadoop.mapreduce.v2.hs.JobHistory) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) HtmlBlock(org.apache.hadoop.yarn.webapp.view.HtmlBlock) BlockForTest(org.apache.hadoop.yarn.webapp.view.BlockForTest) HtmlBlockForTest(org.apache.hadoop.yarn.webapp.view.HtmlBlockForTest) Test(org.junit.Test)

Example 68 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project geode by apache.

the class MessageIdExtractorTest method byteArrayFromIds.

private byte[] byteArrayFromIds(Long connectionId, Long uniqueId) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DataOutputStream dis = new DataOutputStream(byteArrayOutputStream);
    dis.writeLong(connectionId);
    dis.writeLong(uniqueId);
    dis.flush();
    return byteArrayOutputStream.toByteArray();
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream)

Example 69 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project symmetric-ds by JumpMind.

the class SymRollingFileAppenderTest method testMixedMessages.

@Test
public void testMixedMessages() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    SymRollingFileAppender appender = getAppenderForTest(os);
    Exception ex = new Exception("Test exception.");
    Exception ex2 = new Exception("Test exception.");
    LoggingEvent event1 = getLoggingEventForTest("Test Exception.", ex);
    LoggingEvent event2 = getLoggingEventForTest("Test Exception.", ex2);
    LoggingEvent event3 = getLoggingEventForTest("Test Exception.", ex);
    LoggingEvent event4 = getLoggingEventForTest("Test Exception.", ex2);
    appender.append(event1);
    appender.append(event2);
    appender.append(event3);
    appender.append(event4);
    String logging = os.toString("UTF8");
    // 2016-08-11 11:55:38,487 ERROR [] [SymRollingFileAppenderTest] [main] Test Exception. StackTraceKey.init [Exception:1478675418]
    Pattern initPattern = Pattern.compile(".*StackTraceKey.init \\[Exception:([0-9]*)\\].*", Pattern.DOTALL);
    Matcher m = initPattern.matcher(logging);
    if (m.matches()) {
        {
            String stackTraceKey = "Exception:" + m.group(1);
            assertEquals(2, StringUtils.countMatches(logging, stackTraceKey));
        }
        m.matches();
        {
            String stackTraceKey = "Exception:" + m.group(1);
            assertEquals(2, StringUtils.countMatches(logging, stackTraceKey));
        }
    } else {
        fail("Didn't find proper logging pattern.");
    }
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Test(org.junit.Test)

Example 70 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project symmetric-ds by JumpMind.

the class SymRollingFileAppenderTest method testDistinctLogMessages.

@Test
public void testDistinctLogMessages() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    SymRollingFileAppender appender = getAppenderForTest(os);
    Exception ex = new Exception("Test exception.");
    Exception ex2 = new Exception("Test exception.");
    LoggingEvent event1 = getLoggingEventForTest("Test Exception.", ex);
    LoggingEvent event2 = getLoggingEventForTest("Test Exception.", ex2);
    appender.append(event1);
    appender.append(event2);
    String logging = os.toString("UTF8");
    // 2016-08-11 11:55:38,487 ERROR [] [SymRollingFileAppenderTest] [main] Test Exception. StackTraceKey.init [Exception:1478675418]
    Pattern initPattern = Pattern.compile(".*StackTraceKey.init \\[Exception:([0-9]*)\\].*", Pattern.DOTALL);
    Matcher m = initPattern.matcher(logging);
    if (m.matches()) {
        {
            String stackTraceKey = "Exception:" + m.group(1);
            assertEquals(1, StringUtils.countMatches(logging, stackTraceKey));
        }
        m.matches();
        {
            String stackTraceKey = "Exception:" + m.group(1);
            assertEquals(1, StringUtils.countMatches(logging, stackTraceKey));
        }
    } else {
        fail("Didn't find proper logging pattern.");
    }
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)92 Test (org.junit.Test)36 DataOutputStream (java.io.DataOutputStream)15 IOException (java.io.IOException)14 HashSet (java.util.HashSet)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ArrayList (java.util.ArrayList)12 Configuration (org.apache.hadoop.conf.Configuration)12 PrintStream (java.io.PrintStream)10 SparkConf (org.apache.spark.SparkConf)10 Edge (uk.gov.gchq.gaffer.data.element.Edge)10 Element (uk.gov.gchq.gaffer.data.element.Element)10 Entity (uk.gov.gchq.gaffer.data.element.Entity)10 Graph (uk.gov.gchq.gaffer.graph.Graph)10 User (uk.gov.gchq.gaffer.user.User)10 File (java.io.File)9 HashMap (java.util.HashMap)8 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)6 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)6