68 |
63 |
69 private static final String pkgClassPathTestAux3Src = |
64 private static final String pkgClassPathTestAux3Src = |
70 "package pkg;\n" + |
65 "package pkg;\n" + |
71 "public class ClassPathTestAux3 {}"; |
66 "public class ClassPathTestAux3 {}"; |
72 |
67 |
73 ProcessBuilder pb = null; |
|
74 |
|
75 public static void main(String[] args) throws Exception { |
68 public static void main(String[] args) throws Exception { |
76 new ClassPathTest().test(); |
69 new ClassPathTest().test(); |
77 } |
70 } |
|
71 |
|
72 ToolBox tb = new ToolBox(); |
78 |
73 |
79 public void test() throws Exception { |
74 public void test() throws Exception { |
80 createOutputDirAndSourceFiles(); |
75 createOutputDirAndSourceFiles(); |
81 checkCompileCommands(); |
76 checkCompileCommands(); |
82 } |
77 } |
83 |
78 |
84 void createOutputDirAndSourceFiles() throws Exception { |
79 void createOutputDirAndSourceFiles() throws Exception { |
85 //dirs and files creation |
80 //dirs and files creation |
86 ToolBox.createJavaFileFromSource(ClassPathTest1Src); |
81 tb.writeJavaFiles(Paths.get("."), |
87 ToolBox.createJavaFileFromSource(ClassPathTest2Src); |
82 ClassPathTest1Src, |
88 ToolBox.createJavaFileFromSource(ClassPathTest3Src); |
83 ClassPathTest2Src, |
89 ToolBox.createJavaFileFromSource(Paths.get("foo"), |
84 ClassPathTest3Src); |
|
85 tb.writeJavaFiles(Paths.get("foo"), |
90 fooPkgClassPathTestAux1Src); |
86 fooPkgClassPathTestAux1Src); |
91 ToolBox.createJavaFileFromSource(Paths.get("bar"), |
87 tb.writeJavaFiles(Paths.get("bar"), |
92 barPkgClassPathTestAux2Src); |
88 barPkgClassPathTestAux2Src); |
93 ToolBox.createJavaFileFromSource(pkgClassPathTestAux3Src); |
89 tb.writeJavaFiles(Paths.get("."), |
|
90 pkgClassPathTestAux3Src); |
94 } |
91 } |
95 |
92 |
96 void checkCompileCommands() throws Exception { |
93 void checkCompileCommands() throws Exception { |
97 // Without the -cp . parameter the command will fail seems like when called |
94 // Without the -cp . parameter the command will fail seems like when called |
98 // from the command line, the current dir is added to the classpath |
95 // from the command line, the current dir is added to the classpath |
99 // automatically but this is not happening when called using ProcessBuilder |
96 // automatically but this is not happening when called using ProcessBuilder |
100 |
97 |
101 // testJavac success ClassPathTest3.java |
98 // testJavac success ClassPathTest3.java |
102 List<String> mainArgs = new ArrayList<>(); |
99 tb.new JavacTask(ToolBox.Mode.EXEC) |
103 mainArgs.add(ToolBox.javacBinary.toString()); |
100 .classpath(".") |
104 if (ToolBox.testToolVMOpts != null) { |
101 .files("ClassPathTest3.java") |
105 mainArgs.addAll(ToolBox.testToolVMOpts); |
102 .run(); |
106 } |
|
107 |
|
108 List<String> commonArgs = new ArrayList<>(); |
|
109 commonArgs.addAll(mainArgs); |
|
110 commonArgs.addAll(Arrays.asList("-cp", ".")); |
|
111 |
|
112 ToolBox.AnyToolArgs successParams = new ToolBox.AnyToolArgs() |
|
113 .appendArgs(commonArgs) |
|
114 .appendArgs("ClassPathTest3.java"); |
|
115 ToolBox.executeCommand(successParams); |
|
116 |
103 |
117 // testJavac failure ClassPathTest1.java |
104 // testJavac failure ClassPathTest1.java |
118 ToolBox.AnyToolArgs failParams = |
105 tb.new JavacTask(ToolBox.Mode.EXEC) |
119 new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL) |
106 .classpath(".") |
120 .appendArgs(commonArgs) |
107 .files("ClassPathTest1.java") |
121 .appendArgs("ClassPathTest1.java"); |
108 .run(ToolBox.Expect.FAIL); |
122 ToolBox.executeCommand(failParams); |
|
123 |
|
124 // This is done inside the executeCommand method |
|
125 // CLASSPATH=bar; export CLASSPATH |
|
126 |
|
127 Map<String, String> extVars = new TreeMap<>(); |
|
128 extVars.put("CLASSPATH", "bar"); |
|
129 |
109 |
130 // testJavac success ClassPathTest2.java |
110 // testJavac success ClassPathTest2.java |
131 successParams = new ToolBox.AnyToolArgs() |
111 tb.new JavacTask(ToolBox.Mode.EXEC) |
132 .appendArgs(mainArgs) |
112 .envVar("CLASSPATH", "bar") |
133 .appendArgs("ClassPathTest2.java") |
113 .files("ClassPathTest2.java") |
134 .set(extVars); |
114 .run(); |
135 ToolBox.executeCommand(successParams); |
|
136 |
115 |
137 // testJavac failure ClassPathTest1.java |
116 // testJavac failure ClassPathTest1.java |
138 failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL) |
117 tb.new JavacTask(ToolBox.Mode.EXEC) |
139 .appendArgs(mainArgs) |
118 .envVar("CLASSPATH", "bar") |
140 .appendArgs("ClassPathTest1.java") |
119 .files("ClassPathTest1.java") |
141 .set(extVars); |
120 .run(ToolBox.Expect.FAIL); |
142 ToolBox.executeCommand(failParams); |
|
143 |
121 |
144 // testJavac failure ClassPathTest3.java |
122 // testJavac failure ClassPathTest3.java |
145 failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL) |
123 tb.new JavacTask(ToolBox.Mode.EXEC) |
146 .appendArgs(mainArgs) |
124 .envVar("CLASSPATH", "bar") |
147 .appendArgs("ClassPathTest3.java") |
125 .files("ClassPathTest3.java") |
148 .set(extVars); |
126 .run(ToolBox.Expect.FAIL); |
149 ToolBox.executeCommand(failParams); |
|
150 |
127 |
151 // testJavac success -classpath foo ClassPathTest1.java |
128 // testJavac success -classpath foo ClassPathTest1.java |
152 |
129 tb.new JavacTask(ToolBox.Mode.EXEC) |
153 commonArgs.clear(); |
130 .envVar("CLASSPATH", "bar") |
154 commonArgs.addAll(mainArgs); |
131 .classpath("foo") |
155 commonArgs.addAll(Arrays.asList("-cp", "foo")); |
132 .files("ClassPathTest1.java") |
156 |
133 .run(); |
157 successParams = new ToolBox.AnyToolArgs() |
|
158 .appendArgs(commonArgs) |
|
159 .appendArgs("ClassPathTest1.java") |
|
160 .set(extVars); |
|
161 ToolBox.executeCommand(successParams); |
|
162 |
134 |
163 // testJavac failure -classpath foo ClassPathTest2.java |
135 // testJavac failure -classpath foo ClassPathTest2.java |
164 failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL) |
136 tb.new JavacTask(ToolBox.Mode.EXEC) |
165 .appendArgs(commonArgs) |
137 .envVar("CLASSPATH", "bar") |
166 .appendArgs("ClassPathTest2.java") |
138 .classpath("foo") |
167 .set(extVars); |
139 .files("ClassPathTest2.java") |
168 ToolBox.executeCommand(failParams); |
140 .run(ToolBox.Expect.FAIL); |
169 |
141 |
170 // testJavac failure -classpath foo ClassPathTest3.java |
142 // testJavac failure -classpath foo ClassPathTest3.java |
171 failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL) |
143 tb.new JavacTask(ToolBox.Mode.EXEC) |
172 .appendArgs(commonArgs) |
144 .envVar("CLASSPATH", "bar") |
173 .appendArgs("ClassPathTest3.java") |
145 .classpath("foo") |
174 .set(extVars); |
146 .files("ClassPathTest3.java") |
175 ToolBox.executeCommand(failParams); |
147 .run(ToolBox.Expect.FAIL); |
176 } |
148 } |
177 |
149 |
178 } |
150 } |