text:
<!DOCTYPE frameset SYSTEM "frameset.dtd">
<frameset>
    <predicate lemma="absent">
        <note>
            Frames file for 'absent' based on sentences in wsj.  No Verbnet entry.
        </note>

        <roleset id="absent.01" vncls="-" name="cause to be absent">
            <roles>
                <role n="0"  descr="causer, agent"/>
                <role n="1"  descr="absent entity"/>
                <role n="2"  descr="absent from what"/>
            </roles>

            <example name="actually a preposition here">
                <inflection person="ns" tense="ns" aspect="ns" voice="ns" form="ns"/>
                <text>
                    Absent other working capital, he said, the RTC would be forced to
                    delay other thrift resolutions until cash could be raised by
                    selling the bad assets.
                </text>
                <rel>Absent</rel>
                <arg n="1">other working capital</arg>
            </example>

            <example name="all args">
                <inflection person="ns" tense="past" aspect="ns" voice="active" form="full"/>
                <text>
                    John absented himself from yet another Propbank meeting.
                </text>
                <arg n="0"        >John</arg>
                <rel              >absented</rel>
                <arg n="1" f="REC">himself</arg>
                <arg n="2">from yet another Propbank meeting</arg>
            </example>

            <note>
            </note>

        </roleset>
    </predicate>
</frameset>


text:
<?php

/**
 * 	TestHelper
 *
 * 	@author		Evan Johnson
 * 	@created	Apr 26, 2015
 * 	@version	1.0
 */

namespace MockMaker\TestHelper;

class TestHelper
{

    /**
     * Returns the path to the root directory
     *
     * @return  string
     */
    public static function getRootDir()
    {
        return dirname(dirname(dirname(dirname(__FILE__)))) . '/';
    }

    /**
     * Use to output variables to terminal or browser
     * along with descriptions and optional fatality.
     *
     * This should also be able to handle  a single Doctrine entity
     * without dumping an obscene amount of recursion crap on the screen.
     * If you have a doctrine entity as part of another object (i.e. oneToMany entities)
     * it still gets everything dumped for the child entity properties. :(
     *
     * @param	mixed	$var	Variable to debug
     * @param	string	$txt	Text to output with debug (optional)
     * @param	bool	$die	To kill, or not to kill (optional)
     * @param	string	$eol	Use
 or \n for browser or terminal output (optional)
     * @return	stdOutput
     */
    public static function dbug($var, $txt = null, $die = false, $eol = "\n")
    {
        if (stristr($eol, 'br') !== FALSE) {
            echo "
";
        }
        echo "{$eol}{$eol}-------------------------{$eol}";
        if (!is_null($txt)) {
            echo "$txt{$eol}{$eol}";
        }
        echo "Var is type: " . gettype($var) . "{$eol}{$eol}";
        switch (TRUE) {
            case ($var === FALSE):
                echo 'FALSE';
                break;
            case ($var === TRUE):
                echo 'TRUE';
                break;
            case ($var === null):
                echo 'NULL';
                break;
            case (is_object($var)):
                if ($var instanceof \Exception) {
                    echo $var->getTraceAsString();
                    break;
                }
                print_r($var);
                break;
            case (is_array($var)):
                print_r($var);
                break;
            default:
                echo $var;
                break;
        }
        echo "{$eol}-------------------------{$eol}{$eol}";
        if (stristr($eol, 'br') !== FALSE) {
            echo "
";
        }
        if ($die) {
            die();
        }
    }

    /**
     * Quick one liner variable inspection.
     *
     * @param	$var	mixed	Variable to debug
     * @param	$txt	string	Output text preceding debug (optional)
     * @param	$eol	string	Use
 or \n for browser or terminal output (optional)
     */
    public static function oneLine($var, $txt = null, $eol = "\n")
    {
        $string = (!is_null($txt) ) ? "{$txt} | " : "";
        $string .= 'var type:\'' . gettype($var) . '\' : value:\'';
        switch (TRUE) {
            case ($var === FALSE):
                $string .= 'FALSE';
                break;
            case ($var === TRUE):
                $string .= 'TRUE';
                break;
            case ($var === null):
                $string .= 'NULL';
                break;
            case (is_object($var)):
                $string .= 'OBJECT';
                break;
            case (is_array($var)):
                $string .= 'ARRAY';
                break;
            default:
                $string .= $var;
                break;
        }
        $string .= "'{$eol}";
        echo $string;
    }

    /**
     * This uses reflection to set the value of a private/protected parameter
     * to a desired value.
     *
     * Unless you're changing the values of a Mockery/Prophecy created object,
     * you can usually pass the same object as the first 2 parameters.
     *
     * Warning! Uses pass-by-reference!
     *
     * @param	$objToChange		object	Object whose property you want to change.
     * @param	$objToReflect		mixed	Object to use as a base for inspecting the property.
     * @param	$property			string	Name of the property you want to change.
     * @param	$value				string	Value to set the property to.
     */
    public static function setNonPublicValue(&$objToChange, $objToReflect, $property, $value)
    {
        $class = (is_object($objToReflect)) ? get_class($objToReflect) : $objToReflect;
        $refClass = new \ReflectionClass($class);
        $refProp = $refClass->getProperty($property);
        $refProp->setAccessible(true);
        $refProp->setValue($objToChange, $value);
    }

    /**
     * Make non-public methods accessible.
     *
     * $actual = $method->invoke( <class>, <param1>, [<param2>...] );
     *
     * @param	$class			string	Class you want to access method in.
     * @param	$methodName		string	Method you want to make public.
     * @return	\ReflectionMethod
     */
    public static function getAccessibleNonPublicMethod($class, $methodName)
    {
        $className = (is_object($class)) ? get_class($class) : $class;
        $reflection = new \ReflectionClass($className);
        $method = $reflection->getMethod($methodName);
        $method->setAccessible(TRUE);

        return $method;
    }

}

redpajama_set_name: RedPajamaGithub


text:
class User < ActiveRecord::Base
  validates :email, uniqueness: true, presence: true
  after_destroy :destroy_customer

  def customer
    customer_id && BraintreeRails::Customer.new(customer_id)
  end

  private
  def destroy_customer
    BraintreeRails::Customer.delete(customer_id) if customer_id.present?
  end
end


text:
<?php

namespace App\Tests\Functional\Services\FixtureLoader;

use App\Entity\Task\TaskType;
use App\Services\FixtureLoader\TaskTypeFixtureLoader;

class TaskTypeFixtureLoaderTest extends AbstractFixtureLoaderTest
{
    /**
     * @var TaskTypeFixtureLoader
     */
    private $taskTypeFixtureLoader;

    /**
     * @var array
     */
    private $taskTypeData;

    protected function setUp(): void
    {
        parent::setUp();

        $this->taskTypeFixtureLoader = self::$container->get(TaskTypeFixtureLoader::class);
        $taskTypesDataProvider = self::$container->get('app.services.data-provider.task-types');
        $this->taskTypeData = $taskTypesDataProvider->getData();
    }

    protected function getEntityClass(): string
    {
        return TaskType::class;
    }

    protected function getEntityClassesToRemove(): array
    {
        return [
            TaskType::class,
        ];
    }

    public function testLoadFromEmpty()
    {
        $this->assertEmpty($this->repository->findAll());

        $this->taskTypeFixtureLoader->load();

        $this->assertRepositoryTaskTypes($this->taskTypeData);
    }

    public function testLoadFromNonEmpty()
    {
        $this->assertEmpty($this->repository->findAll());

        $name = key($this->taskTypeData);
        $taskTypeProperties = current($this->taskTypeData);

        $reflectionMethod = new \ReflectionMethod(TaskTypeFixtureLoader::class, 'loadTaskType');
        $reflectionMethod->setAccessible(true);
        $reflectionMethod->invoke($this->taskTypeFixtureLoader, $name, $taskTypeProperties);

        $this->assertRepositoryTaskTypes([
            $name => $taskTypeProperties,
        ]);

        $this->taskTypeFixtureLoader->load();
        $this->assertRepositoryTaskTypes($this->taskTypeData);
    }

    private function assertRepositoryTaskTypes(array $taskTypeData)
    {
        $repositoryJobTypes = $this->repository->findAll();

        $this->assertCount(count($taskTypeData), $repositoryJobTypes);

        /* @var TaskType[] $taskTypes */
        $taskTypes = [];

        foreach ($repositoryJobTypes as $taskType) {
            $taskTypes[$taskType->getName()] = $taskType;
        }

        foreach ($taskTypeData as $name => $taskTypeProperties) {
            $taskType = $taskTypes[$name];

            $this->assertEquals($name, $taskType->getName());
            $this->assertEquals($taskTypeProperties['description'], $taskType->getDescription());
            $this->assertEquals($taskTypeProperties['selectable'], $taskType->getSelectable());
        }
    }
}


text:
/* { dg-do compile } */
/* { dg-options "-fgnu-tm" } */

#define TC	__attribute__((transaction_callable))
#define TU	__attribute__((transaction_unsafe))
#define TP	__attribute__((transaction_pure))
#define TS	__attribute__((transaction_safe))
extern void f1(void) TC;
extern void f2(void) TU;
extern void f3(void) TP;
extern void f4(void) TS;

extern void g1(void) TC TS;	/* { dg-error "previously declared" } */

extern int v1 TP;		/* { dg-warning "ignored" } */

typedef void t1(void) TC;
typedef void (*t2)(void) TC;
typedef int t3 TC;		/* { dg-warning "ignored" } */

typedef void u0(void);
typedef u0 u1 TC;
typedef u1 u2 TP;		/* { dg-error "previously declared" } */
typedef u0 *u3 TS;
typedef u3 u4 TU;		/* { dg-error "previously declared" } */


text:
class PublicController < ApplicationController
  layout "public"
  skip_before_action :authenticate!
end


text:
using System;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Management.SiteRecovery.Models;

namespace Microsoft.WindowsAzure.Management.SiteRecovery.Models
{
    /// <summary>
    /// The response model for the Vm object.
    /// </summary>
    public partial class ProtectionEntityResponse : OperationResponse
    {
        private ProtectionEntity _protectionEntity;

        /// <summary>
        /// Optional. Protection Entity object
        /// </summary>
        public ProtectionEntity ProtectionEntity
        {
            get { return this._protectionEntity; }
            set { this._protectionEntity = value; }
        }

        /// <summary>
        /// Initializes a new instance of the ProtectionEntityResponse class.
        /// </summary>
        public ProtectionEntityResponse()
        {
        }
    }
}


text:
all:
	@echo "Generated tests.cpp"
	@bin/cxxtestgen --error-printer -o tests.cpp tests.h
	@echo "Compiling tests"
	@g++ -I /home/manager/Desktop/BioInf/LinuxUnitTest/ ../WaveletTree/WaveletTree/RRR.cpp ../WaveletTree/WaveletTree/RRRTable.cpp ../WaveletTree/WaveletTree/WaveletNode.cpp ../WaveletTree/WaveletTree/WaveletTree.cpp -o main tests.cpp -std=c++11
	@echo "Running tests"
	@./main


text:
/**
 * @module
 * @description
 * The `di` module provides dependency injection container services.
 */
"use strict";
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
var metadata_1 = require('./di/metadata');
exports.HostMetadata = metadata_1.HostMetadata;
exports.InjectMetadata = metadata_1.InjectMetadata;
exports.InjectableMetadata = metadata_1.InjectableMetadata;
exports.OptionalMetadata = metadata_1.OptionalMetadata;
exports.SelfMetadata = metadata_1.SelfMetadata;
exports.SkipSelfMetadata = metadata_1.SkipSelfMetadata;
// we have to reexport * because Dart and TS export two different sets of types
__export(require('./di/decorators'));
var forward_ref_1 = require('./di/forward_ref');
exports.forwardRef = forward_ref_1.forwardRef;
exports.resolveForwardRef = forward_ref_1.resolveForwardRef;
var injector_1 = require('./di/injector');
exports.Injector = injector_1.Injector;
var reflective_injector_1 = require('./di/reflective_injector');
exports.ReflectiveInjector = reflective_injector_1.ReflectiveInjector;
var provider_1 = require('./di/provider');
exports.Binding = provider_1.Binding;
exports.ProviderBuilder = provider_1.ProviderBuilder;
exports.bind = provider_1.bind;
exports.Provider = provider_1.Provider;
exports.provide = provider_1.provide;
var reflective_provider_1 = require('./di/reflective_provider');
exports.ResolvedReflectiveFactory = reflective_provider_1.ResolvedReflectiveFactory;
var reflective_key_1 = require('./di/reflective_key');
exports.ReflectiveKey = reflective_key_1.ReflectiveKey;
var reflective_exceptions_1 = require('./di/reflective_exceptions');
exports.NoProviderError = reflective_exceptions_1.NoProviderError;
exports.AbstractProviderError = reflective_exceptions_1.AbstractProviderError;
exports.CyclicDependencyError = reflective_exceptions_1.CyclicDependencyError;
exports.InstantiationError = reflective_exceptions_1.InstantiationError;
exports.InvalidProviderError = reflective_exceptions_1.InvalidProviderError;
exports.NoAnnotationError = reflective_exceptions_1.NoAnnotationError;
exports.OutOfBoundsError = reflective_exceptions_1.OutOfBoundsError;
var opaque_token_1 = require('./di/opaque_token');
exports.OpaqueToken = opaque_token_1.OpaqueToken;
//# sourceMappingURL=di.js.map


text:
// commit package provides commit logs for storing mutations, as they arrive
// at the server. Mutations also get stored in memory within posting.List.
// So, commit logs are useful to handle machine crashes, and re-init of a
// posting list.
// This package provides functionality to write to a rotating log, and a way
// to quickly filter relevant entries corresponding to an attribute.
package commit

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"runtime/debug"
	"sort"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"time"
	"unsafe"

	"github.com/Sirupsen/logrus"
	"github.com/dgraph-io/dgraph/x"
	"github.com/willf/bloom"
)

var glog = x.Log("commitlog")

type logFile struct {
	sync.RWMutex
	endTs int64 // never modified after creation.
	path  string
	size  int64
	cache *Cache
	bf    *bloom.BloomFilter
}

func (lf *logFile) Cache() *Cache {
	lf.RLock()
	defer lf.RUnlock()
	return lf.cache
}

func (lf *logFile) FillIfEmpty(wg *sync.WaitGroup) {
	lf.Lock()
	defer lf.Unlock()
	defer wg.Done()
	if lf.cache != nil {
		return
	}
	cache := new(Cache)
	if err := FillCache(cache, lf.path); err != nil {
		glog.WithError(err).WithField("path", lf.path).
			Fatal("Unable to fill cache.")
	}
	// No need to acquire lock on cache, because it just
	// got created.
	createAndUpdateBloomFilter(cache)
	lf.cache = cache
}

// Lock must have been acquired.
func createAndUpdateBloomFilter(cache *Cache) {
	hashes := make([]uint32, 50000)
	hashes = hashes[:0]
	if err := streamEntries(cache, 0, 0, func(hdr Header, record []byte) {
		hashes = append(hashes, hdr.hash)
	}); err != nil {
		glog.WithError(err).Fatal("Unable to create bloom filters.")
	}

	n := 100000
	if len(hashes) > n {
		n = len(hashes)
	}
	cache.bf = bloom.NewWithEstimates(uint(n), 0.0001)
	for _, hash := range hashes {
		cache.bf.Add(toBytes(hash))
	}
}

type CurFile struct {
	sync.RWMutex
	f         *os.File
	size      int64
	dirtyLogs int
	cch       unsafe.Pointer // handled via atomics.
}

func (c *CurFile) cache() *Cache {
	if c == nil {
		debug.PrintStack()
		// This got triggered due to a premature cleanup in query_test.go
	}

	v := atomic.LoadPointer(&c.cch)
	if v == nil {
		return nil
	}
	return (*Cache)(v)
}

func (c *CurFile) Size() int64 {
	c.RLock()
	defer c.RUnlock()
	return c.size
}

type ByTimestamp []*logFile

func (b ByTimestamp) Len() int      { return len(b) }
func (b ByTimestamp) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b ByTimestamp) Less(i, j int) bool {
	return b[i].endTs < b[j].endTs
}

type Logger struct {
	// Directory to store logs into.
	dir string

	// Prefix all filenames with this.
	filePrefix string

	// MaxSize is the maximum size of commit log file in bytes,
	// before it gets rotated.
	maxSize int64

	// Sync every N logs. A value of zero or less would mean
	// sync every append to file.
	SyncEvery int

	// Sync every d duration.
	SyncDur time.Duration

	sync.RWMutex
	list      []*logFile
	cf        *CurFile
	lastLogTs int64 // handled via atomics.
	ticker    *time.Ticker
}

func (l *Logger) curFile() *CurFile {
	l.RLock()
	defer l.RUnlock()
	return l.cf
}

func (l *Logger) updateLastLogTs(val int64) {
	for {
		prev := atomic.LoadInt64(&l.lastLogTs)
		if val <= prev {
			return
		}
		if atomic.CompareAndSwapInt64(&l.lastLogTs, prev, val) {
			return
		}
	}
}

func (l *Logger) DeleteCacheOlderThan(v time.Duration) {
	l.RLock()
	defer l.RUnlock()
	s := int64(v.Seconds())
	for _, lf := range l.list {
		if lf.Cache().LastAccessedInSeconds() > s {
			lf.Lock()
			lf.cache = nil
			lf.Unlock()
		}
	}
}

func (l *Logger) periodicSync() {
	glog.WithField("dur", l.SyncDur).Debug("Periodic sync.")
	if l.SyncDur == 0 {
		glog.Debug("No Periodic Sync for commit log.")
		return
	}

	l.ticker = time.NewTicker(l.SyncDur)
	for _ = range l.ticker.C {
		cf := l.curFile()
		if cf == nil {
			continue
		}

		{
			cf.Lock()
			if cf.dirtyLogs > 0 {
				if err := cf.f.Sync(); err != nil {
					glog.WithError(err).Error("While periodically syncing.")
				} else {
					cf.dirtyLogs = 0
					glog.Debug("Successful periodic sync.")
				}
			} else {
				glog.Debug("Skipping periodic sync.")
			}
			cf.Unlock()
		}
	}
}

func (l *Logger) Close() {
	l.Lock()
	defer l.Unlock()

	if l.ticker != nil {
		l.ticker.Stop()
	}
	if l.cf != nil {
		if err := l.cf.f.Close(); err != nil {
			glog.WithError(err).Error("While closing current file.")
		}
		l.cf = nil
	}
}

func NewLogger(dir string, fileprefix string, maxSize int64) *Logger {
	l := new(Logger)
	l.dir = dir
	l.filePrefix = fileprefix
	l.maxSize = maxSize
	return l
}

// A mutex lock should have already been acquired to call this function.
func (l *Logger) handleFile(path string, info os.FileInfo, err error) error {
	if info.IsDir() {
		return nil
	}
	if !strings.HasPrefix(info.Name(), l.filePrefix+"-") {
		return nil
	}
	if !strings.HasSuffix(info.Name(), ".log") {
		return nil
	}
	lidx := strings.LastIndex(info.Name(), ".log")
	tstring := info.Name()[len(l.filePrefix)+1 : lidx]
	glog.WithField("log_ts", tstring).Debug("Found log.")

	// Handle if we find the current log file.
	if tstring == "current" {
		return nil
	}

	ts, err := strconv.ParseInt(tstring, 16, 64)
	if err != nil {
		return err
	}
	lf := new(logFile)
	lf.endTs = ts
	lf.path = path
	l.list = append(l.list, lf)

	l.updateLastLogTs(lf.endTs)
	return nil
}

func (l *Logger) Init() {
	l.Lock()
	defer l.Unlock()

	glog.Debug("Logger init started.")
	{
		// First check if we have a current file.
		path := filepath.Join(l.dir, fmt.Sprintf("%s-current.log", l.filePrefix))
		fi, err := os.Stat(path)
		if err == nil {
			// we have the file. Derive information for counters.
			l.cf = new(CurFile)
			l.cf.size = fi.Size()
			l.cf.dirtyLogs = 0
			cache := new(Cache)
			if ferr := FillCache(cache, path); ferr != nil {
				glog.WithError(ferr).Fatal("Unable to write to cache.")
			}
			createAndUpdateBloomFilter(cache)
			atomic.StorePointer(&l.cf.cch, unsafe.Pointer(cache))
			lastTs, err := lastTimestamp(cache)
			if err != nil {
				glog.WithError(err).Fatal("Unable to read last log timestamp.")
			}
			l.updateLastLogTs(lastTs)

			// Open file for append.
			l.cf.f, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY,
				os.FileMode(0644))
			if err != nil {
				glog.WithError(err).Fatal("Unable to open current file in append mode.")
			}
		}
	}

	if err := filepath.Walk(l.dir, l.handleFile); err != nil {
		glog.WithError(err).Fatal("While walking over directory")
	}
	sort.Sort(ByTimestamp(l.list))
	if l.cf == nil {
		l.createNew()
	}
	go l.periodicSync()
	glog.Debug("Logger init finished.")
}

func (l *Logger) filepath(ts int64) string {
	return fmt.Sprintf("%s-%s.log", l.filePrefix, strconv.FormatInt(ts, 16))
}

type Header struct {
	ts   int64
	hash uint32
	size int32
}

func parseHeader(hdr []byte) (Header, error) {
	buf := bytes.NewBuffer(hdr)
	var h Header
	var err error
	setError(&err, binary.Read(buf, binary.LittleEndian, &h.ts))
	setError(&err, binary.Read(buf, binary.LittleEndian, &h.hash))
	setError(&err, binary.Read(buf, binary.LittleEndian, &h.size))
	if err != nil {
		glog.WithError(err).Error("While parsing header.")
		return h, err
	}
	return h, nil
}

func lastTimestamp(c *Cache) (int64, error) {
	var maxTs int64
	reader := NewReader(c)
	header := make([]byte, 16)
	count := 0
	for {
		n, err := reader.Read(header)
		if err == io.EOF {
			break
		}
		if n < len(header) {
			glog.WithField("n", n).Fatal("Unable to read the full 16 byte header.")
		}
		if err != nil {
			glog.WithError(err).Error("While reading header.")
			return 0, err
		}
		count += 1
		h, err := parseHeader(header)
		if err != nil {
			return 0, err
		}

		if h.ts > maxTs {
			maxTs = h.ts

		} else if h.ts < maxTs {
			glog.WithFields(logrus.Fields{
				"ts":         h.ts,
				"maxts":      maxTs,
				"numrecords": count,
			}).Fatal("Log file doesn't have monotonically increasing records.")
		}
		reader.Discard(int(h.size))
	}
	return maxTs, nil
}

func (l *Logger) rotateCurrent() error {
	l.Lock()
	defer l.Unlock()

	cf := l.cf
	cf.Lock()
	defer cf.Unlock()

	if len(l.list) > 0 {
		last := l.list[len(l.list)-1]
		if last.endTs > atomic.LoadInt64(&l.lastLogTs) {
			return fmt.Errorf("Maxtimestamp is lower than existing commit logs.")
		}
	}

	lastTs := atomic.LoadInt64(&l.lastLogTs)
	newpath := filepath.Join(l.dir, l.filepath(lastTs))
	if err := cf.f.Close(); err != nil {
		return err
	}
	if err := os.Rename(cf.f.Name(), newpath); err != nil {
		glog.WithError(err).WithField("curfile", l.cf.f.Name()).
			WithField("newfile", newpath).Error("While renaming.")
		return err
	}

	lf := new(logFile)
	lf.endTs = lastTs
	lf.path = newpath
	lf.size = cf.size
	lf.cache = cf.cache()
	createAndUpdateBloomFilter(lf.cache)
	l.list = append(l.list, lf)

	l.createNew()
	return nil
}

// Expects a lock has already been acquired.
func (l *Logger) createNew() {
	path := filepath.Join(l.dir, fmt.Sprintf("%s-current.log", l.filePrefix))
	if err := os.MkdirAll(l.dir, 0744); err != nil {
		glog.WithError(err).Fatal("Unable to create directory.")
	}
	f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC,
		os.FileMode(0644))
	if err != nil {
		glog.WithError(err).Fatal("Unable to create a new file.")
	}
	l.cf = new(CurFile)
	l.cf.f = f
	cache := new(Cache)
	createAndUpdateBloomFilter(cache)
	atomic.StorePointer(&l.cf.cch, unsafe.Pointer(cache))
}

func setError(prev *error, n error) {
	if prev == nil {
		prev = &n
	}
	return
}

func (l *Logger) AddLog(hash uint32, value []byte) (int64, error) {
	lbuf := int64(len(value)) + 16
	if l.curFile().Size()+lbuf > l.maxSize {
		if err := l.rotateCurrent(); err != nil {
			glog.WithError(err).Error("While rotating current file out.")
			return 0, err
		}
	}

	cf := l.curFile()
	if cf == nil {
		glog.Fatalf("Current file isn't initialized.")
	}

	cf.Lock()
	defer cf.Unlock()

	ts := time.Now().UnixNano()
	lts := atomic.LoadInt64(&l.lastLogTs)
	if ts < lts {
		ts = lts + 1
		// We don't have to do CompareAndSwap because we've a mutex lock.
	}

	buf := new(bytes.Buffer)
	var err error
	setError(&err, binary.Write(buf, binary.LittleEndian, ts))
	setError(&err, binary.Write(buf, binary.LittleEndian, hash))
	setError(&err, binary.Write(buf, binary.LittleEndian, int32(len(value))))
	_, nerr := buf.Write(value)
	setError(&err, nerr)
	if err != nil {
		return ts, err
	}
	glog.WithField("bytes", buf.Len()).Debug("Log entry buffer.")

	if _, err = cf.f.Write(buf.Bytes()); err != nil {
		glog.WithError(err).Error("While writing to current file.")
		return ts, err
	}
	if _, err = cf.cache().Write(hash, buf.Bytes()); err != nil {
		glog.WithError(err).Error("While writing to current cache.")
		return ts, err
	}
	cf.dirtyLogs += 1
	cf.size += int64(buf.Len())
	l.updateLastLogTs(ts)
	if l.SyncEvery <= 0 || cf.dirtyLogs >= l.SyncEvery {
		cf.dirtyLogs = 0
		glog.Debug("Syncing file")
		return ts, cf.f.Sync()
	}
	return ts, nil
}

// streamEntries allows for hash to be zero.
// This means iterate over all the entries.
func streamEntries(cache *Cache,
	afterTs int64, hash uint32, iter LogIterator) error {

	flog := glog
	reader := NewReader(cache)
	header := make([]byte, 16)
	for {
		_, err := reader.Read(header)
		if err == io.EOF {
			flog.Debug("Cache read complete.")
			break
		}
		if err != nil {
			flog.WithError(err).Fatal("While reading header.")
			return err
		}

		hdr, err := parseHeader(header)
		if err != nil {
			flog.WithError(err).Error("While parsing header.")
			return err
		}

		if (hash == 0 || hdr.hash == hash) && hdr.ts >= afterTs {
			// Iterator expects a copy of the buffer, so create one, instead of
			// creating a big buffer upfront and reusing it.
			data := make([]byte, hdr.size)
			_, err := reader.Read(data)
			if err != nil {
				flog.WithError(err).Fatal("While reading data.")
				return err
			}
			iter(hdr, data)

		} else {
			reader.Discard(int(hdr.size))
		}
	}
	return nil
}

type LogIterator func(hdr Header, record []byte)

func (l *Logger) StreamEntries(afterTs int64, hash uint32,
	iter LogIterator) error {

	if atomic.LoadInt64(&l.lastLogTs) < afterTs {
		return nil
	}

	var wg sync.WaitGroup
	l.RLock()
	for _, lf := range l.list {
		if afterTs < lf.endTs {
			wg.Add(1)
			go lf.FillIfEmpty(&wg)
		}
	}
	l.RUnlock()
	wg.Wait()

	l.RLock()
	var caches []*Cache
	for _, lf := range l.list {
		if afterTs < lf.endTs && lf.cache.Present(hash) {
			caches = append(caches, lf.Cache())
		}
	}
	l.RUnlock()

	// Add current cache.
	if l.curFile().cache().Present(hash) {
		caches = append(caches, l.curFile().cache())
	}
	for _, cache := range caches {
		if cache == nil {
			glog.Error("Cache is nil")
			continue
		}
		if err := streamEntries(cache, afterTs, hash, iter); err != nil {
			return err
		}
	}
	return nil
}


text:
from __future__ import absolute_import, division, print_function, unicode_literals

import random
import unittest
from builtins import zip

from pants.backend.jvm.tasks.jvm_compile.class_not_found_error_patterns import \
  CLASS_NOT_FOUND_ERROR_PATTERNS
from pants.backend.jvm.tasks.jvm_compile.missing_dependency_finder import (ClassNotFoundError,
                                                                           CompileErrorExtractor,
                                                                           StringSimilarityRanker)

class CompileErrorExtractorTest(unittest.TestCase):
  ERROR_MESSAGES = [
    r"""
      [error] /path/to/file/Hello.java:3:1: cannot find symbol
      [error]   symbol:   class Nullable
      [error]   location: package javax.annotation
      [error] import javax.annotation.Nullable;""",
    r"""
      [error] /path/to/file/Hello.java:63:1: cannot access org.apache.thrift.TBase
      [error]   class file for org.apache.thrift.TBase not found""",
    r"""
      [error] /path/to/file/Hello.java:6:1: package a.b.c does not exist
      [error] import a.b.c.ImmutableMap;""",
    r"""
      [error] /path/to/file/Hello.java:36:1: cannot find symbol
      [error]   symbol:   class XYZ
      [error]   location: package a.b.c""",
    r"""
      [error] /path/to/file/Hello.java:102:1: package a.b.c does not exist
      [error]     public static final A<a.b.c.XYZ> xyz = new ...;    """,
    r"""
      [error] ## Exception when compiling /path/to/file/Hello.java and others...
      [error] Type com.twitter.util.lint.Rule not present""",
    r"""
      [error] ## Exception when compiling /path/to/file/Hello.java and others...
      [error] java.lang.NoClassDefFoundError: a.b.c.XYZ""",
    r"""
      [error] /path/to/file/Hello.scala:211:26: exception during macro expansion:
      [error] java.lang.ClassNotFoundException: com.twitter.x.thrift.thriftscala.Y""",
    r"""
      [error] /path/to/file/Hello.scala:7:33: object x is not a member of package a.b.c
      [error] import a.b.c.x.Y""",
    r"""
      java.lang.NoClassDefFoundError: org/apache/thrift/TEnum""",
    r"""
      [error] missing or invalid dependency detected while loading class file 'Logging.class'.
      [error] Could not access type Future in value com.twitter.util,""",
    r"""
      [error] Class a.b.c.X not found - continuing with a stub.""",
    r"""
      [error] /path/to/file/Hello.scala:34:6: Symbol 'type <none>.w.XYZ' is missing from the classpath.
      [error] This symbol is required by 'class a.b.c.DEF'.
      [error] Make sure that type XYZ is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
      [error] A full rebuild may help if 'DEF.class' was compiled against an incompatible version of <none>.w.""",
    r"""
      [error] /path/to/file/Hello.scala:34:6: Symbol 'class u.v.w.XYZ' is missing from the classpath.
      [error] This symbol is required by 'class a.b.c.DEF'.
      [error] Make sure that class XYZ is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
      [error] A full rebuild may help if 'DEF.class' was compiled against an incompatible version of u.v.w.""",
    r"""
      [error] Symbol 'type u.v.w.XYZ' is missing from the classpath.
      [error] This symbol is required by 'method DEF.method'.
      [error] Make sure that type XYZ is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
      [error] A full rebuild may help if 'DEF.class' was compiled against an incompatible version of u.v.w.""",

  ]

  EXPECTED_ERRORS = [
    ClassNotFoundError('/path/to/file/Hello.java', '3', 'javax.annotation.Nullable'),
    ClassNotFoundError('/path/to/file/Hello.java', '63', 'org.apache.thrift.TBase'),
    ClassNotFoundError('/path/to/file/Hello.java', '6', 'a.b.c.ImmutableMap'),
    ClassNotFoundError('/path/to/file/Hello.java', '36', 'a.b.c.XYZ'),
    ClassNotFoundError('/path/to/file/Hello.java', '102', 'a.b.c.XYZ'),
    ClassNotFoundError('/path/to/file/Hello.java', None, 'com.twitter.util.lint.Rule'),
    ClassNotFoundError('/path/to/file/Hello.java', None, 'a.b.c.XYZ'),
    ClassNotFoundError('/path/to/file/Hello.scala', '211', 'com.twitter.x.thrift.thriftscala.Y'),
    ClassNotFoundError('/path/to/file/Hello.scala', '7', 'a.b.c.x.Y'),
    ClassNotFoundError(None, None, 'org.apache.thrift.TEnum'),
    ClassNotFoundError(None, None, 'com.twitter.util.Future'),
    ClassNotFoundError(None, None, 'a.b.c.X'),
    ClassNotFoundError(None, None, 'w.XYZ'),
    ClassNotFoundError(None, None, 'u.v.w.XYZ'),
    ClassNotFoundError(None, None, 'u.v.w.XYZ'),
  ]

  def setUp(self):
    self.compile_error_finder = CompileErrorExtractor(CLASS_NOT_FOUND_ERROR_PATTERNS)

  def test_extract_single_error(self):
    for error_message, expected_error in zip(self.ERROR_MESSAGES, self.EXPECTED_ERRORS):
      self.assertEqual([expected_error], self.compile_error_finder.extract(error_message))

  def test_extract_all_errors(self):
    compile_log = '\n'.join(self.ERROR_MESSAGES)
    self.assertEqual(self.EXPECTED_ERRORS,
      self.compile_error_finder.extract(compile_log))

class StringSimilarityRankerTest(unittest.TestCase):
  def test_rank_dependency_candidates(self):
    not_found_classname = 'com.google.inject.Inject'

    # Expect 3rdparty/jvm/com/google/inject:guice is more similar to the missing class name.
    expected = [
      '3rdparty/jvm/com/google/inject:guice',
      '3rdparty/jvm/com/google/guava:guava',
      '3rdparty/jvm/cascading:cascading-local',
    ]

    shuffled = list(expected)
    random.shuffle(shuffled)
    self.assertEqual(expected, StringSimilarityRanker(not_found_classname).sort(shuffled))


text:
package com.taodian.emop;

public final class Version {
	private static final String version = "0.1.dev";
	public static String getVersion(){
		return version;
	}

	public static String getName(){
		return "ClickGate";
	}

	public static String getBuildDate(){
		return "2013-10-15-1101";
	}
}


text:
package com.af5m.elm.service.persistence.repository;

import java.util.UUID;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.af5m.elm.service.persistence.entity.ResourceEntity;

/**
 * The Interface ResourceRepository.
 */
@Repository
public interface ResourceRepository extends CrudRepository<ResourceEntity, UUID> {

	/**
	 * Find all.
	 *
	 * @param pagable the pagable
	 * @return the page
	 */
	Page<ResourceEntity> findAll(Pageable pagable);

	/**
	 * Find by name.
	 *
	 * @param name the name
	 * @param pageable the pageable
	 * @return the page
	 */
	Page<ResourceEntity> findByName(String name, Pageable pageable);

	/**
	 * Find by uuid.
	 *
	 * @param uuid the uuid
	 * @return the ResourceEntity
	 */
	ResourceEntity findByUuid(UUID uuid);

}


text:
package org.chocosolver.choco.checker.fmk;

import org.chocosolver.solver.Solver;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.solver.variables.SetVar;
import org.chocosolver.solver.variables.Variable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Random;

import static java.lang.System.arraycopy;
import static java.util.Arrays.copyOfRange;
import static org.chocosolver.choco.checker.fmk.Domain.*;
import static org.chocosolver.solver.Solver.writeInFile;
import static org.chocosolver.solver.variables.SetVar.END;
import static org.testng.Assert.fail;

/**
 * @author Jean-Guillaume Fages, Charles Prud'homme
 * @since 01/13
 */
public class Correctness {

    private static final Logger LOGGER = LoggerFactory.getLogger("test");

    public static final int INT = 0;
    public static final int BOOL = 1;
    public static final int SET = 2;

    public static void checkCorrectness(Model modeler, int nbVar, int lowerB, int upperB, long seed, Object parameters) {
        Random r = new Random(seed);
        double[] densities = {0.1, 0.25, 0.5, 0.75, 1.0};
        boolean[] homogeneous = {true, false};
        int loop = 0;
        int[] types = new int[nbVar];
        modeler.fillTypes(types);
        for (int ds = nbVar; ds < upperB; ds++) {
            for (int ide = 0; ide < densities.length; ide++) {
                for (int h = 0; h < homogeneous.length; h++) {
                    Domain[] domains = new Domain[nbVar];
                    for (int i = 0; i < nbVar; i++) {
                        switch (types[i]) {
                            case BOOL:
                                domains[i] = buildBoolDomain(r);
                                break;
                            case INT:
                                domains[i] = buildIntDomain(lowerB, ds, r, densities[ide], homogeneous[h]);
                                break;
                            case SET:
                                domains[i] = buildSetDomain(ds, r, densities[ide], homogeneous[h]);
                                break;
                            default:
                                throw new UnsupportedOperationException();
                        }
                    }
                    Variable[] rvars = new Variable[nbVar];
                    Solver ref = referencePropagation(modeler, nbVar, rvars, domains, parameters);
                    if (ref == null) break; // no solution found for this generated problem
                    for (int d = 0; d < nbVar; d++) {
                        if (types[d] == INT || types[d] == BOOL) {
                            int[] values = getRemovedValues((IntVar) rvars[d], domains[d]);
                            for (int v : values) {
                                loop++;
                                Domain[] _domains = new Domain[nbVar];
                                arraycopy(domains, 0, _domains, 0, nbVar);
                                _domains[d] = new Domain(new int[]{v});
                                checkNoSol(modeler, rvars, _domains, parameters, ref, new Object[]{ds, ide, h, rvars[d], v, loop, seed});
                            }
                        } else if (types[d] == SET) {
                            int[] oldEnv = domains[d].getSetEnv();
                            int[] oldKer = domains[d].getSetKer();
                            // removed
                            int[] rems = getRemovedElements((SetVar) rvars[d], oldEnv);
                            for (int v : rems) {
                                loop++;
                                int[] newKer = new int[oldKer.length + 1];
                                arraycopy(oldKer, 0, newKer, 0, oldKer.length);
                                newKer[oldKer.length] = v;
                                Domain[] _domains = new Domain[nbVar];
                                arraycopy(domains, 0, _domains, 0, nbVar);
                                _domains[d] = new Domain(oldEnv, newKer);
                                checkNoSol(modeler, rvars, _domains, parameters, ref, new Object[]{ds, ide, h, rvars[d], v, loop, seed});
                            }
                            // forced
                            int[] enfs = getForcedElements((SetVar) rvars[d], oldKer);
                            for (int v : enfs) {
                                loop++;
                                int[] newEnv = new int[oldEnv.length - 1];
                                int idx = 0;
                                for (int e : oldEnv) {
                                    if (e != v) {
                                        newEnv[idx++] = e;
                                    }
                                }
                                Domain[] _domains = new Domain[nbVar];
                                arraycopy(domains, 0, _domains, 0, nbVar);
                                _domains[d] = new Domain(newEnv, oldKer);
                                checkNoSol(modeler, rvars, _domains, parameters, ref, new Object[]{ds, ide, h, rvars[d], v, loop, seed});
                            }
                        } else {
                            throw new UnsupportedOperationException();
                        }
                    }
                }
            }
        }
//        System.out.printf("loop: %d\n", loop);
    }

    private static Solver referencePropagation(Model modeler, int nbVar, Variable[] rvars, Domain[] domains, Object parameters) {
        Solver ref = modeler.model(nbVar, rvars, domains, parameters);
        ref.getEnvironment().worldPush();
        try {
            ref.propagate();
        } catch (ContradictionException e) {
            LOGGER.debug("Pas de solution pour ce probleme => rien a tester !");
            return null;
        } catch (Exception e) {
            File f = new File("SOLVER_ERROR.ser");
            try {
                writeInFile(ref, f);
            } catch (IOException ee) {
                ee.printStackTrace();
            }
            LOGGER.error(e.getMessage());
            LOGGER.error("REF:\n{}\n", ref);
            LOGGER.error("{}", f.getAbsolutePath());
            fail();
        }
        return ref;
    }

    private static void checkNoSol(Model m, Variable[] rvars, Domain[] _domains, Object parameters, Solver ref, Object[] logObjects) {
        int nbVar = rvars.length;
        Solver test = m.model(nbVar, rvars, _domains, parameters);
        try {
            if (test.findSolution()) {
                LOGGER.error("ds :{}, ide:{}, h:{}, var:{}, val:{}, loop:{}, seed: {}",
                        logObjects);
                LOGGER.error("REF:\n{}\n", ref);
                ref.getEnvironment().worldPop();
                LOGGER.error("REF:\n{}\nTEST:\n{}", ref, test);
                fail("one solution found");
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            LOGGER.error("ds :{}, ide:{}, h:{}, var:{}, val:{}, loop:{}, seed: {}",
                    logObjects);
            LOGGER.error("REF:\n{}\nTEST:\n{}", ref, test);
            File f = new File("SOLVER_ERROR.ser");
            try {
                writeInFile(ref, f);
            } catch (IOException ee) {
                ee.printStackTrace();
            }
            LOGGER.error("{}", f.getAbsolutePath());
            fail();
        }
    }

    ////////////////////////////////////////////////////////////////////////
    private static int[] getRemovedValues(IntVar variable, Domain domain) {
        int[] d = domain.getIntDom();
        int[] _values = new int[d.length];
        int k = 0;
        for (int i : d) {
            if (!variable.contains(i)) {
                _values[k++] = i;
            }
        }
        return copyOfRange(_values, 0, k);
    }

    ////////////////////////////////////////////////////////////////////////
    private static int[] getRemovedElements(SetVar variable, int[] d) {
        int[] _values = new int[d.length];
        int k = 0;
        for (int i : d) {
            if (!variable.envelopeContains(i)) {
                _values[k++] = i;
            }
        }
        return copyOfRange(_values, 0, k);
    }

    ////////////////////////////////////////////////////////////////////////
    private static int[] getForcedElements(SetVar v, int[] d) {
        int[] _values = new int[v.getKernelSize()];
        int k = 0;
        for (int j = v.getKernelFirst(); j != END; j = v.getKernelNext()) {
            boolean newEl = true;
            for (int i : d) {
                if (i == j) {
                    newEl = false;
                    break;
                }
            }
            if (newEl) {
                _values[k++] = j;
            }
        }
        return copyOfRange(_values, 0, k);
    }
    ////////////////////////////////////////////////////////////////////////
}


text:
package org.jikesrvm.compilers.opt.lir2mir.ppc;

import org.jikesrvm.compilers.opt.OptOptions;
import org.jikesrvm.compilers.opt.driver.CompilerPhase;
import org.jikesrvm.compilers.opt.ir.IR;
import org.jikesrvm.compilers.opt.ir.Operators;

/**
 * Nothing to do on PowerPC.
 */
public abstract class ConvertALUOperators extends CompilerPhase implements Operators {

  /**
   * Return this instance of this phase. This phase contains no
   * per-compilation instance fields.
   * @param ir not used
   * @return this
   */
  public CompilerPhase newExecution(IR ir) {
    return this;
  }

  public final String getName() { return "ConvertALUOps"; }

  public final boolean printingEnabled(OptOptions options, boolean before) {
    return false;
  }

  public final void perform(IR ir) {
    // Nothing to do on PPC
  }
}


text:
@implementation UIWebView (Intercept)

+ (void)load{
    [self swizzlingInClass:self
          originalSelector:@selector(loadHTMLString:baseURL:)
          swizzledSelector:@selector(_loadHTMLString:baseURL:)];
}

+ (void)swizzlingInClass:(Class)cls originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector{
    Class class = cls;

    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

    BOOL didAddMethod =
    class_addMethod(class,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));

    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

- (void) _loadHTMLString:(NSString *)htmlString baseURL:(NSURL *)baseURL{
    [self _loadHTMLString:[WebViewURLIntercept replaceString:htmlString] baseURL:baseURL];
}

@end


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>statsmodels.tsa.arima_process.ArmaProcess.from_coeffs — statsmodels 0.6.1 documentation</title>

    <link rel="stylesheet" href="../_static/nature.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />

    <link rel="shortcut icon" href="../_static/statsmodels_hybi_favico.ico"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="top" title="statsmodels 0.6.1 documentation" href="../index.html" />
    <link rel="up" title="statsmodels.tsa.arima_process.ArmaProcess" href="statsmodels.tsa.arima_process.ArmaProcess.html" />
    <link rel="next" title="statsmodels.tsa.arima_process.ArmaProcess.from_estimation" href="statsmodels.tsa.arima_process.ArmaProcess.from_estimation.html" />
    <link rel="prev" title="statsmodels.tsa.arima_process.ArmaProcess.arma2ma" href="statsmodels.tsa.arima_process.ArmaProcess.arma2ma.html" />
<link rel="stylesheet" href="../../_static/facebox.css" type="text/css" />
<link rel="stylesheet" href="../_static/examples.css" type="text/css" />

Navigation

          index

          next |

          previous |

Install
  |

Support
  |

Bugs
  |

Develop
  |

Examples
  |

FAQ
  |

Time Series analysis tsa |

statsmodels.tsa.arima_process.ArmaProcess |

statsmodels.tsa.arima_process.ArmaProcess.from_coeffs¶

classmethod ArmaProcess.from_coeffs(arcoefs, macoefs, nobs=100)[source]¶

Create ArmaProcess instance from coefficients of the lag-polynomials

<col class="field-name" />
<col class="field-body" />

Parameters:
arcoefs : array-like

Coefficient for autoregressive lag polynomial, not including zero
lag. The sign is inverted to conform to the usual time series
representation of an ARMA process in statistics. See the class
docstring for more information.

macoefs : array-like

Coefficient for moving-average lag polynomial, including zero lag

nobs : int, optional

Length of simulated time series. Used, for example, if a sample
is generated.

Previous topic

statsmodels.tsa.arima_process.ArmaProcess.arma2ma

Next topic

statsmodels.tsa.arima_process.ArmaProcess.from_estimation

This Page

Show Source

Quick search

    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>

    Enter search terms or a module, class or function name.

        © Copyright 2009-2013, Josef Perktold, Skipper Seabold, Jonathan Taylor, statsmodels-developers.
      Created using Sphinx 1.3.1.


text:
require_relative 'specification'

module Trackler
  # Problem is a language-independent definition of an exercise.
  class Problem < Trackler::Specification
  end
end


text:
"""Tests for mechanize.Browser."""

import StringIO
from unittest import TestCase
import re

import mechanize
from mechanize._response import test_html_response
FACTORY_CLASSES = [mechanize.DefaultFactory, mechanize.RobustFactory]

# XXX these 'mock' classes are badly in need of simplification / removal
# (note this stuff is also used by test_useragent.py and test_browser.doctest)
class MockMethod:
    def __init__(self, meth_name, action, handle):
        self.meth_name = meth_name
        self.handle = handle
        self.action = action
    def __call__(self, *args):
        return apply(self.handle, (self.meth_name, self.action)+args)

class MockHeaders(dict):
    def getheaders(self, name):
        name = name.lower()
        return [v for k, v in self.iteritems() if name == k.lower()]

class MockResponse:
    closeable_response = None
    def __init__(self, url="http://example.com/", data=None, info=None):
        self.url = url
        self.fp = StringIO.StringIO(data)
        if info is None: info = {}
        self._info = MockHeaders(info)
    def info(self): return self._info
    def geturl(self): return self.url
    def read(self, size=-1): return self.fp.read(size)
    def seek(self, whence):
        assert whence == 0
        self.fp.seek(0)
    def close(self): pass
    def get_data(self): pass

def make_mock_handler(response_class=MockResponse):
    class MockHandler:
        processor_order = 500
        handler_order = -1
        def __init__(self, methods):
            self._define_methods(methods)
        def _define_methods(self, methods):
            for name, action in methods:
                if name.endswith("_open"):
                    meth = MockMethod(name, action, self.handle)
                else:
                    meth = MockMethod(name, action, self.process)
                setattr(self.__class__, name, meth)
        def handle(self, fn_name, response, *args, **kwds):
            self.parent.calls.append((self, fn_name, args, kwds))
            if response:
                if isinstance(response, mechanize.HTTPError):
                    raise response
                r = response
                r.seek(0)
            else:
                r = response_class()
            req = args[0]
            r.url = req.get_full_url()
            return r
        def process(self, fn_name, action, *args, **kwds):
            self.parent.calls.append((self, fn_name, args, kwds))
            if fn_name.endswith("_request"):
                return args[0]
            else:
                return args[1]
        def close(self): pass
        def add_parent(self, parent):
            self.parent = parent
            self.parent.calls = []
        def __lt__(self, other):
            if not hasattr(other, "handler_order"):
                # Try to preserve the old behavior of having custom classes
                # inserted after default ones (works only for custom user
                # classes which are not aware of handler_order).
                return True
            return self.handler_order < other.handler_order
    return MockHandler

class TestBrowser(mechanize.Browser):
    default_features = []
    default_others = []
    default_schemes = []

class TestBrowser2(mechanize.Browser):
    # XXX better name!
    # As TestBrowser, this is neutered so doesn't know about protocol handling,
    # but still knows what to do with unknown schemes, etc., because
    # UserAgent's default_others list is left intact, including classes like
    # UnknownHandler
    default_features = []
    default_schemes = []

class BrowserTests(TestCase):

    def test_referer(self):
        b = TestBrowser()
        url = "http://www.example.com/"
        r = MockResponse(url,
"""
<title>Title</title>

<form name="form1">
 <input type="hidden" name="foo" value="bar"></input>
 <input type="submit"></input>
 </form>

""", {"content-type": "text/html"})
        b.add_handler(make_mock_handler()([("http_open", r)]))

        # Referer not added by .open()...
        req = mechanize.Request(url)
        b.open(req)
        self.assert_(req.get_header("Referer") is None)
        # ...even if we're visiting a document
        b.open(req)
        self.assert_(req.get_header("Referer") is None)
        # Referer added by .click_link() and .click()
        b.select_form("form1")
        req2 = b.click()
        self.assertEqual(req2.get_header("Referer"), url)
        r2 = b.open(req2)
        req3 = b.click_link(name="apples")
        self.assertEqual(req3.get_header("Referer"), url+"?foo=bar")
        # Referer not added when going from https to http URL
        b.add_handler(make_mock_handler()([("https_open", r)]))
        r3 = b.open(req3)
        req4 = b.click_link(name="secure")
        self.assertEqual(req4.get_header("Referer"),
                         "http://example.com/foo/bar.html")
        r4 = b.open(req4)
        req5 = b.click_link(name="apples")
        self.assert_(not req5.has_header("Referer"))
        # Referer not added for non-http, non-https requests
        b.add_handler(make_mock_handler()([("blah_open", r)]))
        req6 = b.click_link(name="pears")
        self.assert_(not req6.has_header("Referer"))
        # Referer not added when going from non-http, non-https URL
        r4 = b.open(req6)
        req7 = b.click_link(name="apples")
        self.assert_(not req7.has_header("Referer"))

        # XXX Referer added for redirect

    def test_encoding(self):
        import mechanize
        from StringIO import StringIO
        import urllib, mimetools
        # always take first encoding, since that's the one from the real HTTP
        # headers, rather than from HTTP-EQUIV
        b = mechanize.Browser()
        for s, ct in [("", mechanize._html.DEFAULT_ENCODING),

                      ("Foo: Bar\r\n\r\n", mechanize._html.DEFAULT_ENCODING),

                      ("Content-Type: text/html; charset=UTF-8\r\n\r\n",
                       "UTF-8"),

                      ("Content-Type: text/html; charset=UTF-8\r\n"
                       "Content-Type: text/html; charset=KOI8-R\r\n\r\n",
                       "UTF-8"),
                      ]:
            msg = mimetools.Message(StringIO(s))
            r = urllib.addinfourl(StringIO(""), msg, "http://www.example.com/")
            b.set_response(r)
            self.assertEqual(b.encoding(), ct)

    def test_history(self):
        import mechanize
        from mechanize import _response

        def same_response(ra, rb):
            return ra.wrapped is rb.wrapped

        class Handler(mechanize.BaseHandler):
            def http_open(self, request):
                r = _response.test_response(url=request.get_full_url())
                # these tests aren't interested in auto-.reload() behaviour of
                # .back(), so read the response to prevent that happening
                r.get_data()
                return r

        b = TestBrowser2()
        b.add_handler(Handler())
        self.assertRaises(mechanize.BrowserStateError, b.back)
        r1 = b.open("http://example.com/")
        self.assertRaises(mechanize.BrowserStateError, b.back)
        r2 = b.open("http://example.com/foo")
        self.assert_(same_response(b.back(), r1))
        r3 = b.open("http://example.com/bar")
        r4 = b.open("http://example.com/spam")
        self.assert_(same_response(b.back(), r3))
        self.assert_(same_response(b.back(), r1))
        self.assertEquals(b.geturl(), "http://example.com/")
        self.assertRaises(mechanize.BrowserStateError, b.back)
        # reloading does a real HTTP fetch rather than using history cache
        r5 = b.reload()
        self.assert_(not same_response(r5, r1))
        # .geturl() gets fed through to b.response
        self.assertEquals(b.geturl(), "http://example.com/")
        # can go back n times
        r6 = b.open("spam")
        self.assertEquals(b.geturl(), "http://example.com/spam")
        r7 = b.open("/spam")
        self.assert_(same_response(b.response(), r7))
        self.assertEquals(b.geturl(), "http://example.com/spam")
        self.assert_(same_response(b.back(2), r5))
        self.assertEquals(b.geturl(), "http://example.com/")
        self.assertRaises(mechanize.BrowserStateError, b.back, 2)
        r8 = b.open("/spam")

        # even if we get an HTTPError, history, .response() and .request should
        # still get updated
        class Handler2(mechanize.BaseHandler):
            def https_open(self, request):
                r = mechanize.HTTPError(
                    "https://example.com/bad", 503, "Oops",
                    MockHeaders(), StringIO.StringIO())
                return r
        b.add_handler(Handler2())
        self.assertRaises(mechanize.HTTPError, b.open,
                          "https://example.com/badreq")
        self.assertEqual(b.response().geturl(), "https://example.com/bad")
        self.assertEqual(b.request.get_full_url(),
                         "https://example.com/badreq")
        self.assert_(same_response(b.back(), r8))

        # .close() should make use of Browser methods and attributes complain
        # noisily, since they should not be called after .close()
        b.form = "blah"
        b.close()
        for attr in ("form open error retrieve add_handler "
                     "request response set_response geturl reload back "
                     "clear_history set_cookie links forms viewing_html "
                     "encoding title select_form click submit click_link "
                     "follow_link find_link".split()
                     ):
            self.assert_(getattr(b, attr) is None)

    def test_reload_read_incomplete(self):
        import mechanize
        from mechanize._response import test_response
        class Browser(TestBrowser):
            def __init__(self):
                TestBrowser.__init__(self)
                self.reloaded = False
            def reload(self):
                self.reloaded = True
                TestBrowser.reload(self)
        br = Browser()
        data = "<title></title>%s"
        data = data % ("The quick brown fox jumps over the lazy dog."*100)
        class Handler(mechanize.BaseHandler):
            def http_open(self, requst):
                return test_response(data, [("content-type", "text/html")])
        br.add_handler(Handler())

        # .reload() on .back() if the whole response hasn't already been read
        # (.read_incomplete is True)
        r = br.open("http://example.com")
        r.read(10)
        br.open('http://www.example.com/blah')
        self.failIf(br.reloaded)
        br.back()
        self.assert_(br.reloaded)

        # don't reload if already read
        br.reloaded = False
        br.response().read()
        br.open('http://www.example.com/blah')
        br.back()
        self.failIf(br.reloaded)

    def test_viewing_html(self):
        # XXX not testing multiple Content-Type headers
        import mechanize
        url = "http://example.com/"

        for allow_xhtml in False, True:
            for ct, expect in [
                (None, False),
                ("text/plain", False),
                ("text/html", True),

                # don't try to handle XML until we can do it right!
                ("text/xhtml", allow_xhtml),
                ("text/xml", allow_xhtml),
                ("application/xml", allow_xhtml),
                ("application/xhtml+xml", allow_xhtml),

                ("text/html; charset=blah", True),
                (" text/html ; charset=ook ", True),
                ]:
                b = TestBrowser(mechanize.DefaultFactory(
                    i_want_broken_xhtml_support=allow_xhtml))
                hdrs = {}
                if ct is not None:
                    hdrs["Content-Type"] = ct
                b.add_handler(make_mock_handler()([("http_open",
                                            MockResponse(url, "", hdrs))]))
                b.open(url)
                self.assertEqual(b.viewing_html(), expect)

        for allow_xhtml in False, True:
            for ext, expect in [
                (".htm", True),
                (".html", True),

                # don't try to handle XML until we can do it right!
                (".xhtml", allow_xhtml),

                (".html?foo=bar&a=b;whelk#kool", True),
                (".txt", False),
                (".xml", False),
                ("", False),
                ]:
                b = TestBrowser(mechanize.DefaultFactory(
                    i_want_broken_xhtml_support=allow_xhtml))
                url = "http://example.com/foo"+ext
                b.add_handler(make_mock_handler()(
                    [("http_open", MockResponse(url, "", {}))]))
                b.open(url)
                self.assertEqual(b.viewing_html(), expect)

    def test_empty(self):
        for factory_class in FACTORY_CLASSES:
            self._test_empty(factory_class())

    def _test_empty(self, factory):
        import mechanize
        url = "http://example.com/"

        b = TestBrowser(factory=factory)

        self.assert_(b.response() is None)

        # To open a relative reference (often called a "relative URL"), you
        # have to have already opened a URL for it "to be relative to".
        self.assertRaises(mechanize.BrowserStateError, b.open, "relative_ref")

        # we can still clear the history even if we've not visited any URL
        b.clear_history()

        # most methods raise BrowserStateError...
        def test_state_error(method_names):
            for attr in method_names:
                method = getattr(b, attr)
                #print attr
                self.assertRaises(mechanize.BrowserStateError, method)
            self.assertRaises(mechanize.BrowserStateError, b.select_form,
                              name="blah")
            self.assertRaises(mechanize.BrowserStateError, b.find_link,
                              name="blah")
        # ...if not visiting a URL...
        test_state_error(("geturl reload back viewing_html encoding "
                          "click links forms title select_form".split()))
        self.assertRaises(mechanize.BrowserStateError, b.set_cookie, "foo=bar")
        self.assertRaises(mechanize.BrowserStateError, b.submit, nr=0)
        self.assertRaises(mechanize.BrowserStateError, b.click_link, nr=0)
        self.assertRaises(mechanize.BrowserStateError, b.follow_link, nr=0)
        self.assertRaises(mechanize.BrowserStateError, b.find_link, nr=0)
        # ...and lots do so if visiting a non-HTML URL
        b.add_handler(make_mock_handler()(
            [("http_open", MockResponse(url, "", {}))]))
        r = b.open(url)
        self.assert_(not b.viewing_html())
        test_state_error("click links forms title select_form".split())
        self.assertRaises(mechanize.BrowserStateError, b.submit, nr=0)
        self.assertRaises(mechanize.BrowserStateError, b.click_link, nr=0)
        self.assertRaises(mechanize.BrowserStateError, b.follow_link, nr=0)
        self.assertRaises(mechanize.BrowserStateError, b.find_link, nr=0)

        b = TestBrowser()
        r = MockResponse(url,
"""
<title>Title</title>

""", {"content-type": "text/html"})
        b.add_handler(make_mock_handler()([("http_open", r)]))
        r = b.open(url)
        self.assertEqual(b.title(), "Title")
        self.assertEqual(len(list(b.links())), 0)
        self.assertEqual(len(list(b.forms())), 0)
        self.assertRaises(ValueError, b.select_form)
        self.assertRaises(mechanize.FormNotFoundError, b.select_form,
                          name="blah")
        self.assertRaises(mechanize.FormNotFoundError, b.select_form,
                          predicate=lambda form: form is not b.global_form())
        self.assertRaises(mechanize.LinkNotFoundError, b.find_link,
                          name="blah")
        self.assertRaises(mechanize.LinkNotFoundError, b.find_link,
                          predicate=lambda x: True)

    def test_forms(self):
        for factory_class in FACTORY_CLASSES:
            self._test_forms(factory_class())
    def _test_forms(self, factory):
        import mechanize
        url = "http://example.com"

        b = TestBrowser(factory=factory)
        r = test_html_response(
            url=url,
            headers=[("content-type", "text/html")],
            data="""\

<title>Title</title>

<form name="form1">
 <input type="text"></input>
 <input type="checkbox" name="cheeses" value="cheddar"></input>
 <input type="checkbox" name="cheeses" value="edam"></input>
 <input type="submit" name="one"></input>
</form>

<form name="form2">
 <input type="submit" name="two">
</form>

"""
            )
        b.add_handler(make_mock_handler()([("http_open", r)]))
        r = b.open(url)

        forms = list(b.forms())
        self.assertEqual(len(forms), 2)
        for got, expect in zip([f.name for f in forms], [
            "form1", "form2"]):
            self.assertEqual(got, expect)

        self.assertRaises(mechanize.FormNotFoundError, b.select_form, "foo")

        # no form is set yet
        self.assertRaises(AttributeError, getattr, b, "possible_items")
        b.select_form("form1")
        # now unknown methods are fed through to selected mechanize.HTMLForm
        self.assertEqual(
            [i.name for i in b.find_control("cheeses").items],
            ["cheddar", "edam"])
        b["cheeses"] = ["cheddar", "edam"]
        self.assertEqual(b.click_pairs(), [
            ("cheeses", "cheddar"), ("cheeses", "edam"), ("one", "")])

        b.select_form(nr=1)
        self.assertEqual(b.name, "form2")
        self.assertEqual(b.click_pairs(), [("two", "")])

    def test_link_encoding(self):
        for factory_class in FACTORY_CLASSES:
            self._test_link_encoding(factory_class())
    def _test_link_encoding(self, factory):
        import mechanize
        from mechanize._rfc3986 import clean_url
        url = "http://example.com/"
        for encoding in ["UTF-8", "latin-1"]:
            encoding_decl = "; charset=%s" % encoding
            b = TestBrowser(factory=factory)
            r = MockResponse(url, """\
blah——
""", #"
{"content-type": "text/html%s" % encoding_decl})
            b.add_handler(make_mock_handler()([("http_open", r)]))
            r = b.open(url)

            Link = mechanize.Link
            try:
                mdashx2 = u"\u2014".encode(encoding)*2
            except UnicodeError:
                mdashx2 = '——'
            qmdashx2 = clean_url(mdashx2, encoding)
            # base_url, url, text, tag, attrs
            exp = Link(url, "http://example.com/foo/bar%s.html" % qmdashx2,
                       "blah"+mdashx2, "a",
                       [("href", "http://example.com/foo/bar%s.html" % mdashx2),
                        ("name", "name0%s" % mdashx2)])
            # nr
            link = b.find_link()
##             print
##             print exp
##             print link
            self.assertEqual(link, exp)

    def test_link_whitespace(self):
        from mechanize import Link
        for factory_class in FACTORY_CLASSES:
            base_url = "http://example.com/"
            url = "  http://example.com/foo.html%20+ "
            stripped_url = url.strip()
            html = '' % url
            b = TestBrowser(factory=factory_class())
            r = MockResponse(base_url, html, {"content-type": "text/html"})
            b.add_handler(make_mock_handler()([("http_open", r)]))
            r = b.open(base_url)
            link = b.find_link(nr=0)
            self.assertEqual(
                link,
                Link(base_url, stripped_url, "", "a", [("href", url)])
                )

    def test_links(self):
        for factory_class in FACTORY_CLASSES:
            self._test_links(factory_class())
    def _test_links(self, factory):
        import mechanize
        from mechanize import Link
        url = "http://example.com/"

        b = TestBrowser(factory=factory)
        r = MockResponse(url,
"""
<title>Title</title>

<area href="blah" name="foo"></area>
<form name="form2">
 <input type="submit" name="two">
</form>
<frame name="name" href="href" src="src"></frame>

yada yada
rhubarb

""", {"content-type": "text/html"})
        b.add_handler(make_mock_handler()([("http_open", r)]))
        r = b.open(url)

        exp_links = [
            # base_url, url, text, tag, attrs
            Link(url, "http://example.com/foo/bar.html", "", "a",
                 [("href", "http://example.com/foo/bar.html"),
                  ("name", "apples")]),
            Link(url, "spam", "", "a", [("href", "spam"), ("name", "pears")]),
            Link(url, "blah", None, "area",
                 [("href", "blah"), ("name", "foo")]),
            Link(url, "src", None, "frame",
                 [("name", "name"), ("href", "href"), ("src", "src")]),
            Link(url, "src", None, "iframe",
                 [("name", "name2"), ("href", "href"), ("src", "src")]),
            Link(url, "one", "yada yada", "a",
                 [("name", "name3"), ("href", "one")]),
            Link(url, "two", "rhubarb", "a",
                 [("name", "pears"), ("href", "two"), ("weird", "stuff")]),
            Link(url, "foo", None, "iframe",
                 [("src", "foo")]),
            ]
        links = list(b.links())
        self.assertEqual(len(links), len(exp_links))
        for got, expect in zip(links, exp_links):
            self.assertEqual(got, expect)
        # nr
        l = b.find_link()
        self.assertEqual(l.url, "http://example.com/foo/bar.html")
        l = b.find_link(nr=1)
        self.assertEqual(l.url, "spam")
        # text
        l = b.find_link(text="yada yada")
        self.assertEqual(l.url, "one")
        self.assertRaises(mechanize.LinkNotFoundError,
                          b.find_link, text="da ya")
        l = b.find_link(text_regex=re.compile("da ya"))
        self.assertEqual(l.url, "one")
        l = b.find_link(text_regex="da ya")
        self.assertEqual(l.url, "one")
        # name
        l = b.find_link(name="name3")
        self.assertEqual(l.url, "one")
        l = b.find_link(name_regex=re.compile("oo"))
        self.assertEqual(l.url, "blah")
        l = b.find_link(name_regex="oo")
        self.assertEqual(l.url, "blah")
        # url
        l = b.find_link(url="spam")
        self.assertEqual(l.url, "spam")
        l = b.find_link(url_regex=re.compile("pam"))
        self.assertEqual(l.url, "spam")
        l = b.find_link(url_regex="pam")
        self.assertEqual(l.url, "spam")
        # tag
        l = b.find_link(tag="area")
        self.assertEqual(l.url, "blah")
        # predicate
        l = b.find_link(predicate=
                        lambda l: dict(l.attrs).get("weird") == "stuff")
        self.assertEqual(l.url, "two")
        # combinations
        l = b.find_link(name="pears", nr=1)
        self.assertEqual(l.text, "rhubarb")
        l = b.find_link(url="src", nr=0, name="name2")
        self.assertEqual(l.tag, "iframe")
        self.assertEqual(l.url, "src")
        self.assertRaises(mechanize.LinkNotFoundError, b.find_link,
                          url="src", nr=1, name="name2")
        l = b.find_link(tag="a", predicate=
                        lambda l: dict(l.attrs).get("weird") == "stuff")
        self.assertEqual(l.url, "two")

        # .links()
        self.assertEqual(list(b.links(url="src")), [
            Link(url, url="src", text=None, tag="frame",
                 attrs=[("name", "name"), ("href", "href"), ("src", "src")]),
            Link(url, url="src", text=None, tag="iframe",
                 attrs=[("name", "name2"), ("href", "href"), ("src", "src")]),
            ])

    def test_base_uri(self):
        url = "http://example.com/"

        for html, urls in [
            (
"""<base href="http://www.python.org/foo/">

""",
            [
            "http://www.python.org/foo/bar/baz.html",
            "http://www.python.org/bar/baz.html",
            "http://example.com/bar%20%2f%2Fblah;/baz@~._-.html",
            ]),
            (
"""

""",
            [
            "http://example.com/bar/baz.html",
            "http://example.com/bar/baz.html",
            "http://example.com/bar/baz.html",
            ]
            ),
            ]:
            b = TestBrowser()
            r = MockResponse(url, html, {"content-type": "text/html"})
            b.add_handler(make_mock_handler()([("http_open", r)]))
            r = b.open(url)
            self.assertEqual([link.absolute_url for link in b.links()], urls)

    def test_set_cookie(self):
        class CookieTestBrowser(TestBrowser):
            default_features = list(TestBrowser.default_features)+["_cookies"]

        # have to be visiting HTTP/HTTPS URL
        url = "ftp://example.com/"
        br = CookieTestBrowser()
        r = mechanize.make_response(
            "<title>Title</title>",
            [("content-type", "text/html")],
            url,
            200, "OK",
            )
        br.add_handler(make_mock_handler()([("http_open", r)]))
        handler = br._ua_handlers["_cookies"]
        cj = handler.cookiejar
        self.assertRaises(mechanize.BrowserStateError,
                          br.set_cookie, "foo=bar")
        self.assertEqual(len(cj), 0)

        url = "http://example.com/"
        br = CookieTestBrowser()
        r = mechanize.make_response(
            "<title>Title</title>",
            [("content-type", "text/html")],
            url,
            200, "OK",
            )
        br.add_handler(make_mock_handler()([("http_open", r)]))
        handler = br._ua_handlers["_cookies"]
        cj = handler.cookiejar

        # have to be visiting a URL
        self.assertRaises(mechanize.BrowserStateError,
                          br.set_cookie, "foo=bar")
        self.assertEqual(len(cj), 0)

        # normal case
        br.open(url)
        br.set_cookie("foo=bar")
        self.assertEqual(len(cj), 1)
        self.assertEqual(cj._cookies["example.com"]["/"]["foo"].value, "bar")

class ResponseTests(TestCase):

    def test_set_response(self):
        import copy
        from mechanize import response_seek_wrapper

        br = TestBrowser()
        url = "http://example.com/"
        html = """click me"""
        headers = {"content-type": "text/html"}
        r = response_seek_wrapper(MockResponse(url, html, headers))
        br.add_handler(make_mock_handler()([("http_open", r)]))

        r = br.open(url)
        self.assertEqual(r.read(), html)
        r.seek(0)
        self.assertEqual(copy.copy(r).read(), html)
        self.assertEqual(list(br.links())[0].url, "spam")

        newhtml = """click me"""

        r.set_data(newhtml)
        self.assertEqual(r.read(), newhtml)
        self.assertEqual(br.response().read(), html)
        br.response().set_data(newhtml)
        self.assertEqual(br.response().read(), html)
        self.assertEqual(list(br.links())[0].url, "spam")
        r.seek(0)

        br.set_response(r)
        self.assertEqual(br.response().read(), newhtml)
        self.assertEqual(list(br.links())[0].url, "eggs")

    def test_str(self):
        import mimetools
        from mechanize import _response

        br = TestBrowser()
        self.assertEqual(
            str(br),
            "<TestBrowser (not visiting a URL)>"
            )

        fp = StringIO.StringIO('<form name="f"><input /></form>')
        headers = mimetools.Message(
            StringIO.StringIO("Content-type: text/html"))
        response = _response.response_seek_wrapper(
            _response.closeable_response(
            fp, headers, "http://example.com/", 200, "OK"))
        br.set_response(response)
        self.assertEqual(
            str(br),
            "<TestBrowser visiting http://example.com/>"
            )

        br.select_form(nr=0)
        self.assertEqual(
            str(br),
            """\
<TestBrowser visiting http://example.com/
 selected form:
 <f GET http://example.com/ application/x-www-form-urlencoded
  <TextControl(<None>=)>>
>""")

if __name__ == "__main__":
    import unittest
    unittest.main()


text:
package org.broadinstitute.dsde.firecloud.test.analysis

import java.io.{File, PrintWriter}

import org.broadinstitute.dsde.firecloud.fixture.{DownloadUtil, UserFixtures}
import org.broadinstitute.dsde.firecloud.page.workspaces.WorkspaceDataPage
import org.broadinstitute.dsde.firecloud.page.workspaces.methodconfigs.WorkspaceMethodConfigDetailsPage
import org.broadinstitute.dsde.workbench.auth.AuthToken
import org.broadinstitute.dsde.workbench.config.UserPool
import org.broadinstitute.dsde.workbench.fixture._
import org.broadinstitute.dsde.workbench.service.test.WebBrowserSpec
import org.scalatest.{FreeSpec, Matchers}

import scala.collection.immutable.ListMap
import scala.io.Source

class MethodConfigSpec extends FreeSpec with Matchers with WebBrowserSpec with WorkspaceFixtures with UserFixtures with DownloadUtil
  with MethodFixtures with BillingFixtures with TestReporterFixture {

  val wdl = """
              |workflow w {
              |  call t
              |}
              |
              |task t {
              |  String inString
              |  String inWorkspaceRef
              |  String inThisRef
              |  Float inFloat
              |  Int inInt
              |  Boolean inBoolean
              |  File inFile
              |  Array[String] inStringArray
              |  Array[String] inStringArray2
              |  Map[String,String] inStringMap
              |
              |  command {
              |    echo inString ${inString}
              |    echo inWorkspaceRef ${inWorkspaceRef}
              |    echo inThisRef ${inThisRef}
              |    echo inFloat ${inFloat}
              |    echo inInt ${inInt}
              |    echo inBoolean ${inBoolean}
              |    echo inFile ${inFile}
              |    echo inStringArray ${sep=" " inStringArray}
              |    echo inStringArray2 ${sep=" " inStringArray2}
              |    echo inStringMap ${write_map(inStringMap)}
              |  }
              |  output {
              |    String out = stdout()
              |  }
              |  runtime {
              |    docker: "ubuntu"
              |  }
              |}
              |""".stripMargin

  val wdlInputsBase = ListMap(
    "w.t.inString" -> "\"test\"",
    "w.t.inFloat"-> "1.5",
    "w.t.inInt" -> "2",
    "w.t.inBoolean" -> "true",
    "w.t.inFile" -> "\"gs://foo/bar\"",
    "w.t.inStringArray" -> """["foo","bar"]""",
    "w.t.inStringArray2" -> """["say \"hi\"!"]""",
    "w.t.inStringMap" -> """{"foo":"bar"}"""
  )

  val refInputs = ListMap(
    "w.t.inWorkspaceRef" -> "workspace.hello",
    "w.t.inThisRef" -> "this.hello"
  )

  val refInputsJsonFormat = ListMap(
    "w.t.inWorkspaceRef" -> "\"${workspace.hello}\"",
    "w.t.inThisRef" -> "\"${this.hello}\""
  )

  // exercises two things: unmatched variables are silently ignored
  // and input JSON files larger than 4K are handled (fc-app issue #168)
  lazy val unmatchedVariables: Map[String, String] = {
    (1 to 100).map { i => s"unmatched.variable.name.$i" -> "\"surprise!\"" }
  }.toMap

  "input/output auto-suggest" - {
    "stays current with selected root entity type" in {
      val user = UserPool.chooseProjectOwner
      implicit val authToken: AuthToken = user.makeAuthToken()
      withCleanBillingProject(user) { projectName =>
        withWorkspace(projectName, "MethodConfigSpec", attributes = Some(Map("foo" -> "bar"))) { workspaceName =>
          val configName = s"${SimpleMethodConfig.configName}_$workspaceName"
          api.methodConfigurations.createMethodConfigInWorkspace(
            projectName, workspaceName,
            MethodData.SimpleMethod,
            projectName, configName,
            SimpleMethodConfig.snapshotId,
            Map.empty, Map.empty,
            "sample")

          val workspaceSuggestions = Seq("workspace.foo")
          val participantSuggestions = Seq("this.is_a_participant")

          withWebDriver { implicit driver =>
            withSignIn(user) { _ =>
              val configPage = new WorkspaceMethodConfigDetailsPage(projectName, workspaceName, projectName, configName).open
              configPage.isEditing shouldBe false

              configPage.toggleDataModel() shouldBe false
              configPage.isEditing shouldBe true
              configPage.clickAndReadSuggestions("test.hello.name") should contain theSameElementsAs workspaceSuggestions

              configPage.toggleDataModel() shouldBe true
              configPage.readRootEntityTypeSelect shouldBe "sample"
              configPage.clickAndReadSuggestions("test.hello.name") should contain theSameElementsAs workspaceSuggestions

              api.importMetaData(projectName, workspaceName, "entities",
                "entity:participant_id\tis_a_participant\nparticipant1\ttrue")

              // go somewhere else and come back (because reloadPage loses background signed-in state)
              new WorkspaceDataPage(projectName, workspaceName).open
              configPage.open

              configPage.openEditMode()
              configPage.readRootEntityTypeSelect shouldBe "sample"
              configPage.clickAndReadSuggestions("test.hello.name") should contain theSameElementsAs workspaceSuggestions

              configPage.selectRootEntityType("participant")
              configPage.clickAndReadSuggestions("test.hello.name") should contain theSameElementsAs workspaceSuggestions ++ participantSuggestions

              // Unchecking data model should suppress participant suggestions
              configPage.toggleDataModel() shouldBe false
              configPage.clickAndReadSuggestions("test.hello.name") should contain theSameElementsAs workspaceSuggestions

              configPage.toggleDataModel() shouldBe true
              configPage.readRootEntityTypeSelect shouldBe "participant"
              configPage.clickAndReadSuggestions("test.hello.name") should contain theSameElementsAs workspaceSuggestions ++ participantSuggestions
            }
          }
        }
      }
    }
  }

  "Download inputs.json" - {
    "populates connection inputs" in {
      val user = UserPool.chooseProjectOwner
      implicit val authToken: AuthToken = user.makeAuthToken()

      withCleanBillingProject(user) { projectName =>
        // Create a method
        val methodName = uuidWithPrefix("test_JSON_download")
        val method = Method(
          methodName = methodName,
          methodNamespace = "MethodConfigSpec_download",
          snapshotId = 1,
          rootEntityType = "sample",
          synopsis = "",
          documentation = "",
          wdl)
        api.methods.createMethod(method.creationAttributes)
        register cleanUp api.methods.redact(method)

        withWorkspace(projectName, "MethodConfigSpec", attributes = Some(Map("foo" -> "bar"))) { workspaceName =>
          // Create method config
          val configName = s"test_JSON_populate_config_$workspaceName"
          api.methodConfigurations.createMethodConfigInWorkspace(
            projectName, workspaceName, method,
            projectName, configName, method.snapshotId,
            Map.empty, Map.empty, "sample")

          val downloadDir = makeTempDownloadDirectory()

          withWebDriver(downloadDir) { implicit driver =>
            withSignIn(user) { _ =>
              // Go to method config page
              val configPage = new WorkspaceMethodConfigDetailsPage(projectName, workspaceName, projectName, configName).open

              // We should not be in edit mode
              configPage.isEditing shouldBe false

              val inputs = wdlInputsBase ++ refInputs

              // All the input fields should be empty
              inputs.keys.foreach(name => configPage.readFieldValue(name) shouldBe "")

              // Add values to the input fields
              configPage.editMethodConfig(None, None, None, Option(inputs), None)

              // Download the inputs
              val inputsFile = configPage.downloadInputsJson(downloadDir, "inputs.json")

              // Check the downloaded file contains the right values
              val inputsList = Source.fromFile(inputsFile).mkString
              val expected = """{"w.t.inString":"test","w.t.inFloat":1.5,"w.t.inStringArray2":["say \"hi\"!"],"w.t.inThisRef":"${this.hello}","w.t.inWorkspaceRef":"${workspace.hello}","w.t.inInt":2,"w.t.inStringMap":{"foo":"bar"},"w.t.inBoolean":true,"w.t.inStringArray":["foo","bar"],"w.t.inFile":"gs://foo/bar"}"""
              inputsList shouldBe expected
            }
          }
        }
      }
    }
  }

  "Upload inputs.json" - {
    "populates connection inputs" in {
      val user = UserPool.chooseProjectOwner
      implicit val authToken: AuthToken = user.makeAuthToken()

      // Create a method
      withCleanBillingProject(user) { projectName =>
        val methodName = uuidWithPrefix("test_JSON_populate")
        val method = Method(
          methodName = methodName,
          methodNamespace = "MethodConfigSpec_populate",
          snapshotId = 1,
          rootEntityType = "sample",
          synopsis = "",
          documentation = "",
          wdl)
        api.methods.createMethod(method.creationAttributes)
        register cleanUp api.methods.redact(method)

        withWorkspace(projectName, "MethodConfigSpec", attributes = Some(Map("foo" -> "bar"))) { workspaceName =>
          // Create method config
          val configName = s"test_JSON_populate_config_$workspaceName"
          api.methodConfigurations.createMethodConfigInWorkspace(
            projectName, workspaceName, method,
            projectName, configName, method.snapshotId,
            Map.empty, Map.empty, "sample")

          withWebDriver { implicit driver =>
            withSignIn(user) { _ =>
              // Go to method config page
              val configPage = new WorkspaceMethodConfigDetailsPage(projectName, workspaceName, projectName, configName).open

              // We should not be in edit mode
              configPage.isEditing shouldBe false

              val inputs = wdlInputsBase ++ refInputsJsonFormat

              // All the input fields should be empty
              inputs.keys.foreach(name => configPage.readFieldValue(name) shouldBe "")

              // Populate input fields from a large (>4K) JSON containing all the field values and some fields that don't exist
              configPage.populateInputsFromJson(generateInputsJson(inputs ++ unmatchedVariables))

              // We should have been automatically switched to edit mode
              configPage.isEditing shouldBe true

              // for each input, (not included the fields that didn't exist), we should have the correct value
              // for referential inputs ($this._ or $workspace._), it should have removed the "$"
              inputs foreach {
                case (name, expected) =>
                  if (refInputsJsonFormat.contains(name))
                    configPage.readFieldValue(name) shouldBe expected.drop(3).dropRight(2)
                  else configPage.readFieldValue(name) shouldBe expected
              }
            }
          }
        }
      }
    }
  }

  // TODO(RT): temporarily ignoring Dockstore test because their staging enviornment is down
  // (https://staging.dockstore.org:8443) causing all our builds to fail.
  // See GAWB-3822 for the long-term fix.
  "With dockstore method config" - {
    "not using data model and edit goes into edit mode" ignore {
      // written for GAWB-3695 and GAWB-3712
      val user = UserPool.chooseProjectOwner
      implicit val authToken: AuthToken = user.makeAuthToken()

      withCleanBillingProject(user) { projectName =>

        withWorkspace(projectName, "MethodConfigSpec", attributes = Some(Map("foo" -> "bar"))) { workspaceName =>
          // Create method config
          val configName = s"test_dockstore_config_edit_$workspaceName"
          api.methodConfigurations.createDockstoreMethodConfigInWorkspace(
            projectName, workspaceName, DockstoreMethodData.dockstoreMethod,
            projectName, configName)

          withWebDriver { implicit driver =>
            withSignIn(user) { _ =>
              // Go to method config page
              val configPage = new WorkspaceMethodConfigDetailsPage(projectName, workspaceName, projectName, configName).open

              // We should not be using the entity data model
              configPage.isUsingDataModel() shouldBe false

              // We should not be in edit mode
              configPage.isEditing shouldBe false
              configPage.openEditMode()
              configPage.isEditing shouldBe true

            }
          }
        }
      }
    }
  }

  private def generateInputsJson(inputs: Map[String, String]): File = {
    val file = File.createTempFile("MethodConfigSpec_", "_inputs.json")
    val writer = new PrintWriter(file)
    val rows = inputs map { case (k, v) => s""""$k": $v"""}
    val fileContent = s"""{\n  ${rows.mkString(",\n  ")}\n}"""
    writer.write(fileContent)
    writer.close()
    file
  }
}


text:
<?php

if( !defined('SITE_BASE_URL') ){ die('psst...You shouldn't be here.'); }

class Router{

    public static $router_get = array();
    public static $router_post = array();

    public static function get( $uri, $pattern ){
        self::$router_get[$uri] = $pattern;
    }

    public static function post( $uri, $pattern ){
        self::$router_post[$uri] = $pattern;
    }

}


text:
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.testutils;

import com.yahoo.vespa.hosted.provision.persistence.NameResolver;

import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

/**
 * A mock DNS resolver. Can be configured to only answer specific lookups or any lookup.
 *
 * @author mpolden
 */
public class MockNameResolver implements NameResolver {

    private final Map<String, Set<String>> records = new HashMap<>();

    private boolean mockAnyLookup = false;
    private boolean explicitReverseRecords = false;

    public MockNameResolver addReverseRecord(String ipAddress, String hostname) {
        addRecord(ipAddress, hostname);
        return this;
    }

    public MockNameResolver addRecord(String hostname, String... ipAddress) {
        Objects.requireNonNull(hostname, "hostname must be non-null");
        Arrays.stream(ipAddress).forEach(ip -> Objects.requireNonNull(ip, "ipAddress must be non-null"));
        records.computeIfAbsent(hostname, (k) -> new HashSet<>())
               .addAll(Arrays.asList(ipAddress));
        return this;
    }

    public MockNameResolver removeRecord(String name) {
        records.remove(name);
        return this;
    }

    public MockNameResolver mockAnyLookup() {
        this.mockAnyLookup = true;
        return this;
    }

    /**
     * When true, only records added with {@link MockNameResolver#addReverseRecord(String, String)} are considered by
     * {@link MockNameResolver#resolveHostname(String)}. Otherwise the latter returns the IP address by reversing the lookup
     * implicitly.
     */
    public MockNameResolver explicitReverseRecords() {
        this.explicitReverseRecords = true;
        return this;
    }

    @Override
    public Set<String> resolveAll(String name) {
        if (records.containsKey(name)) {
            return records.get(name);
        }
        if (mockAnyLookup) {
            Set<String> ipAddresses = Set.of(randomIpAddress());
            records.put(name, ipAddresses);
            return ipAddresses;
        }
        throw new RuntimeException(new UnknownHostException("Could not resolve: " + name));
    }

    @Override
    public Set<String> resolve(String name, RecordType first, RecordType... rest) {
        return resolveAll(name);
    }

    @Override
    public Optional<String> resolveHostname(String ipAddress) {
        if (!explicitReverseRecords) {
            return records.entrySet().stream()
                          .filter(kv -> kv.getValue().contains(ipAddress))
                          .map(Map.Entry::getKey)
                          .findFirst();
        }
        return Optional.ofNullable(records.get(ipAddress))
                       .flatMap(values -> values.stream().findFirst());
    }

    private static String randomIpAddress() {
        // Generate a random IP in 127/8 (loopback block)
        ThreadLocalRandom random = ThreadLocalRandom.current();
        return String.format("127.%d.%d.%d",
                random.nextInt(1, 255 + 1),
                random.nextInt(1, 255 + 1),
                random.nextInt(1, 255 + 1));
    }

}


text:
package com.dianping.cat.build.report;

import java.util.ArrayList;
import java.util.List;

import org.unidal.lookup.configuration.AbstractResourceConfigurator;
import org.unidal.lookup.configuration.Component;

import com.dianping.cat.alarm.spi.config.AlertConfigManager;
import com.dianping.cat.alarm.spi.decorator.Decorator;
import com.dianping.cat.alarm.spi.receiver.Contactor;
import com.dianping.cat.config.server.ServerConfigManager;
import com.dianping.cat.consumer.heartbeat.HeartbeatAnalyzer;
import com.dianping.cat.report.alert.heartbeat.HeartbeatAlert;
import com.dianping.cat.report.alert.heartbeat.HeartbeatContactor;
import com.dianping.cat.report.alert.heartbeat.HeartbeatDecorator;
import com.dianping.cat.report.alert.heartbeat.HeartbeatRuleConfigManager;
import com.dianping.cat.report.page.heartbeat.service.CompositeHeartbeatService;
import com.dianping.cat.report.page.heartbeat.service.HeartbeatReportService;
import com.dianping.cat.report.page.heartbeat.service.HistoricalHeartbeatService;
import com.dianping.cat.report.page.heartbeat.service.LocalHeartbeatService;
import com.dianping.cat.report.page.heartbeat.task.HeartbeatReportBuilder;
import com.dianping.cat.report.server.RemoteServersManager;
import com.dianping.cat.report.service.ModelService;
import com.dianping.cat.service.ProjectService;

public class HeartbeatComponentConfigurator extends AbstractResourceConfigurator {
	@Override
	public List<Component> defineComponents() {
		List<Component> all = new ArrayList<Component>();

		all.add(A(HeartbeatRuleConfigManager.class));

		all.add(A(HeartbeatReportService.class));

		all.add(A(LocalHeartbeatService.class));
		all.add(C(ModelService.class, "heartbeat-historical", HistoricalHeartbeatService.class) //
								.req(HeartbeatReportService.class, ServerConfigManager.class));
		all.add(C(ModelService.class, HeartbeatAnalyzer.ID, CompositeHeartbeatService.class) //
								.req(ServerConfigManager.class, RemoteServersManager.class) //
								.req(ModelService.class, new String[] { "heartbeat-historical" }, "m_services"));

		all.add(C(Contactor.class, HeartbeatContactor.ID, HeartbeatContactor.class)
								.req(ProjectService.class,	AlertConfigManager.class));
		all.add(C(Decorator.class, HeartbeatDecorator.ID, HeartbeatDecorator.class));

		all.add(A(HeartbeatAlert.class));

		all.add(A(HeartbeatReportBuilder.class));

		return all;
	}
}