5 # Copyright The SCons Foundation
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 Test building Java applications when using Repositories.
35 python
= TestSCons
.python
37 test
= TestSCons
.TestSCons()
39 where_javac
, java_version
= test
.java_where_javac()
40 where_java
= test
.java_where_java()
41 where_javah
= test
.java_where_javah()
43 # On some systems, the alternatives system does not remove javah even if the
44 # preferred Java doesn't have it, check the paths just in case.
45 javacdir
= pathlib
.Path(where_javac
).parent
46 javahdir
= pathlib
.Path(where_javah
).parent
47 if javacdir
!= javahdir
:
48 test
.skip_test("Cannot find Java javah matching javac, skipping test.\n")
54 # TODO: javah no longer exists for Java > 9. Other tests fail
55 # on certain systems because the java_where_* routines are very greedy
56 # and may indicate it's found even if the working java is > 9 if there
57 # was a 1.8 insallation present - and then at runtime it's not found.
58 # In this case we actually pass the javac/javah that's found by those,
59 # which means the test will seem to work. We still need to rework this.
61 ###############################################################################
73 rep1_classes
= test
.workpath('rep1', 'classes')
74 work1_classes
= test
.workpath('work1', 'classes')
75 work3_classes
= test
.workpath('work3', 'classes')
78 opts
= '-Y ' + test
.workpath('rep1')
81 test
.write(['rep1', 'SConstruct'], """
82 DefaultEnvironment(tools=[]) # test speedup
83 env = Environment(tools=['javac', 'javah'], JAVAC=r'"%s"', JAVAH=r'"%s"')
84 classes = env.Java(target='classes', source='src')
85 env.JavaH(target='outdir', source=classes)
88 test
.write(['rep1', 'src', 'Foo1.java'], """\
91 public static void main(String[] args)
93 System.out.println("rep1/src/Foo1.java");
99 test
.write(['rep1', 'src', 'Foo2.java'], """\
102 public static void main(String[] args)
104 System.out.println("rep1/src/Foo2.java");
110 test
.write(['rep1', 'src', 'Foo3.java'], """\
113 public static void main(String[] args)
115 System.out.println("rep1/src/Foo3.java");
121 # Make the repository non-writable,
122 # so we'll detect if we try to write into it accidentally.
123 test
.writable('repository', 0)
126 test
.run(chdir
= 'work1', options
= opts
, arguments
= ".")
128 test
.run(program
= java
,
129 arguments
= "-cp %s Foo1" % work1_classes
,
130 stdout
= "rep1/src/Foo1.java\n")
132 test
.run(program
= java
,
133 arguments
= "-cp %s Foo2" % work1_classes
,
134 stdout
= "rep1/src/Foo2.java\n")
136 test
.run(program
= java
,
137 arguments
= "-cp %s Foo3" % work1_classes
,
138 stdout
= "rep1/src/Foo3.java\n")
140 test
.fail_test(not os
.path
.exists(test
.workpath('work1', 'outdir', 'Foo1.h')))
141 test
.fail_test(not os
.path
.exists(test
.workpath('work1', 'outdir', 'Foo2.h')))
142 test
.fail_test(not os
.path
.exists(test
.workpath('work1', 'outdir', 'Foo3.h')))
144 test
.up_to_date(chdir
= 'work1', options
= opts
, arguments
= ".")
147 test
.subdir(['work1', 'src'])
149 test
.write(['work1', 'src', 'Foo1.java'], """\
152 public static void main(String[] args)
154 System.out.println("work1/src/Foo1.java");
160 test
.write(['work1', 'src', 'Foo2.java'], """\
163 public static void main(String[] args)
165 System.out.println("work1/src/Foo2.java");
171 test
.write(['work1', 'src', 'Foo3.java'], """\
174 public static void main(String[] args)
176 System.out.println("work1/src/Foo3.java");
182 test
.run(chdir
= 'work1', options
= opts
, arguments
= ".")
184 test
.run(program
= java
,
185 arguments
= "-cp %s Foo1" % work1_classes
,
186 stdout
= "work1/src/Foo1.java\n")
188 test
.run(program
= java
,
189 arguments
= "-cp %s Foo2" % work1_classes
,
190 stdout
= "work1/src/Foo2.java\n")
192 test
.run(program
= java
,
193 arguments
= "-cp %s Foo3" % work1_classes
,
194 stdout
= "work1/src/Foo3.java\n")
196 test
.up_to_date(chdir
= 'work1', options
= opts
, arguments
= ".")
199 test
.writable('rep1', 1)
201 test
.run(chdir
= 'rep1', options
= opts
, arguments
= ".")
203 test
.run(program
= java
,
204 arguments
= "-cp %s Foo1" % rep1_classes
,
205 stdout
= "rep1/src/Foo1.java\n")
207 test
.run(program
= java
,
208 arguments
= "-cp %s Foo2" % rep1_classes
,
209 stdout
= "rep1/src/Foo2.java\n")
211 test
.run(program
= java
,
212 arguments
= "-cp %s Foo3" % rep1_classes
,
213 stdout
= "rep1/src/Foo3.java\n")
215 test
.up_to_date(chdir
= 'rep1', options
= opts
, arguments
= ".")
218 test
.writable('repository', 0)
221 test
.up_to_date(chdir
= 'work2', options
= opts
, arguments
= ".")
224 test
.write(['work3', 'SConstruct'], """
225 env = Environment(tools=['javac', 'javah'], JAVAC=r'"%s"', JAVAH=r'"%s"')
226 classes = env.Java(target='classes', source='src')
227 hfiles = env.JavaH(target='outdir', source=classes)
229 """ % (javac
, javah
))
231 test
.run(chdir
= 'work3', options
= opts
, arguments
= ".")
233 test
.fail_test(os
.path
.exists(test
.workpath('work3', 'classes', 'Foo1.class')))
234 test
.fail_test(os
.path
.exists(test
.workpath('work3', 'classes', 'Foo2.class')))
235 test
.fail_test(os
.path
.exists(test
.workpath('work3', 'classes', 'Foo3.class')))
237 test
.fail_test(not os
.path
.exists(test
.workpath('work3', 'outdir', 'Foo1.h')))
238 test
.fail_test(not os
.path
.exists(test
.workpath('work3', 'outdir', 'Foo2.h')))
239 test
.fail_test(not os
.path
.exists(test
.workpath('work3', 'outdir', 'Foo3.h')))
242 # If the Java builder were to interact with Repositories like the
243 # other builders, then we'd uncomment the following test(s).
245 # This tests that, if the .class files are built in the repository,
246 # then a local build says that everything is up-to-date. However,
247 # because the destination target is a directory ("classes") not a
248 # file, we don't detect that the individual .class files are
249 # already there, and think things must be rebuilt.
251 #test.up_to_date(chdir = 'work2', options = opts, arguments = ".")
253 #test.subdir(['work2', 'src'])
255 #test.write(['work2', 'src', 'Foo1.java'], """\
258 # public static void main(String[] args)
260 # System.out.println("work2/src/Foo1.java");
266 #test.write(['work2', 'src', 'Foo2.java'], """\
269 # public static void main(String[] args)
271 # System.out.println("work2/src/Foo2.java");
277 #test.write(['work2', 'src', 'Foo3.java'], """\
280 # public static void main(String[] args)
282 # System.out.println("work2/src/Foo3.java");
288 #test.run(chdir = 'work2', options = opts, arguments = ".")
290 #test.run(program = java,
291 # arguments = "-cp %s Foo1" % work2_classes,
292 # stdout = "work2/src/Foo1.java\n")
294 #test.run(program = java,
295 # arguments = "-cp %s Foo2" % work2_classes,
296 # stdout = "work2/src/Foo2.java\n")
298 #test.run(program = java,
299 # arguments = "-cp %s Foo3" % work2_classes,
300 # stdout = "work2/src/Foo3.java\n")
302 #test.up_to_date(chdir = 'work2', options = opts, arguments = ".")
308 # indent-tabs-mode:nil
310 # vim: set expandtab tabstop=4 shiftwidth=4: