0

I am trying to make tests for ActivityStarter library, which is using annotation processing, but I get strange error:

An expected source declared one or more top-level types that were not present.

Expected top-level types: <[MainActivityStarter]>
Declared by expected file:       
<com/example/activitystarter/MainActivityStarter.java>

The top-level types that were present are as follows: 

- [ainActivityStarter] in </SOURCE_OUTPUT/ainActivityStarter.java>

When I started:

import com.google.testing.compile.JavaFileObjects

import org.junit.Test

import javax.tools.JavaFileObject

import activitystarter.compiler.ActivityStarterProcessor

import com.google.common.truth.Truth.assertAbout
import com.google.testing.compile.JavaSourceSubjectFactory
import org.junit.Assert.*
import com.google.testing.compile.JavaSourceSubjectFactory.javaSource
import com.google.testing.compile.JavaSourcesSubject
import com.google.testing.compile.JavaSourcesSubject.SingleSourceAdapter
import com.google.testing.compile.JavaSourcesSubjectFactory.javaSources


class ActivityStarterTest() {

    @Test
    fun simpleGenerationTest() {
        val beforeProcess = "com.example.activitystarter.MainActivity" to """
import android.app.Activity;
import activitystarter.MakeActivityStarter;

@MakeActivityStarter
public class MainActivity extends Activity {}
        """

        val afterProcess = "com.example.activitystarter.MainActivityStarter" to """
import android.content.Context;
import android.content.Intent;
import android.support.annotation.UiThread;

public class MainActivityStarter {
    @UiThread
    public static void fill(MainActivity activity) {
    }

    @UiThread
    public static void start(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @UiThread
    public static void startWithFlags(Context context, int flags) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(flags);
        context.startActivity(intent);
    }

    @UiThread
    public static Intent getIntent(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        return intent;
    }
}
        """

        processingComparator(beforeProcess, afterProcess)
    }

    fun processingComparator(beforeProcess: Pair<String, String>, afterProcess: Pair<String, String>) {
        val source = JavaFileObjects.forSourceString(beforeProcess.first, beforeProcess.second)
        val bindingSource = JavaFileObjects.forSourceString(afterProcess.first, afterProcess.second)
        assertAbout<SingleSourceAdapter, JavaFileObject, JavaSourceSubjectFactory>(javaSource())
                .that(source)
                .processedWith(ActivityStarterProcessor())
                .compilesWithoutWarnings()
                .and()
                .generatesSources(bindingSource)
    }
}

Both before and after process code was taken from real code before and after process, so it should be ok. Also, in lib there is no way of cutting this first letter:

JavaFile brewJava() {
    return JavaFile.builder(bindingClassName.packageName(), getActivityStarterSpec())
            .addFileComment("Generated code from ActivityStarter. Do not modify!")
            .build();
}

private TypeSpec getActivityStarterSpec() {
    TypeSpec.Builder result = TypeSpec
            .classBuilder(bindingClassName.simpleName())
            .addModifiers(PUBLIC);

and:

    bindingClassName = ClassName.get(packageName, className + "Starter");

Any idea how do deal with it?

dimo414
  • 47,227
  • 18
  • 148
  • 244
MarcinM
  • 428
  • 7
  • 15
  • I would "deal" with this by filing a bug report. – john16384 Feb 05 '17 at 13:38
  • It's not clear to me what you're asking. The "source ... not present" error presumably means your build script is missing some files, but you didn't include that information in your question. Your Java source code seems to have a number of compilation errors (unless it's not actually Java?), and I've no idea what you mean by your titular question about "cutting the first letter" - do you want to strip the word "Starter"? Please post separate questions rather than munging them all into one, and update your question with more context so we can help you resolve it. – dimo414 Apr 20 '17 at 17:50

0 Answers0