view truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTest.java @ 22104:cf19259edf87

TruffleVM.eval and Source.withMimeType
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 24 Aug 2015 08:46:21 +0200
parents
children daaebd13b27a
line wrap: on
line source

/*
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.truffle.api.test.source;

import com.oracle.truffle.api.source.Source;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;

public class SourceTest {
    @Test
    public void assignMimeTypeAndIdentity() {
        Source s1 = Source.fromText("// a comment\n", "Empty comment");
        assertNull("No mime type assigned", s1.getMimeType());
        Source s2 = s1.withMimeType("text/x-c");
        assertEquals("They have the same content", s1.getCode(), s2.getCode());
        assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType());
        assertNotEquals("So they are different", s1, s2);
    }

    @Test
    public void assignMimeTypeAndIdentityForApppendable() {
        Source s1 = Source.fromAppendableText("<stdio>");
        assertNull("No mime type assigned", s1.getMimeType());
        s1.appendCode("// Hello");
        Source s2 = s1.withMimeType("text/x-c");
        assertEquals("They have the same content", s1.getCode(), s2.getCode());
        assertEquals("// Hello", s1.getCode());
        assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType());
        assertNotEquals("So they are different", s1, s2);
    }

    @Test
    public void assignMimeTypeAndIdentityForBytes() {
        String text = "// Hello";
        Source s1 = Source.fromBytes(text.getBytes(StandardCharsets.UTF_8), "Hello", StandardCharsets.UTF_8);
        assertNull("No mime type assigned", s1.getMimeType());
        Source s2 = s1.withMimeType("text/x-c");
        assertEquals("They have the same content", s1.getCode(), s2.getCode());
        assertEquals("// Hello", s1.getCode());
        assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType());
        assertNotEquals("So they are different", s1, s2);
    }

    @Test
    public void assignMimeTypeAndIdentityForReader() throws IOException {
        String text = "// Hello";
        Source s1 = Source.fromReader(new StringReader(text), "Hello");
        assertNull("No mime type assigned", s1.getMimeType());
        Source s2 = s1.withMimeType("text/x-c");
        assertEquals("They have the same content", s1.getCode(), s2.getCode());
        assertEquals("// Hello", s1.getCode());
        assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType());
        assertNotEquals("So they are different", s1, s2);
    }

    @Test
    public void assignMimeTypeAndIdentityForFile() throws IOException {
        File file = File.createTempFile("Hello", ".java");
        file.deleteOnExit();

        String text;
        try (FileWriter w = new FileWriter(file)) {
            text = "// Hello";
            w.write(text);
        }

        Source s1 = Source.fromFileName(file.getPath());
        assertEquals("Recognized as Java", "text/x-java", s1.getMimeType());
        Source s2 = s1.withMimeType("text/x-c");
        assertEquals("They have the same content", s1.getCode(), s2.getCode());
        assertEquals("// Hello", s1.getCode());
        assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType());
        assertNotEquals("So they are different", s1, s2);
    }

    @Test
    public void assignMimeTypeAndIdentityForVirtualFile() throws IOException {
        File file = File.createTempFile("Hello", ".java");
        file.deleteOnExit();

        String text = "// Hello";

        Source s1 = Source.fromFileName(text, file.getPath());
        assertEquals("Recognized as Java", "text/x-java", s1.getMimeType());
        Source s2 = s1.withMimeType("text/x-c");
        assertEquals("They have the same content", s1.getCode(), s2.getCode());
        assertEquals("// Hello", s1.getCode());
        assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType());
        assertNotEquals("So they are different", s1, s2);
    }

    @Test
    public void assignMimeTypeAndIdentityForURL() throws IOException {
        File file = File.createTempFile("Hello", ".java");
        file.deleteOnExit();

        String text;
        try (FileWriter w = new FileWriter(file)) {
            text = "// Hello";
            w.write(text);
        }

        Source s1 = Source.fromURL(file.toURI().toURL(), "Hello.java");
        assertEquals("Threated as plain", "text/plain", s1.getMimeType());
        Source s2 = s1.withMimeType("text/x-c");
        assertEquals("They have the same content", s1.getCode(), s2.getCode());
        assertEquals("// Hello", s1.getCode());
        assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType());
        assertNotEquals("So they are different", s1, s2);
    }
}