/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.nativeplatform.test.plugins;

import org.gradle.api.Action;
import org.gradle.api.Incubating;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.language.nativeplatform.DependentSourceSet;
import org.gradle.model.Defaults;
import org.gradle.model.ModelMap;
import org.gradle.model.RuleSource;
import org.gradle.nativeplatform.plugins.NativeComponentPlugin;
import org.gradle.nativeplatform.tasks.InstallExecutable;
import org.gradle.nativeplatform.test.NativeTestSuiteBinarySpec;
import org.gradle.nativeplatform.test.internal.DefaultNativeTestSuiteBinarySpec;
import org.gradle.nativeplatform.test.internal.NativeTestSuiteBinarySpecInternal;
import org.gradle.nativeplatform.test.tasks.RunTestExecutable;
import org.gradle.platform.base.BinarySpec;
import org.gradle.platform.base.BinaryType;
import org.gradle.platform.base.TypeBuilder;
import org.gradle.platform.base.internal.BinaryNamingScheme;
import org.gradle.testing.base.plugins.TestingModelBasePlugin;

/**
 * A plugin that sets up the infrastructure for testing native binaries.
 */
@Incubating
public class NativeBinariesTestPlugin implements Plugin<Project> {

    public void apply(final Project project) {
        project.getPluginManager().apply(NativeComponentPlugin.class);
        project.getPluginManager().apply(TestingModelBasePlugin.class);
    }

    @SuppressWarnings("UnusedDeclaration")
    static class Rules extends RuleSource {
        @BinaryType
        void nativeTestSuiteBinary(TypeBuilder<NativeTestSuiteBinarySpec> builder) {
            builder.defaultImplementation(DefaultNativeTestSuiteBinarySpec.class);
            builder.internalView(NativeTestSuiteBinarySpecInternal.class);
        }

        @Defaults
        // TODO:LPTR This should be @Finalize @Each NativeTestSuiteBinarySpecInternal
        void attachTestedBinarySourcesToTestBinaries(ModelMap<NativeTestSuiteBinarySpecInternal> binaries) {
            binaries.afterEach(new Action<NativeTestSuiteBinarySpecInternal>() {
                @Override
                public void execute(NativeTestSuiteBinarySpecInternal testSuiteBinary) {
                    BinarySpec testedBinary = testSuiteBinary.getTestedBinary();
                    for (DependentSourceSet testSource : testSuiteBinary.getInputs().withType(DependentSourceSet.class)) {
                        testSource.lib(testedBinary.getInputs());
                    }
                    testSuiteBinary.getInputs().addAll(testedBinary.getInputs());
                }
            });
        }

        @Defaults
        // TODO:LPTR This should be @Finalize @Each NativeTestSuiteBinarySpecInternal
        void configureRunTask(ModelMap<NativeTestSuiteBinarySpecInternal> binaries) {
            binaries.afterEach(new Action<NativeTestSuiteBinarySpecInternal>() {
                @Override
                public void execute(NativeTestSuiteBinarySpecInternal testSuiteBinary) {
                    BinaryNamingScheme namingScheme = testSuiteBinary.getNamingScheme();
                    NativeTestSuiteBinarySpec.TasksCollection tasks = testSuiteBinary.getTasks();
                    InstallExecutable installTask = (InstallExecutable) tasks.getInstall();
                    RunTestExecutable runTask = (RunTestExecutable) tasks.getRun();
                    runTask.getInputs().files(installTask.getOutputs().getFiles());
                    runTask.setExecutable(installTask.getRunScript().getPath());
                    Project project = runTask.getProject();
                    runTask.setOutputDir(namingScheme.getOutputDirectory(project.getBuildDir(), "test-results"));
                }
            });
        }
    }
}

@@ -815,32 +815,21 @@
 age.
-nativeplatform.Dependent
+base.Language
 Sour
@@ -857,56 +857,50 @@
 dle.
-model.Defaults;%0Aimport org.gradle.model.ModelMap
+language.nativeplatform.DependentSourceSet
 ;%0Aim
@@ -921,26 +921,17 @@
 e.model.
-RuleSource
+*
 ;%0Aimport
@@ -2327,355 +2327,82 @@
    @
-Defaults%0A        // TODO:LPTR This should be @Finalize @Each NativeTestSuiteBinarySpecInternal%0A        void attachTestedBinarySourcesToTestBinaries(ModelMap%3CNativeTestSuiteBinarySpecInternal%3E binaries) %7B%0A            binaries.afterEach(new Action%3CNativeTestSuiteBinarySpecInternal%3E() %7B%0A                @Override%0A                public void execute(
+Finalize%0A        void attachTestedBinarySourcesToTestBinaries(@Each final 
 Nati
@@ -2458,31 +2458,29 @@
             
-       
+final
  BinarySpec 
@@ -2544,53 +2544,8 @@
     
-         for (DependentSourceSet testSource :
  tes
@@ -2603,24 +2603,151 @@
 t.class)
+.all(new Action%3CDependentSourceSet%3E() %7B%0A                @Override%0A                public void execute(DependentSourceSet testSource
 ) %7B%0A
-    
         
@@ -2812,38 +2812,38 @@
             
+%7D%0A
     
-%7D%0A
         
             
@@ -2822,32 +2822,36 @@
   %7D%0A            
+%7D);%0A
             test
@@ -2842,37 +2842,34 @@
             test
-Suit
 e
+d
 Binary.getInputs
@@ -2876,74 +2876,70 @@
 ().a
-ddAll(testedBinary.getInputs());%0A                %7D%0A            %7D);
+ll(new Action%3CLanguageSourceSet%3E() %7B%0A                @Override
 %0A   
@@ -2943,27 +2943,24 @@
         
-%7D%0A%0A
         
 @Default
@@ -2955,270 +2955,128 @@
     
-@Defaults%0A        // TODO:LPTR This should be @Finalize @Each NativeTestSuiteBinarySpecInternal%0A        void configureRunTask(ModelMap%3CNativeTestSuiteBinarySpecInternal%3E binaries) %7B%0A            binaries.afterEach(new Action%3CNativeTestSuiteBinarySpecInternal%3E() %7B
+public void execute(LanguageSourceSet testedSource) %7B%0A                    testSuiteBinary.getInputs().add(testedSource);
 %0A   
@@ -3084,33 +3084,25 @@
             
-@Override
+%7D
 %0A           
@@ -3106,32 +3106,77 @@
     
-    public void execute(
+%7D);%0A        %7D%0A%0A        @Finalize%0A        void configureRunTask(@Each 
 Nati
@@ -3220,32 +3220,24 @@
 teBinary) %7B%0A
-        
             
@@ -3317,24 +3317,16 @@
         
- 
-       
 NativeTe
@@ -3391,32 +3391,24 @@
 getTasks();%0A
-        
             
@@ -3491,24 +3491,16 @@
         
-        
 RunTestE
@@ -3563,32 +3563,24 @@
             
-       
- 
 runTask.getI
@@ -3639,32 +3639,24 @@
             
-        
 runTask.setE
@@ -3716,24 +3716,16 @@
         
-      
-  
 Project 
@@ -3752,32 +3752,24 @@
 tProject();%0A
-        
             
@@ -3866,42 +3866,8 @@
 ));%0A
-                %7D%0A            %7D);%0A
     

Use `@Each` instead of manual iteration of ModelMap