diff --git a/framework/pym/play/commands/base.py b/framework/pym/play/commands/base.py index 7d21047f86..1763535e1d 100644 --- a/framework/pym/play/commands/base.py +++ b/framework/pym/play/commands/base.py @@ -77,7 +77,7 @@ def new(app, args, env, cmdloader=None): if os.path.isdir(os.path.join(env["basedir"], 'modules/%s' % f)) and f.find('%s-' % m) == 0: dirname = f break - + if not dirname: print("~ Oops. No module %s found" % m) print("~ Try to install it using 'play install %s'" % m) @@ -99,7 +99,7 @@ def new(app, args, env, cmdloader=None): replaceAll(os.path.join(app.path, 'conf/application.conf'), r'%SECRET_KEY%', secretKey()) print("~") - # Configure modules + # Configure modules for m in md: # Check dependencies.yml of the module depsYaml = os.path.join(env["basedir"], 'modules/%s/conf/dependencies.yml' % m) @@ -110,7 +110,7 @@ def new(app, args, env, cmdloader=None): replaceAll(os.path.join(app.path, 'conf/dependencies.yml'), r'- play\n', '- play\n - %s\n' % moduleDefinition ) except Exception: pass - + cmdloader.commands['dependencies'].execute(command='dependencies', app=app, args=['--sync'], env=env, cmdloader=cmdloader) print("~ OK, the application is created.") @@ -140,11 +140,11 @@ def handle_sigint(signum, frame): else: print("\nKilling Java process") process.kill() - + def run(app, args): global process app.check() - + print("~ Ctrl+C to stop") print("~ ") java_cmd = app.java_cmd(args) @@ -158,7 +158,7 @@ def run(app, args): except OSError: print("Could not execute the java executable, please make sure the JAVA_HOME environment variable is set properly (the java executable should reside at JAVA_HOME/bin/java). ") sys.exit(-1) - print() + print("~") def clean(app): app.check() @@ -180,7 +180,7 @@ def show_modules(app, args): print("~ %s" % module) else: print("~ No modules installed in this application") - print("~ ") + print("~") sys.exit(0) def id(play_env): diff --git a/samples-and-tests/i-am-a-developer/tests.py b/samples-and-tests/i-am-a-developer/tests.py index b3211d76d3..35d2b6d3bf 100755 --- a/samples-and-tests/i-am-a-developer/tests.py +++ b/samples-and-tests/i-am-a-developer/tests.py @@ -1,6 +1,7 @@ #!/usr/bin/python from __future__ import print_function + import os import shutil import ssl @@ -10,19 +11,19 @@ import time import unittest import urllib - from signal import SIGTERM -import mechanize +import mechanize # --- TESTS +DEFAULTS = { + 'host': '127.0.0.1', + 'http.port': '9001', +} -class IamADeveloper(unittest.TestCase): - DEFAULTS = { - 'host': '127.0.0.1', - 'http.port': '9001', - } +class IamADeveloper(unittest.TestCase): + play = None @unittest.skip def testSSLConfig(self): @@ -35,12 +36,14 @@ def testSSLConfig(self): # play new job-app step('Create a new project') - self.play = callPlay(self, ['new', '%s/sslconfigapp' % self.working_directory, '--name=SSLCONFIGAPP']) - self.assertTrue(waitFor(self.play, 'The new application will be created')) - self.assertTrue(waitFor(self.play, 'OK, the application is created')) - self.assertTrue(waitFor(self.play, 'Have fun!')) + with callPlay(self, ['new', '%s/sslconfigapp' % self.working_directory, '--name=SSLCONFIGAPP']) as self.play: + self.assertTrue(waitFor(self.play, 'The new application will be created')) + self.assertTrue(waitFor(self.play, 'OK, the application is created')) + self.assertTrue(waitFor(self.play, 'Have fun!')) - self.play.wait() + self.play.wait() + step("stop play") + killPlay(self.play, 'https') app = '%s/sslconfigapp' % self.working_directory @@ -48,7 +51,7 @@ def testSSLConfig(self): insert(app, "app/controllers/Application.java", 13, ' Logger.info("I am ssl secured!");') edit(app, "conf/application.conf", 32, 'http.port=-1') - edit(app, "conf/application.conf", 33, 'https.port=' + self.DEFAULTS['http.port']) + edit(app, "conf/application.conf", 33, 'https.port=' + DEFAULTS['http.port']) edit(app, "conf/application.conf", 232, 'play.ssl.netty.pipeline = play.server.FlashPolicyHandler,org.jboss.netty.handler.codec.http.HttpRequestDecoder,play.server.StreamChunkAggregator,org.jboss.netty.handler.codec.http.HttpResponseEncoder,org.jboss.netty.handler.codec.http.HttpContentCompressor,org.jboss.netty.handler.stream.ChunkedWriteHandler,play.server.ssl.SslPlayHandler') create(app, 'conf/host.key') @@ -138,21 +141,20 @@ def testSSLConfig(self): # Run the newly created application step('Run our ssl-application') - self.play = callPlay(self, ['run', app]) - # wait for play to be ready - self.assertTrue(waitFor(self.play, 'Listening for HTTPS on port 9000')) + with callPlay(self, ['run', app]) as self.play: + # wait for play to be ready + self.assertTrue(waitFor(self.play, 'Listening for HTTPS on port ' + DEFAULTS['http.port'])) - step("Send request to https") + step("Send request to https") - browser = mechanize.Browser() - response = browser.open('https://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + browser = mechanize.Browser() + response = browser.open('https://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) - step("check that ssl message is logged") - self.assertTrue(waitFor(self.play, 'I am ssl secured!')) + step("check that ssl message is logged") + self.assertTrue(waitFor(self.play, 'I am ssl secured!')) - step("stop play") - killPlay(self.play, 'https') - self.play.wait() + step("stop play") + killPlay(self.play, 'https') # now we're going to manually configure log4j to log debug messages step('using key file with password') @@ -163,21 +165,22 @@ def testSSLConfig(self): # re-run the application with new setting step('re-run our ssl-application') - self.play = callPlay(self, ['run', app]) - # wait for play to be ready - self.assertTrue(waitFor(self.play, 'Listening for HTTPS on port 9000')) + with callPlay(self, ['run', app]) as self.play: + # wait for play to be ready + self.assertTrue(waitFor(self.play, 'Listening for HTTPS on port 9000')) + + step("Send request to https") - step("Send request to https") + browserOpen('https://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) - browser = mechanize.Browser() - response = browser.open('https://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + step("check that ssl message is logged") + self.assertTrue(waitFor(self.play, 'I am ssl secured!')) - step("check that ssl message is logged") - self.assertTrue(waitFor(self.play, 'I am ssl secured!')) + step("stop play") + killPlay(self.play, 'https') step("done testing ssl config") - # @unittest.skip def testLogLevelsAndLog4jConfig(self): # Testing job developing @@ -196,7 +199,7 @@ def testLogLevelsAndLog4jConfig(self): self.play.wait() app = '%s/loglevelsapp' % self.working_directory - edit(app, "conf/application.conf", 32, 'http.port=' + self.DEFAULTS['http.port']) + edit(app, "conf/application.conf", 32, 'http.port=' + DEFAULTS['http.port']) # inserting some log-statements in our controller insert(app, "app/controllers/Application.java", 13, ' Logger.debug("I am a debug message");') @@ -210,14 +213,11 @@ def testLogLevelsAndLog4jConfig(self): with callPlay(self, ['run', app]) as self.play: # wait for play to be ready - self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port'])) + self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port'])) step("Send request to trigger some logging") - browser = mechanize.Browser() - browser.set_handle_robots(False) - - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) step("check that only info log message is logged") self.assertTrue(waitForWithFail(self.play, 'I am an info message', 'I am a debug message')) @@ -249,20 +249,20 @@ def testLogLevelsAndLog4jConfig(self): with callPlay(self, ['run', app]) as self.play: # wait for play to be ready - self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port'])) + self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port'])) step("Send request to trigger some logging") - browser = mechanize.Browser() - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) step("check that both debug and info message is logged") self.assertTrue(waitFor(self.play, 'I am a debug message')) self.assertTrue(waitFor(self.play, 'I am an info message')) + step("stop play") + killPlay(self.play) step("done testing logging") - @unittest.skip def testCreateAndRunForJobProject(self): # Testing job developing @@ -273,14 +273,15 @@ def testCreateAndRunForJobProject(self): # play new job-app step('Create a new project') - self.play = callPlay(self, ['new', '%s/jobapp' % self.working_directory, '--name=JOBAPP']) - self.assertTrue(waitFor(self.play, 'The new application will be created')) - self.assertTrue(waitFor(self.play, 'OK, the application is created')) - self.assertTrue(waitFor(self.play, 'Have fun!')) - self.play.wait() + with callPlay(self, ['new', '%s/jobapp' % self.working_directory, '--name=JOBAPP']) as self.play: + self.assertTrue(waitFor(self.play, 'The new application will be created')) + self.assertTrue(waitFor(self.play, 'OK, the application is created')) + self.assertTrue(waitFor(self.play, 'Have fun!')) + self.play.wait() + app = '%s/jobapp' % self.working_directory - edit(app, "conf/application.conf", 32, 'https.port=' + self.DEFAULTS['http.port']) + edit(app, "conf/application.conf", 32, 'http.port=' + DEFAULTS['http.port']) # create our first job - which is executed sync on startup with @OnApplicationStart createDir(app, 'app/jobs') @@ -303,22 +304,21 @@ def testCreateAndRunForJobProject(self): # Run the newly created application step('Run the newly created job-application') - self.play = callPlay(self, ['run', app]) - # wait for play to be ready - self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port'])) + with callPlay(self, ['run', app]) as self.play: + # wait for play to be ready + self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port'])) - step("Send request to start app") + step("Send request to start app") - browser = mechanize.Browser() - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) - step("check that job completed before processing request") - self.assertTrue(waitFor(self.play, 'Job done')) - self.assertTrue(waitFor(self.play, 'Processing request')) + step("check that job completed before processing request") + self.assertTrue(waitFor(self.play, 'Job done')) + self.assertTrue(waitFor(self.play, 'Processing request')) + + step("stop play") + killPlay(self.play) - step("stop play") - killPlay(self.play) - self.play.wait() # now we change the job to be async step("Change job to async") @@ -327,20 +327,21 @@ def testCreateAndRunForJobProject(self): # start play again step('Run the job-application again') - self.play = callPlay(self, ['run', app]) - # wait for play to be ready - self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port'])) + with callPlay(self, ['run', app]) as self.play: + # wait for play to be ready + self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port'])) - step("Send request to start app") + step("Send request to start app") + response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) - browser = mechanize.Browser() - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + step("check that the request is processed before the job finishes") + self.assertTrue(waitFor(self.play, 'Processing request')) + self.assertTrue(waitFor(self.play, 'Job done')) + step("stop play") + killPlay(self.play) - step("check that the request is processed before the job finishes") - self.assertTrue(waitFor(self.play, 'Processing request')) - self.assertTrue(waitFor(self.play, 'Job done')) + step('Done testing testCreateAndRunForJobProject') - @unittest.skip def testSimpleProjectCreation(self): # Well @@ -351,11 +352,11 @@ def testSimpleProjectCreation(self): # play new yop step('Create a new project') - self.play = callPlay(self, ['new', '%s/yop' % self.working_directory, '--name=YOP']) - self.assertTrue(waitFor(self.play, 'The new application will be created')) - self.assertTrue(waitFor(self.play, 'OK, the application is created')) - self.assertTrue(waitFor(self.play, 'Have fun!')) - self.play.wait() + with callPlay(self, ['new', '%s/yop' % self.working_directory, '--name=YOP']) as self.play: + self.assertTrue(waitFor(self.play, 'The new application will be created')) + self.assertTrue(waitFor(self.play, 'OK, the application is created')) + self.assertTrue(waitFor(self.play, 'Have fun!')) + self.play.wait() self.assertTrue(os.path.exists(os.path.join(self.working_directory, 'yop'))) self.assertTrue(os.path.exists(os.path.join(self.working_directory, 'yop/app'))) @@ -375,413 +376,420 @@ def testSimpleProjectCreation(self): app = '%s/yop' % self.working_directory + edit(app, "conf/application.conf", 32, 'http.port=' + DEFAULTS['http.port']) + # Run the newly created application step('Run the newly created application') - self.play = callPlay(self, ['run', app]) - self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port'])) - - # Start a browser - step('Start a browser') - - browser = mechanize.Browser() - - # Open the home page - step('Open the home page') - - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) - self.assertTrue(waitFor(self.play, "Application 'YOP' is now started !")) - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Your application is ready !') + with callPlay(self, ['run', app]) as self.play: + self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port'])) - html = response.get_data() - self.assertTrue(html.count(b'Your application is ready !')) + # Start a browser + step('Start a browser') - # Open the documentation - step('Open the documentation') + browser = mechanize.Browser() - browser.addheaders = [("Accept-Language", "en")] - response = browser.open( - 'http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'] + '/@documentation') - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Play manual - Documentation') - html = response.get_data() - self.assertTrue(html.count(b'Getting started')) + # Open the home page + step('Open the home page') - # Go back to home - step('Go back to home') + response = browser.open('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) + self.assertTrue(waitFor(self.play, "Application 'YOP' is now started !")) + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Your application is ready !') - response = browser.back() - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Your application is ready !') + html = response.get_data() + self.assertTrue(html.count(b'Your application is ready !')) - # Refresh - step('Refresh home') - response = browser.reload() - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Your application is ready !') - html = response.get_data() - self.assertTrue(html.count(b'Your application is ready !')) + # Open the documentation + step('Open the documentation') - # Make a mistake in Application.java and refresh - step('Make a mistake in Application.java') + browser.addheaders = [("Accept-Language", "en")] + response = browser.open( + 'http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'] + '/@documentation') + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Play manual - Documentation') + html = response.get_data() + self.assertTrue(html.count(b'Getting started')) - edit(app, 'app/controllers/Application.java', 13, ' render()') - try: - browser.reload() - self.fail() + # Go back to home + step('Go back to home') - except urllib.error.URLError as error: + response = browser.back() self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Application error') - html = b''.join(error.readlines()) - self.assertTrue(html.count(b'Compilation error')) - self.assertTrue(html.count(b'insert ";" to complete BlockStatements')) - self.assertTrue(html.count(b'In /app/controllers/Application.java (around line 13)')) - self.assertTrue(html.count(b' render()')) - self.assertTrue(waitFor(self.play, 'ERROR play')) - self.assertTrue( - waitFor(self.play, 'Compilation error (In /app/controllers/Application.java around line 13)')) - self.assertTrue(waitFor(self.play, 'Syntax error, insert ";" to complete BlockStatements')) - - # Refresh again - step('Refresh again') - - try: - browser.reload() - self.fail() + self.assertTrue(browser.title() == 'Your application is ready !') - except urllib.error.HTTPError as error: - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Application error') - html = b''.join(error.readlines()) - self.assertTrue(html.count(b'Compilation error')) - self.assertTrue(html.count(b'insert ";" to complete BlockStatements')) - self.assertTrue(html.count(b'In /app/controllers/Application.java (around line 13)')) - self.assertTrue(html.count(b' render()')) - self.assertTrue(waitFor(self.play, 'ERROR play')) - self.assertTrue( - waitFor(self.play, 'Compilation error (In /app/controllers/Application.java around line 13)')) - self.assertTrue(waitFor(self.play, 'Syntax error, insert ";" to complete BlockStatements')) - - # Correct the error - step('Correct the error') - - edit(app, 'app/controllers/Application.java', 13, ' render();') - response = browser.reload() - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Your application is ready !') - html = response.get_data() - self.assertTrue(html.count(b'Your application is ready !')) - - # Refresh again - step('Refresh again') - - response = browser.reload() - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Your application is ready !') - html = response.get_data() - self.assertTrue(html.count(b'Your application is ready !')) - - # Let's code hello world - step('Let\'s code hello world') - time.sleep(1) - - edit(app, 'app/controllers/Application.java', 12, ' public static void index(String name) {') - edit(app, 'app/controllers/Application.java', 13, ' render(name);') - edit(app, 'app/views/Application/index.html', 2, "#{set title:'Hello world app' /}") - edit(app, 'app/views/Application/index.html', 4, "Hello ${name} !!") - response = browser.reload() - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Hello world app') - html = response.get_data() - self.assertTrue(html.count(b'Hello !!')) - - response = browser.open( - 'http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'] + '/?name=Guillaume') - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Hello world app') - html = response.get_data() - self.assertTrue(html.count(b'Hello Guillaume !!')) - - # Make a mistake in the template - step('Make a mistake in the template') - time.sleep(1) - - edit(app, 'app/views/Application/index.html', 4, "Hello ${name !!") - try: + # Refresh + step('Refresh home') response = browser.reload() - self.fail() - - except urllib.error.HTTPError as error: self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Application error') - html = b''.join(error.readlines()) - self.assertTrue(html.count(b'Template compilation error')) - self.assertTrue(html.count( - b'The template /app/views/Application/index.html does not compile : Unexpected input: \'{\' ')) - self.assertTrue(waitFor(self.play, 'ERROR play')) - self.assertTrue( - waitFor(self.play, 'Template compilation error (In /app/views/Application/index.html around line 0)')) - - # Refresh again - step('Refresh again') - - try: + self.assertTrue(browser.title() == 'Your application is ready !') + html = response.get_data() + self.assertTrue(html.count(b'Your application is ready !')) + + # Make a mistake in Application.java and refresh + step('Make a mistake in Application.java') + + edit(app, 'app/controllers/Application.java', 13, ' render()') + try: + browser.reload() + self.fail() + + except urllib.error.URLError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Application error') + html = b''.join(error.readlines()) + self.assertTrue(html.count(b'Compilation error')) + self.assertTrue(html.count(b'insert ";" to complete BlockStatements')) + self.assertTrue(html.count(b'In /app/controllers/Application.java (around line 13)')) + self.assertTrue(html.count(b' render()')) + self.assertTrue(waitFor(self.play, 'ERROR play')) + self.assertTrue( + waitFor(self.play, 'Compilation error (In /app/controllers/Application.java around line 13)')) + self.assertTrue(waitFor(self.play, 'Syntax error, insert ";" to complete BlockStatements')) + + # Refresh again + step('Refresh again') + + try: + browser.reload() + self.fail() + + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Application error') + html = b''.join(error.readlines()) + self.assertTrue(html.count(b'Compilation error')) + self.assertTrue(html.count(b'insert ";" to complete BlockStatements')) + self.assertTrue(html.count(b'In /app/controllers/Application.java (around line 13)')) + self.assertTrue(html.count(b' render()')) + self.assertTrue(waitFor(self.play, 'ERROR play')) + self.assertTrue( + waitFor(self.play, 'Compilation error (In /app/controllers/Application.java around line 13)')) + self.assertTrue(waitFor(self.play, 'Syntax error, insert ";" to complete BlockStatements')) + + # Correct the error + step('Correct the error') + + edit(app, 'app/controllers/Application.java', 13, ' render();') response = browser.reload() - self.fail() - - except urllib.error.HTTPError as error: self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Application error') - html = b''.join(error.readlines()) - self.assertTrue(html.count(b'Template compilation error')) - self.assertTrue(html.count( - b'The template /app/views/Application/index.html does not compile : Unexpected input: \'{\' ')) - self.assertTrue(waitFor(self.play, 'ERROR play')) - self.assertTrue( - waitFor(self.play, 'Template compilation error (In /app/views/Application/index.html around line 0)')) - - # Try a template runtime exception - step('Try a template runtime exception ') - time.sleep(1) - - edit(app, 'app/views/Application/index.html', 4, "Hello ${user.name}") - try: - response = browser.reload() - self.fail() + self.assertTrue(browser.title() == 'Your application is ready !') + html = response.get_data() + self.assertTrue(html.count(b'Your application is ready !')) - except urllib.error.HTTPError as error: - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Application error') - html = b''.join(error.readlines()) - self.assertTrue(html.count(b'Template execution error ')) - self.assertTrue(html.count(b'In /app/views/Application/index.html (around line 4)')) - self.assertTrue(html.count(b'Cannot get property \'name\' on null object')) - self.assertTrue(waitFor(self.play, 'ERROR play')) - self.assertTrue( - waitFor(self.play, 'Template execution error (In /app/views/Application/index.html around line 4)')) - self.assertTrue( - waitFor(self.play, 'Execution error occurred in template /app/views/Application/index.html.')) - self.assertTrue(waitFor(self.play, 'at /app/views/Application/index.html.(line:4)')) - self.assertTrue(waitFor(self.play, '...')) - - # Refresh again - step('Refresh again') - - try: - response = browser.reload() - self.fail() + # Refresh again + step('Refresh again') - except urllib.error.HTTPError as error: + response = browser.reload() self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Application error') - html = b''.join(error.readlines()) - self.assertTrue(html.count(b'Template execution error ')) - self.assertTrue(html.count(b'In /app/views/Application/index.html (around line 4)')) - self.assertTrue(html.count(b'Cannot get property \'name\' on null object')) - self.assertTrue(waitFor(self.play, 'ERROR play')) - self.assertTrue( - waitFor(self.play, 'Template execution error (In /app/views/Application/index.html around line 4)')) - self.assertTrue( - waitFor(self.play, 'Execution error occurred in template /app/views/Application/index.html.')) - self.assertTrue(waitFor(self.play, 'at /app/views/Application/index.html.(line:4)')) - self.assertTrue(waitFor(self.play, '...')) - - # Fix it - step('Fix it') - time.sleep(1) - - edit(app, 'app/views/Application/index.html', 4, "Hello ${name} !!") - response = browser.reload() - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Hello world app') - html = response.get_data() - self.assertTrue(html.count(b'Hello Guillaume !!')) - - # Make a Java runtime exception - step('Make a Java runtime exception') - - insert(app, 'app/controllers/Application.java', 13, ' int a = 9/0;') - try: + self.assertTrue(browser.title() == 'Your application is ready !') + html = response.get_data() + self.assertTrue(html.count(b'Your application is ready !')) + + # Let's code hello world + step('Let\'s code hello world') + time.sleep(1) + + edit(app, 'app/controllers/Application.java', 12, ' public static void index(String name) {') + edit(app, 'app/controllers/Application.java', 13, ' render(name);') + edit(app, 'app/views/Application/index.html', 2, "#{set title:'Hello world app' /}") + edit(app, 'app/views/Application/index.html', 4, "Hello ${name} !!") response = browser.reload() - self.fail() + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Hello world app') + html = response.get_data() + self.assertTrue(html.count(b'Hello !!')) - except urllib.error.HTTPError as error: + response = browser.open( + 'http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'] + '/?name=Guillaume') self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Application error') - html = b''.join(error.readlines()) - self.assertTrue(html.count(b'Execution exception')) - self.assertTrue(html.count(b'/ by zero')) - self.assertTrue(html.count(b'In /app/controllers/Application.java (around line 13)')) - self.assertTrue(waitFor(self.play, 'ERROR play')) - self.assertTrue( - waitFor(self.play, 'Execution exception (In /app/controllers/Application.java around line 13)')) - self.assertTrue(waitFor(self.play, 'ArithmeticException occurred : / by zero')) - self.assertTrue(waitFor(self.play, 'at controllers.Application.index(Application.java:13)')) - self.assertTrue(waitFor(self.play, '...')) - - # Refresh again - step('Refresh again') - - try: + self.assertTrue(browser.title() == 'Hello world app') + html = response.get_data() + self.assertTrue(html.count(b'Hello Guillaume !!')) + + # Make a mistake in the template + step('Make a mistake in the template') + time.sleep(1) + + edit(app, 'app/views/Application/index.html', 4, "Hello ${name !!") + try: + response = browser.reload() + self.fail() + + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Application error') + html = b''.join(error.readlines()) + self.assertTrue(html.count(b'Template compilation error')) + self.assertTrue(html.count( + b'The template /app/views/Application/index.html does not compile : Unexpected input: \'{\' ')) + self.assertTrue(waitFor(self.play, 'ERROR play')) + self.assertTrue( + waitFor(self.play, 'Template compilation error (In /app/views/Application/index.html around line 0)')) + + # Refresh again + step('Refresh again') + + try: + response = browser.reload() + self.fail() + + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Application error') + html = b''.join(error.readlines()) + self.assertTrue(html.count(b'Template compilation error')) + self.assertTrue(html.count( + b'The template /app/views/Application/index.html does not compile : Unexpected input: \'{\' ')) + self.assertTrue(waitFor(self.play, 'ERROR play')) + self.assertTrue( + waitFor(self.play, 'Template compilation error (In /app/views/Application/index.html around line 0)')) + + # Try a template runtime exception + step('Try a template runtime exception ') + time.sleep(1) + + edit(app, 'app/views/Application/index.html', 4, "Hello ${user.name}") + try: + response = browser.reload() + self.fail() + + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Application error') + html = b''.join(error.readlines()) + self.assertTrue(html.count(b'Template execution error ')) + self.assertTrue(html.count(b'In /app/views/Application/index.html (around line 4)')) + self.assertTrue(html.count(b'Cannot get property \'name\' on null object')) + self.assertTrue(waitFor(self.play, 'ERROR play')) + self.assertTrue( + waitFor(self.play, 'Template execution error (In /app/views/Application/index.html around line 4)')) + self.assertTrue( + waitFor(self.play, 'Execution error occurred in template /app/views/Application/index.html.')) + self.assertTrue(waitFor(self.play, 'at /app/views/Application/index.html.(line:4)')) + self.assertTrue(waitFor(self.play, '...')) + + # Refresh again + step('Refresh again') + + try: + response = browser.reload() + self.fail() + + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Application error') + html = b''.join(error.readlines()) + self.assertTrue(html.count(b'Template execution error ')) + self.assertTrue(html.count(b'In /app/views/Application/index.html (around line 4)')) + self.assertTrue(html.count(b'Cannot get property \'name\' on null object')) + self.assertTrue(waitFor(self.play, 'ERROR play')) + self.assertTrue( + waitFor(self.play, 'Template execution error (In /app/views/Application/index.html around line 4)')) + self.assertTrue( + waitFor(self.play, 'Execution error occurred in template /app/views/Application/index.html.')) + self.assertTrue(waitFor(self.play, 'at /app/views/Application/index.html.(line:4)')) + self.assertTrue(waitFor(self.play, '...')) + + # Fix it + step('Fix it') + time.sleep(1) + + edit(app, 'app/views/Application/index.html', 4, "Hello ${name} !!") response = browser.reload() - self.fail() - - except urllib.error.HTTPError as error: self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Application error') - html = b''.join(error.readlines()) - self.assertTrue(html.count(b'Execution exception')) - self.assertTrue(html.count(b'/ by zero')) - self.assertTrue(html.count(b'In /app/controllers/Application.java (around line 13)')) - self.assertTrue(waitFor(self.play, 'ERROR play')) - self.assertTrue( - waitFor(self.play, 'Execution exception (In /app/controllers/Application.java around line 13)')) - self.assertTrue(waitFor(self.play, 'ArithmeticException occurred : / by zero')) - self.assertTrue(waitFor(self.play, 'at controllers.Application.index(Application.java:13)')) - self.assertTrue(waitFor(self.play, '...')) - - # Fix it - step('Fix it') - time.sleep(1) - - delete(app, 'app/controllers/Application.java', 13) - response = browser.reload() - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Hello world app') - html = response.get_data() - self.assertTrue(html.count(b'Hello Guillaume !!')) - - # Refresh again - step('Refresh again') - - response = browser.reload() - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Hello world app') - html = response.get_data() - self.assertTrue(html.count(b'Hello Guillaume !!')) - - # Create a new route - step('Create a new route') - - insert(app, 'conf/routes', 7, "GET /hello Hello.hello") - try: - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'] + '/hello') - self.fail() - except urllib.error.HTTPError as error: + self.assertTrue(browser.title() == 'Hello world app') + html = response.get_data() + self.assertTrue(html.count(b'Hello Guillaume !!')) + + # Make a Java runtime exception + step('Make a Java runtime exception') + + insert(app, 'app/controllers/Application.java', 13, ' int a = 9/0;') + try: + response = browser.reload() + self.fail() + + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Application error') + html = b''.join(error.readlines()) + self.assertTrue(html.count(b'Execution exception')) + self.assertTrue(html.count(b'/ by zero')) + self.assertTrue(html.count(b'In /app/controllers/Application.java (around line 13)')) + self.assertTrue(waitFor(self.play, 'ERROR play')) + self.assertTrue( + waitFor(self.play, 'Execution exception (In /app/controllers/Application.java around line 13)')) + self.assertTrue(waitFor(self.play, 'ArithmeticException occurred : / by zero')) + self.assertTrue(waitFor(self.play, 'at controllers.Application.index(Application.java:13)')) + self.assertTrue(waitFor(self.play, '...')) + + # Refresh again + step('Refresh again') + + try: + response = browser.reload() + self.fail() + + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Application error') + html = b''.join(error.readlines()) + self.assertTrue(html.count(b'Execution exception')) + self.assertTrue(html.count(b'/ by zero')) + self.assertTrue(html.count(b'In /app/controllers/Application.java (around line 13)')) + self.assertTrue(waitFor(self.play, 'ERROR play')) + self.assertTrue( + waitFor(self.play, 'Execution exception (In /app/controllers/Application.java around line 13)')) + self.assertTrue(waitFor(self.play, 'ArithmeticException occurred : / by zero')) + self.assertTrue(waitFor(self.play, 'at controllers.Application.index(Application.java:13)')) + self.assertTrue(waitFor(self.play, '...')) + + # Fix it + step('Fix it') + time.sleep(1) + + delete(app, 'app/controllers/Application.java', 13) + response = browser.reload() self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Not found') + self.assertTrue(browser.title() == 'Hello world app') + html = response.get_data() + self.assertTrue(html.count(b'Hello Guillaume !!')) - # Create the new controller - step('Create the new controller') - time.sleep(1) + # Refresh again + step('Refresh again') - create(app, 'app/controllers/Hello.java') - insert(app, 'app/controllers/Hello.java', 1, "package controllers;") - insert(app, 'app/controllers/Hello.java', 2, "import play.mvc.*;") - insert(app, 'app/controllers/Hello.java', 3, "public class Hello extends Application {") - insert(app, 'app/controllers/Hello.java', 4, " public static void hello() {") - insert(app, 'app/controllers/Hello.java', 5, ' renderText("Hello");') - insert(app, 'app/controllers/Hello.java', 6, ' }') - insert(app, 'app/controllers/Hello.java', 7, '}') - - # Retry - step('Retry') - - browser.reload() - self.assertTrue(not browser.viewing_html()) - html = response.get_data() - self.assertTrue(html.count(b'Hello')) - - # Rename the Hello controller - step('Rename the Hello controller') - time.sleep(1) - - rename(app, 'app/controllers/Hello.java', 'app/controllers/Hello2.java') - edit(app, 'app/controllers/Hello2.java', 3, "public class Hello2 extends Application {") - - try: - browser.reload() - self.fail() - except urllib.error.HTTPError as error: + response = browser.reload() self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Not found') - - # Refresh again - step('Refresh again') + self.assertTrue(browser.title() == 'Hello world app') + html = response.get_data() + self.assertTrue(html.count(b'Hello Guillaume !!')) + + # Create a new route + step('Create a new route') + + insert(app, 'conf/routes', 7, "GET /hello Hello.hello") + try: + response = browser.open('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'] + '/hello') + self.fail() + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Not found') + + # Create the new controller + step('Create the new controller') + time.sleep(1) + + create(app, 'app/controllers/Hello.java') + insert(app, 'app/controllers/Hello.java', 1, "package controllers;") + insert(app, 'app/controllers/Hello.java', 2, "import play.mvc.*;") + insert(app, 'app/controllers/Hello.java', 3, "public class Hello extends Application {") + insert(app, 'app/controllers/Hello.java', 4, " public static void hello() {") + insert(app, 'app/controllers/Hello.java', 5, ' renderText("Hello");') + insert(app, 'app/controllers/Hello.java', 6, ' }') + insert(app, 'app/controllers/Hello.java', 7, '}') + + # Retry + step('Retry') - try: browser.reload() - self.fail() - except urllib.error.HTTPError as error: - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Not found') + self.assertTrue(not browser.viewing_html()) + html = response.get_data() + self.assertTrue(html.count(b'Hello')) - # Correct the routes file - step('Correct the routes file') - time.sleep(1) + # Rename the Hello controller + step('Rename the Hello controller') + time.sleep(1) - edit(app, 'conf/routes', 7, "GET /hello Hello2.hello") + rename(app, 'app/controllers/Hello.java', 'app/controllers/Hello2.java') + edit(app, 'app/controllers/Hello2.java', 3, "public class Hello2 extends Application {") - browser.reload() - self.assertTrue(not browser.viewing_html()) - html = response.get_data() - self.assertTrue(html.count(b'Hello')) + try: + browser.reload() + self.fail() + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Not found') - # Retry - step('Retry') + # Refresh again + step('Refresh again') - browser.reload() - self.assertTrue(not browser.viewing_html()) - html = response.get_data() - self.assertTrue(html.count(b'Hello')) + try: + browser.reload() + self.fail() + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Not found') - # Rename again - step('Rename again') - time.sleep(1) + # Correct the routes file + step('Correct the routes file') + time.sleep(1) - rename(app, 'app/controllers/Hello2.java', 'app/controllers/Hello3.java') - edit(app, 'conf/routes', 7, "GET /hello Hello3.hello") + edit(app, 'conf/routes', 7, "GET /hello Hello2.hello") - try: browser.reload() - self.fail() + self.assertTrue(not browser.viewing_html()) + html = response.get_data() + self.assertTrue(html.count(b'Hello')) + + # Retry + step('Retry') - except urllib.error.HTTPError as error: - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Application error') - html = b''.join(error.readlines()) - self.assertTrue(html.count(b'Compilation error')) - self.assertTrue(html.count(b'/app/controllers/Hello3.java could not be compiled')) - self.assertTrue(html.count(b'The public type Hello2 must be defined in its own file')) - self.assertTrue(waitFor(self.play, 'ERROR play')) - self.assertTrue(waitFor(self.play, 'Compilation error (In /app/controllers/Hello3.java around line 3)')) - - # Refresh again - step('Refresh again') - - try: browser.reload() - self.fail() - except urllib.error.HTTPError as error: - self.assertTrue(browser.viewing_html()) - self.assertTrue(browser.title() == 'Application error') - html = b''.join(error.readlines()) - self.assertTrue(html.count(b'Compilation error')) - self.assertTrue(html.count(b'/app/controllers/Hello3.java could not be compiled')) - self.assertTrue(html.count(b'The public type Hello2 must be defined in its own file')) - self.assertTrue(waitFor(self.play, 'ERROR play')) - self.assertTrue(waitFor(self.play, 'Compilation error (In /app/controllers/Hello3.java around line 3)')) - - # Fix it - step('Fix it') - - edit(app, 'app/controllers/Hello3.java', 3, "public class Hello3 extends Application {") - browser.reload() - self.assertTrue(not browser.viewing_html()) - html = response.get_data() - self.assertTrue(html.count(b'Hello')) + self.assertTrue(not browser.viewing_html()) + html = response.get_data() + self.assertTrue(html.count(b'Hello')) + + # Rename again + step('Rename again') + time.sleep(1) + + rename(app, 'app/controllers/Hello2.java', 'app/controllers/Hello3.java') + edit(app, 'conf/routes', 7, "GET /hello Hello3.hello") + + try: + browser.reload() + self.fail() + + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Application error') + html = b''.join(error.readlines()) + self.assertTrue(html.count(b'Compilation error')) + self.assertTrue(html.count(b'/app/controllers/Hello3.java could not be compiled')) + self.assertTrue(html.count(b'The public type Hello2 must be defined in its own file')) + self.assertTrue(waitFor(self.play, 'ERROR play')) + self.assertTrue(waitFor(self.play, 'Compilation error (In /app/controllers/Hello3.java around line 3)')) + + # Refresh again + step('Refresh again') + + try: + browser.reload() + self.fail() + except urllib.error.HTTPError as error: + self.assertTrue(browser.viewing_html()) + self.assertTrue(browser.title() == 'Application error') + html = b''.join(error.readlines()) + self.assertTrue(html.count(b'Compilation error')) + self.assertTrue(html.count(b'/app/controllers/Hello3.java could not be compiled')) + self.assertTrue(html.count(b'The public type Hello2 must be defined in its own file')) + self.assertTrue(waitFor(self.play, 'ERROR play')) + self.assertTrue(waitFor(self.play, 'Compilation error (In /app/controllers/Hello3.java around line 3)')) + + # Fix it + step('Fix it') + + edit(app, 'app/controllers/Hello3.java', 3, "public class Hello3 extends Application {") + browser.reload() + self.assertTrue(not browser.viewing_html()) + html = response.get_data() + self.assertTrue(html.count(b'Hello')) + + step('Kill play') + killPlay(self.play) + + step('Done testing testSimpleProjectCreation') def tearDown(self): if self.play: @@ -833,8 +841,10 @@ def waitForWithFail(process, pattern, failPattern): sys.stdout.flush() # print timeoutOccurred if timeoutOccurred: + timer.cancel() return False if line == '@KILLED': + timer.cancel() return False if line: print(line) if failPattern != "" and line.count(failPattern): @@ -855,53 +865,41 @@ def timeout(process): timeoutOccurred = True -def killPlay(process, http='http'): +def killPlay(process, http='http', host=DEFAULTS['host'], port=DEFAULTS['http.port'], ): print("kill %s" % process.pid) try: - urllib.request.urlopen('%s://localhost:9000/@kill' % http) + urllib.request.urlopen("{}://{}:{}/@kill".format(http, host, port)) + + print("terminate") + process.terminate() + + print("process 1 (thread): kill process 2 (pid %s)" % process.pid) + process.kill() + print("process 1 (thread): close process 2 stdout pipe (fd %s)" % process.stdout.fileno()) + + if process.stdout: + process.stdout.close() + + if process.stderr: + process.stderr.close() + + if process.stdin: + process.stdin.close() + + print("wait") + process.wait(10) + # kill subprocess tree, because calling urllib.urlopen(f"{http}://localhost:9000/@kill") is not enough + while True: + if process.poll() is None: + print("Kill Play subprocess") + os.kill(process.pid, SIGTERM) + process.wait(3) + print("play is KILLED") + else: + print("play is KILLED") + return except: pass -# urllib.request.urlopen(f"{http}://" + 'localhost' + ':' + '9001' + '/@kill') - -# print("terminate" ) -# process.terminate() -# -# print("process 1 (thread): kill process 2 (pid %s)" % process.pid) -# process.kill() -# print("process 1 (thread): close process 2 stdout pipe (fd %s)" % process.stdout.fileno()) -# -# print("stdout" ) -# if process.stdout: -# process.stdout.close() -# -# if process.stderr: -# process.stderr.close() -# -# if process.stdin: -# process.stdin.close() -# -# -# print("wait" ) -# process.wait(10) -# print("wait" ) -# process.wait() -# # -# id = process.pid -# -# print("os.kill %s" % id ) -# -# # os.kill(id, SIGTERM) -# -# print("KILLED") -# # kill subprocess tree, because calling urllib.urlopen(f"{http}://localhost:9000/@kill") is not enough -# while True: -# if process.poll() is None: -# print("Kill Play subprocess") -# os.kill(process.pid, SIGTERM) -# process.wait(3) -# else: -# print("KILLED") -# return def step(msg): @@ -962,6 +960,13 @@ def rename(app, fro, to): os.rename(os.path.join(app, fro), os.path.join(app, to)) +def browserOpen(url): + browser = mechanize.Browser() + browser.set_handle_robots(False) + response = browser.open(url) + return response + + if __name__ == '__main__': # thanks to: https://stackoverflow.com/a/35960702/3221476 try: