Support Java when using Repository and SConscriptChdir(0). (Charles Crain)
[scons.git] / test / Repository / Java.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Test building Java applications when using Repositories.
29 """
30
31 import os
32 import string
33 import sys
34 import TestSCons
35
36 python = TestSCons.python
37
38 test = TestSCons.TestSCons()
39
40 java = '/usr/local/j2sdk1.3.1/bin/java'
41 javac = '/usr/local/j2sdk1.3.1/bin/javac'
42
43 if not os.path.exists(javac):
44     print "Could not find Java, skipping test(s)."
45     test.pass_test(1)
46
47 ###############################################################################
48
49 #
50 test.subdir('rep1', ['rep1', 'src'],
51             'work1',
52             'work2')
53
54 #
55 rep1_classes = test.workpath('rep1', 'classes')
56 work1_classes = test.workpath('work1', 'classes')
57
58 #
59 opts = '-Y ' + test.workpath('rep1')
60
61 #
62 test.write(['rep1', 'SConstruct'], """
63 env = Environment(tools = ['javac'],
64                   JAVAC = r'%s')
65 env.Java(target = 'classes', source = 'src')
66 """ % javac)
67
68 test.write(['rep1', 'src', 'Foo1.java'], """\
69 public class Foo1
70 {
71      public static void main(String[] args)
72      {
73           System.out.println("rep1/src/Foo1.java");
74
75      }
76 }
77 """)
78
79 test.write(['rep1', 'src', 'Foo2.java'], """\
80 public class Foo2
81 {
82      public static void main(String[] args)
83      {
84           System.out.println("rep1/src/Foo2.java");
85
86      }
87 }
88 """)
89
90 test.write(['rep1', 'src', 'Foo3.java'], """\
91 public class Foo3
92 {
93      public static void main(String[] args)
94      {
95           System.out.println("rep1/src/Foo3.java");
96
97      }
98 }
99 """)
100
101 # Make the repository non-writable,
102 # so we'll detect if we try to write into it accidentally.
103 test.writable('repository', 0)
104
105 #
106 test.run(chdir = 'work1', options = opts, arguments = ".")
107
108 test.run(program = java,
109          arguments = "-cp %s Foo1" % work1_classes,
110          stdout = "rep1/src/Foo1.java\n")
111
112 test.run(program = java,
113          arguments = "-cp %s Foo2" % work1_classes,
114          stdout = "rep1/src/Foo2.java\n")
115
116 test.run(program = java,
117          arguments = "-cp %s Foo3" % work1_classes,
118          stdout = "rep1/src/Foo3.java\n")
119
120 test.up_to_date(chdir = 'work1', options = opts, arguments = ".")
121
122 #
123 test.subdir(['work1', 'src'])
124
125 test.write(['work1', 'src', 'Foo1.java'], """\
126 public class Foo1
127 {
128      public static void main(String[] args)
129      {
130           System.out.println("work1/src/Foo1.java");
131
132      }
133 }
134 """)
135
136 test.write(['work1', 'src', 'Foo2.java'], """\
137 public class Foo2
138 {
139      public static void main(String[] args)
140      {
141           System.out.println("work1/src/Foo2.java");
142
143      }
144 }
145 """)
146
147 test.write(['work1', 'src', 'Foo3.java'], """\
148 public class Foo3
149 {
150      public static void main(String[] args)
151      {
152           System.out.println("work1/src/Foo3.java");
153
154      }
155 }
156 """)
157
158 test.run(chdir = 'work1', options = opts, arguments = ".")
159
160 test.run(program = java,
161          arguments = "-cp %s Foo1" % work1_classes,
162          stdout = "work1/src/Foo1.java\n")
163
164 test.run(program = java,
165          arguments = "-cp %s Foo2" % work1_classes,
166          stdout = "work1/src/Foo2.java\n")
167
168 test.run(program = java,
169          arguments = "-cp %s Foo3" % work1_classes,
170          stdout = "work1/src/Foo3.java\n")
171
172 test.up_to_date(chdir = 'work1', options = opts, arguments = ".")
173
174 #
175 test.writable('rep1', 1)
176
177 test.run(chdir = 'rep1', options = opts, arguments = ".")
178
179 test.run(program = java,
180          arguments = "-cp %s Foo1" % rep1_classes,
181          stdout = "rep1/src/Foo1.java\n")
182
183 test.run(program = java,
184          arguments = "-cp %s Foo2" % rep1_classes,
185          stdout = "rep1/src/Foo2.java\n")
186
187 test.run(program = java,
188          arguments = "-cp %s Foo3" % rep1_classes,
189          stdout = "rep1/src/Foo3.java\n")
190
191 test.up_to_date(chdir = 'rep1', options = opts, arguments = ".")
192
193 #
194 test.writable('repository', 0)
195
196 #
197 # If the Java builder were to interact with Repositories like the
198 # other builders, then we'd uncomment the following test(s).
199 #
200 # This tests that, if the .class files are built in the repository,
201 # then a local build says that everything is up-to-date.  However,
202 # because the destination target is a directory ("classes") not a
203 # file, we don't detect that the individual .class files are
204 # already there, and think things must be rebuilt.
205 #
206 #test.up_to_date(chdir = 'work2', options = opts, arguments = ".")
207 #
208 #test.subdir(['work2', 'src'])
209 #
210 #test.write(['work2', 'src', 'Foo1.java'], """\
211 #public class Foo1
212 #{
213 #     public static void main(String[] args)
214 #     {
215 #          System.out.println("work2/src/Foo1.java");
216 #
217 #     }
218 #}
219 #""")
220 #
221 #test.write(['work2', 'src', 'Foo2.java'], """\
222 #public class Foo2
223 #{
224 #     public static void main(String[] args)
225 #     {
226 #          System.out.println("work2/src/Foo2.java");
227 #
228 #     }
229 #}
230 #""")
231 #
232 #test.write(['work2', 'src', 'Foo3.java'], """\
233 #public class Foo3
234 #{
235 #     public static void main(String[] args)
236 #     {
237 #          System.out.println("work2/src/Foo3.java");
238 #
239 #     }
240 #}
241 #""")
242 #
243 #test.run(chdir = 'work2', options = opts, arguments = ".")
244 #
245 #test.run(program = java,
246 #         arguments = "-cp %s Foo1" % work2_classes,
247 #         stdout = "work2/src/Foo1.java\n")
248 #
249 #test.run(program = java,
250 #         arguments = "-cp %s Foo2" % work2_classes,
251 #         stdout = "work2/src/Foo2.java\n")
252 #
253 #test.run(program = java,
254 #         arguments = "-cp %s Foo3" % work2_classes,
255 #         stdout = "work2/src/Foo3.java\n")
256 #
257 #test.up_to_date(chdir = 'work2', options = opts, arguments = ".")
258
259 test.pass_test()