Search in sources :

Example 51 with BufferedWriter

use of java.io.BufferedWriter in project jna by java-native-access.

the class Kernel32Test method testGetPrivateProfileString.

public final void testGetPrivateProfileString() throws IOException {
    final File tmp = File.createTempFile("testGetPrivateProfileString", ".ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    writer.println("[Section]");
    writer.println("existingKey = ABC");
    writer.close();
    final char[] buffer = new char[8];
    DWORD len = Kernel32.INSTANCE.GetPrivateProfileString("Section", "existingKey", "DEF", buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
    assertEquals(3, len.intValue());
    assertEquals("ABC", Native.toString(buffer));
    len = Kernel32.INSTANCE.GetPrivateProfileString("Section", "missingKey", "DEF", buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
    assertEquals(3, len.intValue());
    assertEquals("DEF", Native.toString(buffer));
}
Also used : FileWriter(java.io.FileWriter) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 52 with BufferedWriter

use of java.io.BufferedWriter in project jna by java-native-access.

the class Kernel32UtilTest method testGetPrivateProfileSectionNames.

public final void testGetPrivateProfileSectionNames() throws IOException {
    final File tmp = File.createTempFile("testGetPrivateProfileSectionNames", "ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    try {
        writer.println("[S1]");
        writer.println("A=1");
        writer.println("B=X");
        writer.println("[S2]");
        writer.println("C=2");
        writer.println("D=Y");
    } finally {
        writer.close();
    }
    String[] sectionNames = Kernel32Util.getPrivateProfileSectionNames(tmp.getCanonicalPath());
    assertEquals(sectionNames.length, 2);
    assertEquals(sectionNames[0], "S1");
    assertEquals(sectionNames[1], "S2");
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 53 with BufferedWriter

use of java.io.BufferedWriter in project jna by java-native-access.

the class Kernel32UtilTest method testGetPrivateProfileSection.

public final void testGetPrivateProfileSection() throws IOException {
    final File tmp = File.createTempFile("testGetPrivateProfileSection", ".ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    try {
        writer.println("[X]");
        writer.println("A=1");
        writer.println("foo=bar");
    } finally {
        writer.close();
    }
    final String[] lines = Kernel32Util.getPrivateProfileSection("X", tmp.getCanonicalPath());
    assertEquals(lines.length, 2);
    assertEquals(lines[0], "A=1");
    assertEquals(lines[1], "foo=bar");
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 54 with BufferedWriter

use of java.io.BufferedWriter in project jna by java-native-access.

the class Kernel32UtilTest method testWritePrivateProfileString.

public final void testWritePrivateProfileString() throws IOException {
    final File tmp = File.createTempFile("testWritePrivateProfileString", "ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    writer.println("[Section]");
    writer.println("existingKey = ABC");
    writer.println("removedKey = JKL");
    writer.close();
    Kernel32Util.writePrivateProfileString("Section", "existingKey", "DEF", tmp.getCanonicalPath());
    Kernel32Util.writePrivateProfileString("Section", "addedKey", "GHI", tmp.getCanonicalPath());
    Kernel32Util.writePrivateProfileString("Section", "removedKey", null, tmp.getCanonicalPath());
    final BufferedReader reader = new BufferedReader(new FileReader(tmp));
    assertEquals(reader.readLine(), "[Section]");
    assertTrue(reader.readLine().matches("existingKey\\s*=\\s*DEF"));
    assertTrue(reader.readLine().matches("addedKey\\s*=\\s*GHI"));
    assertEquals(reader.readLine(), null);
    reader.close();
}
Also used : FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 55 with BufferedWriter

use of java.io.BufferedWriter in project hadoop by apache.

the class TestDefaultContainerExecutor method testContainerLaunchError.

@Test
public void testContainerLaunchError() throws IOException, InterruptedException {
    if (Shell.WINDOWS) {
        BASE_TMP_PATH = new Path(new File("target").getAbsolutePath(), TestDefaultContainerExecutor.class.getSimpleName());
    }
    Path localDir = new Path(BASE_TMP_PATH, "localDir");
    List<String> localDirs = new ArrayList<String>();
    localDirs.add(localDir.toString());
    List<String> logDirs = new ArrayList<String>();
    Path logDir = new Path(BASE_TMP_PATH, "logDir");
    logDirs.add(logDir.toString());
    Configuration conf = new Configuration();
    conf.set(CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY, "077");
    conf.set(YarnConfiguration.NM_LOCAL_DIRS, localDir.toString());
    conf.set(YarnConfiguration.NM_LOG_DIRS, logDir.toString());
    FileContext lfs = FileContext.getLocalFSFileContext(conf);
    DefaultContainerExecutor mockExec = spy(new DefaultContainerExecutor(lfs));
    mockExec.setConf(conf);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            String diagnostics = (String) invocationOnMock.getArguments()[0];
            assertTrue("Invalid Diagnostics message: " + diagnostics, diagnostics.contains("No such file or directory"));
            return null;
        }
    }).when(mockExec).logOutput(any(String.class));
    String appSubmitter = "nobody";
    String appId = "APP_ID";
    String containerId = "CONTAINER_ID";
    Container container = mock(Container.class);
    ContainerId cId = mock(ContainerId.class);
    ContainerLaunchContext context = mock(ContainerLaunchContext.class);
    HashMap<String, String> env = new HashMap<String, String>();
    env.put("LANG", "C");
    when(container.getContainerId()).thenReturn(cId);
    when(container.getLaunchContext()).thenReturn(context);
    try {
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                ContainerDiagnosticsUpdateEvent event = (ContainerDiagnosticsUpdateEvent) invocationOnMock.getArguments()[0];
                assertTrue("Invalid Diagnostics message: " + event.getDiagnosticsUpdate(), event.getDiagnosticsUpdate().contains("No such file or directory"));
                return null;
            }
        }).when(container).handle(any(ContainerDiagnosticsUpdateEvent.class));
        when(cId.toString()).thenReturn(containerId);
        when(cId.getApplicationAttemptId()).thenReturn(ApplicationAttemptId.newInstance(ApplicationId.newInstance(0, 1), 0));
        when(context.getEnvironment()).thenReturn(env);
        mockExec.createUserLocalDirs(localDirs, appSubmitter);
        mockExec.createUserCacheDirs(localDirs, appSubmitter);
        mockExec.createAppDirs(localDirs, appSubmitter, appId);
        mockExec.createAppLogDirs(appId, logDirs, appSubmitter);
        Path scriptPath = new Path("file:///bin/echo");
        Path tokensPath = new Path("file:///dev/null");
        if (Shell.WINDOWS) {
            File tmp = new File(BASE_TMP_PATH.toString(), "test_echo.cmd");
            BufferedWriter output = new BufferedWriter(new FileWriter(tmp));
            output.write("Exit 1");
            output.write("Echo No such file or directory 1>&2");
            output.close();
            scriptPath = new Path(tmp.getAbsolutePath());
            tmp = new File(BASE_TMP_PATH.toString(), "tokens");
            tmp.createNewFile();
            tokensPath = new Path(tmp.getAbsolutePath());
        }
        Path workDir = localDir;
        Path pidFile = new Path(workDir, "pid.txt");
        mockExec.init();
        mockExec.activateContainer(cId, pidFile);
        int ret = mockExec.launchContainer(new ContainerStartContext.Builder().setContainer(container).setNmPrivateContainerScriptPath(scriptPath).setNmPrivateTokensPath(tokensPath).setUser(appSubmitter).setAppId(appId).setContainerWorkDir(workDir).setLocalDirs(localDirs).setLogDirs(logDirs).build());
        Assert.assertNotSame(0, ret);
    } finally {
        mockExec.deleteAsUser(new DeletionAsUserContext.Builder().setUser(appSubmitter).setSubDir(localDir).build());
        mockExec.deleteAsUser(new DeletionAsUserContext.Builder().setUser(appSubmitter).setSubDir(logDir).build());
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) ContainerDiagnosticsUpdateEvent(org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerDiagnosticsUpdateEvent) ContainerLaunchContext(org.apache.hadoop.yarn.api.records.ContainerLaunchContext) BufferedWriter(java.io.BufferedWriter) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Container(org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) InvocationOnMock(org.mockito.invocation.InvocationOnMock) File(java.io.File) FileContext(org.apache.hadoop.fs.FileContext) Test(org.junit.Test)

Aggregations

BufferedWriter (java.io.BufferedWriter)4214 FileWriter (java.io.FileWriter)2181 File (java.io.File)1879 IOException (java.io.IOException)1847 OutputStreamWriter (java.io.OutputStreamWriter)1344 BufferedReader (java.io.BufferedReader)747 FileOutputStream (java.io.FileOutputStream)656 ArrayList (java.util.ArrayList)386 FileReader (java.io.FileReader)376 InputStreamReader (java.io.InputStreamReader)349 PrintWriter (java.io.PrintWriter)324 Writer (java.io.Writer)324 Test (org.junit.Test)286 FileNotFoundException (java.io.FileNotFoundException)217 OutputStream (java.io.OutputStream)213 HashMap (java.util.HashMap)200 Path (java.nio.file.Path)177 InputStream (java.io.InputStream)171 FileInputStream (java.io.FileInputStream)158 StringWriter (java.io.StringWriter)143