Example #1
0
  /**
   * Tests the S3 access.
   *
   * @throws Exception if test fails
   */
  @Test
  public void s3Test() throws Exception {
    // Create a real object and mock its initClient method
    final S3NutDao dao = spy(new S3NutDao("/path", false, null, -1, "wuic", "login", "pwd", false));

    // Build client mock
    final AmazonS3Client client = mock(AmazonS3Client.class);
    when(dao.initClient()).thenReturn(client);

    // List returned by client
    final ObjectListing list = mock(ObjectListing.class);
    final S3ObjectSummary summary = mock(S3ObjectSummary.class);
    when(summary.getKey()).thenReturn("[cloud].css");
    final S3ObjectSummary summarBis = mock(S3ObjectSummary.class);
    when(summarBis.getKey()).thenReturn("cloud.css");
    when(client.listObjects(any(ListObjectsRequest.class))).thenReturn(list);
    when(list.getObjectSummaries()).thenReturn(Arrays.asList(summary, summarBis));

    // Bytes returned by mocked S3
    final byte[] array = ".cloud { text-align : justify;}".getBytes();
    final S3Object object = mock(S3Object.class);
    when(object.getObjectContent())
        .thenReturn(new S3ObjectInputStream(new ByteArrayInputStream(array), null));
    when(client.getObject(anyString(), anyString())).thenReturn(object);

    // TODO : problem here : we specify '[cloud.css]' but getNuts() returns 'cloud.css' because
    // regex are always activated !
    final NutsHeap nutsHeap = new NutsHeap(Arrays.asList("[cloud].css"), dao, "heap");
    Assert.assertEquals(nutsHeap.getNuts().size(), 1);

    final Engine compressor = new CssYuiCompressorEngine(true, "UTF-8", -1);
    final Engine cacheEngine = new EhCacheEngine(false, null);
    final Engine aggregator = new CGTextAggregatorEngine(true);
    cacheEngine.setNext(compressor);
    compressor.setNext(aggregator);

    final List<Nut> group =
        cacheEngine.parse(new EngineRequest("", "", nutsHeap, new HashMap<NutType, Engine>()));

    Assert.assertFalse(group.isEmpty());
    InputStream is;

    for (Nut res : group) {
      is = res.openStream();
      Assert.assertTrue(IOUtils.readString(new InputStreamReader(is)).length() > 0);
      is.close();
    }
  }
Example #2
0
  /**
   * Test when multiple @import/background are declared on the same line.
   *
   * @throws Exception if test fails
   */
  @Test
  public void multipleImportPerLineTest() throws Exception {
    final AtomicInteger createCount = new AtomicInteger(0);
    final NutDao dao = Mockito.mock(NutDao.class);
    Mockito.when(dao.withRootPath(Mockito.anyString())).thenReturn(dao);
    final Engine engine = new CGCssInspectorEngine(true, "UTF-8");
    final NutsHeap heap = Mockito.mock(NutsHeap.class);
    Mockito.when(dao.create(Mockito.anyString(), Mockito.any(NutDao.PathFormat.class)))
        .thenAnswer(
            new Answer<Object>() {

              /** {@inheritDoc} */
              @Override
              public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
                final List<Nut> retval = new ArrayList<Nut>();
                final Nut nut = Mockito.mock(Nut.class);
                Mockito.when(nut.getName())
                    .thenReturn(String.valueOf(createCount.incrementAndGet()));
                Mockito.when(nut.getNutType()).thenReturn(NutType.CSS);
                Mockito.when(nut.openStream()).thenReturn(new ByteArrayInputStream("".getBytes()));
                Mockito.when(nut.getVersionNumber()).thenReturn(new BigInteger("1"));

                retval.add(nut);
                return retval;
              }
            });
    Mockito.when(heap.getId()).thenReturn("heap");
    Mockito.when(heap.hasCreated(Mockito.any(Nut.class))).thenReturn(true);
    Mockito.when(heap.findDaoFor(Mockito.any(Nut.class))).thenReturn(dao);
    final NutsHeap h = new NutsHeap(null, dao, "heap", heap);
    final List<Nut> nuts = new ArrayList<Nut>();
    final Nut nut = Mockito.mock(Nut.class);
    Mockito.when(nut.getVersionNumber()).thenReturn(new BigInteger("1"));
    Mockito.when(nut.getNutType()).thenReturn(NutType.CSS);
    Mockito.when(nut.getName())
        .thenAnswer(
            new Answer<Object>() {

              /** {@inheritDoc} */
              @Override
              public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
                final String retval = createCount.get() + ".css";
                h.getCreated().add(retval);
                return retval;
              }
            });
    nuts.add(nut);

    String[][] collection =
        new String[][] {
          new String[] {"@import url(\"%s\")", "jquery.ui.core.css"},
          new String[] {"@import \"%s\";", "jquery.ui.accordion.css"},
          new String[] {"@import '%s';", "jquery.ui.autocomplete.css"},
          new String[] {"@import url('%s');", "jquery.ui.button.css"},
          new String[] {"@import \"%s\";", "jquery.ui.datepicker.css"},
          new String[] {"@import '%s';", "jquery.ui.dialog.css"},
          new String[] {"@import url(  \"%s\");", "jquery.ui.menu.css"},
          new String[] {"background: url(\"%s\");", "sprite.png"},
          new String[] {"background: url(%s);", "sprite2.png"},
          new String[] {"background: url('%s');", "sprite3.png"},
          new String[] {"background: #FFF url('%s');", "sprite4.png"},
          new String[] {"@import /* some comments */ url(\"%s\");", "jquery.ui.spinner.css"},
        };

    final StringBuilder builder = new StringBuilder();
    int create = 0;

    for (final String[] c : collection) {
      final String rule = c[0];
      final String path = c[1];
      builder.append(String.format(rule, path));
      create++;
    }

    // ignore comments
    builder.append("/*background: url('sprite5.png');*/");
    builder.append("/*background:\n url('sprite6.png');*/");

    Mockito.when(nut.openStream())
        .thenReturn(new ByteArrayInputStream(builder.toString().getBytes()));
    Mockito.when(heap.getNuts()).thenReturn(nuts);
    Mockito.when(heap.getNutDao()).thenReturn(dao);
    Mockito.when(heap.findDaoFor(Mockito.mock(Nut.class))).thenReturn(dao);
    Mockito.when(heap.getCreated()).thenReturn(new HashSet<String>());

    final EngineRequest request =
        new EngineRequest("wid", "cp", h, new HashMap<NutType, NodeEngine>());
    engine.parse(request);
    Assert.assertEquals(createCount.get(), create);
  }