View Javadoc

1   /*
2    * #%L
3    * xcode-maven-plugin
4    * %%
5    * Copyright (C) 2012 SAP AG
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.sap.prd.mobile.ios.mios;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.io.PrintStream;
26  
27  import junit.framework.Assert;
28  
29  import org.apache.commons.io.output.ByteArrayOutputStream;
30  import org.junit.Test;
31  
32  import com.sap.prd.mobile.ios.mios.Forker;
33  
34  public class ForkerTest
35  {
36  
37    @Test
38    public void testStraightForward() throws Exception
39    {
40  
41      final ByteArrayOutputStream out = new ByteArrayOutputStream();
42      final PrintStream log = new PrintStream(out, true);
43  
44      try {
45  
46        Forker.forkProcess(log, new File(".").getAbsoluteFile(), "echo", "Hello World");
47  
48      }
49      finally {
50        log.close();
51      }
52  
53      Assert.assertEquals("Hello World\n", new String(out.toByteArray()));
54    }
55  
56    @Test(expected = IllegalArgumentException.class)
57    public void testMissingPrintStreamForLogging() throws Exception
58    {
59      Forker.forkProcess(null, new File(".").getAbsoluteFile(), "echo", "Hello World");
60    }
61  
62    @Test(expected = IllegalArgumentException.class)
63    public void testMissingArguments_1() throws Exception
64    {
65  
66      final ByteArrayOutputStream out = new ByteArrayOutputStream();
67      final PrintStream log = new PrintStream(out, true);
68  
69      try {
70  
71        Forker.forkProcess(log, new File(".").getAbsoluteFile(), (String[]) null);
72  
73      }
74      finally {
75        log.close();
76      }
77  
78    }
79  
80    @Test(expected = IllegalArgumentException.class)
81    public void testMissingArguments_2() throws Exception
82    {
83  
84      final ByteArrayOutputStream out = new ByteArrayOutputStream();
85      final PrintStream log = new PrintStream(out, true);
86  
87      try {
88  
89        Forker.forkProcess(log, new File(".").getAbsoluteFile(), new String[0]);
90  
91      }
92      finally {
93        log.close();
94      }
95    }
96  
97    @Test(expected = IllegalArgumentException.class)
98    public void testMissingArguments_3() throws Exception
99    {
100 
101     final ByteArrayOutputStream out = new ByteArrayOutputStream();
102     final PrintStream log = new PrintStream(out, true);
103 
104     try {
105 
106       Forker.forkProcess(log, new File(".").getAbsoluteFile(), "echo", "", "Hello World");
107 
108     }
109     finally {
110       log.close();
111     }
112   }
113 
114   @Test(expected = IOException.class)
115   public void damagedPrintStream() throws Exception
116   {
117 
118     final ByteArrayOutputStream out = new ByteArrayOutputStream();
119 
120     class DamagedPrintStream extends PrintStream
121     {
122       public DamagedPrintStream(OutputStream out)
123       {
124         super(out);
125         setError();
126       }
127     }
128     final PrintStream log = new DamagedPrintStream(out);
129 
130     try {
131 
132       Forker.forkProcess(log, new File(".").getAbsoluteFile(), "echo", "Hello World");
133 
134     }
135     finally {
136       log.close();
137     }
138   }
139 
140 }