Minifier { public static byte[] minify(byte[] contentToMinify){ try (ByteArrayInputStream bis = new ByteArrayInputStream(contentToMinify); ByteArrayOutputStream out = new ByteArrayOutputStream()){ JSMin jsmin= new JSMin(bis,out); jsmin.jsmin(); return out.toByteArray(); } catch (IOException e) { throw new GenerationException("Error when minify",e); } catch (JSMin.UnterminatedCommentException e) { throw new GenerationException("Error when minify: Unterminated Comment",e); } catch (JSMin.UnterminatedStringLiteralException e) { throw new GenerationException("Error when minify: Unterminated String",e); } catch (JSMin.UnterminatedRegExpLiteralException e) { throw new GenerationException("Error when minify: Unterminated RegExp literal",e); } }  static byte[] minify(byte[] contentToMinify);  }
@Test public void should_minify_input() throws IOException { String content = "function PbInputCtrl($scope, $log, widgetNameFactory) {\n" +"\n'use strict';\n" + "\n this.name = widgetNameFactory.getName('pbInput');\n" + "\n if (!$scope.properties.isBound('value')) {\n" + " $log.error('the pbInput property named \"value\" need to be bound to a variable');\n" + " }\n}\n"; String expected = "\n" + "function PbInputCtrl($scope,$log,widgetNameFactory){'use strict';this.name=widgetNameFactory.getName" + "('pbInput');if(!$scope.properties.isBound('value')){$log.error('the pbInput property named \"value\"" + " need to be bound to a variable');}}"; byte[] min = Minifier.minify(content.getBytes()); assertThat(new String(min,"UTF-8")).isEqualTo(expected); }
@Test(expected = GenerationException.class) public void should_throw_exeption_when_content_have_bad_unterminated_comment() throws IOException { String badCommentTemplate ="/** dffsf /"; String content = badCommentTemplate + "content"; Minifier.minify(content.getBytes()); }
@Test(expected = GenerationException.class) public void should_throw_exeption_when_content_have_bad_unterminated_string() throws IOException { String unterminatedString = "'use strict\n"; String content = "function PbInputCtrl($scope, $log, widgetNameFactory) { \n" + unterminatedString; Minifier.minify(content.getBytes()); }